summaryrefslogtreecommitdiff
path: root/backend/png.c
blob: e3522fed9a41877ba95fd1e9fc85409485471eac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
/* png.c - Handles output to PNG file */

/*
    libzint - the open source barcode library
    Copyright (C) 2009 Robin Stuart <robin@zint.org.uk>

    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 <stdio.h>
#ifdef _MSC_VER
#include <fcntl.h>
#include <io.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "common.h"

#ifdef _MSC_VER
#include <malloc.h> 
#endif /* _MSC_VER */

#ifndef NO_PNG
#include "png.h"        /* libpng header; includes zlib.h and setjmp.h */
#endif /* NO_PNG */
#include "maxipng.h"	/* Maxicode shapes */

#include "font.h"	/* Font for human readable text */

#define SSET	"0123456789ABCDEF"

#define	PNG_DATA	100
#define	BMP_DATA	200

#ifndef NO_PNG
struct mainprog_info_type {
    long width;
    long height;
    FILE *outfile;
    jmp_buf jmpbuf;
};

static void writepng_error_handler(png_structp png_ptr, png_const_charp msg)
{
    struct mainprog_info_type  *graphic;

    fprintf(stderr, "writepng libpng error: %s\n", msg);
    fflush(stderr);

    graphic = (struct mainprog_info_type*)png_get_error_ptr(png_ptr);
    if (graphic == NULL) {         /* we are completely hosed now */
        fprintf(stderr,
          "writepng severe error:  jmpbuf not recoverable; terminating.\n");
        fflush(stderr);
        return;
    }
    longjmp(graphic->jmpbuf, 1);
}

int png_pixel_plot(struct zint_symbol *symbol, int image_height, int image_width, char *pixelbuf, int rotate_angle)
{
	struct mainprog_info_type wpng_info;
	struct mainprog_info_type *graphic;
	
#ifndef _MSC_VER
	unsigned char outdata[image_width * 3];
#else
	unsigned char* outdata = (unsigned char*)_alloca(image_width * 3);
#endif
	png_structp  png_ptr;
	png_infop  info_ptr;
	graphic = &wpng_info;
	unsigned char *image_data;
	int i, row, column, err_no;
	int fgred, fggrn, fgblu, bgred, bggrn, bgblu;
	
	switch(rotate_angle) {
		case 0:
		case 180:
			graphic->width = image_width;
			graphic->height = image_height;
			break;
		case 90:
		case 270:
			graphic->width = image_height;
			graphic->height = image_width;
			break;
	}
	
	/* 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 ERROR_INVALID_OPTION;
	}
	if(strlen(symbol->bgcolour) != 6) {
		strcpy(symbol->errtxt, "Malformed background colour target");
		return ERROR_INVALID_OPTION;
	}
	err_no = is_sane(SSET, (unsigned char*)symbol->fgcolour, strlen(symbol->fgcolour));
	if (err_no == ERROR_INVALID_DATA) {
		strcpy(symbol->errtxt, "Malformed foreground colour target");
		return ERROR_INVALID_OPTION;
	}
	err_no = is_sane(SSET, (unsigned char*)symbol->bgcolour, strlen(symbol->bgcolour));
	if (err_no == ERROR_INVALID_DATA) {
		strcpy(symbol->errtxt, "Malformed background colour target");
		return 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]);
	
	/* 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 ERROR_FILE_ACCESS;
		}
#endif
		graphic->outfile = stdout;
	} else {
		if (!(graphic->outfile = fopen(symbol->outfile, "wb"))) {
			strcpy(symbol->errtxt, "Can't open output file");
			return ERROR_FILE_ACCESS;
		}
	}
	
	/* Set up error handling routine as proc() above */
	png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, graphic, writepng_error_handler, NULL);
	if (!png_ptr) {
		strcpy(symbol->errtxt, "Out of memory");
		return ERROR_MEMORY;
	}

	info_ptr = png_create_info_struct(png_ptr);
	if (!info_ptr) {
		png_destroy_write_struct(&png_ptr, NULL);
		strcpy(symbol->errtxt, "Out of memory");
		return ERROR_MEMORY;
	}

	/* catch jumping here */
	if (setjmp(graphic->jmpbuf)) {
		png_destroy_write_struct(&png_ptr, &info_ptr);
		strcpy(symbol->errtxt, "libpng error occurred");
		return ERROR_MEMORY;
	}

	/* open output file with libpng */
	png_init_io(png_ptr, graphic->outfile);

	/* set compression */
	png_set_compression_level(png_ptr,9);

	/* set Header block */
	png_set_IHDR(png_ptr, info_ptr, graphic->width, graphic->height,
		     8, PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
       PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);

	/* write all chunks up to (but not including) first IDAT */
	png_write_info(png_ptr, info_ptr);

	/* set up the transformations:  for now, just pack low-bit-depth pixels
	into bytes (one, two or four pixels per byte) */
	png_set_packing(png_ptr);

	/* Pixel Plotting */

	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 = column * 3;
					switch(*(pixelbuf + (image_width * row) + column))
					{
						case '1':
							outdata[i] = fgred;
							outdata[i + 1] = fggrn;
							outdata[i + 2] = fgblu;
							break;
						default:
							outdata[i] = bgred;
							outdata[i + 1] = bggrn;
							outdata[i + 2] = bgblu;
							break;
				
					}
				}
				/* write row contents to file */
				image_data = outdata;
				png_write_row(png_ptr, image_data);
			}
			break;
		case 90: /* Plot 90 degrees clockwise */
			for(row = 0; row < image_width; row++) {
				for(column = 0; column < image_height; column++) {
					i = column * 3;
					switch(*(pixelbuf + (image_width * (image_height - column - 1)) + row))
					{
						case '1':
							outdata[i] = fgred;
							outdata[i + 1] = fggrn;
							outdata[i + 2] = fgblu;
							break;
						default:
							outdata[i] = bgred;
							outdata[i + 1] = bggrn;
							outdata[i + 2] = bgblu;
							break;
			
					}
				}
		
				/* write row contents to file */
				image_data = outdata;
				png_write_row(png_ptr, image_data);
			}
			break;
		case 180: /* Plot upside down */
			for(row = 0; row < image_height; row++) {
				for(column = 0; column < image_width; column++) {
					i = column * 3;
					switch(*(pixelbuf + (image_width * (image_height - row - 1)) + (image_width - column - 1)))
					{
						case '1':
							outdata[i] = fgred;
							outdata[i + 1] = fggrn;
							outdata[i + 2] = fgblu;
							break;
						default:
							outdata[i] = bgred;
							outdata[i + 1] = bggrn;
							outdata[i + 2] = bgblu;
							break;
			
					}
				}
		
				/* write row contents to file */
				image_data = outdata;
				png_write_row(png_ptr, image_data);
			}
			break;
		case 270: /* Plot 90 degrees anti-clockwise */
			for(row = 0; row < image_width; row++) {
				for(column = 0; column < image_height; column++) {
					i = column * 3;
					switch(*(pixelbuf + (image_width * column) + (image_width - row - 1)))
					{
						case '1':
							outdata[i] = fgred;
							outdata[i + 1] = fggrn;
							outdata[i + 2] = fgblu;
							break;
						default:
							outdata[i] = bgred;
							outdata[i + 1] = bggrn;
							outdata[i + 2] = bgblu;
							break;
	
					}
				}

				/* write row contents to file */
				image_data = outdata;
				png_write_row(png_ptr, image_data);
			}
			break;
	}

	/* End the file */
	png_write_end(png_ptr, NULL);

	/* make sure we have disengaged */
	if (png_ptr && info_ptr) png_destroy_write_struct(&png_ptr, &info_ptr);
	fclose(wpng_info.outfile);
	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, err_no;
	int fgred, fggrn, fgblu, bgred, bggrn, bgblu;
	
	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);

    symbol->bitmap = (char *) malloc(image_width * image_height * 3);

	
	/* 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 ERROR_INVALID_OPTION;
	}
	if(strlen(symbol->bgcolour) != 6) {
		strcpy(symbol->errtxt, "Malformed background colour target");
		return ERROR_INVALID_OPTION;
	}
	err_no = is_sane(SSET, (unsigned char*)symbol->fgcolour, strlen(symbol->fgcolour));
	if (err_no == ERROR_INVALID_DATA) {
		strcpy(symbol->errtxt, "Malformed foreground colour target");
		return ERROR_INVALID_OPTION;
	}
	err_no = is_sane(SSET, (unsigned char*)symbol->bgcolour, strlen(symbol->fgcolour));
	if (err_no == ERROR_INVALID_DATA) {
		strcpy(symbol->errtxt, "Malformed background colour target");
		return 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++) {
					switch(*(pixelbuf + (image_width * row) + column))
					{
						case '1':
							symbol->bitmap[i++] = fgred;
							symbol->bitmap[i++] = fggrn;
							symbol->bitmap[i++] = fgblu;
							break;
						default:
							symbol->bitmap[i++] = bgred;
							symbol->bitmap[i++] = bggrn;
							symbol->bitmap[i++] = bgblu;
							break;
				
					}
				}
			}
			break;
		case 90: /* Plot 90 degrees clockwise */			
			for(row = 0; row < image_width; row++) {
				for(column = 0; column < image_height; column++) {
					switch(*(pixelbuf + (image_width * (image_height - column - 1)) + row))
					{
						case '1':
							symbol->bitmap[i++] = fgred;
							symbol->bitmap[i++] = fggrn;
							symbol->bitmap[i++] = fgblu;
							break;
						default:
							symbol->bitmap[i++] = bgred;
							symbol->bitmap[i++] = bggrn;
							symbol->bitmap[i++] = bgblu;
							break;
			
					}
				}
			}
			break;
		case 180: /* Plot upside down */
			for(row = 0; row < image_height; row++) {
				for(column = 0; column < image_width; column++) {
					switch(*(pixelbuf + (image_width * (image_height - row - 1)) + (image_width - column - 1)))
					{
						case '1':
							symbol->bitmap[i++] = fgred;
							symbol->bitmap[i++] = fggrn;
							symbol->bitmap[i++] = fgblu;
							break;
						default:
							symbol->bitmap[i++] = bgred;
							symbol->bitmap[i++] = bggrn;
							symbol->bitmap[i++] = bgblu;
							break;
			
					}
				}
			}
			break;
		case 270: /* Plot 90 degrees anti-clockwise */
			for(row = 0; row < image_width; row++) {
				for(column = 0; column < image_height; column++) {
					switch(*(pixelbuf + (image_width * column) + (image_width - row - 1)))
					{
						case '1':
							symbol->bitmap[i++] = fgred;
							symbol->bitmap[i++] = fggrn;
							symbol->bitmap[i++] = fgblu;
							break;
						default:
							symbol->bitmap[i++] = bgred;
							symbol->bitmap[i++] = bggrn;
							symbol->bitmap[i++] = bgblu;
							break;
	
					}
				}
			}
			break;
	}

	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;
	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)) == NULL) {
		printf("Insufficient memory for pixel buffer");
		return ERROR_ENCODING_PROBLEM;
	}
	memset(scaled_pixelbuf, '0', scale_width * scale_height);
	
	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
		error_number = 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;
	
	switch(block_pos) {
		case 0: if((block_val & 0x80) != 0) { return_val = 1; } break;
		case 1: if((block_val & 0x40) != 0) { return_val = 1; } break;
		case 2: if((block_val & 0x20) != 0) { return_val = 1; } break;
		case 3: if((block_val & 0x10) != 0) { return_val = 1; } break;
		case 4: if((block_val & 0x08) != 0) { return_val = 1; } break;
		case 5: if((block_val & 0x04) != 0) { return_val = 1; } break;
		case 6: if((block_val & 0x02) != 0) { return_val = 1; } break;
		case 7: if((block_val & 0x01) != 0) { return_val = 1; } break;
	}
	
	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 smalltext, int image_width, int image_height)
{
	/* Put a letter into a position */
	int skip, i, j, glyph_no, alphabet;
	
	skip = 0;
	alphabet = 0;

	if(letter < 33) { skip = 1; }
	if((letter > 127) && (letter < 161)) { skip = 1; }
	
	if(skip == 0) {
		if(letter > 128) {
			alphabet = 1;
			glyph_no = letter - 161;
		} else {
			glyph_no = letter - 33;
		}
		
		if(smalltext) {
			for(i = 0; i <= 8; i++) {
				for(j = 0; j < 5; j++) {
					if(alphabet == 0) {
						if(small_font[(glyph_no * 5) + (i * 475) + j - 1] == 1) {
							*(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1';
						}
					} else {
						if(small_font_extended[(glyph_no * 5) + (i * 475) + j - 1] == 1) {
							*(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1';
						}
					}
				}
			}
		} else {
			for(i = 0; i <= 13; i++) {
				for(j = 0; j < 7; j++) {
					if(alphabet == 0) {
						if(ascii_font[(glyph_no * 7) + (i * 665) + j - 1] == 1) {
							*(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1';
						}
					} else {
						if(ascii_ext_font[(glyph_no * 7) + (i * 665) + j - 1] == 1) {
							*(pixelbuf + (i * image_width) + (yposn * image_width) + xposn + j) = '1';
						}
					}
				}
			}
		}
	}
}

void draw_string(char *pixbuf, char input_string[], int xposn, int yposn, int smalltext, int image_width, int image_height)
{
	/* Plot a string into the pixel buffer */
	int i, string_length, string_left_hand;
	
	string_length = strlen(input_string);
	string_left_hand = xposn - ((7 * string_length) / 2);
	
	for(i = 0; i < string_length; i++) {
		draw_letter(pixbuf, input_string[i], string_left_hand + (i * 7), yposn, smalltext, 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 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;
}

void to_latin1(unsigned char source[], unsigned char preprocessed[])
{
	int j, i, input_length;

	input_length = ustrlen(source);

	j = 0;
	i = 0;
	do {
		if(source[i] < 128) {
			preprocessed[j] = source[i];
			j++;
			i++;
		} else {
			if(source[i] == 0xC2) {
				preprocessed[j] = source[i + 1];
				j++;
				i += 2;
			}
			if(source[i] == 0xC3) {
				preprocessed[j] = source[i + 1] + 64;
				j++;
				i += 2;
			}
		}
	} while (i < input_length);
	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 = NULL;
	int addon_latch = 0, smalltext = 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;
	int tlen = ustrlen(symbol->text);
#ifndef _MSC_VER
	unsigned char local_text[tlen + 1];
#else
	unsigned char* local_text = (unsigned char*)_alloca(tlen+ 1);
#endif

	if(symbol->show_hrt != 0) {
		to_latin1(symbol->text, local_text);
	} else {
		local_text[0] = '\0';
	}

	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) {
		smalltext = 1;
	}

	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(tlen) {
			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 < tlen; i++) {
			if (latch == 1) {
				addon[r] = local_text[i];
				r++;
			}
			if (symbol->text[i] == '+') {
				latch = 1;
			}
		}
	}
	addon[r] = '\0';
	
	if(tlen) {
		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 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(tlen) {
			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] = symbol->text[i];
				}
				textpart[4] = '\0';
				textpos = 2 * (17 + xoffset);
				
				draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height);
				for(i = 0; i < 4; i++) {
					textpart[i] = symbol->text[i + 4];
				}
				textpart[4] = '\0';
				textpos = 2 * (50 + xoffset);
				draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, 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, smalltext, image_width, image_height);
						break;
					case 5:
						textpos = 2 * (xoffset + 100);
						draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, 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] = symbol->text[0];
				textpart[1] = '\0';
				textpos = 2 * (-7 + xoffset);
				draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height);
				for(i = 0; i < 6; i++) {
					textpart[i] = symbol->text[i + 1];
				}
				textpart[6] = '\0';
				textpos = 2 * (24 + xoffset);
				draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height);
				for(i = 0; i < 6; i++) {
					textpart[i] = symbol->text[i + 7];
				}
				textpart[6] = '\0';
				textpos = 2 * (71 + xoffset);
				draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, 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, smalltext, image_width, image_height);
						break;
					case 5:
						textpos = 2 * (xoffset + 128);
						draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, 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] = symbol->text[0];
		textpart[1] = '\0';
		textpos = 2 * (-5 + xoffset);
		draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height);
		for(i = 0; i < 5; i++) {
			textpart[i] = symbol->text[i + 1];
		}
		textpart[5] = '\0';
		textpos = 2 * (27 + xoffset);
		draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height);
		for(i = 0; i < 5; i++) {
			textpart[i] = symbol->text[i + 6];
		}
		textpart[6] = '\0';
		textpos = 2 * (68 + xoffset);
		draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height);
		textpart[0] = symbol->text[11];
		textpart[1] = '\0';
		textpos = 2 * (100 + xoffset);
		draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, 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, smalltext, image_width, image_height);
				break;
			case 5:
				textpos = 2 * (xoffset + 130);
				draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, 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] = symbol->text[0];
		textpart[1] = '\0';
		textpos = 2 * (-5 + xoffset);
		draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height);
		for(i = 0; i < 6; i++) {
			textpart[i] = symbol->text[i + 1];
		}
		textpart[6] = '\0';
		textpos = 2 * (24 + xoffset);
		draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, image_width, image_height);
		textpart[0] = symbol->text[7];
		textpart[1] = '\0';
		textpos = 2 * (55 + xoffset);
		draw_string(pixelbuf, textpart, textpos, default_text_posn, smalltext, 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, smalltext, image_width, image_height);
				break;
			case 5:
				textpos = 2 * (xoffset + 84);
				draw_string(pixelbuf, addon, textpos, image_height - (addon_text_posn * 2) - 13, smalltext, 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) && tlen) {
		textpos = (image_width / 2);
		draw_string(pixelbuf, (char*)local_text, textpos, default_text_posn, smalltext, 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;
}