3232#include < jxl/codestream_header.h>
3333#include < jxl/decode.h>
3434#include < jxl/types.h>
35+ #include < utility>
3536#include < vector>
3637
3738using std::min;
@@ -46,6 +47,13 @@ void JpegXLDecompressor::decode(
4647 if (signature != JXL_SIG_CODESTREAM && signature != JXL_SIG_CONTAINER)
4748 ThrowRDE (" Unable to verify JPEG XL signature" );
4849
50+ if (mInterleave != std::pair (1U , 1U ) && mInterleave != std::pair (2U , 2U ))
51+ ThrowRDE (" Invalid interleave factors" );
52+
53+ if (mInterleave == std::pair (2U , 2U ) && mRaw ->getCpp () != 1 )
54+ ThrowRDE (
55+ " Invalid combination of interleave factors and components per pixel" );
56+
4957 JxlDecoder* decoder = JxlDecoderCreate (nullptr );
5058
5159 if (!decoder)
@@ -129,18 +137,27 @@ void JpegXLDecompressor::decode(
129137
130138 const Array2DRef<uint16_t > tmp (complete_buffer.data (),
131139 basicinfo.num_color_channels * basicinfo.xsize ,
132- basicinfo.xsize );
140+ basicinfo.ysize );
133141
134142 // Now the image is decoded, and we copy the image data
135143 unsigned int copy_w = min (mRaw ->dim .x - offX, basicinfo.xsize );
136144 unsigned int copy_h = min (mRaw ->dim .y - offY, basicinfo.ysize );
137145
138146 const Array2DRef<uint16_t > out (mRaw ->getU16DataAsUncroppedArray2DRef ());
139147 for (unsigned int row = 0 ; row < copy_h; row++) {
148+ unsigned int rowbuf =
149+ (row / mInterleave .first ) +
150+ (row % mInterleave .first ) * (basicinfo.ysize / mInterleave .first );
140151 for (unsigned int col = 0 ; col < basicinfo.num_color_channels * copy_w;
141- col++)
152+ col++) {
153+ unsigned int colbuf =
154+ (col / mInterleave .second ) +
155+ (col % mInterleave .second ) *
156+ ((basicinfo.num_color_channels * basicinfo.xsize ) /
157+ mInterleave .second );
142158 out (offY + row, basicinfo.num_color_channels * offX + col) =
143- tmp (row, col);
159+ tmp (rowbuf, colbuf);
160+ }
144161 }
145162}
146163
0 commit comments