-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBMP.pas
More file actions
534 lines (459 loc) · 14.7 KB
/
BMP.pas
File metadata and controls
534 lines (459 loc) · 14.7 KB
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
(*
Greenfish Icon Editor Pro
Copyright (c) 2012-13 B. Szalkai
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*)
unit BMP;
interface
uses
LclIntf, LclType,
SysUtils, Classes, Graphics, Math, BitmapEx,
PixelFormats;
const
SIG_BMP = $4d42;
// Reads BITMAPINFOHEADER and DIB bits
// ICOFormat should be true iff a 1-bit transparency mask is also present
// Offset specifies image offset relative to curr. stream pos., can be -1 (=default)
function dibRead(bm: TBitmap32; s: TStream; Offset: integer;
ICOFormat: boolean): boolean;
procedure dibWrite(bm: TBitmap32; s: TStream;
ICOFormat: boolean; PixelFormat: TPixelFormat32; Palette: TiePalette;
BitsOffset: PInteger);
function bmpDetect(Data: Pointer; Size: integer): boolean;
function bmpLoadFromStream(bm: TBitmap32; s: TStream): boolean;
procedure bmpSaveToStream(bm: TBitmap32; s: TStream);
function bmpLoadFromFile(bm: TBitmap32; const fn: string): boolean;
procedure bmpSaveToFile(bm: TBitmap32; const fn: string);
implementation
uses
bmExUtils, StreamEx, dlgDebug;
function dibRead;
var
// buffered I/O -> faster
sr: TBufferedReader;
HeaderSize, BytesRead, x, y, i, Padding, nBits, PalCount, PalEntrySize, PixPerByte, PixelMask: integer;
bmih: BITMAPINFOHEADER;
bmch: BITMAPCOREHEADER;
Palette: array[0..255] of TColor;
c: TColor;
Color32: TColor32;
pc32: PColor32;
Buffer: byte;
dummy: Cardinal;
HasNonZeroAlpha, HasInvalidPixel: boolean;
begin
Result := False;
try
// initialize reader
// (we will use no more special stream instructions)
sr := TBufferedReader.Create(s);
try
// peek the header size
sr.Read(@HeaderSize, 4);
sr.Position := sr.Position - 4;
if HeaderSize > 1000 then
Exit;
BytesRead := 0;
if HeaderSize = SizeOf(BITMAPCOREHEADER) then
begin
sr.Read(@bmch, SizeOf(bmch));
if (bmch.bcPlanes <> 1) or (bmch.bcBitCount > 64) then
Exit;
bm.Resize(bmch.bcWidth, IfThen(ICOFormat, bmch.bcHeight div 2, bmch.bcHeight));
nBits := bmch.bcBitCount;
PalCount := 1 shl nBits;
PalEntrySize := 3;
end else
begin
// read BITMAPINFOHEADER
if HeaderSize < SizeOf(bmih) then
Exit;
sr.Read(@bmih, SizeOf(bmih));
sr.Position := sr.Position + HeaderSize-SizeOf(bmih);
if (bmih.biWidth < 0) or (bmih.biPlanes <> 1) or (bmih.biBitCount > 64) then
Exit;
bm.Resize(bmih.biWidth,
IfThen(ICOFormat, bmih.biHeight div 2, bmih.biHeight));
nBits := bmih.biBitCount;
PalCount := bmih.biClrUsed;
PalEntrySize := 4;
end;
inc(BytesRead, HeaderSize);
{$IFDEF GFIEDEBUG}
Log(Format('Loading DIB: %dx%d @ %d-bit, HeaderSize=%d, PalCount=%d, PalEntrySize=%d',
[bm.Width, bm.Height, nBits, HeaderSize, PalCount, PalEntrySize]));
{$ENDIF}
// read bitmap
if nBits = 32 then
begin
if Offset >= 0 then sr.Read(nil, Offset - BytesRead); // skip bytes
// No padding is required
for y := bm.Height - 1 downto 0 do
for x := 0 to bm.Width - 1 do
begin
sr.Read(@Color32, 4);
bm.PixelAddr(x, y)^ := FlipColor32(Color32);
end;
// No AND mask
end else
begin
// --------- read XOR mask
if nBits = 24 then
// RGB
begin
if Offset >= 0 then sr.Read(nil, Offset - BytesRead); // skip bytes
Padding := PadTo4(bm.Width * 3);
for y := bm.Height - 1 downto 0 do
begin
for x := 0 to bm.Width - 1 do
begin
sr.Read(@Color32, 3);
bm.PixelAddr(x, y)^ := FlipColor32(Color32) or cl32Opaque;
end;
sr.Read(@dummy, Padding);
end;
end else
// indexed
begin
// read palette - RGBQuads
if PalCount = 0 then PalCount := 1 shl nBits;
for i := 0 to PalCount - 1 do
begin
sr.Read(@c, PalEntrySize);
c := c and clWhite;
Palette[i] := FlipColor(c);
end;
inc(BytesRead, PalEntrySize*PalCount);
if Offset >= 0 then sr.Read(nil, Offset - BytesRead); // skip bytes
// read DIB bits
PixPerByte := 8 div nBits;
PixelMask := (1 shl nBits) - 1;
Padding := PadTo4((bm.Width*nBits + 7) div 8);
for y := bm.Height - 1 downto 0 do
begin
for i := 0 to (bm.Width*nBits + 7) div 8 - 1 do
begin
sr.Read(@Buffer, 1);
for x := (i+1)*PixPerByte - 1 downto i*PixPerByte do
begin
if x < bm.Width then bm.PixelAddr(x, y)^ :=
Cardinal(Palette[Buffer and PixelMask]) or cl32Opaque;
Buffer := Buffer shr nBits;
end;
end;
sr.Read(@dummy, Padding);
end; // for y
end; // indexed
// --------- read AND mask
if ICOFormat then
begin
// ALIGN = 4
Padding := PadTo4((bm.Width + 7) div 8);
// read as a monochrome (1-bit) bitmap
for y := bm.Height - 1 downto 0 do
begin
for i := 0 to (bm.Width + 7) div 8 - 1 do
begin
sr.Read(@Buffer, 1);
for x := i*8 + 7 downto i*8 do
begin
if x < bm.Width then
begin
if Buffer and 1 <> 0 then
begin
PByteArray(bm.PixelAddr(x, y))[3] := 0;
color32 := bm.PixelAddr(x, y)^;
end;
end;
Buffer := Buffer shr 1;
end;
end;
sr.Read(@dummy, Padding);
end; // for y
end; // if ICOFormat
end; // not 32-bit
// Check AND mask
// Sometimes the AND mask is written as if the whole image were transparent
// but in fact it isn't, and instead the first color in the palette is transparent.
// In addition, sometimes 24-bit bitmaps are written as 32-bit bitmaps with alpha=0.
HasInvalidPixel := False;
HasNonZeroAlpha := False;
pc32 := bm.Data;
for i := 1 to bm.Width*bm.Height do
begin
color32 := pc32^;
if color32 and cl32Opaque = 0 then
begin
// inverted pixels are invalid if no AND mask is present
if (color32 <> cl32Transparent) and
((color32 <> cl32Inverted) or not ICOFormat) then
HasInvalidPixel := True;
end else
HasNonZeroAlpha:=true;
if HasInvalidPixel and HasNonZeroAlpha then
break;
inc(pc32);
end;
if HasInvalidPixel then
begin
pc32 := bm.Data;
if HasNonZeroAlpha then
begin
// replace all 0-alpha pixels with cl32Transparent
for i := 1 to bm.Width*bm.Height do
begin
if pc32^ and cl32Opaque = 0 then
pc32^ := cl32Transparent;
inc(pc32);
end;
end else
begin
// all alpha zero, but has invalid pixel -> probably alpha channel is not needed
for i := 1 to bm.Width*bm.Height do
begin
pc32^ := pc32^ or cl32Opaque;
inc(pc32);
end;
end;
end;
finally
sr.Free;
end;
// success
Result := True;
except
end;
end;
procedure dibWrite;
var
// buffered I/O -> faster
sw: TBufferedWriter;
i, x, y, nBits, PixPerByte, Padding, Index: integer;
p: PColor32;
Color32: TColor32;
c: TColor;
Buffer: byte;
// for writing the padding bytes
zero: Cardinal;
bmih: BITMAPINFOHEADER;
begin
zero := 0;
nBits := pf32ToBitCount[PixelFormat];
sw := TBufferedWriter.Create(s);
try
// write the bitmap info header
FillChar(bmih, SizeOf(bmih), 0);
with bmih do
begin
biSize := SizeOf(bmih);
biWidth := bm.Width;
biHeight := bm.Height;
if ICOFormat then biHeight := biHeight * 2;
biPlanes := 1;
biBitCount := nBits;
biCompression := BI_RGB;
end;
sw.Write(@bmih, SizeOf(bmih));
// update bits offset
if BitsOffset <> nil then
begin
// header
BitsOffset^ := SizeOf(bmih);
// palette
if PixelFormat < pf32_24bit then inc(BitsOffset^, 1 shl (2 + nBits));
end;
// write bitmap
// --------- write XOR mask
case PixelFormat of
pf32_32bit: begin
// No padding is required, bytes/row is always divisible by 4
for y := bm.Height - 1 downto 0 do
for x := 0 to bm.Width - 1 do
begin
Color32 := FlipColor32(bm.PixelAddr(x, y)^);
// prevent inverted color from appearing
if Color32 and cl32Opaque = 0 then Color32 := cl32Transparent;
sw.Write(@Color32, 4);
end;
end; // 32-bit
pf32_24bit: begin
Padding := PadTo4(bm.Width * 3);
for y := bm.Height - 1 downto 0 do
begin
for x := 0 to bm.Width - 1 do
begin
p := bm.PixelAddr(x, y);
// there may be 0-alpha non-transparent/inverted pixels
if (PByteArray(p)[3] = 0) and (p^ <> cl32Inverted) then
c := clBlack else c := FlipColor(p^ and $ffffff);
sw.Write(@c, 3);
end; // for x
sw.Write(@zero, Padding);
end; // for y
end;
else begin // indexed
// write palette - RGBQuads
// We always have to write 1 shl nBits entries, because many
// programs do not take biClrUsed into account
// DELPHI BUG: 1 shl nBits cannot be written, internal error...
for i := 0 to Round(Exp(Ln(2) * nBits)) - 1 do
begin
if i < Palette.Count then
c := FlipColor(Palette[i]) else
c := 0;
sw.Write(@c, 4);
end;
// write DIB
PixPerByte := 8 div nBits;
Padding := PadTo4((bm.Width*nBits + 7) div 8);
for y := bm.Height - 1 downto 0 do
begin
for i := 0 to (bm.Width*nBits + 7) div 8 - 1 do
begin
Buffer := 0;
for x := i*PixPerByte to (i+1)*PixPerByte - 1 do
begin
Buffer := Buffer shl nBits;
if x >= bm.Width then Continue;
p := bm.PixelAddr(x, y);
// determine color
if (PByteArray(p)[3] = 0) and (p^ <> cl32Inverted) then
c := clBlack else c := p^ and $ffffff;
// determine index
Index := Palette.IndexOf(c);
if Index = -1 then Index := 0;
// write data
Buffer := Buffer or Index;
end; // for x
sw.Write(@Buffer, 1);
end; // for i
sw.Write(@zero, Padding);
end; // for y
end; // indexed
end; // case
// --------- write AND mask
if ICOFormat then
begin
// pad to 4 bytes
Padding := PadTo4((bm.Width + 7) div 8);
// write as a monochrome (1-bit) bitmap
for y := bm.Height - 1 downto 0 do
begin
for i := 0 to (bm.Width + 7) div 8 - 1 do
begin
Buffer := 0;
for x := i*8 to i*8 + 7 do
begin
Buffer := Buffer shl 1;
if (x < bm.Width) and
(PByteArray(bm.PixelAddr(x, y))[3] = 0) then
// transparent or inverted -> 1
Buffer := Buffer or 1;
end;
sw.Write(@Buffer, 1);
end; // for i
sw.Write(@zero, Padding);
end; // for y
end; // if ICOFormat
finally
sw.Free;
end;
end;
function bmpDetect;
begin
Result := (Size >= 2) and (PWord(Data)^ = SIG_BMP);
end;
function bmpLoadFromStream;
var
bmfh: BITMAPFILEHEADER;
begin
Result := False;
try
// read file header
s.ReadBuffer(bmfh, SizeOf(bmfh));
// check
if bmfh.bfType <> SIG_BMP then Exit;
// read dib bits
Result := dibRead(bm, s, bmfh.bfOffBits - SizeOf(bmfh), False);
except
end;
end;
function GetBitmapSize(bm: TBitmap32; pf: TPixelFormat32): integer;
begin
// bitmap data
Result := (bm.Width*bm.Height*pf32ToBitCount[pf] + 7) div 8;
// palette
if pf < pf32_24bit then inc(Result, 4 shl pf32ToBitCount[pf]);
end;
procedure bmpSaveToStream;
var
oldPos, newPos, BitsOffset: integer;
bmfh: BITMAPFILEHEADER;
pf: TPixelFormat32;
Palette: TiePalette;
begin
oldPos := s.Position;
// leave place for file header
s.Seek(SizeOf(bmfh), soFromCurrent);
Palette := nil;
try
// optimize pixel format
if bm.HasTransparency then pf := pf32_32bit else
begin
Palette := TiePalette.Create;
pf := GetPixelFormat32(bm, nil, nil, nil, Palette);
// sometimes bitmaps get smaller if they use no palette!
if GetBitmapSize(bm, pf) > GetBitmapSize(bm, pf32_24bit) then
pf := pf32_24bit;
end;
// write dib bits
dibWrite(bm, s, False, pf, Palette, @BitsOffset);
finally
if Palette <> nil then Palette.Free;
end;
// write file header
newPos := s.Position;
s.Position := oldPos;
FillChar(bmfh, SizeOf(bmfh), 0);
bmfh.bfType := SIG_BMP;
bmfh.bfSize := newPos - oldPos;
bmfh.bfOffBits := SizeOf(bmfh) + BitsOffset;
s.WriteBuffer(bmfh, SizeOf(bmfh));
s.Position := newPos;
end;
function bmpLoadFromFile;
var
s: TStream;
begin
try
s := TFileStream.Create(fn, fmOpenRead);
try
Result := bmpLoadFromStream(bm, s);
finally
s.Free;
end;
except
Result := False;
end;
end;
procedure bmpSaveToFile;
var
s: TStream;
begin
s := TFileStream.Create(fn, fmCreate);
try
bmpSaveToStream(bm, s);
finally
s.Free;
end;
end;
end.