From 21f6863577b31e50a344169950c7a053de40710d Mon Sep 17 00:00:00 2001 From: Alexandre Perrin Date: Sun, 11 Jun 2017 09:55:11 +0200 Subject: [PATCH] Added a row_average member to struct quirc. Prep to remove the VLA from threshold() --- lib/quirc.c | 13 ++++++++++++- lib/quirc_internal.h | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/quirc.c b/lib/quirc.c index a9da85d..48686a4 100644 --- a/lib/quirc.c +++ b/lib/quirc.c @@ -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 */ 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; } diff --git a/lib/quirc_internal.h b/lib/quirc_internal.h index 3df54f3..70d481d 100644 --- a/lib/quirc_internal.h +++ b/lib/quirc_internal.h @@ -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;