Added a row_average member to struct quirc.

Prep to remove the VLA from threshold()
This commit is contained in:
Alexandre Perrin 2017-06-11 09:55:11 +02:00
parent 7b26f6e193
commit 21f6863577
2 changed files with 13 additions and 1 deletions

View file

@ -37,9 +37,11 @@ struct quirc *quirc_new(void)
void quirc_destroy(struct quirc *q) void quirc_destroy(struct quirc *q)
{ {
free(q->image); 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 */
if (sizeof(*q->image) != sizeof(*q->pixels)) if (sizeof(*q->image) != sizeof(*q->pixels))
free(q->pixels); free(q->pixels);
free(q->row_average);
free(q); free(q);
} }
@ -47,6 +49,7 @@ int quirc_resize(struct quirc *q, int w, int h)
{ {
uint8_t *image = NULL; uint8_t *image = NULL;
quirc_pixel_t *pixels = NULL; quirc_pixel_t *pixels = NULL;
int *row_average = NULL;
/* /*
* XXX: w and h should be size_t (or at least unsigned) as negatives * 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; 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 */ /* alloc succeeded, update `q` with the new size and buffers */
q->w = w; q->w = w;
q->h = h; q->h = h;
@ -94,12 +102,15 @@ int quirc_resize(struct quirc *q, int w, int h)
free(q->pixels); free(q->pixels);
q->pixels = pixels; q->pixels = pixels;
} }
free(q->row_average);
q->row_average = row_average;
return 0; return 0;
/* NOTREACHED */ /* NOTREACHED */
fail: fail:
free(image); free(image);
free(pixels); free(pixels);
free(row_average);
return -1; return -1;
} }

View file

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