Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace getPixel with getPixels, which makes it 10 times faster #386

Merged
merged 1 commit into from
Mar 28, 2022

Conversation

anyongjin
Copy link
Contributor

getPixel is not suitable for all pixels calculating, it is too slow.
Here is a test before/ after change(with previous slim_first.pdf):
I've tested getPixels for whole image or for one row, they are nearly cost the same time. So i edit the code to handle it row by row for lower memory usage.

-- getPixel
I/PDImageXObject: decode: 205, softmask: 0, type: 0
I/PDImageXObject: decode: 8, softmask: 64, type: 1
I/PDImageXObject: decode: 5, softmask: 51, type: 1
I/PDImageXObject: decode: 7, softmask: 63, type: 1
I/PDImageXObject: decode: 7, softmask: 66, type: 1
I/PDImageXObject: decode: 68, softmask: 657, type: 1

-- getPixels for row
I/PDImageXObject: decode: 205, softmask: 0, type: 0
I/PDImageXObject: decode: 6, softmask: 4, type: 1
I/PDImageXObject: decode: 5, softmask: 3, type: 1
I/PDImageXObject: decode: 6, softmask: 6, type: 1
I/PDImageXObject: decode: 7, softmask: 5, type: 1
I/PDImageXObject: decode: 67, softmask: 35, type: 1

replace getImage method in PDImageXObject.java to reproduce the log:

    public Bitmap getImage(Rect region, int subsampling) throws IOException
    {
        if (region == null && subsampling == cachedImageSubsampling && cachedImage != null)
        {
            Bitmap cached = cachedImage.get();
            if (cached != null)
            {
                return cached;
            }
        }

        long start = System.currentTimeMillis();
        // get image as RGB
        Bitmap image = SampledImageReader.getRGBImage(this, region, subsampling, getColorKeyMask());
        long decodeEnd = System.currentTimeMillis();


        int maskTyp = 0;
        // soft mask (overrides explicit mask)
        PDImageXObject softMask = getSoftMask();
        if (softMask != null)
        {
            float[] matte = extractMatte(softMask);
            image = applyMask(image, softMask.getOpaqueImage(), true, matte);
            maskTyp = 1;
        }
        else
        {
            // explicit mask - to be applied only if /ImageMask true
            PDImageXObject mask = getMask();
            if (mask != null && mask.isStencil())
            {
                maskTyp = 2;
                image = applyMask(image, mask.getOpaqueImage(), false, null);
            }
        }
        long allEnd = System.currentTimeMillis();
        Log.i("PDImageXObject", String.format("decode: %d, softmask: %d, type: %d", decodeEnd - start, allEnd - decodeEnd, maskTyp));

        if (region == null && subsampling <= cachedImageSubsampling)
        {
            // only cache full-image renders, and prefer lower subsampling frequency, as lower
            // subsampling means higher quality and longer render times.
            cachedImageSubsampling = subsampling;
            cachedImage = new SoftReference<Bitmap>(image);
        }

        return image;
    }

@TomRoush TomRoush added the type: enhancement Request to add/improve a feature label Mar 28, 2022
@TomRoush TomRoush added this to the v2.0.18.0 milestone Mar 28, 2022
@TomRoush TomRoush merged commit 4337ae4 into TomRoush:master Mar 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement Request to add/improve a feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants