Merge pull request #40 from kAworu/vla-cleanup
Refactoring to remove the use of c99 VLA
This commit is contained in:
commit
eca042a787
4 changed files with 29 additions and 14 deletions
19
demo/mjpeg.c
19
demo/mjpeg.c
|
@ -15,6 +15,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <jpeglib.h>
|
#include <jpeglib.h>
|
||||||
|
@ -224,21 +225,25 @@ int mjpeg_decode_rgb32(struct mjpeg_decoder *mj,
|
||||||
return -1;
|
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) {
|
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;
|
uint8_t *scr = out + pitch * mj->dinfo.output_scanline;
|
||||||
int i;
|
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++) {
|
for (i = 0; i < mj->dinfo.image_width; i++) {
|
||||||
scr[0] = output[2];
|
scr[0] = rgb[2];
|
||||||
scr[1] = output[1];
|
scr[1] = rgb[1];
|
||||||
scr[2] = output[0];
|
scr[2] = rgb[0];
|
||||||
scr += 4;
|
scr += 4;
|
||||||
output += 3;
|
rgb += 3;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(rgb);
|
||||||
|
|
||||||
jpeg_finish_decompress(&mj->dinfo);
|
jpeg_finish_decompress(&mj->dinfo);
|
||||||
|
|
||||||
|
|
|
@ -196,9 +196,7 @@ static void threshold(struct quirc *q)
|
||||||
threshold_s = THRESHOLD_S_MIN;
|
threshold_s = THRESHOLD_S_MIN;
|
||||||
|
|
||||||
for (y = 0; y < q->h; y++) {
|
for (y = 0; y < q->h; y++) {
|
||||||
int row_average[q->w];
|
memset(q->row_average, 0, q->w * sizeof(int));
|
||||||
|
|
||||||
memset(row_average, 0, sizeof(row_average));
|
|
||||||
|
|
||||||
for (x = 0; x < q->w; x++) {
|
for (x = 0; x < q->w; x++) {
|
||||||
int w, u;
|
int w, u;
|
||||||
|
@ -216,12 +214,12 @@ static void threshold(struct quirc *q)
|
||||||
avg_u = (avg_u * (threshold_s - 1)) /
|
avg_u = (avg_u * (threshold_s - 1)) /
|
||||||
threshold_s + row[u];
|
threshold_s + row[u];
|
||||||
|
|
||||||
row_average[w] += avg_w;
|
q->row_average[w] += avg_w;
|
||||||
row_average[u] += avg_u;
|
q->row_average[u] += avg_u;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (x = 0; x < q->w; x++) {
|
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))
|
(100 - THRESHOLD_T) / (200 * threshold_s))
|
||||||
row[x] = QUIRC_PIXEL_BLACK;
|
row[x] = QUIRC_PIXEL_BLACK;
|
||||||
else
|
else
|
||||||
|
|
13
lib/quirc.c
13
lib/quirc.c
|
@ -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 to avoid a double free */
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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;
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue