mjpeg_decode_rgb32() refactoring.

Use calloc(3) & free(3) instead of c99 VLA.
This commit is contained in:
Alexandre Perrin 2017-06-11 10:07:23 +02:00
parent 90371f36a0
commit 6158aeb30a

View file

@ -15,6 +15,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <setjmp.h> #include <setjmp.h>
#include <jpeglib.h> #include <jpeglib.h>
@ -224,21 +225,25 @@ int mjpeg_decode_rgb32(struct mjpeg_decoder *mj,
return -1; return -1;
} }
uint8_t *rgb = calloc(mj->dinfo.image_width, 3);
if (!rgb) {
fprintf(stderr, "memory allocation failed\n");
return -1;
}
while (mj->dinfo.output_scanline < mj->dinfo.image_height) { while (mj->dinfo.output_scanline < mj->dinfo.image_height) {
uint8_t rgb[mj->dinfo.image_width * 3];
uint8_t *output = rgb;
uint8_t *scr = out + pitch * mj->dinfo.output_scanline; uint8_t *scr = out + pitch * mj->dinfo.output_scanline;
int i; int i;
jpeg_read_scanlines(&mj->dinfo, &output, 1); jpeg_read_scanlines(&mj->dinfo, &rgb, 1);
for (i = 0; i < mj->dinfo.image_width; i++) { for (i = 0; i < mj->dinfo.image_width; i++) {
scr[0] = output[2]; scr[0] = rgb[2];
scr[1] = output[1]; scr[1] = rgb[1];
scr[2] = output[0]; scr[2] = rgb[0];
scr += 4; scr += 4;
output += 3; rgb += 3;
} }
} }
free(rgb);
jpeg_finish_decompress(&mj->dinfo); jpeg_finish_decompress(&mj->dinfo);