public static WritableRaster asRaster()

in src/main/java/org/apache/pdfbox/jbig2/image/Bitmaps.java [78:170]


    public static WritableRaster asRaster(Bitmap bitmap, final ImageReadParam param,
            final FilterType filterType)
    {
        if (bitmap == null)
            throw new IllegalArgumentException("bitmap must not be null");

        if (param == null)
            throw new IllegalArgumentException("param must not be null");

        final Dimension sourceRenderSize = param.getSourceRenderSize();

        double scaleX;
        double scaleY;
        if (sourceRenderSize != null)
        {
            scaleX = sourceRenderSize.getWidth() / bitmap.getWidth();
            scaleY = sourceRenderSize.getHeight() / bitmap.getHeight();
        }
        else
        {
            scaleX = scaleY = 1;
        }

        Rectangle sourceRegion = param.getSourceRegion();
        if (sourceRegion != null && !bitmap.getBounds().equals(sourceRegion))
        {
            // make sure we don't request an area outside of the source bitmap
            sourceRegion = bitmap.getBounds().intersection(sourceRegion);

            // get region of interest
            bitmap = Bitmaps.extract(sourceRegion, bitmap);
        }

        /*
         * Subsampling is the advance of columns/rows for each pixel in the according direction. The resulting image's
         * quality will be bad because we loose information if we step over columns/rows. For example, a thin line (1
         * pixel high) may disappear completely. To avoid this we use resize filters if scaling will be performed
         * anyway. The resize filters use scale factors, one for horizontal and vertical direction. We care about the
         * given subsampling steps by adjusting the scale factors. If scaling is not performed, subsampling is performed
         * in its original manner.
         */

        final boolean requiresScaling = scaleX != 1 || scaleY != 1;

        final boolean requiresXSubsampling = param.getSourceXSubsampling() != 1;
        final boolean requiresYSubsampling = param.getSourceYSubsampling() != 1;

        if (requiresXSubsampling && requiresYSubsampling)
        {
            // Apply vertical and horizontal subsampling
            if (requiresScaling)
            {
                scaleX /= (double) param.getSourceXSubsampling();
                scaleY /= (double) param.getSourceYSubsampling();
            }
            else
            {
                bitmap = subsample(bitmap, param);
            }
        }
        else
        {
            if (requiresXSubsampling)
            {
                // Apply horizontal subsampling only
                if (requiresScaling)
                {
                    scaleX /= (double) param.getSourceXSubsampling();
                }
                else
                {
                    bitmap = Bitmaps.subsampleX(bitmap, param.getSourceXSubsampling(),
                            param.getSubsamplingXOffset());
                }
            }

            if (requiresYSubsampling)
            {
                // Apply vertical subsampling only
                if (requiresScaling)
                {
                    scaleY /= (double) param.getSourceYSubsampling();
                }
                else
                {
                    bitmap = Bitmaps.subsampleY(bitmap, param.getSourceYSubsampling(),
                            param.getSubsamplingYOffset());
                }
            }
        }

        return buildRaster(bitmap, filterType, scaleX, scaleY);
    }