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
2020-06-04 20:45:25 +03:00
Copyright ( C ) 2009 - 2020 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
# 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
2019-12-19 03:37:55 +03:00
INTERNAL int bmp_pixel_plot ( struct zint_symbol * symbol , char * pixelbuf ) {
2016-09-11 10:42:31 +03:00
int i , row , column ;
2020-06-04 20:45:25 +03:00
unsigned int fgred , fggrn , fgblu , bgred , bggrn , bgblu ;
2016-07-20 01:02:39 +03:00
int row_size ;
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 ;
2016-07-20 01:02:39 +03:00
2020-06-04 20:45:25 +03:00
row_size = 4 * ( ( 24 * symbol - > bitmap_width + 31 ) / 32 ) ;
data_size = symbol - > bitmap_height * row_size ;
data_offset = sizeof ( bitmap_file_header_t ) + sizeof ( bitmap_info_header_t ) ;
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
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 */
2016-09-11 10:42:31 +03:00
for ( row = 0 ; row < symbol - > bitmap_height ; row + + ) {
for ( column = 0 ; column < symbol - > bitmap_width ; column + + ) {
i = ( 3 * column ) + ( row * row_size ) ;
switch ( * ( pixelbuf + ( symbol - > bitmap_width * ( symbol - > bitmap_height - row - 1 ) ) + column ) ) {
2020-01-06 23:01:48 +03:00
case ' W ' : // White
bitmap [ i ] = 255 ;
bitmap [ i + 1 ] = 255 ;
bitmap [ i + 2 ] = 255 ;
break ;
case ' C ' : // Cyan
bitmap [ i ] = 255 ;
bitmap [ i + 1 ] = 255 ;
bitmap [ i + 2 ] = 0 ;
break ;
case ' B ' : // Blue
bitmap [ i ] = 255 ;
bitmap [ i + 1 ] = 0 ;
bitmap [ i + 2 ] = 0 ;
break ;
case ' M ' : // Magenta
bitmap [ i ] = 255 ;
bitmap [ i + 1 ] = 0 ;
bitmap [ i + 2 ] = 255 ;
break ;
case ' R ' : // Red
bitmap [ i ] = 0 ;
bitmap [ i + 1 ] = 0 ;
bitmap [ i + 2 ] = 255 ;
break ;
case ' Y ' : // Yellow
bitmap [ i ] = 0 ;
bitmap [ i + 1 ] = 255 ;
bitmap [ i + 2 ] = 255 ;
break ;
case ' G ' : // Green
bitmap [ i ] = 0 ;
bitmap [ i + 1 ] = 255 ;
bitmap [ i + 2 ] = 0 ;
break ;
case ' K ' : // Black
bitmap [ i ] = 0 ;
bitmap [ i + 1 ] = 0 ;
bitmap [ i + 2 ] = 0 ;
break ;
2016-09-11 10:42:31 +03:00
case ' 1 ' :
2016-10-02 12:45:47 +03:00
bitmap [ i ] = fgblu ;
bitmap [ i + 1 ] = fggrn ;
bitmap [ i + 2 ] = fgred ;
2016-09-11 10:42:31 +03:00
break ;
default :
2016-10-02 12:45:47 +03:00
bitmap [ i ] = bgblu ;
bitmap [ i + 1 ] = bggrn ;
bitmap [ i + 2 ] = bgred ;
2016-09-11 10:42:31 +03:00
break ;
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
2016-09-06 00:06:50 +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 ;
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 ;
2016-09-06 00:06:50 +03:00
2016-07-20 01:02:39 +03:00
bmp_posn = bitmap_file_start ;
2016-09-06 00:06:50 +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 ) ) ;
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 ) ) {
2017-07-27 18:01:53 +03:00
strcpy ( symbol - > errtxt , " 600: Can't open output file " ) ;
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 ) ;
2017-07-27 18:01:53 +03:00
strcpy ( symbol - > errtxt , " 601: Can't open output file " ) ;
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
}