@@ -90,8 +90,9 @@ void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo,
9090 // process 16 lines of Y and 8 lines of U/V each time.
9191 while (cinfo->next_scanline < cinfo->image_height ) {
9292 // deitnerleave u and v
93- deinterleave (vuPlanar, uRows, vRows, cinfo->next_scanline , width);
93+ deinterleave (vuPlanar, uRows, vRows, cinfo->next_scanline , width, height );
9494
95+ // Jpeg library ignores the rows whose indices are greater than height.
9596 for (int i = 0 ; i < 16 ; i++) {
9697 // y row
9798 y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides [0 ];
@@ -112,8 +113,10 @@ void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo,
112113}
113114
114115void Yuv420SpToJpegEncoder::deinterleave (uint8_t * vuPlanar, uint8_t * uRows,
115- uint8_t * vRows, int rowIndex, int width) {
116- for (int row = 0 ; row < 8 ; ++row) {
116+ uint8_t * vRows, int rowIndex, int width, int height) {
117+ int numRows = (height - rowIndex) / 2 ;
118+ if (numRows > 8 ) numRows = 8 ;
119+ for (int row = 0 ; row < numRows; ++row) {
117120 int offset = ((rowIndex >> 1 ) + row) * fStrides [1 ];
118121 uint8_t * vu = vuPlanar + offset;
119122 for (int i = 0 ; i < (width >> 1 ); ++i) {
@@ -164,6 +167,7 @@ void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo,
164167 while (cinfo->next_scanline < cinfo->image_height ) {
165168 deinterleave (yuvOffset, yRows, uRows, vRows, cinfo->next_scanline , width, height);
166169
170+ // Jpeg library ignores the rows whose indices are greater than height.
167171 for (int i = 0 ; i < 16 ; i++) {
168172 // y row
169173 y[i] = yRows + i * width;
@@ -185,7 +189,9 @@ void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo,
185189
186190void Yuv422IToJpegEncoder::deinterleave (uint8_t * yuv, uint8_t * yRows, uint8_t * uRows,
187191 uint8_t * vRows, int rowIndex, int width, int height) {
188- for (int row = 0 ; row < 16 ; ++row) {
192+ int numRows = height - rowIndex;
193+ if (numRows > 16 ) numRows = 16 ;
194+ for (int row = 0 ; row < numRows; ++row) {
189195 uint8_t * yuvSeg = yuv + (rowIndex + row) * fStrides [0 ];
190196 for (int i = 0 ; i < (width >> 1 ); ++i) {
191197 int indexY = row * width + (i << 1 );
0 commit comments