Skip to content

Commit

Permalink
Make image/detector malloc free
Browse files Browse the repository at this point in the history
  • Loading branch information
kikuchan committed Jan 22, 2024
1 parent f0d216f commit 4bd8abf
Show file tree
Hide file tree
Showing 10 changed files with 206 additions and 137 deletions.
27 changes: 20 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
BUILDDIR?=$(abspath ./build/system)

#CFLAGS += -DUSE_MALLOC_BUFFER
#CFLAGS += -DNO_MALLOC
#CFLAGS += -DNO_PRINTF
#CFLAGS += -DNO_CALLBACK
#CFLAGS += -DNO_CANVAS_BUFFER

#CFLAGS += -DNO_KANJI_TABLE

#CFLAGS += -DNO_MQR
#CFLAGS += -DNO_RMQR

#CFLAGS += -DNO_DEBUG

.PHONY: all clean examples cli wasm win32 mac

all: cli

cli:
@BUILDDIR=$(BUILDDIR) $(MAKE) -C cli
@BUILDDIR=$(BUILDDIR) CFLAGS="$(CFLAGS)" $(MAKE) -C cli

install: cli
@BUILDDIR=$(BUILDDIR) $(MAKE) -C src install
@BUILDDIR=$(BUILDDIR) $(MAKE) -C cli install
@BUILDDIR=$(BUILDDIR) CFLAGS="$(CFLAGS)" $(MAKE) -C src install
@BUILDDIR=$(BUILDDIR) CFLAGS="$(CFLAGS)" $(MAKE) -C cli install

clean:
@BUILDDIR=$(BUILDDIR) $(MAKE) -C cli clean
@BUILDDIR=$(BUILDDIR) CFLAGS="$(CFLAGS)" $(MAKE) -C cli clean
-rmdir $(BUILDDIR)

wasm:
@BUILDDIR=$(abspath ./build/wasm) NO_SHLIB=1 $(MAKE) -C wasm clean all
@BUILDDIR=$(abspath ./build/wasm) NO_SHLIB=1 CFLAGS="$(CFLAGS)" $(MAKE) -C wasm clean all

win32:
@BUILDDIR=$(abspath ./build/win32) CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar NO_SHLIB=1 DLL=1 $(MAKE) clean cli
@BUILDDIR=$(abspath ./build/win32) CC=i686-w64-mingw32-gcc AR=i686-w64-mingw32-ar NO_SHLIB=1 DLL=1 CFLAGS="$(CFLAGS)" $(MAKE) clean cli

mac: cli
cp build/system/qrean build/system/qrean-detect ./dist/
Expand All @@ -29,4 +42,4 @@ dist: wasm win32
cp build/wasm/Qrean.* build/win32/*exe ./dist/

test: cli
@LD_LIBRARY_PATH=$(BUILDDIR) BUILDDIR=$(BUILDDIR) $(MAKE) -C tests
@LD_LIBRARY_PATH=$(BUILDDIR) BUILDDIR=$(BUILDDIR) CFLAGS="$(CFLAGS)" $(MAKE) -C tests
33 changes: 16 additions & 17 deletions cli/qrean-detect/qrean-detect.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "image.h"

image_t *img;
image_t image;

image_t *detected;
FILE *debug_out;
Expand Down Expand Up @@ -113,15 +113,11 @@ static void on_found(qrean_detector_perspective_t *warp, void *opaque)
fprintf(out, "%s\n", buffer);
}

void decode(image_t* img)
{
}

void done(pngle_t *pngle)
{
image_t *mono = image_clone(img);
CREATE_IMAGE_BY_CLONE(mono, &image);

image_digitize(mono, img, gamma_value);
image_digitize(&mono, &image, gamma_value);
// image_morphology_open(mono);
// image_morphology_close(mono);

Expand All @@ -132,23 +128,23 @@ void done(pngle_t *pngle)
gettimeofday(&tv, NULL);
for (int i = 0; i < n; i++) {
int num_candidates;
qrean_detector_qr_finder_candidate_t *candidates = qrean_detector_scan_qr_finder_pattern(mono, &num_candidates);
qrean_detector_qr_finder_candidate_t *candidates = qrean_detector_scan_qr_finder_pattern(&mono, &num_candidates);

if (flag_debug) {
// detected = image_clone(img);
detected = image_clone(mono);
// detected = image_clone(&image);
detected = image_clone(&mono);
for (int i = 0; i < num_candidates; i++) {
image_draw_filled_ellipse(detected, candidates[i].center, 5, 5, PIXEL(255, 0, 0));
image_draw_extent(detected, candidates[i].extent, PIXEL(255, 0, 0), 1);
}
}

found += qrean_detector_try_decode_qr(mono, candidates, num_candidates, on_found, NULL);
found += qrean_detector_try_decode_mqr(mono, candidates, num_candidates, on_found, NULL);
found += qrean_detector_try_decode_rmqr(mono, candidates, num_candidates, on_found, NULL);
found += qrean_detector_try_decode_tqr(mono, candidates, num_candidates, on_found, NULL);
found += qrean_detector_try_decode_qr(&mono, candidates, num_candidates, on_found, NULL);
found += qrean_detector_try_decode_mqr(&mono, candidates, num_candidates, on_found, NULL);
found += qrean_detector_try_decode_rmqr(&mono, candidates, num_candidates, on_found, NULL);
found += qrean_detector_try_decode_tqr(&mono, candidates, num_candidates, on_found, NULL);

found += qrean_detector_scan_barcodes(mono, on_found, NULL);
found += qrean_detector_scan_barcodes(&mono, on_found, NULL);
}
gettimeofday(&tv2, NULL);

Expand All @@ -160,6 +156,8 @@ void done(pngle_t *pngle)
fprintf(stderr, "Not found\n");
}
if (debug_out) image_save_as_png(detected, debug_out);

DESTROY_IMAGE(mono);
}

void draw_pixel(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h, const uint8_t rgba[4])
Expand All @@ -168,14 +166,15 @@ void draw_pixel(pngle_t *pngle, uint32_t x, uint32_t y, uint32_t w, uint32_t h,
uint8_t g = rgba[1];
uint8_t b = rgba[2];

image_draw_pixel(img, POINT(x, y), PIXEL(r, g, b));
image_draw_pixel(&image, POINT(x, y), PIXEL(r, g, b));
}

void init_screen(pngle_t *pngle, uint32_t w, uint32_t h)
{
W = w;
H = h;
img = new_image(W, H);

image_init(&image, W, H, calloc(W * H, sizeof(image_pixel_t)));
}

const char *progname;
Expand Down
39 changes: 21 additions & 18 deletions cli/qrean/qrean.c
Original file line number Diff line number Diff line change
Expand Up @@ -302,49 +302,50 @@ int main(int argc, char *argv[])
return -1;
}

qrean_t *qrean = new_qrean(code);
if (!qrean) {
qrean_t qrean = create_qrean(code);
if (!qrean_is_valid(&qrean)) {
fprintf(stderr, "Unknown code or version; %d\n", code);
return -1;
}

qrean_set_bitmap_scale(qrean, scale);
if (padding) qrean_set_bitmap_padding(qrean, *padding);
if (QREAN_IS_TYPE_QRFAMILY(qrean)) {
qrean_set_qr_version(qrean, version);
qrean_set_qr_errorlevel(qrean, level);
qrean_set_qr_maskpattern(qrean, mask);
qrean_set_bitmap_scale(&qrean, scale);
if (padding) qrean_set_bitmap_padding(&qrean, *padding);
if (QREAN_IS_TYPE_QRFAMILY(&qrean)) {
qrean_set_qr_version(&qrean, version);
qrean_set_qr_errorlevel(&qrean, level);
qrean_set_qr_maskpattern(&qrean, mask);

if (!qrean_check_qr_combination(qrean)) {
if (!qrean_check_qr_combination(&qrean)) {
fprintf(stderr, "Invalid combination of VERSION/LEVEL/MASK\n");
return -1;
}

qrean_set_eci_code(qrean, eci_code);
qrean_set_eci_code(&qrean, eci_code);
}

if (save_as == SAVE_AS_DEFAULT) save_as = isatty(fileno(out)) ? SAVE_AS_TXT : SAVE_AS_PNG;

size_t wrote = qrean_write_buffer(qrean, data, len, data_type);
size_t wrote = qrean_write_buffer(&qrean, data, len, data_type);
if (!wrote) {
fprintf(stderr, "Size exceed or mismatch\n");
return -1;
}

size_t width = qrean_get_bitmap_width(qrean);
size_t height = qrean_get_bitmap_height(qrean);
size_t width = qrean_get_bitmap_width(&qrean);
size_t height = qrean_get_bitmap_height(&qrean);
size_t size = width * height * 4;
image_t *img = new_image(width, height);

qrean_read_bitmap(qrean, img->buffer, size, 32);
CREATE_IMAGE(img, width, height);

qrean_read_bitmap(&qrean, img.buffer, size, 32);

switch (save_as) {
case SAVE_AS_PNG:
image_save_as_png(img, out);
image_save_as_png(&img, out);
break;

case SAVE_AS_PPM:
image_save_as_ppm(img, out);
image_save_as_ppm(&img, out);
break;

default:
Expand All @@ -353,12 +354,14 @@ int main(int argc, char *argv[])
unsigned int cp = GetConsoleOutputCP();
SetConsoleOutputCP(65001);
#endif
qrean_dump(qrean, out);
qrean_dump(&qrean, out);
#ifdef __WIN32
SetConsoleOutputCP(cp);
#endif
break;
}

DESTROY_IMAGE(img);

return 0;
}
14 changes: 0 additions & 14 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ SRCS += code_code93.c
SRCS += code_itf.c
SRCS += code_nw7.c

## Works only without NO_MALLOC
SRCS += image.c
SRCS += detector.c

Expand All @@ -45,19 +44,6 @@ CFLAGS += -Wall # -Werror
CFLAGS += -Wno-misleading-indentation
CFLAGS += -fPIC

#CFLAGS += -D USE_MALLOC_BUFFER
#CFLAGS += -D NO_MALLOC
#CFLAGS += -D NO_PRINTF
#CFLAGS += -D NO_CALLBACK
#CFLAGS += -D NO_CANVAS_BUFFER

#CFLAGS += -D NO_KANJI_TABLE

#CFLAGS += -D NO_MQR
#CFLAGS += -D NO_RMQR

#CFLAGS += -D NO_DEBUG

LDFLAGS += -lm
ifndef NO_DEBUG
CFLAGS += -g
Expand Down
Loading

0 comments on commit 4bd8abf

Please sign in to comment.