Source file bmp.ml
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
open Images
open Util
type bmp = {
bmpFileHeader : bitmapfileheader;
bmpInfoHeader : bitmapinfoheader;
bmpRgbQuad : rgb array;
bmpBytes : bytes;
}
and bicompression =
| BI_RGB
| BI_RLE8
| BI_RLE4
and bibitcount =
| Monochrome
| Color16
| Color256
| ColorRGB
| ColorRGBA
let bytes_read = ref 0
let read_byte ic = incr bytes_read; input_byte ic
let skip_byte ic = incr bytes_read; ignore (input_byte ic)
let read_word ic =
let b0 = read_byte ic in
let b1 = read_byte ic in
(b1 lsl 8) + b0
let read_dword ic =
let b0 = read_byte ic in
let b1 = read_byte ic in
let b2 = read_byte ic in
let b3 = read_byte ic in
(b3 lsl 24) + (b2 lsl 16) + (b1 lsl 8) + b0
let read_bit_count ic =
match read_word ic with
| 1 -> Monochrome | 4 -> Color16 | 8 -> Color256 | 24 -> ColorRGB | 32 -> ColorRGBA
| n -> failwith ("invalid colors number : " ^ string_of_int n)
let read_compression ic =
match read_dword ic with
| 0 -> BI_RGB | 1 -> BI_RLE8 | 2 -> BI_RLE4
| n -> failwith ("invalid compression mode : " ^ string_of_int n)
let load_rgbquad ic =
let b = read_byte ic in
let g = read_byte ic in
let r = read_byte ic in
let _u = read_byte ic in
{ b = b; g = g; r = r; }
let ic =
let bfType = read_word ic in
if bfType <> 19778 then failwith "Invalid file tag";
let bfSize = read_dword ic in
let bfReserved1 = read_word ic in
let bfReserved2 = read_word ic in
let bfOffBits = read_dword ic in
{ bfType; bfSize; bfReserved1; bfReserved2; bfOffBits; }
let ic =
try
let biSize = read_dword ic in
let biWidth = read_dword ic in
let biHeight = read_dword ic in
let biPlanes = read_word ic in
let biBitCount = read_bit_count ic in
let biCompression = read_compression ic in
let biSizeImage = read_dword ic in
let biXPelsPerMeter = read_dword ic in
let biYPelsPerMeter = read_dword ic in
let biClrUsed = read_dword ic in
let biClrImportant = read_dword ic in
{ biSize; biWidth; biHeight;
biPlanes; biBitCount;
biCompression; biSizeImage;
biXPelsPerMeter; biYPelsPerMeter;
biClrUsed; biClrImportant;
}
with
| (Failure s as e) ->
prerr_endline s;
raise e
let load_colors bfh _bih ic =
let cmaplength =
(bfh.bfOffBits - 54) / 4 in
Array.init cmaplength (fun _i -> load_rgbquad ic)
let load_image8data bih ic =
let bitmap = Bytes.create (bih.biWidth * bih.biHeight) in
match bih.biCompression with
| BI_RGB ->
let pad = ((bih.biWidth + 3) / 4) * 4 in
for i = bih.biHeight - 1 downto 0 do
let bitmapindex = ref (i * bih.biWidth) in
for j = 0 to pad - 1 do
let c = Char.chr (read_byte ic) in
if j < bih.biWidth then bitmap << !bitmapindex & c;
incr bitmapindex
done
done;
bitmap
| BI_RLE8 ->
let x = ref 0 in
let y = ref 0 in
let bitmapindex = ref (!x + (bih.biHeight - !y - 1) * bih.biWidth) in
while !y < bih.biHeight do
match read_byte ic with
| 0 ->
begin
match read_byte ic with
| 0 ->
x := 0;
incr y;
bitmapindex := !x + (bih.biHeight - !y - 1) * bih.biWidth
| 1 ->
y := bih.biHeight
| 2 ->
let c1 = read_byte ic in
x := !x + c1;
let c2 = read_byte ic in
y := !y + c2;
bitmapindex := !x + (bih.biHeight - !y - 1) * bih.biWidth
| c ->
for _i = 0 to c - 1 do
let c1 = read_byte ic in
bitmap << !bitmapindex & Char.chr c1;
incr x;
incr bitmapindex
done;
if c land 1 <> 0 then skip_byte ic
end
| c ->
let c1 = read_byte ic in
for _i = 0 to c - 1 do
bitmap << !bitmapindex & Char.chr c1;
incr x;
incr bitmapindex
done
done;
bitmap
| BI_RLE4 ->
failwith ("Invalid compression mode : BI_RLE4")
let load_image1data bih ic =
let bitmap = Bytes.create (bih.biWidth * bih.biHeight) in
let c = ref 0 in
let pad = ((bih.biWidth + 31) / 32) * 32 in
for i = bih.biHeight - 1 downto 0 do
let bitmapindex = ref (i * bih.biWidth) in
let bnum = ref 0 in
for j = 0 to pad - 1 do
if !bnum land 7 = 0 then
begin
c := read_byte ic;
bnum := 0;
end;
if j < bih.biWidth then
begin
bitmap << !bitmapindex & if !c land 0x80 <> 0 then '\001' else '\000';
incr bitmapindex;
c := !c lsl 1;
end;
incr bnum
done
done;
bitmap
let load_image4data bih ic =
let bitmap = Bytes.create (bih.biWidth * bih.biHeight) in
match bih.biCompression with
| BI_RGB ->
let pad = ((bih.biWidth + 7) / 8) * 8 in
let c = ref 0 in
for i = bih.biHeight - 1 downto 0 do
let bitmapindex = ref (i * bih.biWidth) in
let nyblenum = ref 0 in
for j = 0 to pad -1 do
if !nyblenum land 1 = 0 then
begin
c := read_byte ic;
nyblenum := 0
end;
if j < bih.biWidth then
begin
bitmap << !bitmapindex & Char.chr ((!c land 0xf0) lsr 4);
incr bitmapindex;
c := !c lsl 4
end;
incr nyblenum
done
done;
bitmap
| BI_RLE4 ->
let x = ref 0 in
let y = ref 0 in
let bitmapindex = ref (!x + (bih.biHeight - !y - 1) * bih.biWidth) in
let c1 = ref 0 in
while !y < bih.biHeight do
match read_byte ic with
| 0 ->
begin
match read_byte ic with
| 0 ->
x := 0;
incr y;
bitmapindex := !x + (bih.biHeight - !y - 1) * bih.biWidth
| 1 ->
y := bih.biHeight
| 2 ->
let c' = read_byte ic in
x := !x + c';
let c'' = read_byte ic in
y := !y + c'';
bitmapindex := !x + (bih.biHeight - !y - 1) * bih.biWidth
| c ->
for i = 0 to c - 1 do
if i land 1 = 0 then c1 := read_byte ic;
let c = if i land 1 <> 0 then !c1 else !c1 lsr 4 in
bitmap << !bitmapindex & Char.chr (c land 0x0F);
incr x;
incr bitmapindex
done;
if c land 3 = 1 || c land 3 = 2 then skip_byte ic
end
| c ->
let c1 = read_byte ic in
let col1 = c1 land 0x0F
and col2 = (c1 lsr 4) land 0x0F in
for i = 0 to c - 1 do
let c = if i land 1 <> 0 then col1 else col2 in
bitmap << !bitmapindex & Char.chr c;
incr x;
incr bitmapindex
done
done;
bitmap
| BI_RLE8 ->
failwith ("Invalid compression mode : BI_RLE8")
let load_image24data bih ic =
let bitmap = Bytes.create ((bih.biWidth * bih.biHeight) * 3) in
let pad = (4 - ((bih.biWidth * 3) mod 4)) land 0x03 in
let pp = ref 0 in
for i = bih.biHeight - 1 downto 0 do
pp := (i * bih.biWidth * 3);
for _j = 0 to bih.biWidth - 1 do
bitmap << !pp + 2 & Char.chr (read_byte ic);
bitmap << !pp + 1 & Char.chr (read_byte ic);
bitmap << !pp & Char.chr (read_byte ic);
pp := !pp + 3
done;
for _j = 0 to pad - 1 do skip_byte ic done;
done;
bitmap
let load_image32data bih ic =
let bitmap = Bytes.create ((bih.biWidth * bih.biHeight) * 4) in
let pp = ref 0 in
for i = bih.biHeight - 1 downto 0 do
pp := (i * bih.biWidth * 4);
for _j = 0 to bih.biWidth - 1 do
bitmap << !pp + 2 & Char.chr (read_byte ic);
bitmap << !pp + 1 & Char.chr (read_byte ic);
bitmap << !pp + 0 & Char.chr (read_byte ic);
bitmap << !pp + 3 & Char.chr (read_byte ic);
pp := !pp + 4
done;
done;
bitmap
let load_imagedata bih ic =
match bih.biBitCount with
| Monochrome -> load_image1data bih ic
| Color16 -> load_image4data bih ic
| Color256 -> load_image8data bih ic
| ColorRGB -> load_image24data bih ic
| ColorRGBA -> load_image32data bih ic
let skip_to ic n =
while !bytes_read <> n do skip_byte ic done
let fname =
let ic = open_in_bin fname in
bytes_read := 0;
try
let _bfh = load_bitmapfileheader ic in
let bih = load_bitmapinfoheader ic in
close_in ic;
{ header_width = bih.biWidth;
header_height = bih.biHeight;
header_infos = []; }
with
| _ ->
close_in ic;
raise Wrong_file_type
let read_bmp ic =
bytes_read := 0;
let bfh = load_bitmapfileheader ic in
let bih = load_bitmapinfoheader ic in
let colormap = load_colors bfh bih ic in
skip_to ic bfh.bfOffBits;
let bitmap = load_imagedata bih ic in
{ bmpFileHeader = bfh;
bmpInfoHeader = bih;
bmpRgbQuad = colormap;
bmpBytes = bitmap; }
let read_bmp_file fname =
let ic = open_in_bin fname in
let bmp = read_bmp ic in
close_in ic;
bmp
let image_of_bmp = function
{ bmpFileHeader = _bfh;
bmpInfoHeader = bih;
bmpRgbQuad = colormap;
bmpBytes = bitmap; } ->
match bih.biBitCount with
| ColorRGB ->
Rgb24 (Rgb24.create_with bih.biWidth bih.biHeight [] bitmap)
| ColorRGBA ->
Rgba32 (Rgba32.create_with bih.biWidth bih.biHeight [] bitmap)
| Monochrome | Color16 | Color256 ->
Index8
(Index8.create_with bih.biWidth bih.biHeight []
{ map = colormap; max = 256; } (-1) bitmap)
let load fname _opts = image_of_bmp (read_bmp_file fname)
let bytes_written = ref 0
let write_byte oc b = incr bytes_written; output_byte oc b
let output_word oc w =
let b0 = w land 255 in
let b1 = (w lsr 8) land 255 in
output_byte oc b0;
output_byte oc b1
let write_word oc w =
output_word oc w;
bytes_written := !bytes_written + 2
let output_dword oc dw =
let b0 = dw land 255 in
let b1 = (dw lsr 8) land 255 in
let b2 = (dw lsr 16) land 255 in
let b3 = (dw lsr 24) land 255 in
output_byte oc b0;
output_byte oc b1;
output_byte oc b2;
output_byte oc b3
let write_dword oc dw =
output_dword oc dw;
bytes_written := !bytes_written + 4
let write_bit_count oc bc =
let byte = match bc with
| Monochrome -> 1 | Color16 -> 4 | Color256 -> 8 | ColorRGB -> 24 | ColorRGBA -> 32 in
write_word oc byte
let write_compression oc c =
let dword = match c with
| BI_RGB -> 0 | BI_RLE8 -> 1 | BI_RLE4 -> 2 in
write_dword oc dword
let write_rgbquad oc rgb =
let b = rgb.b in
let g = rgb.g in
let r = rgb.r in
let u = 0 in
write_byte oc b;
write_byte oc g;
write_byte oc r;
write_byte oc u
let oc = function {
bfType = bft;
bfSize = bfs;
bfReserved1 = bfr1;
bfReserved2 = bfr2;
bfOffBits = bfob
} ->
let start_index = !bytes_written in
write_word oc bft;
let bfSize_index = !bytes_written in
write_dword oc bfs;
write_word oc bfr1;
write_word oc bfr2;
let bfOffBits_index = !bytes_written in
write_dword oc bfob;
let = !bytes_written in
start_index, bfSize_index, bfOffBits_index, end_bmpFileHeader
let oc = function {
biSize = bis;
biWidth = biw;
biHeight = bih;
biPlanes = bip;
biBitCount = bibc;
biCompression = bic;
biSizeImage = bisi;
biXPelsPerMeter = bixpm;
biYPelsPerMeter = biypm;
biClrUsed = bicu;
biClrImportant = bici
} ->
let biSize_index = !bytes_written in
write_dword oc bis;
write_dword oc biw;
write_dword oc bih;
write_word oc bip;
write_bit_count oc bibc;
write_compression oc bic;
let biSizeImage_index = !bytes_written in
write_dword oc bisi;
write_dword oc bixpm;
write_dword oc biypm;
write_dword oc bicu;
write_dword oc bici;
let = !bytes_written in
biSize_index, biSizeImage_index, end_bmpInfoHeader
let write_colors oc color_map =
if Array.length color_map = 0 then write_byte oc 0
else Array.iter (write_rgbquad oc) color_map
let write_end_of_scan_line oc = write_byte oc 0; write_byte oc 0
let write_end_of_bitmap oc = write_byte oc 0; write_byte oc 1
let write_pad oc n = for _i = 0 to n - 1 do write_byte oc 0 done
let rec write_rle_code oc n c =
if n <= 255 then begin
write_byte oc n;
write_byte oc c
end else begin
write_rle_code oc 255 c;
write_rle_code oc (n - 255) c end
let write_rle oc n char = write_rle_code oc n (Char.code char)
let write_rle4 oc n char =
let code = Char.code char in
write_rle_code oc n (code lsl 4 + code)
let pad_bytes n = (4 - (n mod 4)) land 0x03
let write_image1data bmp oc =
let bih = bmp.bmpInfoHeader in
if bih.biCompression <> BI_RGB
then failwith "invalid compression for a monochrome bitmap" else
let start_bitmap_index = !bytes_written in
let bitmap = bmp.bmpBytes in
let width = bih.biWidth in
let height = bih.biHeight in
let = pad_bytes ((width + 7) / 8) in
for i = height - 1 downto 0 do
let start = i * width in
let lim = (i + 1) * width - 1 in
let rec write_line x count accu =
if count = 8 then begin
write_byte oc accu;
if x <= lim then write_line x 0 0 end else
let chunk = (bitmap @% x) lsl (7 - count) in
let new_accu = chunk + accu in
if x = lim then write_byte oc new_accu
else write_line (x + 1) (count + 1) new_accu in
write_line start 0 0;
write_pad oc extra_padding_bytes;
done;
let end_bitmap_index = !bytes_written in
start_bitmap_index, end_bitmap_index
let write_image24data bmp oc =
let bih = bmp.bmpInfoHeader in
if bih.biCompression <> BI_RGB
then failwith "invalid compression for a rgb bitmap" else
let start_bitmap_index = !bytes_written in
let bitmap = bmp.bmpBytes in
let width = bih.biWidth in
let height = bih.biHeight in
let = pad_bytes (width * 3) in
for i = height - 1 downto 0 do
let start = i * width * 3 in
let lim = (i + 1) * width * 3 - 1 in
let rec write_line x =
write_byte oc (bitmap @% x + 2);
write_byte oc (bitmap @% x + 1);
write_byte oc (bitmap @% x );
let new_x = x + 3 in
if new_x < lim then write_line new_x in
write_line start;
write_pad oc extra_padding_bytes;
done;
let end_bitmap_index = !bytes_written in
start_bitmap_index, end_bitmap_index
let write_image32data bmp oc =
let bih = bmp.bmpInfoHeader in
if bih.biCompression <> BI_RGB
then failwith "invalid compression for a rgba bitmap" else
let start_bitmap_index = !bytes_written in
let bitmap = bmp.bmpBytes in
let width = bih.biWidth in
let height = bih.biHeight in
for i = height - 1 downto 0 do
let start = i * width * 3 in
let lim = (i + 1) * width * 4 - 1 in
let rec write_line x =
write_byte oc (bitmap @% x + 3);
write_byte oc (bitmap @% x + 2);
write_byte oc (bitmap @% x + 1);
write_byte oc (bitmap @% x );
let new_x = x + 4 in
if new_x < lim then write_line new_x in
write_line start;
done;
let end_bitmap_index = !bytes_written in
start_bitmap_index, end_bitmap_index
let write_image4data bmp oc =
let bih = bmp.bmpInfoHeader in
let start_bitmap_index = !bytes_written in
let bitmap = bmp.bmpBytes in
let width = bih.biWidth in
let height = bih.biHeight in
match bih.biCompression with
| BI_RGB ->
let = pad_bytes ((width + 1) / 2) in
for i = height - 1 downto 0 do
let start = i * width in
let lim = (i + 1) * width - 1 in
let rec write_line x count accu =
if count = 2 then begin
write_byte oc accu;
if x <= lim then write_line x 0 0 end else
let chunk = (bitmap @% x) lsl (4 - count) in
let new_accu = chunk + accu in
if x = lim then write_byte oc new_accu
else write_line (x + 1) (count + 1) new_accu in
write_line start 0 0;
write_pad oc extra_padding_bytes;
done;
let end_bitmap_index = !bytes_written in
start_bitmap_index, end_bitmap_index
| BI_RLE4 ->
for i = height - 1 downto 0 do
let start = i * width in
let lim = (i + 1) * width - 1 in
let rec write_line x count pred =
let cur = Bytes.get bitmap x in
if cur = pred then
if x = lim then write_rle4 oc (count + 1) pred
else write_line (x + 1) (count + 1) pred
else begin
write_rle4 oc count pred;
if x = lim then write_rle4 oc 1 cur
else write_line (x + 1) 1 cur
end in
write_line start 0 (Bytes.get bitmap start);
write_end_of_scan_line oc;
done;
write_end_of_bitmap oc;
let end_bitmap_index = !bytes_written in
start_bitmap_index, end_bitmap_index
| BI_RLE8 ->
failwith ("Invalid compression mode : BI_RLE8")
let write_image8data bmp oc =
let bih = bmp.bmpInfoHeader in
let start_bitmap_index = !bytes_written in
let bitmap = bmp.bmpBytes in
let width = bih.biWidth in
let height = bih.biHeight in
match bih.biCompression with
| BI_RGB ->
let = pad_bytes width in
for i = height - 1 downto 0 do
let start = i * width in
let lim = (i + 1) * width - 1 in
let rec write_line x =
write_byte oc (bitmap @% x);
if x < lim then write_line (x + 1) in
write_line start;
write_pad oc extra_padding_bytes;
done;
let end_bitmap_index = !bytes_written in
start_bitmap_index, end_bitmap_index
| BI_RLE8 ->
for i = height - 1 downto 0 do
let start = i * width in
let lim = (i + 1) * width - 1 in
let rec write_line x count pred =
let cur = Bytes.get bitmap x in
if cur = pred then
if x = lim then write_rle oc (count + 1) pred
else write_line (x + 1) (count + 1) pred
else begin
write_rle oc count pred;
if x = lim then write_rle oc 1 cur
else write_line (x + 1) 1 cur
end in
write_line start 0 (Bytes.get bitmap start);
write_end_of_scan_line oc;
done;
write_end_of_bitmap oc;
let end_bitmap_index = !bytes_written in
start_bitmap_index, end_bitmap_index
| BI_RLE4 ->
failwith ("Invalid compression mode : BI_RLE8")
let write_image_data oc bmp =
let bih = bmp.bmpInfoHeader in
match bih.biBitCount with
| Monochrome -> write_image1data bmp oc
| Color16 -> write_image4data bmp oc
| Color256 -> write_image8data bmp oc
| ColorRGB -> write_image24data bmp oc
| ColorRGBA -> write_image32data bmp oc
let bmp_of_image img =
match img with
| Rgb24 bitmap ->
let biW = bitmap.Rgb24.width
and biH = bitmap.Rgb24.height
and data = Rgb24.dump bitmap in
let bfh = {
bfType = 19778 ;
bfSize = -1 ;
bfReserved1 = 0;
bfReserved2 = 0;
bfOffBits = -1
} in
let bih =
{
biSize = -1;
biWidth = biW; biHeight = biH;
biPlanes = 1;
biBitCount = ColorRGB;
biCompression = BI_RGB;
biSizeImage = -1 ;
biXPelsPerMeter = 600; biYPelsPerMeter = 600;
biClrUsed = 0;
biClrImportant = 0 } in
{ bmpFileHeader = bfh;
bmpInfoHeader = bih;
bmpRgbQuad = [||];
bmpBytes = data }
| Rgba32 bitmap ->
let biW = bitmap.Rgba32.width
and biH = bitmap.Rgba32.height
and data = Rgba32.dump bitmap in
let bfh = {
bfType = 19778 ;
bfSize = -1 ;
bfReserved1 = 0;
bfReserved2 = 0;
bfOffBits = -1
} in
let bih =
{
biSize = -1;
biWidth = biW; biHeight = biH;
biPlanes = 1;
biBitCount = ColorRGBA;
biCompression = BI_RGB;
biSizeImage = -1 ;
biXPelsPerMeter = 600; biYPelsPerMeter = 600;
biClrUsed = 0;
biClrImportant = 0 } in
{ bmpFileHeader = bfh;
bmpInfoHeader = bih;
bmpRgbQuad = [||];
bmpBytes = data }
| Index8 bitmap ->
let colormap = bitmap.Index8.colormap.map
and biW = bitmap.Index8.width
and biH = bitmap.Index8.height
and data = Index8.dump bitmap in
let bfh = {
bfType = 19778 ;
bfSize = -1 ;
bfReserved1 = 0;
bfReserved2 = 0;
bfOffBits = -1
} in
let biBitCount,biClrUsed,biCompression,biClrImportant =
let col_map_len = Array.length colormap in
match col_map_len with
| n when n <= 2 -> Monochrome, 2, BI_RGB, 2
| 16 -> Color16, col_map_len, BI_RGB, 0
| n when n <= 16 -> Color16, col_map_len, BI_RLE4, 0
| 256 -> Color256, col_map_len, BI_RGB, 0
| n when n <= 256 -> Color256, col_map_len, BI_RLE8, 0
| _n -> failwith "Too many colors for a bitmap with 8 bits per pixel" in
let bih =
{ biSize = -1; biWidth = biW; biHeight = biH;
biPlanes = 1; biBitCount = biBitCount;
biCompression = biCompression; biSizeImage = -1;
biXPelsPerMeter = 600; biYPelsPerMeter = 600;
biClrUsed = biClrUsed; biClrImportant = biClrImportant; } in
{ bmpFileHeader = bfh;
bmpInfoHeader = bih;
bmpRgbQuad = colormap;
bmpBytes = data; }
| _ -> raise Wrong_image_type
let write_bmp oc = function
{ bmpFileHeader = ;
bmpInfoHeader = ;
bmpRgbQuad = colormap;
bmpBytes = _bitmap } as bmp ->
bytes_written := 0;
let start_index, bfSize_index, bfOffBits_index, =
write_bmpFileHeader oc bmpFileHeader in
let = end_bmpFileHeader in
let biSize_index, biSizeImage_index, =
write_bmpInfoHeader oc bmpInfoHeader in
write_colors oc colormap;
let start_bitmap_index, end_bitmap_index =
write_image_data oc bmp in
let bfSize =
!bytes_written - start_index in
seek_out oc bfSize_index;
output_dword oc bfSize;
let bfOffBits =
start_bitmap_index - start_index in
seek_out oc bfOffBits_index;
output_dword oc bfOffBits;
let biSize =
end_bmpInfoHeader - start_bmpInfoHeader in
seek_out oc biSize_index;
output_dword oc biSize;
let biSizeImage =
end_bitmap_index - start_bitmap_index in
seek_out oc biSizeImage_index;
output_dword oc biSizeImage
let write_bmp_file fname bmp =
let oc = open_out_bin fname in
write_bmp oc bmp;
close_out oc
let save fname _opts img = write_bmp_file fname (bmp_of_image img)
let () = add_methods Bmp
{ check_header = check_header;
load = Some load;
save = Some save;
load_sequence = None;
save_sequence = None;
}
let save_bmp = write_bmp_file
and load_bmp = read_bmp_file