Add outer alloc memory buffer options.

Add outer_alloc flag
Add int quirc_set_image_buffer(struct quirc* q, uint8_t* image_buffer);
call quirc_set_image_buffer(struct quirc* q, uint8_t* image_buffer); before int quirc_resize(struct quirc *q, int w, int h);
tested on esp32s2 (esp-idf 4.2)
This commit is contained in:
hassenlin 2021-10-08 16:44:16 +08:00
parent 516d91a94d
commit 883f44d8b0
3 changed files with 51 additions and 16 deletions

View file

@ -36,7 +36,9 @@ struct quirc *quirc_new(void)
void quirc_destroy(struct quirc *q)
{
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)
@ -44,9 +46,23 @@ void quirc_destroy(struct quirc *q)
free(q->flood_fill_vars);
free(q);
}
int quirc_resize(struct quirc *q, int w, int h)
{
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;
@ -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.
*/
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 */
@ -81,6 +99,7 @@ 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.
*/
if (q->image)
(void)memcpy(image , q->image , min);
/* alloc a new buffer for q->pixels if needed */
@ -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;
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;
@ -133,6 +156,7 @@ int quirc_resize(struct quirc *q, int w, int h)
return 0;
/* NOTREACHED */
fail:
if (!q->outer_alloc)
free(image);
free(pixels);
free(vars);

View file

@ -36,6 +36,15 @@ 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.
*

View file

@ -103,6 +103,8 @@ struct quirc {
size_t num_flood_fill_vars;
struct quirc_flood_fill_vars *flood_fill_vars;
uint8_t outer_alloc;
};
/************************************************************************