Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add outer alloc memory buffer options. #120

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 39 additions & 15 deletions lib/quirc.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,38 @@ struct quirc *quirc_new(void)

void quirc_destroy(struct quirc *q)
{
free(q->image);
if (!q->outer_alloc)
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 (!QUIRC_PIXEL_ALIAS_IMAGE)
free(q->pixels);
free(q->flood_fill_vars);
free(q);
}

int quirc_resize(struct quirc *q, int w, int h)
{
uint8_t *image = NULL;
quirc_pixel_t *pixels = NULL;
int quirc_set_image_buffer(struct quirc* q, uint8_t* image_buffer) {
if (q->image == image_buffer) {
q->outer_alloc = (q->image == NULL)? 0 : 1;
return 0;
}
if (q->image != NULL && !q->outer_alloc) {
free(q->image);
q->image = NULL;
q->outer_alloc = 0;
}
if (image_buffer != NULL) {
q->image = image_buffer;
q->outer_alloc = 1;
}
return 0;
}
int quirc_resize(struct quirc* q, int w, int h) {
uint8_t* image = NULL;
quirc_pixel_t* pixels = NULL;
size_t num_vars;
size_t vars_byte_size;
struct quirc_flood_fill_vars *vars = NULL;
struct quirc_flood_fill_vars* vars = NULL;

/*
* XXX: w and h should be size_t (or at least unsigned) as negatives
Expand All @@ -66,9 +82,11 @@ int quirc_resize(struct quirc *q, int w, int h)
* alloc a new buffer for q->image. We avoid realloc(3) because we want
* on failure to be leave `q` in a consistant, unmodified state.
*/
image = calloc(w, h);
if (!image)
goto fail;
if (!q->outer_alloc) {
image = calloc(w , h);
if (!image)
goto fail;
}

/* compute the "old" (i.e. currently allocated) and the "new"
(i.e. requested) image dimensions */
Expand All @@ -81,11 +99,12 @@ int quirc_resize(struct quirc *q, int w, int h)
* old buffer when the new size is greater and (b) to write beyond the
* new buffer when the new size is smaller, hence the min computation.
*/
(void)memcpy(image, q->image, min);
if (q->image)
(void)memcpy(image , q->image , min);

/* alloc a new buffer for q->pixels if needed */
if (!QUIRC_PIXEL_ALIAS_IMAGE) {
pixels = calloc(newdim, sizeof(quirc_pixel_t));
pixels = calloc(newdim , sizeof(quirc_pixel_t));
if (!pixels)
goto fail;
}
Expand Down Expand Up @@ -120,8 +139,12 @@ int quirc_resize(struct quirc *q, int w, int h)
/* alloc succeeded, update `q` with the new size and buffers */
q->w = w;
q->h = h;
free(q->image);
q->image = image;
if(!q->outer_alloc) {
if (q->image != NULL)
free(q->image);
q->image = image;
}

if (!QUIRC_PIXEL_ALIAS_IMAGE) {
free(q->pixels);
q->pixels = pixels;
Expand All @@ -133,7 +156,8 @@ int quirc_resize(struct quirc *q, int w, int h)
return 0;
/* NOTREACHED */
fail:
free(image);
if (!q->outer_alloc)
free(image);
free(pixels);
free(vars);

Expand Down
11 changes: 10 additions & 1 deletion lib/quirc.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,22 @@ struct quirc *quirc_new(void);
/* Destroy a QR-code recognizer. */
void quirc_destroy(struct quirc *q);

/* Set the QR-code recognizer. use internal allocation memory or external
* allocation memory buffer.
* specified before call quirc_resize. use internal allocation memory buffer.
*
* This function returns 0 on success, or -1 if sufficient memory could
* not be allocated.
*/
int quirc_set_image_buffer(struct quirc* q, uint8_t* image_buffer);

/* Resize the QR-code recognizer. The size of an image must be
* specified before codes can be analyzed.
*
* This function returns 0 on success, or -1 if sufficient memory could
* not be allocated.
*/
int quirc_resize(struct quirc *q, int w, int h);
int quirc_resize(struct quirc* q, int w, int h);

/* These functions are used to process images for QR-code recognition.
* quirc_begin() must first be called to obtain access to a buffer into
Expand Down
2 changes: 2 additions & 0 deletions lib/quirc_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ struct quirc {

size_t num_flood_fill_vars;
struct quirc_flood_fill_vars *flood_fill_vars;

uint8_t outer_alloc;
};

/************************************************************************
Expand Down