Bug 696884 - gs renders JPX image with subsampled components incorrectly
Summary: gs renders JPX image with subsampled components incorrectly
Status: RESOLVED FIXED
Alias: None
Product: Ghostscript
Classification: Unclassified
Component: JPX/JBIG2 encode/decode (show other bugs)
Version: master
Hardware: PC All
: P4 normal
Assignee: Sebastian Rasmussen
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-06-29 09:56 UTC by Sebastian Rasmussen
Modified: 2023-08-28 13:30 UTC (History)
1 user (show)

See Also:
Customer:
Word Size: ---


Attachments
Testcase PDF-file. (1.24 KB, application/binary)
2016-06-29 09:56 UTC, Sebastian Rasmussen
Details
Sample image showing unpatched code using Luratech JPX decoder. (6.43 KB, image/png)
2016-06-29 10:17 UTC, Sebastian Rasmussen
Details
Sample image showing unpatched code using Openjpeg JPX decoder. (4.96 KB, image/png)
2016-06-29 10:18 UTC, Sebastian Rasmussen
Details
Sample image showing patched code using Luratech JPX decoder. (4.31 KB, image/png)
2016-06-29 10:18 UTC, Sebastian Rasmussen
Details
Attempt at patching Ghostscript to better handle attached sample image. (3.09 KB, patch)
2016-06-29 10:19 UTC, Sebastian Rasmussen
Details | Diff
Proposed patch for handling subsampled j2k components better (3.66 KB, patch)
2023-03-26 04:46 UTC, Sebastian Rasmussen
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Sebastian Rasmussen 2016-06-29 09:56:12 UTC
Created attachment 12653 [details]
Testcase PDF-file.

I took the following file from openjpeg:

https://github.com/uclouvain/openjpeg-data/blob/master/input/nonregression/issue411-ycc420.jp2

and embedded it in a PDF-file. Ghostscript attempts to render it but does so incorrectly, moreover it generates a few valgrind errors.
Comment 1 Sebastian Rasmussen 2016-06-29 10:17:11 UTC
Created attachment 12654 [details]
Sample image showing unpatched code using Luratech JPX decoder.
Comment 2 Sebastian Rasmussen 2016-06-29 10:18:01 UTC
Created attachment 12655 [details]
Sample image showing unpatched code using Openjpeg JPX decoder.
Comment 3 Sebastian Rasmussen 2016-06-29 10:18:21 UTC
Created attachment 12656 [details]
Sample image showing patched code using Luratech JPX decoder.
Comment 4 Sebastian Rasmussen 2016-06-29 10:19:17 UTC
Created attachment 12657 [details]
Attempt at patching Ghostscript to better handle attached sample image.
Comment 5 Sebastian Rasmussen 2016-06-29 10:24:42 UTC
Please see 696885 for how MuPDF renders this file.
Comment 6 Peter Cherepanov 2021-01-02 02:55:51 UTC
The problem is still reproducible in the current master branch. There's a patch for Luratech but no patch for openjpeg.
Comment 7 Ray Johnston 2021-01-02 18:57:49 UTC
Now that we have dropped Luratech and openjpeg is our JPX codec, we probably
should assign it to someone to actually fix it. Bumping back to Henry for
assignment.
Comment 8 Sebastian Rasmussen 2023-03-25 18:50:59 UTC
I retested this on latest git HEAD, commit 3635f4c75e54e337a4eebcf6db3eef0e60f9cebf, and the incorrect diagonal pattern is still present. The valgrind issues I mentioned were fixed in commit 9388ecefe9d8659d8434aa78d07a1fa247c8ea4e.
Comment 9 Sebastian Rasmussen 2023-03-26 04:46:06 UTC
Created attachment 23892 [details]
Proposed patch for handling subsampled j2k components better

This proposed patch is under review. Maybe it can be better optimized.
Comment 10 Sebastian Rasmussen 2023-08-28 13:30:38 UTC
Fixed in 

commit fd5007e72efe82dbf892bd0a5ef58c992a4d58ca
Author: Sebastian Rasmussen <sebras@gmail.com>
Date:   Sun Mar 26 04:37:40 2023 +0200

    Bug 696884: Handle subsampled components correctly for JPEG2000 images.
    
    Previously if a PDF had an JPEG2000 image using subsampling which was
    not a multiple of the image width, rendering would appear staggered.
    The upsampling code didn't take the up to dx - 1 upsampled samples
    into account at the end of the scanline.
    
    The JPEG2000 image in the PDF in the bug is 32 by 32 pixels where component 0
    is not subsampled (dx/dy are both 1) and components 1 and 2 are subsampled
    (dx/dy are both 2).
    
    Thanks for the maths in this commit Robin!