Merge pull request #40 from kAworu/vla-cleanup

Refactoring to remove the use of c99 VLA
This commit is contained in:
Daniel Beer 2017-06-11 22:30:33 +12:00 committed by GitHub
commit eca042a787
4 changed files with 29 additions and 14 deletions

View file

@ -15,6 +15,7 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <jpeglib.h>
@ -224,21 +225,25 @@ int mjpeg_decode_rgb32(struct mjpeg_decoder *mj,
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) {
uint8_t rgb[mj->dinfo.image_width * 3];
uint8_t *output = rgb;
uint8_t *scr = out + pitch * mj->dinfo.output_scanline;
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++) {
scr[0] = output[2];
scr[1] = output[1];
scr[2] = output[0];
scr[0] = rgb[2];
scr[1] = rgb[1];
scr[2] = rgb[0];
scr += 4;
output += 3;
rgb += 3;
}
}
free(rgb);
jpeg_finish_decompress(&mj->dinfo);

View file

@ -196,9 +196,7 @@ static void threshold(struct quirc *q)
threshold_s = THRESHOLD_S_MIN;
for (y = 0; y < q->h; y++) {
int row_average[q->w];
memset(row_average, 0, sizeof(row_average));
memset(q->row_average, 0, q->w * sizeof(int));
for (x = 0; x < q->w; x++) {
int w, u;
@ -216,12 +214,12 @@ static void threshold(struct quirc *q)
avg_u = (avg_u * (threshold_s - 1)) /
threshold_s + row[u];
row_average[w] += avg_w;
row_average[u] += avg_u;
q->row_average[w] += avg_w;
q->row_average[u] += avg_u;
}
for (x = 0; x < q->w; x++) {
if (row[x] < row_average[x] *
if (row[x] < q->row_average[x] *
(100 - THRESHOLD_T) / (200 * threshold_s))
row[x] = QUIRC_PIXEL_BLACK;
else

View file

@ -37,9 +37,11 @@ struct quirc *quirc_new(void)
void quirc_destroy(struct quirc *q)
{
free(q->image);
/* q->pixels may alias q->image when their type representation is of the
same size, so we need to be careful here to avoid a double free */
if (sizeof(*q->image) != sizeof(*q->pixels))
free(q->pixels);
free(q->row_average);
free(q);
}
@ -47,6 +49,7 @@ int quirc_resize(struct quirc *q, int w, int h)
{
uint8_t *image = NULL;
quirc_pixel_t *pixels = NULL;
int *row_average = NULL;
/*
* XXX: w and h should be size_t (or at least unsigned) as negatives
@ -85,6 +88,11 @@ int quirc_resize(struct quirc *q, int w, int h)
goto fail;
}
/* alloc a new buffer for q->row_average */
row_average = calloc(w, sizeof(int));
if (!row_average)
goto fail;
/* alloc succeeded, update `q` with the new size and buffers */
q->w = w;
q->h = h;
@ -94,12 +102,15 @@ int quirc_resize(struct quirc *q, int w, int h)
free(q->pixels);
q->pixels = pixels;
}
free(q->row_average);
q->row_average = row_average;
return 0;
/* NOTREACHED */
fail:
free(image);
free(pixels);
free(row_average);
return -1;
}

View file

@ -77,6 +77,7 @@ struct quirc_grid {
struct quirc {
uint8_t *image;
quirc_pixel_t *pixels;
int *row_average; /* used by threshold() */
int w;
int h;