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

Problem decoding multiple tiles with get_decoded_tile when cmap/pclr/cdef boxes are present in jp2 file #484

Closed
gcode-importer opened this issue Mar 30, 2015 · 5 comments

Comments

@gcode-importer
Copy link

Originally reported on Google Code with ID 484

What steps will reproduce the problem?

1. Compile the attached program against r2997 and patches 254 + 447
2  Run the program with the attached 0.jp2 file.  ./jp2tile 0.jp2 

What is the expected output?
See the pgm. The first tile (upper right corner) is correct the other tiles
are wrong.

What version of the product are you using? On what operating system?
cvs r2997 + patches 254 + 447. If only r2997 also the first tile is bad

When running the program with valgrind there is an error about access of
an uninitialized variable. this might cause a problem.

==16781== Invalid read of size 4
==16781==    at 0x417CC1: opj_j2k_get_tile (j2k.c:9870)
==16781==    by 0x41F010: opj_jp2_get_tile (jp2.c:2744)
==16781==    by 0x404043: opj_get_decoded_tile (openjpeg.c:511)
==16781==    by 0x4015CF: main (jp2tile.c:274)
==16781==  Address 0x4d2c518 is 24 bytes before a block of size 34,080 alloc'd
==16781==    at 0x4A057BB: calloc (vg_replace_malloc.c:593)
==16781==    by 0x4066AD: opj_j2k_read_siz (j2k.c:2220)
==16781==    by 0x4113E3: opj_j2k_read_header_procedure (j2k.c:7288)
==16781==    by 0x4116B1: opj_j2k_exec (j2k.c:7356)
==16781==    by 0x4103B7: opj_j2k_read_header (j2k.c:6851)
==16781==    by 0x41EA0F: opj_jp2_read_header (jp2.c:2560)
==16781==    by 0x403D39: opj_read_header (openjpeg.c:391)
==16781==    by 0x40146C: main (jp2tile.c:245)
==16781==

Line 9870
 9866         for (compno=0; compno < p_image->numcomps; ++compno)
 9867         {
 9868                 OPJ_INT32 l_comp_x1, l_comp_y1;
 9869
 9870                 l_img_comp->factor = p_j2k->m_private_image->comps[compno]  
   .factor;
 9871
 9872                 l_img_comp->x0 = (OPJ_UINT32)opj_int_ceildiv((OPJ_INT32)p_  
   image->x0, (OPJ_INT32)l_img_comp->dx);






Reported by hjpriester on 2015-03-30 12:25:10


- _Attachment: [jp2tile.c](https://storage.googleapis.com/google-code-attachments/openjpeg/issue-484/comment-0/jp2tile.c)_ - _Attachment: [02-output.pgm](https://storage.googleapis.com/google-code-attachments/openjpeg/issue-484/comment-0/02-output.pgm)_ - _Attachment: [0.jp2](https://storage.googleapis.com/google-code-attachments/openjpeg/issue-484/comment-0/0.jp2)_
@gcode-importer
Copy link
Author

Hi hjpriester,

Quick analysis on my end.
First off, I'd like to point out that with conformant images & no patch, the issue(s)
is/are still present.
Images with multiple tiles, cmap/pclr and/or cdef boxes will show this behavior. 0.jp2
has cmap/pclr and multiple tiles (but is non conformant - this is why patches are needed).

When looking at your code, I can see that your passing the same opj_image_t to opj_get_decoded_tile
=> That's why you're getting a valgrind error.
Not sure how this should be handled in OpenJPEG. I guess documentation, checks or design
is flawed.
In the mean time, to get rid of this valgrind error (& possible crash - I got one),
you shall pass a new copy of the image you got with opj_read_header on each iteration.
Here's why : 
1st iteration => tile properly decode, image is updated with cmap/pclr info. It now
has 3 components instead of 1on input.
2nd iteration => This image with 3 components is fed to opj_get_decoded_tile & that's
not what it's expecting.

Now, with this loop "fixed" :
1st iteration => you get the correct 3 components image
next iterations => only 1 component in output image, cmap/pclr info is not applied
anymore.

I guess this can be easily fixed by removing opj_jp2_free_pclr(color); at the end of
opj_jp2_apply_pclr (this shouldn't cause any leak, I don't know about other side effects)
For cdef boxes, the fix won't be that easy because data is modified inside the function
opj_jp2_apply_cdef. A deep copy of that info shall be all that's needed.


Reported by mayeut on 2015-05-16 11:16:25

  • Status changed: Accepted

@gcode-importer
Copy link
Author

Image from test suite with the same behavior (doesn't require patches) :
cmap/pclr :
input/nonregression/issue104_jpxstream.jp2

I couldn't find one for cdef

Reported by mayeut on 2015-05-16 12:01:44

@gcode-importer
Copy link
Author

thanks for looking at it. when I use opj_read_header to create an image copy I get a
message end of file reached.
so I guess I have to open the file, create codec for each tile then.

I also tried opj_copy_image_header but this does not fix the valgrind error.

Reported by hjpriester on 2015-05-18 14:51:26

@hjpriester
Copy link

I tried current version and removed the opj_jp2_free_pclr(color) function in opj_jp2_apply_pclr. this fixes the problem in my case.

@rouault
Copy link
Collaborator

rouault commented Sep 7, 2017

This issue has been fixed per c67e1cd

@rouault rouault closed this as completed Sep 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants