From 08e51cf5bb62d2e8ed9c70bf60175749aa37e02b Mon Sep 17 00:00:00 2001 From: Robin Stuart Date: Tue, 19 Jul 2016 23:02:39 +0100 Subject: [PATCH] Separate PNG and BMP file handling routines and create common framework to allow addition of more output file formats --- Makefile | 240 ------- backend/CMakeLists.txt | 5 +- backend/bmp.c | 237 +++++++ backend/library.c | 25 +- backend/{maxipng.h => maxihex.h} | 0 backend/png.c | 1010 ------------------------------ backend/raster.c | 841 +++++++++++++++++++++++++ backend/zint.h | 15 +- 8 files changed, 1105 insertions(+), 1268 deletions(-) delete mode 100644 Makefile create mode 100644 backend/bmp.c rename backend/{maxipng.h => maxihex.h} (100%) create mode 100644 backend/raster.c diff --git a/Makefile b/Makefile deleted file mode 100644 index 2564a351..00000000 --- a/Makefile +++ /dev/null @@ -1,240 +0,0 @@ -# CMAKE generated file: DO NOT EDIT! -# Generated by "Unix Makefiles" Generator, CMake Version 3.2 - -# Default target executed when no arguments are given to make. -default_target: all -.PHONY : default_target - -# Allow only one "make -f Makefile2" at a time, but pass parallelism. -.NOTPARALLEL: - -#============================================================================= -# Special targets provided by cmake. - -# Disable implicit rules so canonical targets will work. -.SUFFIXES: - -# Remove some rules from gmake that .SUFFIXES does not remove. -SUFFIXES = - -.SUFFIXES: .hpux_make_needs_suffix_list - -# Suppress display of executed commands. -$(VERBOSE).SILENT: - -# A target that is always out of date. -cmake_force: -.PHONY : cmake_force - -#============================================================================= -# Set environment variables for the build. - -# The shell in which to execute make rules. -SHELL = /bin/sh - -# The CMake executable. -CMAKE_COMMAND = /usr/bin/cmake - -# The command to remove a file. -RM = /usr/bin/cmake -E remove -f - -# Escaping for special characters. -EQUALS = = - -# The top-level source directory on which CMake was run. -CMAKE_SOURCE_DIR = /home/hooper/zint/zint-code - -# The top-level build directory on which CMake was run. -CMAKE_BINARY_DIR = /home/hooper/zint/zint-code - -#============================================================================= -# Targets provided globally by CMake. - -# Special rule for the target install/strip -install/strip: preinstall - @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing the project stripped..." - /usr/bin/cmake -DCMAKE_INSTALL_DO_STRIP=1 -P cmake_install.cmake -.PHONY : install/strip - -# Special rule for the target install/strip -install/strip/fast: install/strip -.PHONY : install/strip/fast - -# Special rule for the target edit_cache -edit_cache: - @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "No interactive CMake dialog available..." - /usr/bin/cmake -E echo No\ interactive\ CMake\ dialog\ available. -.PHONY : edit_cache - -# Special rule for the target edit_cache -edit_cache/fast: edit_cache -.PHONY : edit_cache/fast - -# Special rule for the target rebuild_cache -rebuild_cache: - @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Running CMake to regenerate build system..." - /usr/bin/cmake -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) -.PHONY : rebuild_cache - -# Special rule for the target rebuild_cache -rebuild_cache/fast: rebuild_cache -.PHONY : rebuild_cache/fast - -# Special rule for the target install -install: preinstall - @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..." - /usr/bin/cmake -P cmake_install.cmake -.PHONY : install - -# Special rule for the target install -install/fast: preinstall/fast - @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Install the project..." - /usr/bin/cmake -P cmake_install.cmake -.PHONY : install/fast - -# Special rule for the target list_install_components -list_install_components: - @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Available install components are: \"Devel\" \"Unspecified\"" -.PHONY : list_install_components - -# Special rule for the target list_install_components -list_install_components/fast: list_install_components -.PHONY : list_install_components/fast - -# Special rule for the target install/local -install/local: preinstall - @$(CMAKE_COMMAND) -E cmake_echo_color --switch=$(COLOR) --cyan "Installing only the local directory..." - /usr/bin/cmake -DCMAKE_INSTALL_LOCAL_ONLY=1 -P cmake_install.cmake -.PHONY : install/local - -# Special rule for the target install/local -install/local/fast: install/local -.PHONY : install/local/fast - -# The main all target -all: cmake_check_build_system - $(CMAKE_COMMAND) -E cmake_progress_start /home/hooper/zint/zint-code/CMakeFiles /home/hooper/zint/zint-code/CMakeFiles/progress.marks - $(MAKE) -f CMakeFiles/Makefile2 all - $(CMAKE_COMMAND) -E cmake_progress_start /home/hooper/zint/zint-code/CMakeFiles 0 -.PHONY : all - -# The main clean target -clean: - $(MAKE) -f CMakeFiles/Makefile2 clean -.PHONY : clean - -# The main clean target -clean/fast: clean -.PHONY : clean/fast - -# Prepare targets for installation. -preinstall: all - $(MAKE) -f CMakeFiles/Makefile2 preinstall -.PHONY : preinstall - -# Prepare targets for installation. -preinstall/fast: - $(MAKE) -f CMakeFiles/Makefile2 preinstall -.PHONY : preinstall/fast - -# clear depends -depend: - $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 1 -.PHONY : depend - -#============================================================================= -# Target rules for targets named uninstall - -# Build rule for target. -uninstall: cmake_check_build_system - $(MAKE) -f CMakeFiles/Makefile2 uninstall -.PHONY : uninstall - -# fast build rule for target. -uninstall/fast: - $(MAKE) -f CMakeFiles/uninstall.dir/build.make CMakeFiles/uninstall.dir/build -.PHONY : uninstall/fast - -#============================================================================= -# Target rules for targets named zint - -# Build rule for target. -zint: cmake_check_build_system - $(MAKE) -f CMakeFiles/Makefile2 zint -.PHONY : zint - -# fast build rule for target. -zint/fast: - $(MAKE) -f backend/CMakeFiles/zint.dir/build.make backend/CMakeFiles/zint.dir/build -.PHONY : zint/fast - -#============================================================================= -# Target rules for targets named zint_frontend - -# Build rule for target. -zint_frontend: cmake_check_build_system - $(MAKE) -f CMakeFiles/Makefile2 zint_frontend -.PHONY : zint_frontend - -# fast build rule for target. -zint_frontend/fast: - $(MAKE) -f frontend/CMakeFiles/zint_frontend.dir/build.make frontend/CMakeFiles/zint_frontend.dir/build -.PHONY : zint_frontend/fast - -#============================================================================= -# Target rules for targets named QZint - -# Build rule for target. -QZint: cmake_check_build_system - $(MAKE) -f CMakeFiles/Makefile2 QZint -.PHONY : QZint - -# fast build rule for target. -QZint/fast: - $(MAKE) -f backend_qt4/CMakeFiles/QZint.dir/build.make backend_qt4/CMakeFiles/QZint.dir/build -.PHONY : QZint/fast - -#============================================================================= -# Target rules for targets named zint-qt - -# Build rule for target. -zint-qt: cmake_check_build_system - $(MAKE) -f CMakeFiles/Makefile2 zint-qt -.PHONY : zint-qt - -# fast build rule for target. -zint-qt/fast: - $(MAKE) -f frontend_qt4/CMakeFiles/zint-qt.dir/build.make frontend_qt4/CMakeFiles/zint-qt.dir/build -.PHONY : zint-qt/fast - -# Help Target -help: - @echo "The following are some of the valid targets for this Makefile:" - @echo "... all (the default if no target is provided)" - @echo "... clean" - @echo "... depend" - @echo "... install/strip" - @echo "... uninstall" - @echo "... edit_cache" - @echo "... rebuild_cache" - @echo "... install" - @echo "... list_install_components" - @echo "... install/local" - @echo "... zint" - @echo "... zint_frontend" - @echo "... QZint" - @echo "... zint-qt" -.PHONY : help - - - -#============================================================================= -# Special targets to cleanup operation of make. - -# Special rule to run CMake to check the build system integrity. -# No rule that depends on this can have commands that come from listfiles -# because they might be regenerated. -cmake_check_build_system: - $(CMAKE_COMMAND) -H$(CMAKE_SOURCE_DIR) -B$(CMAKE_BINARY_DIR) --check-build-system CMakeFiles/Makefile.cmake 0 -.PHONY : cmake_check_build_system - diff --git a/backend/CMakeLists.txt b/backend/CMakeLists.txt index 26b8b3b9..67beb4bf 100644 --- a/backend/CMakeLists.txt +++ b/backend/CMakeLists.txt @@ -4,11 +4,12 @@ project(zint) find_package(PNG) -set(zint_COMMON_SRCS common.c library.c render.c ps.c large.c reedsol.c gs1.c svg.c png.c) +set(zint_COMMON_SRCS common.c library.c render.c large.c reedsol.c gs1.c) set(zint_ONEDIM_SRCS code.c code128.c 2of5.c upcean.c telepen.c medical.c plessey.c rss.c) set(zint_POSTAL_SRCS postal.c auspost.c imail.c) set(zint_TWODIM_SRCS code16k.c dmatrix.c pdf417.c qr.c maxicode.c composite.c aztec.c code49.c code1.c gridmtx.c hanxin.c) -set(zint_SRCS ${zint_COMMON_SRCS} ${zint_ONEDIM_SRCS} ${zint_POSTAL_SRCS} ${zint_TWODIM_SRCS} ) +set(zint_OUTPUT_SRCS render.c ps.c svg.c bmp.c png.c raster.c) +set(zint_SRCS ${zint_OUTPUT_SRCS} ${zint_COMMON_SRCS} ${zint_ONEDIM_SRCS} ${zint_POSTAL_SRCS} ${zint_TWODIM_SRCS}) if(PNG_FOUND) include_directories( ${PNG_INCLUDES} ) diff --git a/backend/bmp.c b/backend/bmp.c new file mode 100644 index 00000000..6b3beab6 --- /dev/null +++ b/backend/bmp.c @@ -0,0 +1,237 @@ +/* bmp.c - Handles output to PNG file */ + +/* + libzint - the open source barcode library + Copyright (C) 2009-2016 Robin Stuart + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the project nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + */ + +#include +#include +#include +#include "common.h" +#include "bmp.h" /* Bitmap header structure */ +#include + +#define SSET "0123456789ABCDEF" + +int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) { + int i, row, column, errno; + int fgred, fggrn, fgblu, bgred, bggrn, bgblu; + int row_size; + unsigned int data_size; + unsigned char *bitmap_file_start, *bmp_posn; + FILE *bmp_file; + + switch (rotate_angle) { + case 0: + case 180: + symbol->bitmap_width = image_width; + symbol->bitmap_height = image_height; + break; + case 90: + case 270: + symbol->bitmap_width = image_height; + symbol->bitmap_height = image_width; + break; + } + + if (symbol->bitmap != NULL) + free(symbol->bitmap); + + row_size = 4 * floor((24 * symbol->bitmap_width + 31) / 32); + symbol->bitmap = (char *) malloc(row_size * symbol->bitmap_height); + + /* sort out colour options */ + to_upper((unsigned char*) symbol->fgcolour); + to_upper((unsigned char*) symbol->bgcolour); + + if (strlen(symbol->fgcolour) != 6) { + strcpy(symbol->errtxt, "Malformed foreground colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + if (strlen(symbol->bgcolour) != 6) { + strcpy(symbol->errtxt, "Malformed background colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + errno = is_sane(SSET, (unsigned char*) symbol->fgcolour, strlen(symbol->fgcolour)); + if (errno == ZINT_ERROR_INVALID_DATA) { + strcpy(symbol->errtxt, "Malformed foreground colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + errno = is_sane(SSET, (unsigned char*) symbol->bgcolour, strlen(symbol->fgcolour)); + if (errno == ZINT_ERROR_INVALID_DATA) { + strcpy(symbol->errtxt, "Malformed background colour target"); + return ZINT_ERROR_INVALID_OPTION; + } + + fgred = (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]); + fggrn = (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]); + fgblu = (16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]); + bgred = (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]); + bggrn = (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]); + bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]); + + /* Pixel Plotting */ + i = 0; + switch (rotate_angle) { + case 0: /* Plot the right way up */ + for (row = 0; row < image_height; row++) { + for (column = 0; column < image_width; column++) { + i = (3 * column) + (row * row_size); + switch (*(pixelbuf + (image_width * (image_height - row - 1)) + column)) { + case '1': + symbol->bitmap[i] = fgblu; + symbol->bitmap[i + 1] = fggrn; + symbol->bitmap[i + 2] = fgred; + break; + default: + symbol->bitmap[i] = bgblu; + symbol->bitmap[i + 1] = bggrn; + symbol->bitmap[i + 2] = bgred; + break; + + } + } + } + break; + case 90: /* Plot 90 degrees clockwise */ + for (row = 0; row < image_width; row++) { + for (column = 0; column < image_height; column++) { + i = (3 * column) + (row * row_size); + switch (*(pixelbuf + (image_width * (image_height - column - 1)) + (image_width - row - 1))) { + case '1': + symbol->bitmap[i] = fgblu; + symbol->bitmap[i + 1] = fggrn; + symbol->bitmap[i + 2] = fgred; + break; + default: + symbol->bitmap[i] = bgblu; + symbol->bitmap[i + 1] = bggrn; + symbol->bitmap[i + 2] = bgred; + break; + + } + } + } + break; + case 180: /* Plot upside down */ + for (row = 0; row < image_height; row++) { + for (column = 0; column < image_width; column++) { + i = (3 * column) + (row * row_size); + switch (*(pixelbuf + (image_width * row) + (image_width - column - 1))) { + case '1': + symbol->bitmap[i] = fgblu; + symbol->bitmap[i + 1] = fggrn; + symbol->bitmap[i + 2] = fgred; + break; + default: + symbol->bitmap[i] = bgblu; + symbol->bitmap[i + 1] = bggrn; + symbol->bitmap[i + 2] = bgred; + break; + + } + } + } + break; + case 270: /* Plot 90 degrees anti-clockwise */ + for (row = 0; row < image_width; row++) { + for (column = 0; column < image_height; column++) { + i = (3 * column) + (row * row_size); + switch (*(pixelbuf + (image_width * column) + row)) { + case '1': + symbol->bitmap[i] = fgblu; + symbol->bitmap[i + 1] = fggrn; + symbol->bitmap[i + 2] = fgred; + break; + default: + symbol->bitmap[i] = bgblu; + symbol->bitmap[i + 1] = bggrn; + symbol->bitmap[i + 2] = bgred; + break; + + } + } + } + break; + } + + data_size = symbol->bitmap_height * row_size; + symbol->bitmap_byte_length = data_size; + + bitmap_file_header_t file_header; + bitmap_info_header_t info_header; + + file_header.header_field = 0x4d42; // "BM" + file_header.file_size = sizeof(bitmap_file_header_t) + sizeof(bitmap_info_header_t) + data_size; + file_header.reserved = 0; + file_header.data_offset = sizeof(bitmap_file_header_t) + sizeof(bitmap_info_header_t); + + info_header.header_size = sizeof(bitmap_info_header_t); + info_header.width = symbol->bitmap_width; + info_header.height = symbol->bitmap_height; + info_header.colour_planes = 1; + info_header.bits_per_pixel = 24; + info_header.compression_method = 0; // BI_RGB + info_header.image_size = 0; + info_header.horiz_res = 0; + info_header.vert_res = 0; + info_header.colours = 0; + info_header.important_colours = 0; + + bitmap_file_start = (unsigned char*)malloc(file_header.file_size); + memset(bitmap_file_start, 0xff, file_header.file_size); + + bmp_posn = bitmap_file_start; + memcpy( bitmap_file_start, &file_header, sizeof(bitmap_file_header_t)); + bmp_posn += sizeof(bitmap_file_header_t); + memcpy(bmp_posn, &info_header, sizeof(bitmap_info_header_t) ); + bmp_posn += sizeof(bitmap_info_header_t); + memcpy(bmp_posn, symbol->bitmap, data_size); + + /* Open output file in binary mode */ + if ((symbol->output_options & BARCODE_STDOUT) != 0) { +#ifdef _MSC_VER + if (-1 == _setmode(_fileno(stdout), _O_BINARY)) { + strcpy(symbol->errtxt, "Can't open output file"); + return ZINT_ERROR_FILE_ACCESS; + } +#endif + bmp_file = stdout; + } else { + if (!(bmp_file = fopen(symbol->outfile, "wb"))) { + strcpy(symbol->errtxt, "Can't open output file"); + return ZINT_ERROR_FILE_ACCESS; + } + } + + fwrite(bitmap_file_start, file_header.file_size, 1, bmp_file); + fclose(bmp_file); + + return 0; +} \ No newline at end of file diff --git a/backend/library.c b/backend/library.c index f26e6eed..e038e107 100644 --- a/backend/library.c +++ b/backend/library.c @@ -183,15 +183,10 @@ extern int code_one(struct zint_symbol *symbol, unsigned char source[], int leng extern int grid_matrix(struct zint_symbol *symbol, const unsigned char source[], int length); /* Grid Matrix */ extern int han_xin(struct zint_symbol * symbol, const unsigned char source[], int length); /* Han Xin */ -#ifndef NO_PNG -extern int png_handle(struct zint_symbol *symbol, int rotate_angle); -#endif - -extern int render_plot(struct zint_symbol *symbol, float width, float height); - -extern int bmp_handle(struct zint_symbol *symbol, int rotate_angle); -extern int ps_plot(struct zint_symbol *symbol); -extern int svg_plot(struct zint_symbol *symbol); +extern int plot_raster(struct zint_symbol *symbol, int rotate_angle, int file_type); /* Plot to PNG or BMP */ +extern int render_plot(struct zint_symbol *symbol, float width, float height); /* Plot to gLabels */ +extern int ps_plot(struct zint_symbol *symbol); /* Plot to EPS */ +extern int svg_plot(struct zint_symbol *symbol); /* Plot to SVG */ void error_tag(char error_string[], int error_number) { char error_buffer[100]; @@ -943,14 +938,18 @@ int ZBarcode_Print(struct zint_symbol *symbol, int rotate_angle) { output[3] = '\0'; to_upper((unsigned char*) output); -#ifndef NO_PNG if (!(strcmp(output, "PNG"))) { if (symbol->scale < 1.0) { symbol->text[0] = '\0'; } - error_number = png_handle(symbol, rotate_angle); + error_number = plot_raster(symbol, rotate_angle, OUT_PNG_FILE); + } else + if (!(strcmp(output, "BMP"))) { + if (symbol->scale < 1.0) { + symbol->text[0] = '\0'; + } + error_number = plot_raster(symbol, rotate_angle, OUT_BMP_FILE); } else -#endif if (!(strcmp(output, "TXT"))) { error_number = dump_plot(symbol); } else @@ -988,7 +987,7 @@ int ZBarcode_Buffer(struct zint_symbol *symbol, int rotate_angle) { return ZINT_ERROR_INVALID_OPTION; } - error_number = bmp_handle(symbol, rotate_angle); + error_number = plot_raster(symbol, rotate_angle, OUT_BMP_FILE); error_tag(symbol->errtxt, error_number); return error_number; } diff --git a/backend/maxipng.h b/backend/maxihex.h similarity index 100% rename from backend/maxipng.h rename to backend/maxihex.h diff --git a/backend/png.c b/backend/png.c index 68c9d131..cb08dcd5 100644 --- a/backend/png.c +++ b/backend/png.c @@ -39,29 +39,13 @@ #include #include "common.h" -#ifdef _MSC_VER -#include -#endif /* _MSC_VER */ - #ifndef NO_PNG #include #include #include -#endif /* NO_PNG */ -#include "maxipng.h" /* Maxicode shapes */ - -#include "font.h" /* Font for human readable text */ - -#include "bmp.h" /* Bitmap header structure */ -#include #define SSET "0123456789ABCDEF" -#define PNG_DATA 100 -#define BMP_DATA 200 - -#ifndef NO_PNG - struct mainprog_info_type { long width; long height; @@ -314,997 +298,3 @@ int png_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width return 0; } #endif /* NO_PNG */ - -int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) { - int i, row, column, errno; - int fgred, fggrn, fgblu, bgred, bggrn, bgblu; - int row_size; - unsigned int data_size; - unsigned char *bitmap_file_start, *bmp_posn; - FILE *bmp_file; - - switch (rotate_angle) { - case 0: - case 180: - symbol->bitmap_width = image_width; - symbol->bitmap_height = image_height; - break; - case 90: - case 270: - symbol->bitmap_width = image_height; - symbol->bitmap_height = image_width; - break; - } - - if (symbol->bitmap != NULL) - free(symbol->bitmap); - - row_size = 4 * floor((24 * symbol->bitmap_width + 31) / 32); - symbol->bitmap = (char *) malloc(row_size * symbol->bitmap_height); - - /* sort out colour options */ - to_upper((unsigned char*) symbol->fgcolour); - to_upper((unsigned char*) symbol->bgcolour); - - if (strlen(symbol->fgcolour) != 6) { - strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ZINT_ERROR_INVALID_OPTION; - } - if (strlen(symbol->bgcolour) != 6) { - strcpy(symbol->errtxt, "Malformed background colour target"); - return ZINT_ERROR_INVALID_OPTION; - } - errno = is_sane(SSET, (unsigned char*) symbol->fgcolour, strlen(symbol->fgcolour)); - if (errno == ZINT_ERROR_INVALID_DATA) { - strcpy(symbol->errtxt, "Malformed foreground colour target"); - return ZINT_ERROR_INVALID_OPTION; - } - errno = is_sane(SSET, (unsigned char*) symbol->bgcolour, strlen(symbol->fgcolour)); - if (errno == ZINT_ERROR_INVALID_DATA) { - strcpy(symbol->errtxt, "Malformed background colour target"); - return ZINT_ERROR_INVALID_OPTION; - } - - fgred = (16 * ctoi(symbol->fgcolour[0])) + ctoi(symbol->fgcolour[1]); - fggrn = (16 * ctoi(symbol->fgcolour[2])) + ctoi(symbol->fgcolour[3]); - fgblu = (16 * ctoi(symbol->fgcolour[4])) + ctoi(symbol->fgcolour[5]); - bgred = (16 * ctoi(symbol->bgcolour[0])) + ctoi(symbol->bgcolour[1]); - bggrn = (16 * ctoi(symbol->bgcolour[2])) + ctoi(symbol->bgcolour[3]); - bgblu = (16 * ctoi(symbol->bgcolour[4])) + ctoi(symbol->bgcolour[5]); - - /* Pixel Plotting */ - i = 0; - switch (rotate_angle) { - case 0: /* Plot the right way up */ - for (row = 0; row < image_height; row++) { - for (column = 0; column < image_width; column++) { - i = (3 * column) + (row * row_size); - switch (*(pixelbuf + (image_width * (image_height - row - 1)) + column)) { - case '1': - symbol->bitmap[i] = fgblu; - symbol->bitmap[i + 1] = fggrn; - symbol->bitmap[i + 2] = fgred; - break; - default: - symbol->bitmap[i] = bgblu; - symbol->bitmap[i + 1] = bggrn; - symbol->bitmap[i + 2] = bgred; - break; - - } - } - } - break; - case 90: /* Plot 90 degrees clockwise */ - for (row = 0; row < image_width; row++) { - for (column = 0; column < image_height; column++) { - i = (3 * column) + (row * row_size); - switch (*(pixelbuf + (image_width * (image_height - column - 1)) + (image_width - row - 1))) { - case '1': - symbol->bitmap[i] = fgblu; - symbol->bitmap[i + 1] = fggrn; - symbol->bitmap[i + 2] = fgred; - break; - default: - symbol->bitmap[i] = bgblu; - symbol->bitmap[i + 1] = bggrn; - symbol->bitmap[i + 2] = bgred; - break; - - } - } - } - break; - case 180: /* Plot upside down */ - for (row = 0; row < image_height; row++) { - for (column = 0; column < image_width; column++) { - i = (3 * column) + (row * row_size); - switch (*(pixelbuf + (image_width * row) + (image_width - column - 1))) { - case '1': - symbol->bitmap[i] = fgblu; - symbol->bitmap[i + 1] = fggrn; - symbol->bitmap[i + 2] = fgred; - break; - default: - symbol->bitmap[i] = bgblu; - symbol->bitmap[i + 1] = bggrn; - symbol->bitmap[i + 2] = bgred; - break; - - } - } - } - break; - case 270: /* Plot 90 degrees anti-clockwise */ - for (row = 0; row < image_width; row++) { - for (column = 0; column < image_height; column++) { - i = (3 * column) + (row * row_size); - switch (*(pixelbuf + (image_width * column) + row)) { - case '1': - symbol->bitmap[i] = fgblu; - symbol->bitmap[i + 1] = fggrn; - symbol->bitmap[i + 2] = fgred; - break; - default: - symbol->bitmap[i] = bgblu; - symbol->bitmap[i + 1] = bggrn; - symbol->bitmap[i + 2] = bgred; - break; - - } - } - } - break; - } - - data_size = symbol->bitmap_height * row_size; - symbol->bitmap_byte_length = data_size; - - bitmap_file_header_t file_header; - bitmap_info_header_t info_header; - - file_header.header_field = 0x4d42; // "BM" - file_header.file_size = sizeof(bitmap_file_header_t) + sizeof(bitmap_info_header_t) + data_size; - file_header.reserved = 0; - file_header.data_offset = sizeof(bitmap_file_header_t) + sizeof(bitmap_info_header_t); - - info_header.header_size = sizeof(bitmap_info_header_t); - info_header.width = symbol->bitmap_width; - info_header.height = symbol->bitmap_height; - info_header.colour_planes = 1; - info_header.bits_per_pixel = 24; - info_header.compression_method = 0; // BI_RGB - info_header.image_size = 0; - info_header.horiz_res = 0; - info_header.vert_res = 0; - info_header.colours = 0; - info_header.important_colours = 0; - - bitmap_file_start = (unsigned char*)malloc(file_header.file_size); - memset(bitmap_file_start, 0xff, file_header.file_size); - - bmp_posn = bitmap_file_start; - memcpy( bitmap_file_start, &file_header, sizeof(bitmap_file_header_t)); - bmp_posn += sizeof(bitmap_file_header_t); - memcpy(bmp_posn, &info_header, sizeof(bitmap_info_header_t) ); - bmp_posn += sizeof(bitmap_info_header_t); - memcpy(bmp_posn, symbol->bitmap, data_size); - - /* Open output file in binary mode */ - if ((symbol->output_options & BARCODE_STDOUT) != 0) { -#ifdef _MSC_VER - if (-1 == _setmode(_fileno(stdout), _O_BINARY)) { - strcpy(symbol->errtxt, "Can't open output file"); - return ZINT_ERROR_FILE_ACCESS; - } -#endif - bmp_file = stdout; - } else { - if (!(bmp_file = fopen(symbol->outfile, "wb"))) { - strcpy(symbol->errtxt, "Can't open output file"); - return ZINT_ERROR_FILE_ACCESS; - } - } - - fwrite(bitmap_file_start, file_header.file_size, 1, bmp_file); - fclose(bmp_file); - - return 0; -} - -int png_to_file(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle, int image_type) { - int error_number; - float scaler = symbol->scale; - char *scaled_pixelbuf; - int horiz, vert, i; - int scale_width, scale_height; - - if (scaler == 0) { - scaler = 0.5; - } - scale_width = image_width * scaler; - scale_height = image_height * scaler; - - /* Apply scale options by creating another pixel buffer */ - if (!(scaled_pixelbuf = (char *) malloc(scale_width * scale_height))) { - printf("Insufficient memory for pixel buffer"); - return ZINT_ERROR_ENCODING_PROBLEM; - } else { - for (i = 0; i < (scale_width * scale_height); i++) { - *(scaled_pixelbuf + i) = '0'; - } - } - - for (vert = 0; vert < scale_height; vert++) { - for (horiz = 0; horiz < scale_width; horiz++) { - *(scaled_pixelbuf + (vert * scale_width) + horiz) = *(pixelbuf + ((int) (vert / scaler) * image_width) + (int) (horiz / scaler)); - } - } - - if (image_type == PNG_DATA) { -#ifndef NO_PNG - error_number = png_pixel_plot(symbol, scale_height, scale_width, scaled_pixelbuf, rotate_angle); -#else - return ZINT_ERROR_INVALID_OPTION; -#endif - } else { - error_number = bmp_pixel_plot(symbol, scale_height, scale_width, scaled_pixelbuf, rotate_angle); - } - - free(scaled_pixelbuf); - - return error_number; -} - -void draw_bar(char *pixelbuf, int xpos, int xlen, int ypos, int ylen, int image_width, int image_height) { - /* Draw a rectangle */ - int i, j, png_ypos; - - png_ypos = image_height - ypos - ylen; - /* This fudge is needed because EPS measures height from the bottom up but - PNG measures y position from the top down */ - - for (i = (xpos); i < (xpos + xlen); i++) { - for (j = (png_ypos); j < (png_ypos + ylen); j++) { - *(pixelbuf + (image_width * j) + i) = '1'; - } - } -} - -int bullseye_pixel(int row, int col) { - int block_val, block_pos, return_val; - - block_val = bullseye_compressed[(row * 12) + (col / 8)]; - return_val = 0; - block_pos = col % 8; - - if (block_val & (0x80 >> block_pos)) { - return_val = 1; - } - - return return_val; -} - -void draw_bullseye(char *pixelbuf, int image_width, int xoffset, int yoffset) { - /* Central bullseye in Maxicode symbols */ - int i, j; - - for (j = 103; j < 196; j++) { - for (i = 0; i < 93; i++) { - if (bullseye_pixel(j - 103, i)) { - /* if(bullseye[(((j - 103) * 93) + i)] == 1) { */ - *(pixelbuf + (image_width * j) + (image_width * yoffset) + i + 99 + xoffset) = '1'; - } - } - } -} - -void draw_hexagon(char *pixelbuf, int image_width, int xposn, int yposn) { - /* Put a hexagon into the pixel buffer */ - int i, j; - - for (i = 0; i < 12; i++) { - for (j = 0; j < 10; j++) { - if (hexagon[(i * 10) + j] == 1) { - *(pixelbuf + (image_width * i) + (image_width * yposn) + xposn + j) = '1'; - } - } - } -} - -void draw_letter(char *pixelbuf, unsigned char letter, int xposn, int yposn, int textflags, int image_width, int image_height) { - /* Put a letter into a position */ - int skip, x, y, glyph_no, max_x, max_y; - - skip = 0; - - if (letter < 33) { - skip = 1; - } - - if ((letter > 127) && (letter < 161)) { - skip = 1; - } - - if (xposn < 0 || yposn < 0) { - skip = 1; - } - - if (skip == 0) { - if (letter > 128) { - glyph_no = letter - 66; - } else { - glyph_no = letter - 33; - } - - - switch (textflags) { - case 1: // small font 5x9 - max_x = 5; - max_y = 9; - - if (xposn + max_x >= image_width) { - max_x = image_width - xposn - 1; - } - - if (yposn + max_y >= image_height) { - max_y = image_height - yposn - 1; - } - - for (y = 0; y < max_y; y++) { - for (x = 0; x < max_x; x++) { - if (small_font[(glyph_no * 9) + y] & (0x10 >> x)) { - *(pixelbuf + (y * image_width) + (yposn * image_width) + xposn + x) = '1'; - } - } - } - break; - - case 2: // bold font -> twice the regular font - { - char * linePtr; - max_x = 7; - max_y = 14; - - if (xposn + max_x + 1 >= image_width) { - max_x = image_width - xposn - 2; - } - - if (yposn + max_y >= image_height) { - max_y = image_height - yposn - 1; - } - - linePtr = pixelbuf + (yposn * image_width) + xposn + 1; - for (y = 0; y < max_y; y++) { - char * pixelPtr = linePtr; - int extra_dot = 0; - for (x = 0; x < 7; x++) { - if (ascii_font[(glyph_no * 14) + y] & (0x40 >> x)) { - *pixelPtr = '1'; - extra_dot = 1; - } else { - if (extra_dot) { - *pixelPtr = '1'; - } - - extra_dot = 0; - } - - ++pixelPtr; - } - - if (extra_dot) { - *pixelPtr = '1'; - } - - linePtr += image_width; - } - } - break; - - default: // regular font 7x15 - max_x = 7; - max_y = 14; - - if (xposn + max_x >= image_width) { - max_x = image_width - xposn - 1; - } - - if (yposn + max_y >= image_height) { - max_y = image_height - yposn - 1; - } - - for (y = 0; y < max_y; y++) { - for (x = 0; x < 7; x++) { - if (ascii_font[(glyph_no * 14) + y] & (0x40 >> x)) { - *(pixelbuf + (y * image_width) + (yposn * image_width) + xposn + x) = '1'; - } - } - } - break; - } - } -} - -/* Plot a string into the pixel buffer */ -void draw_string(char *pixbuf, char input_string[], int xposn, int yposn, int textflags, int image_width, int image_height) { - int i, string_length, string_left_hand, letter_width = 7; - - switch (textflags) { - case 1: // small font 5x9 - letter_width = 5; - break; - - case 2: // bold font -> width of the regular font + 1 extra dot + 1 extra space - letter_width = 9; - break; - - default: // regular font 7x15 - letter_width = 7; - break; - } - - string_length = strlen(input_string); - string_left_hand = xposn - ((letter_width * string_length) / 2); - - for (i = 0; i < string_length; i++) { - draw_letter(pixbuf, input_string[i], string_left_hand + (i * letter_width), yposn, textflags, image_width, image_height); - } - -} - -int maxi_png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) { - int i, row, column, xposn, yposn; - int image_height, image_width; - char *pixelbuf; - int error_number; - int xoffset, yoffset; - - xoffset = symbol->border_width + symbol->whitespace_width; - yoffset = symbol->border_width; - image_width = 300 + (2 * xoffset * 2); - image_height = 300 + (2 * yoffset * 2); - - if (!(pixelbuf = (char *) malloc(image_width * image_height))) { - printf("Insifficient memory for pixel buffer"); - return ZINT_ERROR_ENCODING_PROBLEM; - } else { - for (i = 0; i < (image_width * image_height); i++) { - *(pixelbuf + i) = '0'; - } - } - - draw_bullseye(pixelbuf, image_width, (2 * xoffset), (2 * yoffset)); - - for (row = 0; row < symbol->rows; row++) { - yposn = row * 9; - for (column = 0; column < symbol->width; column++) { - xposn = column * 10; - if (module_is_set(symbol, row, column)) { - if (row & 1) { - /* Odd (reduced) row */ - xposn += 5; - draw_hexagon(pixelbuf, image_width, xposn + (2 * xoffset), yposn + (2 * yoffset)); - } else { - /* Even (full) row */ - draw_hexagon(pixelbuf, image_width, xposn + (2 * xoffset), yposn + (2 * yoffset)); - } - } - } - } - - if (((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { - /* boundary bars */ - draw_bar(pixelbuf, 0, image_width, 0, symbol->border_width * 2, image_width, image_height); - draw_bar(pixelbuf, 0, image_width, 300 + (symbol->border_width * 2), symbol->border_width * 2, image_width, image_height); - } - - if ((symbol->output_options & BARCODE_BOX) != 0) { - /* side bars */ - draw_bar(pixelbuf, 0, symbol->border_width * 2, 0, image_height, image_width, image_height); - draw_bar(pixelbuf, 300 + ((symbol->border_width + symbol->whitespace_width + symbol->whitespace_width) * 2), symbol->border_width * 2, 0, image_height, image_width, image_height); - } - - error_number = png_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); - free(pixelbuf); - return error_number; -} - -/* Convert UTF-8 to Latin1 Codepage for the interpretation line */ -void to_latin1(unsigned char source[], unsigned char preprocessed[]) { - int j, i, input_length; - - input_length = ustrlen(source); - - j = 0; - i = 0; - while (i < input_length) { - switch (source[i]) { - case 0xC2: - /* UTF-8 C2xxh */ - /* Character range: C280h (latin: 80h) to C2BFh (latin: BFh) */ - i++; - preprocessed[j] = source[i]; - j++; - break; - case 0xC3: - /* UTF-8 C3xx */ - /* Character range: C380h (latin: C0h) to C3BFh (latin: FFh) */ - i++; - preprocessed[j] = source[i] + 64; - j++; - break; - default: - /* Process ASCII (< 80h), all other unicode points are ignored */ - if (source[i] < 128) { - preprocessed[j] = source[i]; - j++; - } - break; - } - i++; - } - preprocessed[j] = '\0'; - - return; -} - -int png_plot(struct zint_symbol *symbol, int rotate_angle, int data_type) { - int textdone, main_width, comp_offset, large_bar_count; - char textpart[10], addon[6]; - float addon_text_posn, preset_height, large_bar_height; - int i, r, textoffset, yoffset, xoffset, latch, image_width, image_height; - char *pixelbuf; - int addon_latch = 0, textflags = 0; - int this_row, block_width, plot_height, plot_yposn, textpos; - float row_height, row_posn; - int error_number; - int default_text_posn; - int next_yposn; -#ifndef _MSC_VER - unsigned char local_text[ustrlen(symbol->text) + 1]; -#else - unsigned char* local_text = (unsigned char*) _alloca(ustrlen(symbol->text) + 1); -#endif - - if (symbol->show_hrt != 0) { - /* Copy text from symbol */ - to_latin1(symbol->text, local_text); - } else { - /* No text needed */ - switch (symbol->symbology) { - case BARCODE_EANX: - case BARCODE_EANX_CC: - case BARCODE_ISBNX: - case BARCODE_UPCA: - case BARCODE_UPCE: - case BARCODE_UPCA_CC: - case BARCODE_UPCE_CC: - /* For these symbols use dummy text to ensure formatting is done - * properly even if no text is required */ - for (i = 0; i < ustrlen(symbol->text); i++) { - if (symbol->text[i] == '+') { - local_text[i] = '+'; - } else { - local_text[i] = ' '; - } - local_text[ustrlen(symbol->text)] = '\0'; - } - break; - default: - /* For everything else, just remove the text */ - local_text[0] = '\0'; - break; - } - } - - textdone = 0; - main_width = symbol->width; - strcpy(addon, ""); - comp_offset = 0; - addon_text_posn = 0.0; - row_height = 0; - if (symbol->output_options & SMALL_TEXT) { - textflags = 1; - } - else if (symbol->output_options & BOLD_TEXT) { - textflags = 2; - } - - if (symbol->height == 0) { - symbol->height = 50; - } - - large_bar_count = 0; - preset_height = 0.0; - for (i = 0; i < symbol->rows; i++) { - preset_height += symbol->row_height[i]; - if (symbol->row_height[i] == 0) { - large_bar_count++; - } - } - - if (large_bar_count == 0) { - symbol->height = preset_height; - large_bar_height = 10; - } else { - large_bar_height = (symbol->height - preset_height) / large_bar_count; - } - - while (!(module_is_set(symbol, symbol->rows - 1, comp_offset))) { - comp_offset++; - } - - /* Certain symbols need whitespace otherwise characters get chopped off the sides */ - if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) - || (symbol->symbology == BARCODE_ISBNX)) { - switch (ustrlen(local_text)) { - case 13: /* EAN 13 */ - case 16: - case 19: - if (symbol->whitespace_width == 0) { - symbol->whitespace_width = 10; - } - main_width = 96 + comp_offset; - break; - default: - main_width = 68 + comp_offset; - } - } - - if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { - if (symbol->whitespace_width == 0) { - symbol->whitespace_width = 10; - main_width = 96 + comp_offset; - } - } - - if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { - if (symbol->whitespace_width == 0) { - symbol->whitespace_width = 10; - main_width = 51 + comp_offset; - } - } - - latch = 0; - r = 0; - /* Isolate add-on text */ - if (is_extendable(symbol->symbology)) { - for (i = 0; i < ustrlen(local_text); i++) { - if (latch == 1) { - addon[r] = local_text[i]; - r++; - } - if (symbol->text[i] == '+') { - latch = 1; - } - } - } - addon[r] = '\0'; - - if (ustrlen(local_text) != 0) { - textoffset = 9; - } else { - textoffset = 0; - } - - xoffset = symbol->border_width + symbol->whitespace_width; - yoffset = symbol->border_width; - image_width = 2 * (symbol->width + xoffset + xoffset); - image_height = 2 * (symbol->height + textoffset + yoffset + yoffset); - - if (!(pixelbuf = (char *) malloc(image_width * image_height))) { - printf("Insufficient memory for pixel buffer"); - return ZINT_ERROR_ENCODING_PROBLEM; - } else { - for (i = 0; i < (image_width * image_height); i++) { - *(pixelbuf + i) = '0'; - } - } - - if (((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { - default_text_posn = image_height - 17; - } else { - default_text_posn = image_height - 17 - symbol->border_width - symbol->border_width; - } - - row_posn = textoffset + yoffset; - next_yposn = textoffset + yoffset; - row_height = 0; - - /* Plot the body of the symbol to the pixel buffer */ - for (r = 0; r < symbol->rows; r++) { - this_row = symbol->rows - r - 1; /* invert r otherwise plots upside down */ - row_posn += row_height; - plot_yposn = next_yposn; - if (symbol->row_height[this_row] == 0) { - row_height = large_bar_height; - } else { - row_height = symbol->row_height[this_row]; - } - next_yposn = (int) (row_posn + row_height); - plot_height = next_yposn - plot_yposn; - - i = 0; - if (module_is_set(symbol, this_row, 0)) { - latch = 1; - } else { - latch = 0; - } - - do { - block_width = 0; - do { - block_width++; - } while (module_is_set(symbol, this_row, i + block_width) == module_is_set(symbol, this_row, i)); - if ((addon_latch == 0) && (r == 0) && (i > main_width)) { - plot_height = (int) (row_height - 5.0); - plot_yposn = (int) (row_posn - 5.0); - addon_text_posn = row_posn + row_height - 8.0; - addon_latch = 1; - } - if (latch == 1) { - /* a bar */ - draw_bar(pixelbuf, (i + xoffset) * 2, block_width * 2, plot_yposn * 2, plot_height * 2, image_width, image_height); - latch = 0; - } else { - /* a space */ - latch = 1; - } - i += block_width; - - } while (i < symbol->width); - } - - xoffset += comp_offset; - - if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) || (symbol->symbology == BARCODE_ISBNX)) { - /* guard bar extensions and text formatting for EAN8 and EAN13 */ - switch (ustrlen(local_text)) { - case 8: /* EAN-8 */ - case 11: - case 14: - draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (32 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (34 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (64 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (66 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - for (i = 0; i < 4; i++) { - textpart[i] = local_text[i]; - } - textpart[4] = '\0'; - textpos = 2 * (17 + xoffset); - - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - for (i = 0; i < 4; i++) { - textpart[i] = local_text[i + 4]; - } - textpart[4] = '\0'; - textpos = 2 * (50 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - textdone = 1; - switch (strlen(addon)) { - case 2: - textpos = 2 * (xoffset + 86); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); - break; - case 5: - textpos = 2 * (xoffset + 100); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); - break; - } - - break; - case 13: /* EAN 13 */ - case 16: - case 19: - draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (92 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (94 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - - textpart[0] = local_text[0]; - textpart[1] = '\0'; - textpos = 2 * (-7 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - for (i = 0; i < 6; i++) { - textpart[i] = local_text[i + 1]; - } - textpart[6] = '\0'; - textpos = 2 * (24 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - for (i = 0; i < 6; i++) { - textpart[i] = local_text[i + 7]; - } - textpart[6] = '\0'; - textpos = 2 * (71 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - textdone = 1; - switch (strlen(addon)) { - case 2: - textpos = 2 * (xoffset + 114); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); - break; - case 5: - textpos = 2 * (xoffset + 128); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); - break; - } - break; - - } - } - - if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { - /* guard bar extensions and text formatting for UPCA */ - latch = 1; - - i = 0 + comp_offset; - do { - block_width = 0; - do { - block_width++; - } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); - if (latch == 1) { - /* a bar */ - draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - latch = 0; - } else { - /* a space */ - latch = 1; - } - i += block_width; - } while (i < 11 + comp_offset); - draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - latch = 1; - i = 85 + comp_offset; - do { - block_width = 0; - do { - block_width++; - } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); - if (latch == 1) { - /* a bar */ - draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - latch = 0; - } else { - /* a space */ - latch = 1; - } - i += block_width; - } while (i < 96 + comp_offset); - textpart[0] = local_text[0]; - textpart[1] = '\0'; - textpos = 2 * (-5 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - for (i = 0; i < 5; i++) { - textpart[i] = local_text[i + 1]; - } - textpart[5] = '\0'; - textpos = 2 * (27 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - for (i = 0; i < 5; i++) { - textpart[i] = local_text[i + 6]; - } - textpart[6] = '\0'; - textpos = 2 * (68 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - textpart[0] = local_text[11]; - textpart[1] = '\0'; - textpos = 2 * (100 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - textdone = 1; - switch (strlen(addon)) { - case 2: - textpos = 2 * (xoffset + 116); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); - break; - case 5: - textpos = 2 * (xoffset + 130); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); - break; - } - - } - - if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { - /* guard bar extensions and text formatting for UPCE */ - draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - draw_bar(pixelbuf, (50 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); - - textpart[0] = local_text[0]; - textpart[1] = '\0'; - textpos = 2 * (-5 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - for (i = 0; i < 6; i++) { - textpart[i] = local_text[i + 1]; - } - textpart[6] = '\0'; - textpos = 2 * (24 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - textpart[0] = local_text[7]; - textpart[1] = '\0'; - textpos = 2 * (55 + xoffset); - draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); - textdone = 1; - switch (strlen(addon)) { - case 2: - textpos = 2 * (xoffset + 70); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); - break; - case 5: - textpos = 2 * (xoffset + 84); - draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); - break; - } - - } - - xoffset -= comp_offset; - - /* Put boundary bars or box around symbol */ - if (((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { - /* boundary bars */ - draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * 2, textoffset * 2, symbol->border_width * 2, image_width, image_height); - draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * 2, (textoffset + symbol->height + symbol->border_width) * 2, symbol->border_width * 2, image_width, image_height); - if ((symbol->output_options & BARCODE_BIND) != 0) { - if ((symbol->rows > 1) && (is_stackable(symbol->symbology) == 1)) { - /* row binding */ - for (r = 1; r < symbol->rows; r++) { - draw_bar(pixelbuf, xoffset * 2, symbol->width * 2, ((r * row_height) + textoffset + yoffset - 1) * 2, 2 * 2, image_width, image_height); - } - } - } - } - - if ((symbol->output_options & BARCODE_BOX) != 0) { - /* side bars */ - draw_bar(pixelbuf, 0, symbol->border_width * 2, textoffset * 2, (symbol->height + (2 * symbol->border_width)) * 2, image_width, image_height); - draw_bar(pixelbuf, (symbol->width + xoffset + xoffset - symbol->border_width) * 2, symbol->border_width * 2, textoffset * 2, (symbol->height + (2 * symbol->border_width)) * 2, image_width, image_height); - } - - /* Put the human readable text at the bottom */ - if ((textdone == 0) && (ustrlen(local_text) != 0)) { - textpos = (image_width / 2); - draw_string(pixelbuf, (char*) local_text, textpos, default_text_posn, textflags, image_width, image_height); - } - - error_number = png_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); - free(pixelbuf); - return error_number; -} - -#ifndef NO_PNG - -int png_handle(struct zint_symbol *symbol, int rotate_angle) { - int error; - - if (symbol->symbology == BARCODE_MAXICODE) { - error = maxi_png_plot(symbol, rotate_angle, PNG_DATA); - } else { - - error = png_plot(symbol, rotate_angle, PNG_DATA); - } - - return error; -} -#endif /* NO_PNG */ - -int bmp_handle(struct zint_symbol *symbol, int rotate_angle) { - int error; - - if (symbol->symbology == BARCODE_MAXICODE) { - error = maxi_png_plot(symbol, rotate_angle, BMP_DATA); - } else { - error = png_plot(symbol, rotate_angle, BMP_DATA); - } - - return error; -} diff --git a/backend/raster.c b/backend/raster.c new file mode 100644 index 00000000..ba7a2017 --- /dev/null +++ b/backend/raster.c @@ -0,0 +1,841 @@ +/* raster.c - Handles output to raster files */ + +/* + libzint - the open source barcode library + Copyright (C) 2009-2016 Robin Stuart + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the project nor the names of its contributors + may be used to endorse or promote products derived from this software + without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + SUCH DAMAGE. + */ + +#include +#ifdef _MSC_VER +#include +#include +#endif +#include +#include +#include "common.h" + +#ifdef _MSC_VER +#include +#endif /* _MSC_VER */ + +#include "maxihex.h" /* Maxicode shapes */ +#include "font.h" /* Font for human readable text */ + +#ifndef NO_PNG +extern int png_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle); +#endif /* NO_PNG */ +extern int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle); + + +int save_raster_image_to_file(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle, int image_type) { + int error_number; + float scaler = symbol->scale; + char *scaled_pixelbuf; + int horiz, vert, i; + int scale_width, scale_height; + + if (scaler == 0) { + scaler = 0.5; + } + scale_width = image_width * scaler; + scale_height = image_height * scaler; + + /* Apply scale options by creating another pixel buffer */ + if (!(scaled_pixelbuf = (char *) malloc(scale_width * scale_height))) { + printf("Insufficient memory for pixel buffer"); + return ZINT_ERROR_ENCODING_PROBLEM; + } else { + for (i = 0; i < (scale_width * scale_height); i++) { + *(scaled_pixelbuf + i) = '0'; + } + } + + for (vert = 0; vert < scale_height; vert++) { + for (horiz = 0; horiz < scale_width; horiz++) { + *(scaled_pixelbuf + (vert * scale_width) + horiz) = *(pixelbuf + ((int) (vert / scaler) * image_width) + (int) (horiz / scaler)); + } + } + + if (image_type == OUT_PNG_FILE) { +#ifndef NO_PNG + error_number = png_pixel_plot(symbol, scale_height, scale_width, scaled_pixelbuf, rotate_angle); +#else + return ZINT_ERROR_INVALID_OPTION; +#endif + } else { + error_number = bmp_pixel_plot(symbol, scale_height, scale_width, scaled_pixelbuf, rotate_angle); + } + + free(scaled_pixelbuf); + + return error_number; +} + +void draw_bar(char *pixelbuf, int xpos, int xlen, int ypos, int ylen, int image_width, int image_height) { + /* Draw a rectangle */ + int i, j, png_ypos; + + png_ypos = image_height - ypos - ylen; + /* This fudge is needed because EPS measures height from the bottom up but + PNG measures y position from the top down */ + + for (i = (xpos); i < (xpos + xlen); i++) { + for (j = (png_ypos); j < (png_ypos + ylen); j++) { + *(pixelbuf + (image_width * j) + i) = '1'; + } + } +} + +int bullseye_pixel(int row, int col) { + int block_val, block_pos, return_val; + + block_val = bullseye_compressed[(row * 12) + (col / 8)]; + return_val = 0; + block_pos = col % 8; + + if (block_val & (0x80 >> block_pos)) { + return_val = 1; + } + + return return_val; +} + +void draw_bullseye(char *pixelbuf, int image_width, int xoffset, int yoffset) { + /* Central bullseye in Maxicode symbols */ + int i, j; + + for (j = 103; j < 196; j++) { + for (i = 0; i < 93; i++) { + if (bullseye_pixel(j - 103, i)) { + /* if(bullseye[(((j - 103) * 93) + i)] == 1) { */ + *(pixelbuf + (image_width * j) + (image_width * yoffset) + i + 99 + xoffset) = '1'; + } + } + } +} + +void draw_hexagon(char *pixelbuf, int image_width, int xposn, int yposn) { + /* Put a hexagon into the pixel buffer */ + int i, j; + + for (i = 0; i < 12; i++) { + for (j = 0; j < 10; j++) { + if (hexagon[(i * 10) + j] == 1) { + *(pixelbuf + (image_width * i) + (image_width * yposn) + xposn + j) = '1'; + } + } + } +} + +void draw_letter(char *pixelbuf, unsigned char letter, int xposn, int yposn, int textflags, int image_width, int image_height) { + /* Put a letter into a position */ + int skip, x, y, glyph_no, max_x, max_y; + + skip = 0; + + if (letter < 33) { + skip = 1; + } + + if ((letter > 127) && (letter < 161)) { + skip = 1; + } + + if (xposn < 0 || yposn < 0) { + skip = 1; + } + + if (skip == 0) { + if (letter > 128) { + glyph_no = letter - 66; + } else { + glyph_no = letter - 33; + } + + + switch (textflags) { + case 1: // small font 5x9 + max_x = 5; + max_y = 9; + + if (xposn + max_x >= image_width) { + max_x = image_width - xposn - 1; + } + + if (yposn + max_y >= image_height) { + max_y = image_height - yposn - 1; + } + + for (y = 0; y < max_y; y++) { + for (x = 0; x < max_x; x++) { + if (small_font[(glyph_no * 9) + y] & (0x10 >> x)) { + *(pixelbuf + (y * image_width) + (yposn * image_width) + xposn + x) = '1'; + } + } + } + break; + + case 2: // bold font -> twice the regular font + { + char * linePtr; + max_x = 7; + max_y = 14; + + if (xposn + max_x + 1 >= image_width) { + max_x = image_width - xposn - 2; + } + + if (yposn + max_y >= image_height) { + max_y = image_height - yposn - 1; + } + + linePtr = pixelbuf + (yposn * image_width) + xposn + 1; + for (y = 0; y < max_y; y++) { + char * pixelPtr = linePtr; + int extra_dot = 0; + for (x = 0; x < 7; x++) { + if (ascii_font[(glyph_no * 14) + y] & (0x40 >> x)) { + *pixelPtr = '1'; + extra_dot = 1; + } else { + if (extra_dot) { + *pixelPtr = '1'; + } + + extra_dot = 0; + } + + ++pixelPtr; + } + + if (extra_dot) { + *pixelPtr = '1'; + } + + linePtr += image_width; + } + } + break; + + default: // regular font 7x15 + max_x = 7; + max_y = 14; + + if (xposn + max_x >= image_width) { + max_x = image_width - xposn - 1; + } + + if (yposn + max_y >= image_height) { + max_y = image_height - yposn - 1; + } + + for (y = 0; y < max_y; y++) { + for (x = 0; x < 7; x++) { + if (ascii_font[(glyph_no * 14) + y] & (0x40 >> x)) { + *(pixelbuf + (y * image_width) + (yposn * image_width) + xposn + x) = '1'; + } + } + } + break; + } + } +} + +/* Plot a string into the pixel buffer */ +void draw_string(char *pixbuf, char input_string[], int xposn, int yposn, int textflags, int image_width, int image_height) { + int i, string_length, string_left_hand, letter_width = 7; + + switch (textflags) { + case 1: // small font 5x9 + letter_width = 5; + break; + + case 2: // bold font -> width of the regular font + 1 extra dot + 1 extra space + letter_width = 9; + break; + + default: // regular font 7x15 + letter_width = 7; + break; + } + + string_length = strlen(input_string); + string_left_hand = xposn - ((letter_width * string_length) / 2); + + for (i = 0; i < string_length; i++) { + draw_letter(pixbuf, input_string[i], string_left_hand + (i * letter_width), yposn, textflags, image_width, image_height); + } + +} + +int plot_raster_maxicode(struct zint_symbol *symbol, int rotate_angle, int data_type) { + int i, row, column, xposn, yposn; + int image_height, image_width; + char *pixelbuf; + int error_number; + int xoffset, yoffset; + + xoffset = symbol->border_width + symbol->whitespace_width; + yoffset = symbol->border_width; + image_width = 300 + (2 * xoffset * 2); + image_height = 300 + (2 * yoffset * 2); + + if (!(pixelbuf = (char *) malloc(image_width * image_height))) { + printf("Insifficient memory for pixel buffer"); + return ZINT_ERROR_ENCODING_PROBLEM; + } else { + for (i = 0; i < (image_width * image_height); i++) { + *(pixelbuf + i) = '0'; + } + } + + draw_bullseye(pixelbuf, image_width, (2 * xoffset), (2 * yoffset)); + + for (row = 0; row < symbol->rows; row++) { + yposn = row * 9; + for (column = 0; column < symbol->width; column++) { + xposn = column * 10; + if (module_is_set(symbol, row, column)) { + if (row & 1) { + /* Odd (reduced) row */ + xposn += 5; + draw_hexagon(pixelbuf, image_width, xposn + (2 * xoffset), yposn + (2 * yoffset)); + } else { + /* Even (full) row */ + draw_hexagon(pixelbuf, image_width, xposn + (2 * xoffset), yposn + (2 * yoffset)); + } + } + } + } + + if (((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { + /* boundary bars */ + draw_bar(pixelbuf, 0, image_width, 0, symbol->border_width * 2, image_width, image_height); + draw_bar(pixelbuf, 0, image_width, 300 + (symbol->border_width * 2), symbol->border_width * 2, image_width, image_height); + } + + if ((symbol->output_options & BARCODE_BOX) != 0) { + /* side bars */ + draw_bar(pixelbuf, 0, symbol->border_width * 2, 0, image_height, image_width, image_height); + draw_bar(pixelbuf, 300 + ((symbol->border_width + symbol->whitespace_width + symbol->whitespace_width) * 2), symbol->border_width * 2, 0, image_height, image_width, image_height); + } + + error_number = save_raster_image_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); + free(pixelbuf); + return error_number; +} + +/* Convert UTF-8 to Latin1 Codepage for the interpretation line */ +void to_latin1(unsigned char source[], unsigned char preprocessed[]) { + int j, i, input_length; + + input_length = ustrlen(source); + + j = 0; + i = 0; + while (i < input_length) { + switch (source[i]) { + case 0xC2: + /* UTF-8 C2xxh */ + /* Character range: C280h (latin: 80h) to C2BFh (latin: BFh) */ + i++; + preprocessed[j] = source[i]; + j++; + break; + case 0xC3: + /* UTF-8 C3xx */ + /* Character range: C380h (latin: C0h) to C3BFh (latin: FFh) */ + i++; + preprocessed[j] = source[i] + 64; + j++; + break; + default: + /* Process ASCII (< 80h), all other unicode points are ignored */ + if (source[i] < 128) { + preprocessed[j] = source[i]; + j++; + } + break; + } + i++; + } + preprocessed[j] = '\0'; + + return; +} + +int plot_raster_default(struct zint_symbol *symbol, int rotate_angle, int data_type) { + int textdone, main_width, comp_offset, large_bar_count; + char textpart[10], addon[6]; + float addon_text_posn, preset_height, large_bar_height; + int i, r, textoffset, yoffset, xoffset, latch, image_width, image_height; + char *pixelbuf; + int addon_latch = 0, textflags = 0; + int this_row, block_width, plot_height, plot_yposn, textpos; + float row_height, row_posn; + int error_number; + int default_text_posn; + int next_yposn; +#ifndef _MSC_VER + unsigned char local_text[ustrlen(symbol->text) + 1]; +#else + unsigned char* local_text = (unsigned char*) _alloca(ustrlen(symbol->text) + 1); +#endif + + if (symbol->show_hrt != 0) { + /* Copy text from symbol */ + to_latin1(symbol->text, local_text); + } else { + /* No text needed */ + switch (symbol->symbology) { + case BARCODE_EANX: + case BARCODE_EANX_CC: + case BARCODE_ISBNX: + case BARCODE_UPCA: + case BARCODE_UPCE: + case BARCODE_UPCA_CC: + case BARCODE_UPCE_CC: + /* For these symbols use dummy text to ensure formatting is done + * properly even if no text is required */ + for (i = 0; i < ustrlen(symbol->text); i++) { + if (symbol->text[i] == '+') { + local_text[i] = '+'; + } else { + local_text[i] = ' '; + } + local_text[ustrlen(symbol->text)] = '\0'; + } + break; + default: + /* For everything else, just remove the text */ + local_text[0] = '\0'; + break; + } + } + + textdone = 0; + main_width = symbol->width; + strcpy(addon, ""); + comp_offset = 0; + addon_text_posn = 0.0; + row_height = 0; + if (symbol->output_options & SMALL_TEXT) { + textflags = 1; + } + else if (symbol->output_options & BOLD_TEXT) { + textflags = 2; + } + + if (symbol->height == 0) { + symbol->height = 50; + } + + large_bar_count = 0; + preset_height = 0.0; + for (i = 0; i < symbol->rows; i++) { + preset_height += symbol->row_height[i]; + if (symbol->row_height[i] == 0) { + large_bar_count++; + } + } + + if (large_bar_count == 0) { + symbol->height = preset_height; + large_bar_height = 10; + } else { + large_bar_height = (symbol->height - preset_height) / large_bar_count; + } + + while (!(module_is_set(symbol, symbol->rows - 1, comp_offset))) { + comp_offset++; + } + + /* Certain symbols need whitespace otherwise characters get chopped off the sides */ + if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) + || (symbol->symbology == BARCODE_ISBNX)) { + switch (ustrlen(local_text)) { + case 13: /* EAN 13 */ + case 16: + case 19: + if (symbol->whitespace_width == 0) { + symbol->whitespace_width = 10; + } + main_width = 96 + comp_offset; + break; + default: + main_width = 68 + comp_offset; + } + } + + if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { + if (symbol->whitespace_width == 0) { + symbol->whitespace_width = 10; + main_width = 96 + comp_offset; + } + } + + if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { + if (symbol->whitespace_width == 0) { + symbol->whitespace_width = 10; + main_width = 51 + comp_offset; + } + } + + latch = 0; + r = 0; + /* Isolate add-on text */ + if (is_extendable(symbol->symbology)) { + for (i = 0; i < ustrlen(local_text); i++) { + if (latch == 1) { + addon[r] = local_text[i]; + r++; + } + if (symbol->text[i] == '+') { + latch = 1; + } + } + } + addon[r] = '\0'; + + if (ustrlen(local_text) != 0) { + textoffset = 9; + } else { + textoffset = 0; + } + + xoffset = symbol->border_width + symbol->whitespace_width; + yoffset = symbol->border_width; + image_width = 2 * (symbol->width + xoffset + xoffset); + image_height = 2 * (symbol->height + textoffset + yoffset + yoffset); + + if (!(pixelbuf = (char *) malloc(image_width * image_height))) { + printf("Insufficient memory for pixel buffer"); + return ZINT_ERROR_ENCODING_PROBLEM; + } else { + for (i = 0; i < (image_width * image_height); i++) { + *(pixelbuf + i) = '0'; + } + } + + if (((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { + default_text_posn = image_height - 17; + } else { + default_text_posn = image_height - 17 - symbol->border_width - symbol->border_width; + } + + row_posn = textoffset + yoffset; + next_yposn = textoffset + yoffset; + row_height = 0; + + /* Plot the body of the symbol to the pixel buffer */ + for (r = 0; r < symbol->rows; r++) { + this_row = symbol->rows - r - 1; /* invert r otherwise plots upside down */ + row_posn += row_height; + plot_yposn = next_yposn; + if (symbol->row_height[this_row] == 0) { + row_height = large_bar_height; + } else { + row_height = symbol->row_height[this_row]; + } + next_yposn = (int) (row_posn + row_height); + plot_height = next_yposn - plot_yposn; + + i = 0; + if (module_is_set(symbol, this_row, 0)) { + latch = 1; + } else { + latch = 0; + } + + do { + block_width = 0; + do { + block_width++; + } while (module_is_set(symbol, this_row, i + block_width) == module_is_set(symbol, this_row, i)); + if ((addon_latch == 0) && (r == 0) && (i > main_width)) { + plot_height = (int) (row_height - 5.0); + plot_yposn = (int) (row_posn - 5.0); + addon_text_posn = row_posn + row_height - 8.0; + addon_latch = 1; + } + if (latch == 1) { + /* a bar */ + draw_bar(pixelbuf, (i + xoffset) * 2, block_width * 2, plot_yposn * 2, plot_height * 2, image_width, image_height); + latch = 0; + } else { + /* a space */ + latch = 1; + } + i += block_width; + + } while (i < symbol->width); + } + + xoffset += comp_offset; + + if ((((symbol->symbology == BARCODE_EANX) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_EANX_CC)) || (symbol->symbology == BARCODE_ISBNX)) { + /* guard bar extensions and text formatting for EAN8 and EAN13 */ + switch (ustrlen(local_text)) { + case 8: /* EAN-8 */ + case 11: + case 14: + draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (32 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (34 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (64 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (66 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + for (i = 0; i < 4; i++) { + textpart[i] = local_text[i]; + } + textpart[4] = '\0'; + textpos = 2 * (17 + xoffset); + + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + for (i = 0; i < 4; i++) { + textpart[i] = local_text[i + 4]; + } + textpart[4] = '\0'; + textpos = 2 * (50 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + textdone = 1; + switch (strlen(addon)) { + case 2: + textpos = 2 * (xoffset + 86); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); + break; + case 5: + textpos = 2 * (xoffset + 100); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); + break; + } + + break; + case 13: /* EAN 13 */ + case 16: + case 19: + draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (92 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (94 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + + textpart[0] = local_text[0]; + textpart[1] = '\0'; + textpos = 2 * (-7 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + for (i = 0; i < 6; i++) { + textpart[i] = local_text[i + 1]; + } + textpart[6] = '\0'; + textpos = 2 * (24 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + for (i = 0; i < 6; i++) { + textpart[i] = local_text[i + 7]; + } + textpart[6] = '\0'; + textpos = 2 * (71 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + textdone = 1; + switch (strlen(addon)) { + case 2: + textpos = 2 * (xoffset + 114); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); + break; + case 5: + textpos = 2 * (xoffset + 128); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); + break; + } + break; + + } + } + + if (((symbol->symbology == BARCODE_UPCA) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCA_CC)) { + /* guard bar extensions and text formatting for UPCA */ + latch = 1; + + i = 0 + comp_offset; + do { + block_width = 0; + do { + block_width++; + } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); + if (latch == 1) { + /* a bar */ + draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + latch = 0; + } else { + /* a space */ + latch = 1; + } + i += block_width; + } while (i < 11 + comp_offset); + draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + latch = 1; + i = 85 + comp_offset; + do { + block_width = 0; + do { + block_width++; + } while (module_is_set(symbol, symbol->rows - 1, i + block_width) == module_is_set(symbol, symbol->rows - 1, i)); + if (latch == 1) { + /* a bar */ + draw_bar(pixelbuf, (i + xoffset - comp_offset) * 2, block_width * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + latch = 0; + } else { + /* a space */ + latch = 1; + } + i += block_width; + } while (i < 96 + comp_offset); + textpart[0] = local_text[0]; + textpart[1] = '\0'; + textpos = 2 * (-5 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + for (i = 0; i < 5; i++) { + textpart[i] = local_text[i + 1]; + } + textpart[5] = '\0'; + textpos = 2 * (27 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + for (i = 0; i < 5; i++) { + textpart[i] = local_text[i + 6]; + } + textpart[6] = '\0'; + textpos = 2 * (68 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + textpart[0] = local_text[11]; + textpart[1] = '\0'; + textpos = 2 * (100 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + textdone = 1; + switch (strlen(addon)) { + case 2: + textpos = 2 * (xoffset + 116); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); + break; + case 5: + textpos = 2 * (xoffset + 130); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); + break; + } + + } + + if (((symbol->symbology == BARCODE_UPCE) && (symbol->rows == 1)) || (symbol->symbology == BARCODE_UPCE_CC)) { + /* guard bar extensions and text formatting for UPCE */ + draw_bar(pixelbuf, (0 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (2 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (46 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (48 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + draw_bar(pixelbuf, (50 + xoffset) * 2, 1 * 2, (4 + (int) yoffset) * 2, 5 * 2, image_width, image_height); + + textpart[0] = local_text[0]; + textpart[1] = '\0'; + textpos = 2 * (-5 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + for (i = 0; i < 6; i++) { + textpart[i] = local_text[i + 1]; + } + textpart[6] = '\0'; + textpos = 2 * (24 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + textpart[0] = local_text[7]; + textpart[1] = '\0'; + textpos = 2 * (55 + xoffset); + draw_string(pixelbuf, textpart, textpos, default_text_posn, textflags, image_width, image_height); + textdone = 1; + switch (strlen(addon)) { + case 2: + textpos = 2 * (xoffset + 70); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); + break; + case 5: + textpos = 2 * (xoffset + 84); + draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, textflags, image_width, image_height); + break; + } + + } + + xoffset -= comp_offset; + + /* Put boundary bars or box around symbol */ + if (((symbol->output_options & BARCODE_BOX) != 0) || ((symbol->output_options & BARCODE_BIND) != 0)) { + /* boundary bars */ + draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * 2, textoffset * 2, symbol->border_width * 2, image_width, image_height); + draw_bar(pixelbuf, 0, (symbol->width + xoffset + xoffset) * 2, (textoffset + symbol->height + symbol->border_width) * 2, symbol->border_width * 2, image_width, image_height); + if ((symbol->output_options & BARCODE_BIND) != 0) { + if ((symbol->rows > 1) && (is_stackable(symbol->symbology) == 1)) { + /* row binding */ + for (r = 1; r < symbol->rows; r++) { + draw_bar(pixelbuf, xoffset * 2, symbol->width * 2, ((r * row_height) + textoffset + yoffset - 1) * 2, 2 * 2, image_width, image_height); + } + } + } + } + + if ((symbol->output_options & BARCODE_BOX) != 0) { + /* side bars */ + draw_bar(pixelbuf, 0, symbol->border_width * 2, textoffset * 2, (symbol->height + (2 * symbol->border_width)) * 2, image_width, image_height); + draw_bar(pixelbuf, (symbol->width + xoffset + xoffset - symbol->border_width) * 2, symbol->border_width * 2, textoffset * 2, (symbol->height + (2 * symbol->border_width)) * 2, image_width, image_height); + } + + /* Put the human readable text at the bottom */ + if ((textdone == 0) && (ustrlen(local_text) != 0)) { + textpos = (image_width / 2); + draw_string(pixelbuf, (char*) local_text, textpos, default_text_posn, textflags, image_width, image_height); + } + + error_number = save_raster_image_to_file(symbol, image_height, image_width, pixelbuf, rotate_angle, data_type); + free(pixelbuf); + return error_number; +} + +int plot_raster(struct zint_symbol *symbol, int rotate_angle, int file_type) { + int error; + +#ifdef NO_PNG + if (file_type == OUT_PNG_FILE) { + printf("libpng not found"); + return ZINT_ERROR_INVALID_OPTION; + } +#endif /* NO_PNG */ + + if (symbol->symbology == BARCODE_MAXICODE) { + error = plot_raster_maxicode(symbol, rotate_angle, file_type); + } else { + error = plot_raster_default(symbol, rotate_angle, file_type); + } + + return error; +} diff --git a/backend/zint.h b/backend/zint.h index d8aa72cd..ccae4196 100644 --- a/backend/zint.h +++ b/backend/zint.h @@ -92,6 +92,7 @@ extern "C" { char *bitmap; int bitmap_width; int bitmap_height; + unsigned int bitmap_byte_length; struct zint_render *rendered; }; @@ -192,6 +193,7 @@ extern "C" { #define BARCODE_CODEONE 141 #define BARCODE_GRIDMATRIX 142 +// Output options #define BARCODE_NO_ASCII 1 #define BARCODE_BIND 2 #define BARCODE_BOX 4 @@ -202,23 +204,30 @@ extern "C" { #define CMYK_COLOUR 128 #define BARCODE_DOTTY_MODE 256 +// Input data types #define DATA_MODE 0 #define UNICODE_MODE 1 #define GS1_MODE 2 #define KANJI_MODE 3 #define SJIS_MODE 4 +// Data Matrix specific options #define DM_SQUARE 100 -#define DM_DMRE 101 +#define DM_DMRE 101 +// Warning and error conditions #define ZINT_WARN_INVALID_OPTION 2 #define ZINT_ERROR_TOO_LONG 5 -#define ZINT_ERROR_INVALID_DATA 6 +#define ZINT_ERROR_INVALID_DATA 6 #define ZINT_ERROR_INVALID_CHECK 7 #define ZINT_ERROR_INVALID_OPTION 8 #define ZINT_ERROR_ENCODING_PROBLEM 9 -#define ZINT_ERROR_FILE_ACCESS 10 +#define ZINT_ERROR_FILE_ACCESS 10 #define ZINT_ERROR_MEMORY 11 + +// Raster file types +#define OUT_PNG_FILE 100 +#define OUT_BMP_FILE 120 #if defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(_MSC_VER) #if defined (DLL_EXPORT) || defined(PIC) || defined(_USRDLL)