mirror of
https://github.com/zint/zint
synced 2024-11-16 20:57:25 +13:00
Add bitmap (BMP) file support
And upgrade symbol->bitmap structure to reflect BMP format Based on work by Adrian Keller
This commit is contained in:
parent
e39d2ff915
commit
75384ca009
67
backend/bmp.h
Normal file
67
backend/bmp.h
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
/* zint.h - definitions for libzint
|
||||||
|
|
||||||
|
libzint - the open source barcode library
|
||||||
|
Copyright (C) 2009-2016 Robin Stuart <rstuart114@gmail.com>
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef BMP_H
|
||||||
|
#define BMP_H
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
typedef struct bitmap_file_header {
|
||||||
|
uint16_t header_field;
|
||||||
|
uint32_t file_size;
|
||||||
|
uint32_t reserved;
|
||||||
|
uint32_t data_offset;
|
||||||
|
} __attribute__((__packed__ )) bitmap_file_header_t;
|
||||||
|
|
||||||
|
typedef struct bitmap_info_header {
|
||||||
|
uint32_t header_size;
|
||||||
|
int32_t width;
|
||||||
|
int32_t height;
|
||||||
|
uint16_t colour_planes;
|
||||||
|
uint16_t bits_per_pixel;
|
||||||
|
uint32_t compression_method;
|
||||||
|
uint32_t image_size;
|
||||||
|
int32_t horiz_res;
|
||||||
|
int32_t vert_res;
|
||||||
|
uint32_t colours;
|
||||||
|
uint32_t important_colours;
|
||||||
|
} __attribute__((__packed__ )) bitmap_info_header_t;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif /* BMP_H */
|
||||||
|
|
122
backend/png.c
122
backend/png.c
@ -52,6 +52,9 @@
|
|||||||
|
|
||||||
#include "font.h" /* Font for human readable text */
|
#include "font.h" /* Font for human readable text */
|
||||||
|
|
||||||
|
#include "bmp.h" /* Bitmap header structure */
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
#define SSET "0123456789ABCDEF"
|
#define SSET "0123456789ABCDEF"
|
||||||
|
|
||||||
#define PNG_DATA 100
|
#define PNG_DATA 100
|
||||||
@ -315,6 +318,10 @@ int png_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
|
|||||||
int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle) {
|
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 i, row, column, errno;
|
||||||
int fgred, fggrn, fgblu, bgred, bggrn, bgblu;
|
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) {
|
switch (rotate_angle) {
|
||||||
case 0:
|
case 0:
|
||||||
@ -332,8 +339,8 @@ int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
|
|||||||
if (symbol->bitmap != NULL)
|
if (symbol->bitmap != NULL)
|
||||||
free(symbol->bitmap);
|
free(symbol->bitmap);
|
||||||
|
|
||||||
symbol->bitmap = (char *) malloc(image_width * image_height * 3);
|
row_size = 4 * floor((24 * symbol->bitmap_width + 31) / 32);
|
||||||
|
symbol->bitmap = (char *) malloc(row_size * symbol->bitmap_height);
|
||||||
|
|
||||||
/* sort out colour options */
|
/* sort out colour options */
|
||||||
to_upper((unsigned char*) symbol->fgcolour);
|
to_upper((unsigned char*) symbol->fgcolour);
|
||||||
@ -371,16 +378,17 @@ int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
|
|||||||
case 0: /* Plot the right way up */
|
case 0: /* Plot the right way up */
|
||||||
for (row = 0; row < image_height; row++) {
|
for (row = 0; row < image_height; row++) {
|
||||||
for (column = 0; column < image_width; column++) {
|
for (column = 0; column < image_width; column++) {
|
||||||
switch (*(pixelbuf + (image_width * row) + column)) {
|
i = (3 * column) + (row * row_size);
|
||||||
|
switch (*(pixelbuf + (image_width * (image_height - row - 1)) + column)) {
|
||||||
case '1':
|
case '1':
|
||||||
symbol->bitmap[i++] = fgred;
|
symbol->bitmap[i] = fgblu;
|
||||||
symbol->bitmap[i++] = fggrn;
|
symbol->bitmap[i + 1] = fggrn;
|
||||||
symbol->bitmap[i++] = fgblu;
|
symbol->bitmap[i + 2] = fgred;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
symbol->bitmap[i++] = bgred;
|
symbol->bitmap[i] = bgblu;
|
||||||
symbol->bitmap[i++] = bggrn;
|
symbol->bitmap[i + 1] = bggrn;
|
||||||
symbol->bitmap[i++] = bgblu;
|
symbol->bitmap[i + 2] = bgred;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -390,16 +398,17 @@ int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
|
|||||||
case 90: /* Plot 90 degrees clockwise */
|
case 90: /* Plot 90 degrees clockwise */
|
||||||
for (row = 0; row < image_width; row++) {
|
for (row = 0; row < image_width; row++) {
|
||||||
for (column = 0; column < image_height; column++) {
|
for (column = 0; column < image_height; column++) {
|
||||||
switch (*(pixelbuf + (image_width * (image_height - column - 1)) + row)) {
|
i = (3 * column) + (row * row_size);
|
||||||
|
switch (*(pixelbuf + (image_width * (image_height - column - 1)) + (image_width - row - 1))) {
|
||||||
case '1':
|
case '1':
|
||||||
symbol->bitmap[i++] = fgred;
|
symbol->bitmap[i] = fgblu;
|
||||||
symbol->bitmap[i++] = fggrn;
|
symbol->bitmap[i + 1] = fggrn;
|
||||||
symbol->bitmap[i++] = fgblu;
|
symbol->bitmap[i + 2] = fgred;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
symbol->bitmap[i++] = bgred;
|
symbol->bitmap[i] = bgblu;
|
||||||
symbol->bitmap[i++] = bggrn;
|
symbol->bitmap[i + 1] = bggrn;
|
||||||
symbol->bitmap[i++] = bgblu;
|
symbol->bitmap[i + 2] = bgred;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -409,16 +418,17 @@ int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
|
|||||||
case 180: /* Plot upside down */
|
case 180: /* Plot upside down */
|
||||||
for (row = 0; row < image_height; row++) {
|
for (row = 0; row < image_height; row++) {
|
||||||
for (column = 0; column < image_width; column++) {
|
for (column = 0; column < image_width; column++) {
|
||||||
switch (*(pixelbuf + (image_width * (image_height - row - 1)) + (image_width - column - 1))) {
|
i = (3 * column) + (row * row_size);
|
||||||
|
switch (*(pixelbuf + (image_width * row) + (image_width - column - 1))) {
|
||||||
case '1':
|
case '1':
|
||||||
symbol->bitmap[i++] = fgred;
|
symbol->bitmap[i] = fgblu;
|
||||||
symbol->bitmap[i++] = fggrn;
|
symbol->bitmap[i + 1] = fggrn;
|
||||||
symbol->bitmap[i++] = fgblu;
|
symbol->bitmap[i + 2] = fgred;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
symbol->bitmap[i++] = bgred;
|
symbol->bitmap[i] = bgblu;
|
||||||
symbol->bitmap[i++] = bggrn;
|
symbol->bitmap[i + 1] = bggrn;
|
||||||
symbol->bitmap[i++] = bgblu;
|
symbol->bitmap[i + 2] = bgred;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -428,16 +438,17 @@ int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
|
|||||||
case 270: /* Plot 90 degrees anti-clockwise */
|
case 270: /* Plot 90 degrees anti-clockwise */
|
||||||
for (row = 0; row < image_width; row++) {
|
for (row = 0; row < image_width; row++) {
|
||||||
for (column = 0; column < image_height; column++) {
|
for (column = 0; column < image_height; column++) {
|
||||||
switch (*(pixelbuf + (image_width * column) + (image_width - row - 1))) {
|
i = (3 * column) + (row * row_size);
|
||||||
|
switch (*(pixelbuf + (image_width * column) + row)) {
|
||||||
case '1':
|
case '1':
|
||||||
symbol->bitmap[i++] = fgred;
|
symbol->bitmap[i] = fgblu;
|
||||||
symbol->bitmap[i++] = fggrn;
|
symbol->bitmap[i + 1] = fggrn;
|
||||||
symbol->bitmap[i++] = fgblu;
|
symbol->bitmap[i + 2] = fgred;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
symbol->bitmap[i++] = bgred;
|
symbol->bitmap[i] = bgblu;
|
||||||
symbol->bitmap[i++] = bggrn;
|
symbol->bitmap[i + 1] = bggrn;
|
||||||
symbol->bitmap[i++] = bgblu;
|
symbol->bitmap[i + 2] = bgred;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -446,6 +457,57 @@ int bmp_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
data_size = symbol->bitmap_height * row_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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user