Skip to content

Commit 0ee71ad

Browse files
Bart SearsAndroid (Google) Code Review
authored andcommitted
Merge "Fix native crash while saving a panorama." into jb-mr1-dev
2 parents be29d82 + 4b63f14 commit 0ee71ad

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

core/jni/android/graphics/YuvToJpegEncoder.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

114115
void 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

186190
void 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);

core/jni/android/graphics/YuvToJpegEncoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Yuv420SpToJpegEncoder : public YuvToJpegEncoder {
5555
void deinterleaveYuv(uint8_t* yuv, int width, int height,
5656
uint8_t*& yPlanar, uint8_t*& uPlanar, uint8_t*& vPlanar);
5757
void deinterleave(uint8_t* vuPlanar, uint8_t* uRows, uint8_t* vRows,
58-
int rowIndex, int width);
58+
int rowIndex, int width, int height);
5959
void compress(jpeg_compress_struct* cinfo, uint8_t* yuv, int* offsets);
6060
};
6161

0 commit comments

Comments
 (0)