jai-imageio / jai-imageio-jpeg2000

JPEG2000 support for Java Advanced Imaging Image I/O Tools API
Other
74 stars 56 forks source link

Overflow in ForwCompTransf.calcMixedBitDepths #48

Open Daniel-Alievsky opened 3 months ago

Daniel-Alievsky commented 3 months ago

While using SCIFIO JPEG2000 codec, based on jai-imageio-jpeg2000, we have an exception while an attempt to encode 32-bit integer pixel samples:

Exception in thread "main" java.lang.IllegalArgumentException: 0 <= 0 at jj2000.j2k.util.MathUtil.log2(MathUtil.java:67) at jj2000.j2k.image.forwcomptransf.ForwCompTransf.calcMixedBitDepths(ForwCompTransf.java:235) at jj2000.j2k.image.forwcomptransf.ForwCompTransf.initForwICT(ForwCompTransf.java:307) at jj2000.j2k.image.forwcomptransf.ForwCompTransf.setTile(ForwCompTransf.java:762) at jj2000.j2k.image.ImgDataAdapter.setTile(ImgDataAdapter.java:288) at jj2000.j2k.image.ImgDataAdapter.setTile(ImgDataAdapter.java:288) at jj2000.j2k.wavelet.analysis.ForwWTFull.setTile(ForwWTFull.java:903) at jj2000.j2k.image.ImgDataAdapter.setTile(ImgDataAdapter.java:288) at jj2000.j2k.image.ImgDataAdapter.setTile(ImgDataAdapter.java:288) at jj2000.j2k.roi.encoder.ROIScaler.setTile(ROIScaler.java:696) at jj2000.j2k.image.ImgDataAdapter.setTile(ImgDataAdapter.java:288) at jj2000.j2k.entropy.encoder.StdEntropyCoder.setTile(StdEntropyCoder.java:1267) at jj2000.j2k.entropy.encoder.EBCOTRateAllocator.(EBCOTRateAllocator.java:281) at jj2000.j2k.entropy.encoder.PostCompRateAllocator.createInstance(PostCompRateAllocator.java:260) at com.github.jaiimageio.jpeg2000.impl.J2KImageWriter.write(J2KImageWriter.java:347)

The reason is the following code:

tdepth[0] = MathUtil.log2((1<<ntdepth[0])+(2<<ntdepth[1])+
                                      (1<<ntdepth[2])-1)-2+1;

When ntdepth[0]==32 (32 bits/sample), the << operator leads to 32-bit overflow and actually returns 1 instead of 1^32.