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

Fix a jpx inclusion tag tree bug #5727

Closed
wants to merge 1 commit into from
Closed

Fix a jpx inclusion tag tree bug #5727

wants to merge 1 commit into from

Conversation

jpambrun
Copy link

The current code only works when every code-block includes at least some coding pass in layer 0. Otherwise, the tag tree will have x preceding 0, where x is the number of layers before the code-block is first included (See section B.10.2 and B.10.4 of ITU-T Rec. T.800). Such file appears severely corrupted as everything read beyond that point is misaligned.

This commit purge the preceding zeros when the �inclusionTree is constructed and throw an error if the number of preceding zeros does not match the current layer (Tag tree is invalid or the code-block was supposed to be included in a earlier packet).

@Snuffleupagus
Copy link
Collaborator

@jpambrun Please provide a PDF file that can be used to test the PR.
Also, PRs touching code in /core should ideally add a PDF test-case to the test suite.

Edit: Please make sure that you read the style guide when submitting patches.

@@ -1125,6 +1125,11 @@ var JpxImage = (function JpxImageClosure() {
zeroBitPlanesTree = new TagTree(width, height);
precinct.inclusionTree = inclusionTree;
precinct.zeroBitPlanesTree = zeroBitPlanesTree;
for(var l=0; l < layerNumber; l++){
if(readBits(1)!== 0){
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: these lines are missing whitespaces, it should be

for (var l = 0; l < layerNumber; l++) {
  if (readBits(1) !== 0) {

@jpambrun
Copy link
Author

I use your decoder in another project unrelated to PDFs. I suppose images with multiples layers are very uncommon in PDFs and code-blocks not included in layers 0 must be even more uncommon.

I suppose I could try to generate a PDF with a carefully crafted JPEG2000 with there characteristics, but it would mean a lot of work. The files I have now are 16 bits signed grayscale medical images (I had to slightly modify the coder to accommodate that) so I can't just embed them in a PDF. Kakadu and Matlab have no issue reading those files.

I can resubmit with correct spacing, but it's a question of whether you want to support JPEG2000 features that are not strictly PDF related. It is currently not faithful to the specification though.

@CodingFabian
Copy link
Contributor

i think adding minor features which might not be used by a pdf, but makes decoding work according to spec are fine, as long as it does not slow down pdf rendering.
maybe we should consider adding support for providing "image-only" test cases for decoders?

@jpambrun
Copy link
Author

PDF.js's JPEG2000 decoder is fantastic. I'm sure most of you know that the DICOM medical image format is the other big use casse of j2k. I think we will see significant shift towards web technologies in the near future in the medical domain. Both large standardisation bodies, DICOM and HL7, are already adopting RESTful apis for their next revisions and the deployement benefits of web applications are now very clear to the diagnostic imaging community.

Consequently, many applications will need a cross browser j2k decoder. Your implementation appears to be the best option and I hope we will be able to contribute bug fixes as we explore use cases that are outside of the PDF scope.

@Snuffleupagus
Copy link
Collaborator

For me personally, I don't mind us adding reasonable amounts of code to e.g. jpx.js for use cases that are outside of what is needed for PDF files.
However, if significant amounts of "unneeded" (from the PDF perspective) code is added, this could unnecessarily increase the size of PDF.js. @brendandahl and @yurydelendik: What is our policy for adding code to the image decoders, if it's not strictly PDF related? (Perhaps related: #5649 (comment).)

For this particular PR, there is just one small issue, as I see it: We still have no way of confirming that it works correctly. Please note that I don't doubt that it does work, but from experience it's usually not a good idea to land unverified PRs.

@jpambrun Also, please squash the commits.

@@ -1125,6 +1125,11 @@ var JpxImage = (function JpxImageClosure() {
zeroBitPlanesTree = new TagTree(width, height);
precinct.inclusionTree = inclusionTree;
precinct.zeroBitPlanesTree = zeroBitPlanesTree;
for (var l=0; l < layerNumber; l++) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: there's still not enough whitespace around all operators. Please add spaces before and after =.

The current code only works when every code-block includes at least some coding pass in layer 0. Otherwise, the tag tree will have x preceding 0, where x is the number of layers before the code-block is first included (See section B.10.2 and B.10.4 of ITU-T Rec. T.800). Such file appears severely corrupted as everything read beyond that point is misaligned.

This commit purge the preceding zeros when the inclusionTree is constructed and throw an error if the number of preceding zeros does not match the current layer (Tag tree is invalid or the code-block was supposed to be included in a earlier packet).
@jpambrun
Copy link
Author

I appreciate your need to confirm that it works correctly. I will try to produce and embed a 8bit grayscale image with this layer configuration in a PDF. However, I cannot do regression testing.

I made the mistake of using Github's online code editor and had trouble squashing them. Sorry abbout that.

@jpambrun
Copy link
Author

I have created this file that shows the issue in PDF.js, but not in chrome and zathura.

https://dl.dropboxusercontent.com/u/168338/j2kInclusionTreeBug.pdf

@jpambrun
Copy link
Author

I have been asked if I could share the modifications that I have made to support 16bit signed image.So I have made a fork here: https://github.com/jpambrun/jpx-medical. I'm not familiar with the etiquette and hope it's ok.

I have made a demo, http://jpambrun.github.io/, of what can be achieve with your j2k decoder and I hope it can illustrate the benefits of supporting other applications. In the demo, 500 medical images are streamed as needed in 3 quality layers from a standard compliant DICOM files. The first image is displayed in under a second and the first pass of all 500 images are decoded in about 10 seconds on a typical computer. It's very fast, even compared to native apps.

This was referenced Feb 23, 2015
@jpambrun
Copy link
Author

I've tested the PDFs from #5649, and I now think that this PR may be incomplete because some of them trigger the added "JPX Error: Invalid tag tree".

@jpambrun jpambrun closed this Feb 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants