-
Notifications
You must be signed in to change notification settings - Fork 16
/
bmpsuite.c
2175 lines (1941 loc) · 57.2 KB
/
bmpsuite.c
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
//
// BMP Suite (2012 rewrite)
// Copyright (C) 2012-2023 Jason Summers
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
//
// Image files generated by this program are in the public domain.
//
#ifdef _WIN32
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#ifdef _WIN32
#include <direct.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
// #define SMALL_IMAGES
#define BMP_MAX_SIZE 100000
#define bmpovl_width 78
#define bmpovl_height 26
#define bmpovl_xpos 25
#define bmpovl_ypos 19
static const char *bmpovl[] = {
"11111111111111111111.......11....................11...11111111111111111111....",
"1111111111111111111111.....111..................111...1111111111111111111111..",
"11222222222222222222111....1111................1111...11222222222222222222111.",
"112222222222222222222111...11211..............11211...112222222222222222222111",
"112211111111111111122211...112211............112211...112211111111111111122211",
"112211111111111111112211...1122211..........1122211...112211111111111111112211",
"112211...........1112211...11222211........11222211...112211...........1112211",
"112211............112211...112212211......112212211...112211............112211",
"112211............112211...1122112211....1122112211...112211............112211",
"112211...........1112211...11221112211..11221112211...112211...........1112211",
"11221111111111111112211....112211112211112211112211...112211111111111111112211",
"1122111111111111112211.....112211.1122112211.112211...112211111111111111122211",
"112222222222222222211......112211..11222211..112211...112222222222222222222111",
"112222222222222222211......112211...112211...112211...11222222222222222222111.",
"1122111111111111112211.....112211....1111....112211...1122111111111111111111..",
"11221111111111111112211....112211.....11.....112211...11221111111111111111....",
"112211...........1112211...112211............112211...112211..................",
"112211............112211...112211............112211...112211..................",
"112211............112211...112211............112211...112211..................",
"112211...........1112211...112211............112211...112211..................",
"112211111111111111112211...112211............112211...112211..................",
"112211111111111111122211...112211............112211...112211..................",
"112222222222222222222111...112211............112211...112211..................",
"11222222222222222222111....112211............112211...112211..................",
"1111111111111111111111.....111111............111111...111111..................",
"11111111111111111111.......111111............111111...111111.................."
};
#define I_R 0
#define I_G 1
#define I_B 2
#define I_A 3
struct color_f {
double s[4]; // [0]=R, [1]=G, [2]=B, [3]=A
};
struct color_i {
unsigned int s[4]; // R, G, B, A
};
struct global_context {
unsigned char *mem;
};
// Data specific to each image
struct context {
const char *filename;
unsigned char *mem;
size_t mem_used;
int bpp;
int pal_entries;
int clr_used;
int headersize;
int bitfieldssize;
int palettesize;
int extrabytessize;
int bitsoffset; // Offset from beginning of file
int bitssize;
int profile_offset; // Offset from beginning of file
int profile_size;
int w, h;
int rowsize;
int xpelspermeter, ypelspermeter;
#define CMPR_RLE8 1
#define CMPR_RLE4 2
#define CMPR_JPEG 4
#define CMPR_PNG 5
#define CMPR_HUFFMAN1D 3
#define CMPR_RLE24 4
#define BI_BITFIELDS 3
#define BI_ALPHABITFIELDS 6
int compression;
int pal_gs; // grayscale palette
int pal_wb; // 2-color, palette[0] = white
int pal_bg; // 2-color, blue & green
int pal_p1; // 1-color
unsigned int bf[4]; // bitfields for R, G, B, A. Used if bpp==16 or 32.
unsigned int nbits[4];
unsigned int bf_shift[4];
// The dither codes tell how to quantize each sample.
// -1 = default; 0 = no dithering; 1 = dithering
int dither[4];
int topdown;
int embed_profile;
int profile_to_embed;
int swaprg;
int link_profile;
int fakealpha;
int halfheight;
int zero_biSizeImage;
int bad_biSizeImage;
int bad_bfSize;
int bad_width;
int bad_reallybig;
int bad_bitcount;
int bad_planes;
int bad_palettesize;
int bad_rle;
int bad_rle_bis;
int bad_rle_ter;
int cut_rle;
int bad_eof;
int rletrns;
int palette_reserve; // Reserve palette color #0
int cbsize_flag;
int set_hotspot;
int trnstype; // Transparency type: 0=none, 1=binary, 2=full
int ba_fmt;
int ba_hdr_size;
int huff_lsb; // No longer used
};
static void set_int16(struct context *c, size_t offset, int v)
{
c->mem[offset] = v&0xff;
c->mem[offset+1] = (v>>8)&0xff;
}
static void set_uint16(struct context *c, size_t offset, unsigned int v)
{
c->mem[offset] = v&0xff;
c->mem[offset+1] = (v>>8)&0xff;
}
static void set_int32(struct context *c, size_t offset, int v)
{
c->mem[offset] = v&0xff;
c->mem[offset+1] = (v>>8)&0xff;
c->mem[offset+2] = (v>>16)&0xff;
c->mem[offset+3] = (v>>24)&0xff;
}
static void set_uint32(struct context *c, size_t offset, unsigned int v)
{
c->mem[offset] = v&0xff;
c->mem[offset+1] = (v>>8)&0xff;
c->mem[offset+2] = (v>>16)&0xff;
c->mem[offset+3] = (v>>24)&0xff;
}
// Returns an int between 0 and (numcc-1), inclusive.
static int scale_to_int(double x, int numcc)
{
int s;
#define BMPSUITE_EPSILON 0.0000001
s = (int)(0.5+BMPSUITE_EPSILON+x*(numcc-1));
if(s<0) s=0;
if(s>numcc-1) s=numcc-1;
return s;
}
static double srgb_to_linear(double v_srgb)
{
if(v_srgb<=0.04045) {
return v_srgb/12.92;
}
else {
return pow( (v_srgb+0.055)/(1.055) , 2.4);
}
}
static double linear_to_srgb(double v_linear)
{
if(v_linear<=0.0031308) {
return v_linear*12.92;
}
else {
return 1.055*pow(v_linear, 1.0/2.4) - 0.055;
}
}
static void get_pixel_color(struct context *c, int x1, int y1,
struct color_f *clr)
{
unsigned char t;
int x, y;
#ifdef SMALL_IMAGES
x = x1+21;
y = y1+16;
#else
x = x1;
y = y1;
#endif
if(x>=bmpovl_xpos && x<(bmpovl_xpos+bmpovl_width) &&
y>=bmpovl_ypos && y<(bmpovl_ypos+bmpovl_height))
{
t = bmpovl[y-bmpovl_ypos][x-bmpovl_xpos];
if(t=='1') {
clr->s[I_R] = 0.0; clr->s[I_G] = 0.0; clr->s[I_B] = 0.0; clr->s[I_A] = 1.0;
goto done;
}
else if(t=='2') {
if(c->trnstype==2) { // alpha transparency
// Make the inside of the overlay transparent, if possible.
if( (y-bmpovl_ypos)<(bmpovl_height/2) ) {
// Make the top half completely transparent ("transparent green").
clr->s[I_R] = 0.0; clr->s[I_G] = 1.0; clr->s[I_B] = 0.0; clr->s[I_A] = 0.0;
}
else {
// Make the bottom half a red gradient from transparent to opaque.
clr->s[I_R] = 1.0; clr->s[I_G] = 0.0; clr->s[I_B] = 0.0;
clr->s[I_A] = 2*((double)(y-bmpovl_ypos)) /(bmpovl_height) -1.0;
}
}
else if(c->trnstype==1) { // binary transparency
clr->s[I_R] = 1.0; clr->s[I_G] = 1.0; clr->s[I_B] = 1.0; clr->s[I_A] = 0.0;
}
else {
clr->s[I_R] = 1.0; clr->s[I_G] = 1.0; clr->s[I_B] = 1.0; clr->s[I_A] = 1.0;
}
goto done;
}
}
clr->s[I_A] = 1.0;
// Standard truecolor image
if(x<32) {
clr->s[I_R] = ((double)(63-y))/63.0;
clr->s[I_G] = ((double)(x%32))/31.0;
clr->s[I_B] = ((double)(x%32))/31.0;
}
else if(x<64) {
clr->s[I_R] = ((double)(x%32))/31.0;
clr->s[I_G] = ((double)(63-y))/63.0;
clr->s[I_B] = ((double)(x%32))/31.0;
}
else if(x<96) {
clr->s[I_R] = ((double)(x%32))/31.0;
clr->s[I_G] = ((double)(x%32))/31.0;
clr->s[I_B] = ((double)(63-y))/63.0;
}
else {
clr->s[I_R] = ((double)(159-y))/255.0;
clr->s[I_G] = ((double)(159-y))/255.0;
clr->s[I_B] = ((double)(159-y + x%32))/255.0;
}
done:
if(c->fakealpha)
clr->s[I_A] = 1.0 - ((double)y)/(c->h-1);
}
static int ordered_dither_lowlevel(double fraction, int x, int y)
{
double threshold;
static const float pattern[64] = {
0.5/64,48.5/64,12.5/64,60.5/64, 3.5/64,51.5/64,15.5/64,63.5/64,
32.5/64,16.5/64,44.5/64,28.5/64,35.5/64,19.5/64,47.5/64,31.5/64,
8.5/64,56.5/64, 4.5/64,52.5/64,11.5/64,59.5/64, 7.5/64,55.5/64,
40.5/64,24.5/64,36.5/64,20.5/64,43.5/64,27.5/64,39.5/64,23.5/64,
2.5/64,50.5/64,14.5/64,62.5/64, 1.5/64,49.5/64,13.5/64,61.5/64,
34.5/64,18.5/64,46.5/64,30.5/64,33.5/64,17.5/64,45.5/64,29.5/64,
10.5/64,58.5/64, 6.5/64,54.5/64, 9.5/64,57.5/64, 5.5/64,53.5/64,
42.5/64,26.5/64,38.5/64,22.5/64,41.5/64,25.5/64,37.5/64,21.5/64
};
threshold = pattern[(x%8) + 8*(y%8)];
return (fraction >= threshold) ? 1 : 0;
}
// 'v' is on a scale from 0.0 to 1.0.
// numcc is the number of output color codes, e.g. 256. The result will be
// from 0 to (numcc-1).
// Not all combinations of flags are supported.
static int quantize(double v_to_1, int numcc, int x, int y,
int dither, int from_srgb, int to_srgb)
{
double v_to_1_linear;
double v_to_1_targetcs;
double v_to_maxcc;
double floor_to_maxcc, ceil_to_maxcc;
double floor_to_1, ceil_to_1;
double floor_to_1_linear, ceil_to_1_linear;
double fraction;
int all_linear;
int maxcc = numcc-1;
if(dither<1 && !from_srgb && !to_srgb) {
return scale_to_int(v_to_1, numcc);
}
all_linear = (!from_srgb && !to_srgb);
if(all_linear)
v_to_1_targetcs = v_to_1;
else
v_to_1_targetcs = from_srgb ? v_to_1 : linear_to_srgb(v_to_1);
v_to_maxcc = v_to_1_targetcs*maxcc;
floor_to_maxcc = floor(v_to_maxcc);
if(floor_to_maxcc>=(double)maxcc) return maxcc;
ceil_to_maxcc = floor_to_maxcc+1.0;
// The two possible values to return are floor_to_maxcc and ceil_to_maxcc.
// v_to_maxcc's brightness is some fraction of the way between
// floor_to_maxcc's brightness and ceil_to_maxcc's brightness, and we need
// to calculate that fraction. To do that, convert everything to a linear
// colorspace.
floor_to_1 = floor_to_maxcc/maxcc;
ceil_to_1 = ceil_to_maxcc/maxcc;
if(all_linear)
floor_to_1_linear = floor_to_1;
else
floor_to_1_linear = srgb_to_linear(floor_to_1);
v_to_1_linear = from_srgb ? srgb_to_linear(v_to_1) : v_to_1;
if(all_linear)
ceil_to_1_linear = ceil_to_1;
else
ceil_to_1_linear = srgb_to_linear(ceil_to_1);
fraction = (v_to_1_linear-floor_to_1_linear)/(ceil_to_1_linear-floor_to_1_linear);
if(ordered_dither_lowlevel(fraction,x,y))
return (int)ceil_to_maxcc;
else
return (int)floor_to_maxcc;
}
static double srgb_to_linear_gray(const struct color_f *clr)
{
return srgb_to_linear(clr->s[I_R])*0.212655 +
srgb_to_linear(clr->s[I_G])*0.715158 +
srgb_to_linear(clr->s[I_B])*0.072187;
}
// colorspaceflag = convert from sRGB to linear
// Windows Imaging Component (WIC) "Native Pixel Formats Overview" document
// hints that this format might use "scRGB" color space, but even if that's
// true in some sense, evidence suggests the encoding is linear.
static void write_64bppfixedpoint16(struct context *c, size_t offset,
double val1, int colorspaceflag)
{
double val;
unsigned int v;
if(colorspaceflag)
val = srgb_to_linear(val1);
else
val = val1;
if(val<0.0) {
// Negative numbers are supported by this format, but we don't
// currently use them.
v = 0;
}
else {
// 0.0 -> 0
// 1.0 -> 8192
v = (unsigned int)(val*8192.0 + 0.5);
if(v>8192) v = 8192;
}
set_uint16(c, offset, v);
}
static void set_pixel(struct context *c, int x, int y,
const struct color_f *clr)
{
struct color_i qclr;
int tmp1, tmp2, tmp3;
int p;
int z;
size_t row_offs;
size_t offs;
double tmpd;
unsigned int u;
if(c->topdown)
row_offs = y*c->rowsize;
else
row_offs = (c->h-y-1)*c->rowsize;
if(c->bpp==64) {
offs = row_offs + 8*x;
write_64bppfixedpoint16(c, c->bitsoffset+offs+0, clr->s[I_B], 1);
write_64bppfixedpoint16(c, c->bitsoffset+offs+2, clr->s[I_G], 1);
write_64bppfixedpoint16(c, c->bitsoffset+offs+4, clr->s[I_R], 1);
// AFAICT, this format uses non-premultiplied alpha (which is what
// other BMP formats presumably use, but 64bpp is different enough
// that we shouldn't just assume).
write_64bppfixedpoint16(c, c->bitsoffset+offs+6, clr->s[I_A], 0);
}
else if(c->bpp==32) {
offs = row_offs + 4*x;
for(z=0; z<3; z++) {
qclr.s[z] = quantize(clr->s[z], 1<<c->nbits[z], x, y, 0, 0, 0);
}
if(c->nbits[I_A])
qclr.s[I_A] = quantize(clr->s[I_A], 1<<c->nbits[I_A], x, y, c->dither[I_A], 0, 0);
u = (qclr.s[I_R]<<c->bf_shift[I_R]) | (qclr.s[I_G]<<c->bf_shift[I_G]) | (qclr.s[I_B]<<c->bf_shift[I_B]);
if(c->nbits[I_A]) u |= qclr.s[I_A]<<c->bf_shift[I_A];
c->mem[c->bitsoffset+offs+0] = (unsigned char)(u&0xff);
c->mem[c->bitsoffset+offs+1] = (unsigned char)((u>>8)&0xff);
c->mem[c->bitsoffset+offs+2] = (unsigned char)((u>>16)&0xff);
c->mem[c->bitsoffset+offs+3] = (unsigned char)((u>>24)&0xff);
}
else if(c->bpp==24) {
offs = row_offs + 3*x;
for(z=0; z<3; z++) {
qclr.s[z] = quantize(clr->s[z], 256, x, y, 0, 0, 0);
}
if(c->swaprg) {
u = qclr.s[I_R]; qclr.s[I_R] = qclr.s[I_G]; qclr.s[I_G] = u;
}
c->mem[c->bitsoffset+offs+0] = (unsigned char)qclr.s[I_B];
c->mem[c->bitsoffset+offs+1] = (unsigned char)qclr.s[I_G];
c->mem[c->bitsoffset+offs+2] = (unsigned char)qclr.s[I_R];
}
else if(c->bpp==16) {
offs = row_offs + 2*x;
for(z=0; z<3; z++) {
if(c->dither[z]==1)
qclr.s[z] = quantize(clr->s[z], 1<<c->nbits[z], x, y, 1, 1, 1);
else
qclr.s[z] = quantize(clr->s[z], 1<<c->nbits[z], x, y, 0, 0, 0);
}
if(c->nbits[I_A])
qclr.s[I_A] = quantize(clr->s[I_A], 1<<c->nbits[I_A], x, y, c->dither[I_A], 0, 0);
u = (qclr.s[I_R]<<c->bf_shift[I_R]) | (qclr.s[I_G]<<c->bf_shift[I_G]) | (qclr.s[I_B]<<c->bf_shift[I_B]);
if(c->nbits[I_A]) u |= qclr.s[I_A]<<c->bf_shift[I_A];
c->mem[c->bitsoffset+offs+0] = (unsigned char)(u&0xff);
c->mem[c->bitsoffset+offs+1] = (unsigned char)((u>>8)&0xff);
}
else if(c->bpp==8) {
offs = row_offs + x;
if (c->pal_gs) {
tmpd = srgb_to_linear_gray(clr);
p = quantize(tmpd, c->pal_entries - c->palette_reserve, x, y, 1, 0, 1);
if(c->palette_reserve) {
if(p<c->pal_entries) p++;
}
}
else {
tmp1 = quantize(clr->s[I_R], 6, x, y, 1, 1, 1);
tmp2 = quantize(clr->s[I_G], 7, x, y, 1, 1, 1);
tmp3 = quantize(clr->s[I_B], 6, x, y, 1, 1, 1);
p = tmp1 + tmp2*6 + tmp3*42;
if(c->palette_reserve) {
if(p<255) p++;
}
}
c->mem[c->bitsoffset+offs] = p;
}
else if(c->bpp==4) {
offs = row_offs + x/2;
if (c->pal_gs) {
tmpd = srgb_to_linear_gray(clr);
p = quantize(tmpd, c->pal_entries - c->palette_reserve, x, y, 1, 0, 1);
if(c->palette_reserve) {
if(p<c->pal_entries) p++;
}
}
else {
tmp1 = quantize(clr->s[I_R], 2, x, y, 1, 1, 1);
tmp2 = quantize(clr->s[I_G], 3, x, y, 1, 1, 1);
tmp3 = quantize(clr->s[I_B], 2, x, y, 1, 1, 1);
p = tmp1 + tmp2*2 + tmp3*6;
}
if(x%2)
c->mem[c->bitsoffset+offs] |= p;
else
c->mem[c->bitsoffset+offs] |= p<<4;
}
else if(c->bpp==2) {
offs = row_offs + x/4;
tmpd = srgb_to_linear_gray(clr);
tmp1 = quantize(tmpd, 4, x, y, 1, 0, 1);
c->mem[c->bitsoffset+offs] |= tmp1<<(2*(3-x%4));
}
else if(c->bpp==1) {
offs = row_offs + x/8;
tmpd = srgb_to_linear_gray(clr);
tmp1 = quantize(tmpd, 2, x, y, 1, 0, 1);
if(c->pal_wb) tmp1 = 1-tmp1; // Palette starts with white, so invert the colors.
if(c->pal_p1) tmp1 = 0;
if(tmp1) {
c->mem[c->bitsoffset+offs] |= 1<<(7-x%8);
}
}
}
static void calc_rle_run_lens(struct context *c, const unsigned char *row,
int *run_lens, int pixels_per_row)
{
int i,k,n;
for(i=0;i<pixels_per_row;i++) {
n=0;
for(k=i;k<pixels_per_row;k++) {
if(n>=255) break;
if(c->rletrns && row[k]==0) {
// A "transparent" pixel.
if(k-i==0) { n++; continue; } // start of a transparent run
if(row[k-1]==0) { n++; continue; } // continuing a transparent run;
break; // transparent pixel stops a nontransparent run.
}
if(c->rletrns && k-i>=1) {
if(row[k]!=0 && row[k-1]==0) {
// nontransparent pixel stops a transparent run.
break;
}
}
if(c->compression==CMPR_RLE4 && k-i<=1) { n++; continue; } // (RLE4) First two pixels can always be part of the run
if(c->compression!=CMPR_RLE4 && k-i<=0) { n++; continue; } // (RLE8) First pixels can always be part of the run
if(c->compression==CMPR_RLE4 && row[k]==row[k-2]) { n++; continue; }
if(c->compression!=CMPR_RLE4 && row[k]==row[k-1]) { n++; continue; }
break;
}
run_lens[i] = n;
}
}
// Note: This is not a particularly good BMP RLE compression algorithm.
// I don't recommend copying it.
static int write_bits_rle(struct context *c)
{
size_t curpos; // where in c->mem to write to next
size_t rowpos;
int pixels_per_row;
unsigned char *row = NULL;
struct color_i *row24 = NULL;
int *run_lens = NULL;
int i,j;
int j_logical;
int k;
int tmp1, tmp2, tmp3;
struct color_f clr;
int unc_len;
int unc_len_padded;
int thresh;
int retval = 0;
curpos = c->bitsoffset;
pixels_per_row = c->w;
row = malloc(pixels_per_row);
if(!row) goto done;
row24 = malloc(pixels_per_row * sizeof(struct color_i));
if(!row24) goto done;
run_lens = malloc(sizeof(int)*pixels_per_row);
if(!run_lens) goto done;
thresh = c->compression==CMPR_RLE4 ? 5 : 4;
for(j=0;j< (c->cut_rle ? c->h - 5 : c->h);j++) {
j_logical = c->topdown ? j : c->h-1-j;
// Temporarily store the palette indices in row[]
for(i=0;i<c->w;i++) {
get_pixel_color(c,i,j_logical, &clr);
if(c->compression==CMPR_RLE4) {
tmp1 = quantize(clr.s[I_R],2,i,j_logical,1,1,1);
tmp2 = quantize(clr.s[I_G],3,i,j_logical,1,1,1);
tmp3 = quantize(clr.s[I_B],2,i,j_logical,1,1,1);
row[i] = c->palette_reserve + tmp1 + tmp2*2 + tmp3*6;
}
else {
tmp1 = quantize(clr.s[I_R],6,i,j_logical,1,1,1);
tmp2 = quantize(clr.s[I_G],7,i,j_logical,1,1,1);
tmp3 = quantize(clr.s[I_B],6,i,j_logical,1,1,1);
row[i] = c->palette_reserve + tmp1 + tmp2*6 + tmp3*42;
// Calculate the would-be palette color. RLE24 needs this.
// Note that the RLE24 image uses only the 8-bit colors, since
// that makes it more compressible.
row24[i].s[I_R] = quantize(((double)tmp1)/5.0, 256, 0, 0, 0, 0, 0);
row24[i].s[I_G] = quantize(((double)tmp2)/6.0, 256, 0, 0, 0, 0, 0);
row24[i].s[I_B] = quantize(((double)tmp3)/5.0, 256, 0, 0, 0, 0, 0);
}
if(c->rletrns && clr.s[I_A]<0.5) {
row[i] = 0;
}
}
// Figure out the largest possible run length for each starting pixel.
calc_rle_run_lens(c,row,run_lens,pixels_per_row);
rowpos = 0; // index into row[]
while((int)rowpos < pixels_per_row) {
if(c->rletrns && row[rowpos]==0) { // transparent pixel
c->mem[curpos++] = 0;
c->mem[curpos++] = 2;
c->mem[curpos++] = run_lens[rowpos] + (unsigned char)(c->bad_rle?5:0) + (unsigned char)(c->bad_rle_bis?127:0); // x delta
c->mem[curpos++] = (unsigned char)(c->bad_rle?2:0) + (unsigned char)(c->bad_rle_ter?1:0); // y delta
rowpos += run_lens[rowpos];
continue;
}
if (c->cut_rle && ((int)rowpos > (pixels_per_row/2))) {
if ((j & 3) == 0) {
break;
}
}
if(run_lens[rowpos]<thresh) {
// Consider writing an uncompressed segment
// Find next run that's 'thresh' or larger
for(k=rowpos;k<pixels_per_row;k++) {
if(c->rletrns && row[k]==0) { break; } // Also have to stop at a transparent pixel
if(run_lens[k]>=thresh) { break; }
}
// If there's at least 3(?) pixels before it, write an uncompressed segment.
if(k != -1 && k-rowpos >= 3) {
unc_len = k-rowpos;
if(c->compression==CMPR_RLE24) {
c->mem[curpos++] = 0;
c->mem[curpos++] = unc_len;
for(i=0; i<unc_len; i++) {
c->mem[curpos++] = row24[rowpos].s[I_B];
c->mem[curpos++] = row24[rowpos].s[I_G];
c->mem[curpos++] = row24[rowpos].s[I_R];
rowpos++;
}
if(unc_len%2) {
c->mem[curpos++] = 0; // padding
}
continue;
}
if(c->compression==CMPR_RLE4)
unc_len_padded = unc_len + (3-(unc_len+3)%4);
else
unc_len_padded = unc_len + (unc_len%2);
c->mem[curpos++] = 0;
c->mem[curpos++] = unc_len;
for(i=0;i<unc_len_padded;i++) {
unsigned char v;
if(i<unc_len) v=row[rowpos++];
else v=0; // padding
if(c->compression==CMPR_RLE4) {
if(i%2==0)
c->mem[curpos] = v<<4;
else
c->mem[curpos++] |= v;
}
else {
c->mem[curpos++] = v;
}
}
continue;
}
}
if((int)rowpos >= pixels_per_row) break;
if(c->rletrns && row[rowpos]==0) break;
// Write a compressed segment
c->mem[curpos++] = run_lens[rowpos] + (unsigned char)(c->bad_rle?1:0);
if(c->compression==CMPR_RLE4) {
c->mem[curpos] = row[rowpos]<<4;
if(run_lens[rowpos]>=2)
c->mem[curpos] |= row[rowpos+1];
curpos++;
}
else if(c->compression==CMPR_RLE24) {
c->mem[curpos++] = row24[rowpos].s[I_B];
c->mem[curpos++] = row24[rowpos].s[I_G];
c->mem[curpos++] = row24[rowpos].s[I_R];
}
else {
c->mem[curpos] = row[rowpos];
curpos++;
}
rowpos += run_lens[rowpos];
}
// Write EOL (0 0) or EOBMP (0 1) marker.
c->mem[curpos++] = 0;
c->mem[curpos++] = (j==c->h-1) ? 1 : 0;
}
if (c->cut_rle) {
c->mem[curpos++] = 0;
c->mem[curpos++] = 1;
}
c->bitssize = curpos - c->bitsoffset;
c->mem_used = c->bitsoffset + c->bitssize;
retval = 1;
done:
free(row);
free(row24);
free(run_lens);
return retval;
}
static unsigned char reverse_bits_in_byte(unsigned char x)
{
unsigned int i;
unsigned char y = 0;
for(i=0; i<=7; i++) {
if(x & (1<<i)) y |= (1<<(7-i));
}
return y;
}
static int write_bits_fromfile(struct context *c, const char *fn,
unsigned int reverse_bits_flag)
{
int retval = 0;
FILE *f = NULL;
f=fopen(fn,"rb");
if(!f) goto done;
c->bitssize = fread(&c->mem[c->bitsoffset], 1, 100000-c->bitsoffset, f);
if(c->bitssize<1) goto done;
c->mem_used = c->bitsoffset + c->bitssize;
if(reverse_bits_flag) {
int i;
for(i=0; i<c->bitssize; i++) {
c->mem[c->bitsoffset+i] = reverse_bits_in_byte(c->mem[c->bitsoffset+i]);
}
}
retval = 1;
done:
if(f) fclose(f);
return retval;
}
static int write_bits(struct context *c)
{
int i,j;
struct color_f clr;
c->rowsize = (((c->w * c->bpp)+31)/32)*4;
c->bitssize = c->rowsize*c->h;
c->mem_used = c->bitsoffset + c->bitssize;
for(j=0;j<c->h;j++) {
for(i=0;i<c->w;i++) {
get_pixel_color(c, i, c->halfheight ? j*2 : j, &clr);
set_pixel(c, i, j, &clr);
}
}
return 1;
}
static int write_profile(struct context *c, const char *fn, int gap)
{
int retval = 0;
FILE *f = NULL;
f=fopen(fn,"rb");
if(!f) goto done;
// The "+ gap" leaves a gap of unused bytes between the bits and the
// profile, just to be difficult.
c->profile_offset = c->bitsoffset + c->bitssize + gap;
c->profile_size = fread(&c->mem[c->profile_offset], 1, 100000-c->profile_offset, f);
if(c->profile_size<1) goto done;
c->mem_used = c->profile_offset + c->profile_size;
retval = 1;
done:
if(f) fclose(f);
return retval;
}
static int write_lprofile(struct context *c)
{
// Filenames are always encoded in Windows-1252.
// 0x95=bullet; 0xeb="e" with a diaeresis
static const char prof_fn[] = "C:\\temp\\test\x95\xeb.icc";
c->profile_offset = c->bitsoffset + c->bitssize + 10;
c->profile_size = 1+strlen(prof_fn);
memcpy(&c->mem[c->profile_offset], prof_fn, c->profile_size);
c->mem_used = c->profile_offset + c->profile_size;
return 1;
}
static void write_bitfields(struct context *c)
{
size_t offs;
if(c->bitfieldssize!=12 && c->bitfieldssize!=16) return;
offs = 14+c->headersize;
set_uint32(c,offs ,c->bf[I_R]);
set_uint32(c,offs+4,c->bf[I_G]);
set_uint32(c,offs+8,c->bf[I_B]);
if(c->bitfieldssize==16) {
set_uint32(c,offs+12,c->bf[I_A]);
}
}
static void write_palette(struct context *c)
{
size_t offs;
int i, ii;
int r,g,b;
int bppe; // bytes per palette entry
offs = c->ba_hdr_size+14+c->headersize+c->bitfieldssize;
bppe = (c->headersize<=12) ? 3 : 4;
if(c->bpp==8) {
if(c->palette_reserve) {
c->mem[offs+2] = 128;
c->mem[offs+1] = 0;
c->mem[offs+0] = 255;
}
if(c->pal_gs) {
// Grayscale palette
for(i=c->palette_reserve;i<c->pal_entries;i++) {
ii = i-c->palette_reserve;
if(i>=252+c->palette_reserve) continue;
c->mem[offs+bppe*i+2] = quantize(ii/(double)(c->pal_entries - c->palette_reserve - 1), 256, 0, 0, 0, 0, 0);
c->mem[offs+bppe*i+1] = quantize(ii/(double)(c->pal_entries - c->palette_reserve - 1), 256, 0, 0, 0, 0, 0);
c->mem[offs+bppe*i+0] = quantize(ii/(double)(c->pal_entries - c->palette_reserve - 1), 256, 0, 0, 0, 0, 0);
}
}
else {
// R6G7B6 palette
// Entry for a given (R,G,B) is R + G*6 + B*42
for(i=c->palette_reserve;i<c->pal_entries;i++) {
ii = i-c->palette_reserve;
if(i>=252+c->palette_reserve) continue;
r = ii%6;
g = (ii%42)/6;
b = ii/42;
c->mem[offs+bppe*i+2] = quantize(((double)r)/5.0, 256, 0, 0, 0, 0, 0);
c->mem[offs+bppe*i+1] = quantize(((double)g)/6.0, 256, 0, 0, 0, 0, 0);
c->mem[offs+bppe*i+0] = quantize(((double)b)/5.0, 256, 0, 0, 0, 0, 0);
}
}
}
else if(c->bpp==4) {
if(c->palette_reserve) {
c->mem[offs+2] = 128;
c->mem[offs+1] = 0;
c->mem[offs+0] = 255;
}
if(c->pal_gs) {
// Grayscale palette
for(i=c->palette_reserve;i<c->pal_entries;i++) {
ii = i-c->palette_reserve;
r = quantize(ii/(double)(c->pal_entries - c->palette_reserve - 1),
256, 0, 0, 0, 0, 0);
c->mem[offs+bppe*i+2] = r;
c->mem[offs+bppe*i+1] = r;
c->mem[offs+bppe*i+0] = r;
}
}
else {
for(i=c->palette_reserve;i<c->pal_entries;i++) {
ii = i-c->palette_reserve;
r = ii%2;
g = (ii%6)/2;
b = ii/6;
c->mem[offs+4*i+2] = quantize(((double)r)/1.0, 256, 0, 0, 0, 0, 0);
c->mem[offs+4*i+1] = quantize(((double)g)/2.0, 256, 0, 0, 0, 0, 0);
c->mem[offs+4*i+0] = quantize(((double)b)/1.0, 256, 0, 0, 0, 0, 0);
}
}
}
else if(c->bpp==2) {
if(c->pal_gs) {
for(i=0;i<4;i++) {
// A 4-shade grayscale palette
c->mem[offs+4*i+2] = 85*i;
c->mem[offs+4*i+1] = 85*i;
c->mem[offs+4*i+0] = 85*i;
}
}
else {
for(i=0;i<4;i++) {
r = i%2;
g = (i == 3) ? 1 : 0;
b = i/2;
c->mem[offs+4*i+2] = quantize(((double)r)/1.0, 256, 0, 0, 0, 0, 0);
c->mem[offs+4*i+1] = quantize(((double)g)/1.0, 256, 0, 0, 0, 0, 0);
c->mem[offs+4*i+0] = quantize(((double)b)/1.0, 256, 0, 0, 0, 0, 0);
}
}
}
else if(c->bpp==1) {
if(c->pal_entries==2) {
if(c->pal_wb) {
c->mem[offs+4*0+2] = 255;
c->mem[offs+4*0+1] = 255;
c->mem[offs+4*0+0] = 255;
}
else if(c->pal_bg) {
c->mem[offs+4*0+2] = 64;
c->mem[offs+4*0+1] = 64;
c->mem[offs+4*0+0] = 255;
c->mem[offs+4*1+2] = 64;
c->mem[offs+4*1+1] = 255;
c->mem[offs+4*1+0] = 64;
}
else {
c->mem[offs+4*1+2] = 255;
c->mem[offs+4*1+1] = 255;
c->mem[offs+4*1+0] = 255;
}
}
else { // assuming c->pal_p1
c->mem[offs+4*0+2] = 64;
c->mem[offs+4*0+1] = 64;
c->mem[offs+4*0+0] = 255;
}
}
else if(c->bpp>8) {
// Write a 'suggested' palette.
for(i=0;i<c->pal_entries;i++) {
if(i<=255) {
c->mem[offs+4*i+2] = (unsigned char)i;
c->mem[offs+4*i+1] = (unsigned char)i;
c->mem[offs+4*i+0] = (unsigned char)i;
}
}
}
}
static void write_ba_header(struct context *c)
{
c->mem[0]='B';
c->mem[1]='A';
set_int32(c, 2, 14+14+c->headersize);
}
static void write_fileheader(struct context *c, int offset)
{
c->mem[offset+0]='B';
c->mem[offset+1]='M';
if(c->bad_bfSize)
set_int32(c,offset+2,0x7ddddddd);
else if(c->cbsize_flag)
set_int32(c,offset+2,14+c->headersize);
else
set_int32(c,offset+2,(int)(c->mem_used - c->ba_hdr_size));
if(c->set_hotspot) {
set_int16(c, offset+6, 21);