Skip to content

Commit

Permalink
Merge pull request #40 from kAworu/vla-cleanup
Browse files Browse the repository at this point in the history
Refactoring to remove the use of c99 VLA
  • Loading branch information
dlbeer committed Jun 11, 2017
2 parents 7b26f6e + d3780f3 commit eca042a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 14 deletions.
19 changes: 12 additions & 7 deletions demo/mjpeg.c
Expand Up @@ -15,6 +15,7 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include <jpeglib.h>
Expand Down Expand Up @@ -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);

Expand Down
10 changes: 4 additions & 6 deletions lib/identify.c
Expand Up @@ -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;
Expand All @@ -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
Expand Down
13 changes: 12 additions & 1 deletion lib/quirc.c
Expand Up @@ -37,16 +37,19 @@ 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);
}

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
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions lib/quirc_internal.h
Expand Up @@ -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;

Expand Down

0 comments on commit eca042a

Please sign in to comment.