2016-07-20 01:09:08 +03:00
/* bmp.c - Handles output to Windows Bitmap file */
2016-07-20 01:02:39 +03:00
/*
libzint - the open source barcode library
2021-06-10 13:15:39 +03:00
Copyright ( C ) 2009 - 2021 Robin Stuart < rstuart114 @ gmail . com >
2016-07-20 01:02:39 +03:00
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions
are met :
2017-10-23 22:37:52 +03:00
1. Redistributions of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
2016-07-20 01:02:39 +03:00
2. Redistributions in binary form must reproduce the above copyright
notice , this list of conditions and the following disclaimer in the
2017-10-23 22:37:52 +03:00
documentation and / or other materials provided with the distribution .
2016-07-20 01:02:39 +03:00
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
2017-10-23 22:37:52 +03:00
without specific prior written permission .
2016-07-20 01:02:39 +03:00
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
2017-10-23 22:37:52 +03:00
OUT OF THE USE OF THIS SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF
2016-07-20 01:02:39 +03:00
SUCH DAMAGE .
*/
2019-12-19 03:37:55 +03:00
/* vim: set ts=4 sw=4 et : */
2016-07-20 01:02:39 +03:00
2021-06-10 13:15:39 +03:00
# include <errno.h>
2016-07-20 01:02:39 +03:00
# include <stdio.h>
# include "common.h"
# include "bmp.h" /* Bitmap header structure */
2016-09-03 20:45:09 +03:00
# ifdef _MSC_VER
# include <io.h>
# include <fcntl.h>
# endif
2016-07-20 01:02:39 +03:00
2020-11-01 21:32:55 +03:00
INTERNAL int bmp_pixel_plot ( struct zint_symbol * symbol , unsigned char * pixelbuf ) {
2016-09-11 10:42:31 +03:00
int i , row , column ;
2016-07-20 01:02:39 +03:00
int row_size ;
2020-08-12 02:18:10 +03:00
int bits_per_pixel ;
int colour_count ;
2020-06-04 20:45:25 +03:00
unsigned int data_offset , data_size , file_size ;
2016-07-20 01:02:39 +03:00
unsigned char * bitmap_file_start , * bmp_posn ;
2020-06-04 20:45:25 +03:00
unsigned char * bitmap ;
2016-07-20 01:02:39 +03:00
FILE * bmp_file ;
2016-09-03 20:45:09 +03:00
bitmap_file_header_t file_header ;
bitmap_info_header_t info_header ;
2020-08-12 02:18:10 +03:00
color_ref_t bg_color_ref ;
color_ref_t fg_color_ref ;
2020-08-12 15:19:56 +03:00
color_ref_t ultra_color_ref [ 8 ] ;
2016-07-20 01:02:39 +03:00
2020-08-12 02:18:10 +03:00
if ( symbol - > symbology = = BARCODE_ULTRA ) {
2020-08-12 15:19:56 +03:00
bits_per_pixel = 4 ;
colour_count = 9 ;
2020-08-12 02:18:10 +03:00
} else {
bits_per_pixel = 1 ;
colour_count = 2 ;
}
row_size = 4 * ( ( bits_per_pixel * symbol - > bitmap_width + 31 ) / 32 ) ;
2020-06-04 20:45:25 +03:00
data_size = symbol - > bitmap_height * row_size ;
2021-06-10 13:15:39 +03:00
data_offset = sizeof ( bitmap_file_header_t ) + sizeof ( bitmap_info_header_t ) ;
data_offset + = colour_count * sizeof ( color_ref_t ) ;
2020-06-04 20:45:25 +03:00
file_size = data_offset + data_size ;
2020-06-15 18:06:11 +03:00
bitmap_file_start = ( unsigned char * ) malloc ( file_size ) ;
2020-06-04 20:45:25 +03:00
if ( bitmap_file_start = = NULL ) {
strcpy ( symbol - > errtxt , " 602: Out of memory " ) ;
return ZINT_ERROR_MEMORY ;
}
memset ( bitmap_file_start , 0 , file_size ) ; /* Not required but keeps padding bytes consistent */
2016-09-06 00:06:50 +03:00
2020-06-04 20:45:25 +03:00
bitmap = bitmap_file_start + data_offset ;
2016-07-20 01:02:39 +03:00
2020-08-12 02:18:10 +03:00
fg_color_ref . red = ( 16 * ctoi ( symbol - > fgcolour [ 0 ] ) ) + ctoi ( symbol - > fgcolour [ 1 ] ) ;
fg_color_ref . green = ( 16 * ctoi ( symbol - > fgcolour [ 2 ] ) ) + ctoi ( symbol - > fgcolour [ 3 ] ) ;
fg_color_ref . blue = ( 16 * ctoi ( symbol - > fgcolour [ 4 ] ) ) + ctoi ( symbol - > fgcolour [ 5 ] ) ;
fg_color_ref . reserved = 0x00 ;
bg_color_ref . red = ( 16 * ctoi ( symbol - > bgcolour [ 0 ] ) ) + ctoi ( symbol - > bgcolour [ 1 ] ) ;
bg_color_ref . green = ( 16 * ctoi ( symbol - > bgcolour [ 2 ] ) ) + ctoi ( symbol - > bgcolour [ 3 ] ) ;
bg_color_ref . blue = ( 16 * ctoi ( symbol - > bgcolour [ 4 ] ) ) + ctoi ( symbol - > bgcolour [ 5 ] ) ;
bg_color_ref . reserved = 0x00 ;
2021-06-10 13:15:39 +03:00
2020-08-12 15:19:56 +03:00
for ( i = 0 ; i < 8 ; i + + ) {
ultra_color_ref [ i ] . red = colour_to_red ( i + 1 ) ;
ultra_color_ref [ i ] . green = colour_to_green ( i + 1 ) ;
ultra_color_ref [ i ] . blue = colour_to_blue ( i + 1 ) ;
ultra_color_ref [ i ] . reserved = 0x00 ;
}
2016-07-20 01:02:39 +03:00
/* Pixel Plotting */
2020-08-12 02:18:10 +03:00
if ( symbol - > symbology = = BARCODE_ULTRA ) {
for ( row = 0 ; row < symbol - > bitmap_height ; row + + ) {
for ( column = 0 ; column < symbol - > bitmap_width ; column + + ) {
2020-08-12 15:19:56 +03:00
i = ( column / 2 ) + ( row * row_size ) ;
2020-08-12 02:18:10 +03:00
switch ( * ( pixelbuf + ( symbol - > bitmap_width * ( symbol - > bitmap_height - row - 1 ) ) + column ) ) {
case ' C ' : // Cyan
2020-08-12 15:19:56 +03:00
bitmap [ i ] + = 1 < < ( 4 * ( 1 - ( column % 2 ) ) ) ;
2020-08-12 02:18:10 +03:00
break ;
case ' B ' : // Blue
2020-08-12 15:19:56 +03:00
bitmap [ i ] + = 2 < < ( 4 * ( 1 - ( column % 2 ) ) ) ;
2020-08-12 02:18:10 +03:00
break ;
case ' M ' : // Magenta
2020-08-12 15:19:56 +03:00
bitmap [ i ] + = 3 < < ( 4 * ( 1 - ( column % 2 ) ) ) ;
2020-08-12 02:18:10 +03:00
break ;
case ' R ' : // Red
2020-08-12 15:19:56 +03:00
bitmap [ i ] + = 4 < < ( 4 * ( 1 - ( column % 2 ) ) ) ;
2020-08-12 02:18:10 +03:00
break ;
case ' Y ' : // Yellow
2020-08-12 15:19:56 +03:00
bitmap [ i ] + = 5 < < ( 4 * ( 1 - ( column % 2 ) ) ) ;
2020-08-12 02:18:10 +03:00
break ;
case ' G ' : // Green
2020-08-12 15:19:56 +03:00
bitmap [ i ] + = 6 < < ( 4 * ( 1 - ( column % 2 ) ) ) ;
2020-08-12 02:18:10 +03:00
break ;
case ' K ' : // Black
2020-08-12 15:19:56 +03:00
bitmap [ i ] + = 7 < < ( 4 * ( 1 - ( column % 2 ) ) ) ;
2020-08-12 02:18:10 +03:00
break ;
2020-08-12 15:19:56 +03:00
case ' W ' : // White
bitmap [ i ] + = 8 < < ( 4 * ( 1 - ( column % 2 ) ) ) ;
2020-08-12 02:18:10 +03:00
break ;
}
}
}
} else {
for ( row = 0 ; row < symbol - > bitmap_height ; row + + ) {
for ( column = 0 ; column < symbol - > bitmap_width ; column + + ) {
i = ( column / 8 ) + ( row * row_size ) ;
if ( ( * ( pixelbuf + ( symbol - > bitmap_width * ( symbol - > bitmap_height - row - 1 ) ) + column ) ) = = ' 1 ' ) {
bitmap [ i ] + = ( 0x01 < < ( 7 - ( column % 8 ) ) ) ;
}
2016-07-20 01:02:39 +03:00
}
2016-09-11 10:42:31 +03:00
}
2016-07-20 01:02:39 +03:00
}
symbol - > bitmap_byte_length = data_size ;
2016-09-06 00:06:50 +03:00
file_header . header_field = 0x4d42 ; // "BM"
2020-06-04 20:45:25 +03:00
file_header . file_size = file_size ;
2016-07-20 01:02:39 +03:00
file_header . reserved = 0 ;
2020-06-04 20:45:25 +03:00
file_header . data_offset = data_offset ;
2016-07-20 01:02:39 +03:00
2021-06-10 13:15:39 +03:00
info_header . header_size = sizeof ( bitmap_info_header_t ) ;
2016-07-20 01:02:39 +03:00
info_header . width = symbol - > bitmap_width ;
info_header . height = symbol - > bitmap_height ;
info_header . colour_planes = 1 ;
2020-08-12 02:18:10 +03:00
info_header . bits_per_pixel = bits_per_pixel ;
2016-07-20 01:02:39 +03:00
info_header . compression_method = 0 ; // BI_RGB
info_header . image_size = 0 ;
info_header . horiz_res = 0 ;
info_header . vert_res = 0 ;
2020-08-12 02:18:10 +03:00
info_header . colours = colour_count ;
info_header . important_colours = colour_count ;
2021-06-10 13:15:39 +03:00
2016-07-20 01:02:39 +03:00
bmp_posn = bitmap_file_start ;
2021-06-10 13:15:39 +03:00
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 ) ) ;
2020-08-12 15:19:56 +03:00
bmp_posn + = sizeof ( bitmap_info_header_t ) ;
memcpy ( bmp_posn , & bg_color_ref , sizeof ( color_ref_t ) ) ;
if ( symbol - > symbology = = BARCODE_ULTRA ) {
for ( i = 0 ; i < 8 ; i + + ) {
bmp_posn + = sizeof ( color_ref_t ) ;
memcpy ( bmp_posn , & ultra_color_ref [ i ] , sizeof ( color_ref_t ) ) ;
}
2021-06-10 13:15:39 +03:00
} else {
2020-08-12 02:18:10 +03:00
bmp_posn + = sizeof ( color_ref_t ) ;
memcpy ( bmp_posn , & fg_color_ref , sizeof ( color_ref_t ) ) ;
}
2016-09-06 00:06:50 +03:00
2016-07-20 01:02:39 +03:00
/* Open output file in binary mode */
if ( ( symbol - > output_options & BARCODE_STDOUT ) ! = 0 ) {
# ifdef _MSC_VER
if ( - 1 = = _setmode ( _fileno ( stdout ) , _O_BINARY ) ) {
2021-06-10 13:15:39 +03:00
sprintf ( symbol - > errtxt , " 600: Can't open output file (%d: %.30s) " , errno , strerror ( errno ) ) ;
2017-06-28 22:46:29 +03:00
free ( bitmap_file_start ) ;
2016-07-20 01:02:39 +03:00
return ZINT_ERROR_FILE_ACCESS ;
}
# endif
bmp_file = stdout ;
} else {
if ( ! ( bmp_file = fopen ( symbol - > outfile , " wb " ) ) ) {
2017-06-28 22:46:29 +03:00
free ( bitmap_file_start ) ;
2021-06-10 13:15:39 +03:00
sprintf ( symbol - > errtxt , " 601: Can't open output file (%d: %.30s) " , errno , strerror ( errno ) ) ;
2016-07-20 01:02:39 +03:00
return ZINT_ERROR_FILE_ACCESS ;
}
}
2016-09-06 00:06:50 +03:00
2016-07-20 01:02:39 +03:00
fwrite ( bitmap_file_start , file_header . file_size , 1 , bmp_file ) ;
fclose ( bmp_file ) ;
2016-09-06 00:06:50 +03:00
2016-09-03 20:45:09 +03:00
free ( bitmap_file_start ) ;
2016-07-20 01:02:39 +03:00
return 0 ;
2017-06-28 22:46:29 +03:00
}