define a QUIRC_PIXEL_ALIAS_IMAGE constant to explicit conditional path

Before this patch, the "sizeof dance" was confusing, fix #54.
This commit is contained in:
Alexandre Perrin 2019-07-01 21:19:39 +01:00
parent 5d71b1853e
commit f700e456b4
3 changed files with 6 additions and 4 deletions

View file

@ -1071,7 +1071,7 @@ static void test_grouping(struct quirc *q, int i)
static void pixels_setup(struct quirc *q, uint8_t threshold) static void pixels_setup(struct quirc *q, uint8_t threshold)
{ {
if (sizeof(*q->image) == sizeof(*q->pixels)) { if (QUIRC_PIXEL_ALIAS_IMAGE) {
q->pixels = (quirc_pixel_t *)q->image; q->pixels = (quirc_pixel_t *)q->image;
} }

View file

@ -39,7 +39,7 @@ 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 /* 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 */ same size, so we need to be careful here to avoid a double free */
if (sizeof(*q->image) != sizeof(*q->pixels)) if (!QUIRC_PIXEL_ALIAS_IMAGE)
free(q->pixels); free(q->pixels);
free(q); free(q);
} }
@ -80,7 +80,7 @@ int quirc_resize(struct quirc *q, int w, int h)
(void)memcpy(image, q->image, min); (void)memcpy(image, q->image, min);
/* alloc a new buffer for q->pixels if needed */ /* alloc a new buffer for q->pixels if needed */
if (sizeof(*q->image) != sizeof(*q->pixels)) { if (!QUIRC_PIXEL_ALIAS_IMAGE) {
pixels = calloc(newdim, sizeof(quirc_pixel_t)); pixels = calloc(newdim, sizeof(quirc_pixel_t));
if (!pixels) if (!pixels)
goto fail; goto fail;
@ -91,7 +91,7 @@ int quirc_resize(struct quirc *q, int w, int h)
q->h = h; q->h = h;
free(q->image); free(q->image);
q->image = image; q->image = image;
if (sizeof(*q->image) != sizeof(*q->pixels)) { if (!QUIRC_PIXEL_ALIAS_IMAGE) {
free(q->pixels); free(q->pixels);
q->pixels = pixels; q->pixels = pixels;
} }

View file

@ -32,8 +32,10 @@
#define QUIRC_PERSPECTIVE_PARAMS 8 #define QUIRC_PERSPECTIVE_PARAMS 8
#if QUIRC_MAX_REGIONS < UINT8_MAX #if QUIRC_MAX_REGIONS < UINT8_MAX
#define QUIRC_PIXEL_ALIAS_IMAGE 1
typedef uint8_t quirc_pixel_t; typedef uint8_t quirc_pixel_t;
#elif QUIRC_MAX_REGIONS < UINT16_MAX #elif QUIRC_MAX_REGIONS < UINT16_MAX
#define QUIRC_PIXEL_ALIAS_IMAGE 0
typedef uint16_t quirc_pixel_t; typedef uint16_t quirc_pixel_t;
#else #else
#error "QUIRC_MAX_REGIONS > 65534 is not supported" #error "QUIRC_MAX_REGIONS > 65534 is not supported"