forked from graphics32/GR32PNG
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGR32_Png.pas
2137 lines (1837 loc) · 62.1 KB
/
GR32_Png.pas
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
unit GR32_PNG;
(* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1 or LGPL 2.1 with linking exception
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Alternatively, the contents of this file may be used under the terms of the
* Free Pascal modified version of the GNU Lesser General Public License
* Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
* of this license are applicable instead of those above.
* Please see the file LICENSE.txt for additional information concerning this
* license.
*
* The Original Code is Graphics32
*
* The Initial Developer of the Original Code is
* Christian-W. Budde
*
* Portions created by the Initial Developer are Copyright (C) 2000-2009
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** *)
interface
{$I GR32.inc}
{$I GR32_PngCompilerSwitches.inc}
uses
Classes, Graphics, SysUtils, GR32, GR32_PortableNetworkGraphic;
type
TProgressEvent = procedure(Sender: TObject; Percent: Single) of object;
TPortableNetworkGraphic32 = class(TPortableNetworkGraphic)
private
FProgressEvent: TProgressEvent;
procedure AssignPropertiesFromBitmap32(Bitmap32: TCustomBitmap32);
function GetBackgroundColor: TColor32;
protected
function GR32Scanline(Bitmap: TObject; Y: Integer): Pointer; virtual;
function GR32ScanlineProgress(Bitmap: TObject; Y: Integer): Pointer; virtual;
public
procedure AssignTo(Dest: TPersistent); override;
procedure Assign(Source: TPersistent); override;
procedure DrawToBitmap32(Bitmap32: TCustomBitmap32); virtual;
property Background: TColor32 read GetBackgroundColor;
property Progress: TProgressEvent read FProgressEvent write FProgressEvent;
end;
function IsValidPNG(const Filename: string): Boolean; {$IFDEF USEINLINING} inline; {$ENDIF}
procedure LoadBitmap32FromPNG(Bitmap: TBitmap32; const Filename: string); overload; {$IFDEF USEINLINING} inline; {$ENDIF}
procedure LoadBitmap32FromPNG(Bitmap: TBitmap32; Stream: TStream); overload; {$IFDEF USEINLINING} inline; {$ENDIF}
procedure SaveBitmap32ToPNG(Bitmap: TBitmap32; FileName: string); overload; {$IFDEF USEINLINING} inline; {$ENDIF}
procedure SaveBitmap32ToPNG(Bitmap: TBitmap32; Stream: TStream); overload; {$IFDEF USEINLINING} inline; {$ENDIF}
implementation
resourcestring
RCStrUnsupportedFormat = 'Unsupported Format';
RCStrDataIncomplete = 'Data not complete';
type
TCustomPngNonInterlacedDecoder = class(TCustomPngDecoder)
protected
FBytesPerRow: Integer;
FRowByteSize: Integer;
procedure TransferData(Source: Pointer; Destination: PColor32); virtual; abstract;
public
constructor Create(Stream: TStream; Header: TChunkPngImageHeader;
Gamma: TChunkPngGamma = nil; Palette: TChunkPngPalette = nil;
Transparency: TCustomPngTransparency = nil); override;
destructor Destroy; override;
procedure DecodeToScanline(Bitmap: TObject; ScanLineCallback: TScanLineCallback); override;
end;
TPngNonInterlacedGrayscale1bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedGrayscale2bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedGrayscale4bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedGrayscale8bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedGrayscale16bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedTrueColor8bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedTrueColor16bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedPaletteDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedPalette8bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedGrayscaleAlpha8bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedGrayscaleAlpha16bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedTrueColorAlpha8bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TPngNonInterlacedTrueColorAlpha16bitDecoder = class(TCustomPngNonInterlacedDecoder)
protected
procedure TransferData(Source: Pointer; Destination: PColor32); override;
end;
TCustomPngAdam7Decoder = class(TCustomPngDecoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); virtual; abstract;
public
constructor Create(Stream: TStream; Header: TChunkPngImageHeader;
Gamma: TChunkPngGamma = nil; Palette: TChunkPngPalette = nil;
Transparency: TCustomPngTransparency = nil); override;
destructor Destroy; override;
procedure DecodeToScanline(Bitmap: TObject; ScanLineCallback: TScanLineCallback); override;
end;
TPngAdam7Grayscale1bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7Grayscale2bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7Grayscale4bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7Grayscale8bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7Grayscale16bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7TrueColor8bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7TrueColor16bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7Palette1bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7Palette2bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7Palette4bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7Palette8bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7GrayscaleAlpha8bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7GrayscaleAlpha16bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7TrueColorAlpha8bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TPngAdam7TrueColorAlpha16bitDecoder = class(TCustomPngAdam7Decoder)
protected
procedure TransferData(const Pass: Byte; Source: Pointer; Destination: PColor32); override;
end;
TCustomPngNonInterlacedEncoder = class(TCustomPngEncoder)
protected
FBytesPerRow: Integer;
FRowByteSize: Integer;
function ColorInPalette(Color: TColor32): Integer; virtual;
procedure TransferData(Source: PColor32; Destination: Pointer); virtual; abstract;
public
constructor Create(Stream: TStream; Header: TChunkPngImageHeader;
Gamma: TChunkPngGamma = nil; Palette: TChunkPngPalette = nil;
Transparency: TCustomPngTransparency = nil); override;
destructor Destroy; override;
procedure EncodeFromScanline(Bitmap: TObject; ScanLineCallback: TScanLineCallback); override;
end;
TPngNonInterlacedGrayscale1bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedGrayscale2bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedGrayscale4bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedGrayscale8bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedTrueColor8bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedPalette1bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedPalette2bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedPalette4bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedPalette8bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedGrayscaleAlpha8bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPngNonInterlacedTrueColorAlpha8bitEncoder = class(TCustomPngNonInterlacedEncoder)
protected
procedure TransferData(Source: PColor32; Destination: Pointer); override;
end;
TPalette24 = array of TRGB24;
function IsValidPNG(const Filename: string): Boolean;
begin
try
with TPortableNetworkGraphic32.Create do
try
LoadFromFile(Filename);
Result := True;
finally
Free;
end;
except
Result := False;
end;
end;
procedure LoadBitmap32FromPNG(Bitmap: TBitmap32; const Filename: string);
begin
with TPortableNetworkGraphic32.Create do
try
LoadFromFile(Filename);
AssignTo(Bitmap);
finally
Free;
end;
end;
procedure LoadBitmap32FromPNG(Bitmap: TBitmap32; Stream: TStream);
begin
with TPortableNetworkGraphic32.Create do
try
LoadFromStream(Stream);
AssignTo(Bitmap);
finally
Free;
end;
end;
procedure SaveBitmap32ToPNG(Bitmap: TBitmap32; FileName: string);
begin
with TPortableNetworkGraphic32.Create do
try
Assign(Bitmap);
SaveToFile(Filename);
finally
Free;
end;
end;
procedure SaveBitmap32ToPNG(Bitmap: TBitmap32; Stream: TStream);
begin
with TPortableNetworkGraphic32.Create do
try
Assign(Bitmap);
SaveToStream(Stream);
finally
Free;
end;
end;
{ TPortableNetworkGraphic32 }
function TPortableNetworkGraphic32.GetBackgroundColor: TColor32;
var
ResultColor32: TColor32Entry absolute Result;
begin
if Assigned(FBackgroundChunk) then
begin
if FBackgroundChunk.Background is TPngBackgroundColorFormat04 then
with TPngBackgroundColorFormat04(FBackgroundChunk.Background) do
begin
ResultColor32.R := GraySampleValue;
ResultColor32.G := GraySampleValue;
ResultColor32.B := GraySampleValue;
ResultColor32.A := $FF;
end
else
if FBackgroundChunk.Background is TPngBackgroundColorFormat26 then
with TPngBackgroundColorFormat26(FBackgroundChunk.Background) do
begin
ResultColor32.R := RedSampleValue;
ResultColor32.G := GreenSampleValue;
ResultColor32.B := BlueSampleValue;
ResultColor32.A := $FF;
end;
if FBackgroundChunk.Background is TPngBackgroundColorFormat3 then
with TPngBackgroundColorFormat3(FBackgroundChunk.Background) do
begin
ResultColor32.R := PaletteEntry[PaletteIndex].R;
ResultColor32.G := PaletteEntry[PaletteIndex].R;
ResultColor32.B := PaletteEntry[PaletteIndex].R;
ResultColor32.A := $FF;
end;
end
else
Result := $0;
end;
function TPortableNetworkGraphic32.GR32Scanline(Bitmap: TObject; Y: Integer): Pointer;
begin
if Bitmap is TCustomBitmap32 then
Result := TCustomBitmap32(Bitmap).ScanLine[Y]
else
Result := nil;
end;
function TPortableNetworkGraphic32.GR32ScanlineProgress(Bitmap: TObject;
Y: Integer): Pointer;
begin
Result := GR32Scanline(Bitmap, Y);
if FImageHeader.Height > 0 then
FProgressEvent(Self, 100 * Y / FImageHeader.Height)
else
FProgressEvent(Self, 100);
end;
procedure TPortableNetworkGraphic32.DrawToBitmap32(Bitmap32: TCustomBitmap32);
var
DecoderClass: TCustomPngDecoderClass;
DataStream: TMemoryStream;
Transparency: TCustomPngTransparency;
begin
DataStream := TMemoryStream.Create;
try
// decompress image data to data stream
DecompressImageDataToStream(DataStream);
// reset data stream position
DataStream.Seek(0, soFromBeginning);
case ImageHeader.InterlaceMethod of
imNone:
case ImageHeader.ColorType of
ctGrayscale:
case ImageHeader.BitDepth of
1:
DecoderClass := TPngNonInterlacedGrayscale1bitDecoder;
2:
DecoderClass := TPngNonInterlacedGrayscale2bitDecoder;
4:
DecoderClass := TPngNonInterlacedGrayscale4bitDecoder;
8:
DecoderClass := TPngNonInterlacedGrayscale8bitDecoder;
16:
DecoderClass := TPngNonInterlacedGrayscale16bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctTrueColor :
case ImageHeader.BitDepth of
8:
DecoderClass := TPngNonInterlacedTrueColor8bitDecoder;
16:
DecoderClass := TPngNonInterlacedTrueColor16bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctIndexedColor :
case ImageHeader.BitDepth of
1, 2, 4:
DecoderClass := TPngNonInterlacedPaletteDecoder;
8:
DecoderClass := TPngNonInterlacedPalette8bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctGrayscaleAlpha :
case ImageHeader.BitDepth of
8:
DecoderClass := TPngNonInterlacedGrayscaleAlpha8bitDecoder;
16:
DecoderClass := TPngNonInterlacedGrayscaleAlpha16bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctTrueColorAlpha :
case ImageHeader.BitDepth of
8:
DecoderClass := TPngNonInterlacedTrueColorAlpha8bitDecoder;
16:
DecoderClass := TPngNonInterlacedTrueColorAlpha16bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
else raise EPngError.Create(RCStrUnsupportedFormat);
end;
imAdam7 :
case ImageHeader.ColorType of
ctGrayscale :
case ImageHeader.BitDepth of
1:
DecoderClass := TPngAdam7Grayscale1bitDecoder;
2:
DecoderClass := TPngAdam7Grayscale2bitDecoder;
4:
DecoderClass := TPngAdam7Grayscale4bitDecoder;
8:
DecoderClass := TPngAdam7Grayscale8bitDecoder;
16:
DecoderClass := TPngAdam7Grayscale16bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctTrueColor :
case ImageHeader.BitDepth of
8:
DecoderClass := TPngAdam7TrueColor8bitDecoder;
16:
DecoderClass := TPngAdam7TrueColor16bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctIndexedColor :
case ImageHeader.BitDepth of
1:
DecoderClass := TPngAdam7Palette1bitDecoder;
2:
DecoderClass := TPngAdam7Palette2bitDecoder;
4:
DecoderClass := TPngAdam7Palette4bitDecoder;
8:
DecoderClass := TPngAdam7Palette8bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctGrayscaleAlpha :
case ImageHeader.BitDepth of
8:
DecoderClass := TPngAdam7GrayscaleAlpha8bitDecoder;
16:
DecoderClass := TPngAdam7GrayscaleAlpha16bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctTrueColorAlpha :
case ImageHeader.BitDepth of
8:
DecoderClass := TPngAdam7TrueColorAlpha8bitDecoder;
16:
DecoderClass := TPngAdam7TrueColorAlpha16bitDecoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
if Assigned(FTransparencyChunk) then
Transparency := FTransparencyChunk.Transparency
else
Transparency := nil;
with DecoderClass.Create(DataStream, FImageHeader, FGammaChunk,
FPaletteChunk, Transparency) do
try
if Assigned(FProgressEvent) then
DecodeToScanline(Bitmap32, GR32ScanlineProgress)
else
DecodeToScanline(Bitmap32, GR32Scanline);
finally
Free;
end;
finally
FreeAndNil(DataStream);
end;
end;
function ColorIndexInPalette(Color: TColor32; Palette: TPalette24): Integer;
begin
for Result := 0 to Length(Palette) - 1 do
if (TColor32Entry(Color).R = Palette[Result].R) and
(TColor32Entry(Color).G = Palette[Result].G) and
(TColor32Entry(Color).B = Palette[Result].B) then
Exit;
Result := -1;
end;
procedure TPortableNetworkGraphic32.AssignPropertiesFromBitmap32(
Bitmap32: TCustomBitmap32);
var
Index: Integer;
IsAlpha: Boolean;
IsGrayScale: Boolean;
IsPalette: Boolean;
Color: TColor32;
TempPalette: TPalette24;
TempAlpha: Byte;
begin
with Bitmap32 do
begin
// basic properties
ImageHeader.Width := Width;
ImageHeader.Height := Height;
ImageHeader.CompressionMethod := 0;
ImageHeader.InterlaceMethod := imNone;
// initialize
SetLength(TempPalette, 0);
IsGrayScale := True;
IsPalette := True;
IsAlpha := False;
TempAlpha := 0;
// check every pixel in the bitmap for the use of the alpha channel,
// whether the image is grayscale or whether the colors can be stored
// as a palette (and build the palette at the same time
for Index := 0 to Width * Height - 1 do
begin
Color := Bits[Index];
// check whether the palette is empty
if Length(TempPalette) = 0 then
begin
IsAlpha := TColor32Entry(Color).A < 255 ;
// eventually store first alpha component
if IsAlpha then
TempAlpha := TColor32Entry(Color).A;
SetLength(TempPalette, 1);
TempPalette[0].R := TColor32Entry(Color).R;
TempPalette[0].G := TColor32Entry(Color).G;
TempPalette[0].B := TColor32Entry(Color).B;
IsGrayScale := (TColor32Entry(Color).R = TColor32Entry(Color).G) and
(TColor32Entry(Color).B = TColor32Entry(Color).G);
end
else
begin
// check alpha channel
if (TColor32Entry(Color).A < 255) then
begin
if IsAlpha then
if IsPalette and (TempAlpha <> TColor32Entry(Color).A) then
IsPalette := False
else
else
TempAlpha := TColor32Entry(Color).A;
IsAlpha := True;
end;
if ColorIndexInPalette(Color, TempPalette) < 0 then
begin
if IsPalette then
if (Length(TempPalette) < 256) then
begin
SetLength(TempPalette, Length(TempPalette) + 1);
TempPalette[Length(TempPalette) - 1].R := TColor32Entry(Color).R;
TempPalette[Length(TempPalette) - 1].G := TColor32Entry(Color).G;
TempPalette[Length(TempPalette) - 1].B := TColor32Entry(Color).B;
if IsGrayScale and not
((TColor32Entry(Color).R = TColor32Entry(Color).G) and
(TColor32Entry(Color).B = TColor32Entry(Color).G)) then
IsGrayScale := False;
end
else
IsPalette := False
else
if not ((TColor32Entry(Color).R = TColor32Entry(Color).G) and
(TColor32Entry(Color).B = TColor32Entry(Color).G)) then
IsGrayScale := False;
end;
end;
if IsAlpha and (not IsPalette) and (not IsGrayScale) then
Break;
end;
// temporary fix for the case that a palette and an alpha channel has been detected
if IsPalette and IsAlpha then
IsPalette := False;
// set image header
with ImageHeader do
if IsGrayScale then
if IsAlpha then
begin
ColorType := ctGrayscaleAlpha;
BitDepth := 8;
end
else
begin
ColorType := ctIndexedColor; // ctGrayscale
if Length(TempPalette) <= 2 then
BitDepth := 1
else
if Length(TempPalette) <= 4 then
BitDepth := 2
else
if Length(TempPalette) <= 16 then
BitDepth := 4
else
BitDepth := 8;
end
else
if IsPalette then
begin
ColorType := ctIndexedColor;
if Length(TempPalette) <= 2 then
BitDepth := 1 else
if Length(TempPalette) <= 4 then
BitDepth := 2 else
if Length(TempPalette) <= 16 then
BitDepth := 4
else
BitDepth := 8;
end
else
if IsAlpha then
begin
ColorType := ctTrueColorAlpha;
BitDepth := 8;
end
else
begin
ColorType := ctTrueColor;
BitDepth := 8;
end;
// eventually prepare palette
if ImageHeader.HasPalette then
begin
Assert(Length(TempPalette) <= 256);
if not Assigned(FPaletteChunk) then
FPaletteChunk := TChunkPngPalette.Create(ImageHeader);
FPaletteChunk.Count := Length(TempPalette);
for Index := 0 to Length(TempPalette) - 1 do
FPaletteChunk.PaletteEntry[Index] := TempPalette[Index];
end;
{$IFDEF StoreGamma}
// add linear gamma chunk
if not Assigned(FGammaChunk) then
FGammaChunk := TChunkPngGamma.Create(ImageHeader);
FGammaChunk.GammaAsSingle := 1;
{$ELSE}
// delete any gama correction table
if Assigned(FGammaChunk) then
FreeAndNil(FGammaChunk);
{$ENDIF}
end;
end;
procedure TPortableNetworkGraphic32.Assign(Source: TPersistent);
var
EncoderClass: TCustomPngEncoderClass;
DataStream: TMemoryStream;
begin
if Source is TCustomBitmap32 then
with TCustomBitmap32(Source) do
begin
// Assign
AssignPropertiesFromBitmap32(TCustomBitmap32(Source));
case ImageHeader.ColorType of
ctGrayscale:
case ImageHeader.BitDepth of
1:
EncoderClass := TPngNonInterlacedGrayscale1bitEncoder;
2:
EncoderClass := TPngNonInterlacedGrayscale2bitEncoder;
4:
EncoderClass := TPngNonInterlacedGrayscale4bitEncoder;
8:
EncoderClass := TPngNonInterlacedGrayscale8bitEncoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctTrueColor:
EncoderClass := TPngNonInterlacedTrueColor8bitEncoder;
ctIndexedColor:
case ImageHeader.BitDepth of
1 :
EncoderClass := TPngNonInterlacedPalette1bitEncoder;
2 :
EncoderClass := TPngNonInterlacedPalette2bitEncoder;
4 :
EncoderClass := TPngNonInterlacedPalette4bitEncoder;
8 :
EncoderClass := TPngNonInterlacedPalette8bitEncoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
ctGrayscaleAlpha:
EncoderClass := TPngNonInterlacedGrayscaleAlpha8bitEncoder;
ctTrueColorAlpha:
EncoderClass := TPngNonInterlacedTrueColorAlpha8bitEncoder;
else
raise EPngError.Create(RCStrUnsupportedFormat);
end;
DataStream := TMemoryStream.Create;
with DataStream do
try
with EncoderClass.Create(DataStream, FImageHeader, FGammaChunk, FPaletteChunk) do
try
if Assigned(FProgressEvent) then
EncodeFromScanline(TCustomBitmap32(Source), GR32ScanlineProgress)
else
EncodeFromScanline(TCustomBitmap32(Source), GR32Scanline);
finally
Free;
end;
// reset data stream position
DataStream.Seek(0, soFromBeginning);
// compress image data from data stream
CompressImageDataFromStream(DataStream);
finally
FreeAndNil(DataStream);
end;
end
else
inherited;
end;
procedure TPortableNetworkGraphic32.AssignTo(Dest: TPersistent);
begin
if Dest is TCustomBitmap32 then
begin
TCustomBitmap32(Dest).Width := ImageHeader.Width;
TCustomBitmap32(Dest).Height := ImageHeader.Height;
DrawToBitmap32(TCustomBitmap32(Dest));
end
else
inherited;
end;
const
CRowStart: array [0..6] of Integer = (0, 0, 4, 0, 2, 0, 1);
CColumnStart: array [0..6] of Integer = (0, 4, 0, 2, 0, 1, 0);
CRowIncrement: array [0..6] of Integer = (8, 8, 8, 4, 4, 2, 2);
CColumnIncrement: array [0..6] of Integer = (8, 8, 4, 4, 2, 2, 1);
CGrayScaleTable1Bit: array [0..1] of Byte = (0, $FF);
CGrayScaleTable2Bit: array [0..3] of Byte = (0, $55, $AA, $FF);
CGrayScaleTable4Bit: array [0..15] of Byte = (0, $11, $22, $33, $44, $55,
$66, $77, $88, $99, $AA, $BB, $CC, $DD, $EE, $FF);
{ TCustomPngNonInterlacedDecoder }
constructor TCustomPngNonInterlacedDecoder.Create(Stream: TStream;
Header: TChunkPngImageHeader; Gamma: TChunkPngGamma;
Palette: TChunkPngPalette; Transparency: TCustomPngTransparency);
begin
inherited;
FBytesPerRow := FHeader.BytesPerRow;
FRowByteSize := FBytesPerRow + 1;
GetMem(FRowBuffer[0], FRowByteSize);
GetMem(FRowBuffer[1], FRowByteSize);
end;
destructor TCustomPngNonInterlacedDecoder.Destroy;
begin
Dispose(FRowBuffer[0]);
Dispose(FRowBuffer[1]);
inherited;
end;
procedure TCustomPngNonInterlacedDecoder.DecodeToScanline(
Bitmap: TObject; ScanLineCallback: TScanLineCallback);
var
Index: Integer;
CurrentRow: Integer;
PixelByteSize: Integer;
AdaptiveFilterMethod: TAdaptiveFilterMethod;
UsedFilters: TAvailableAdaptiveFilterMethods;
begin
// initialize variables
CurrentRow := 0;
UsedFilters := [];
PixelByteSize := FHeader.PixelByteSize;
FillChar(FRowBuffer[1 - CurrentRow]^[0], FRowByteSize, 0);
for Index := 0 to FHeader.Height - 1 do
begin
// read data from stream
if FStream.Read(FRowBuffer[CurrentRow][0], FRowByteSize) <> FRowByteSize then
raise EPngError.Create(RCStrDataIncomplete);
// get active filter method
AdaptiveFilterMethod := TAdaptiveFilterMethod(FRowBuffer[CurrentRow]^[0]);
// filter current row
DecodeFilterRow(AdaptiveFilterMethod, FRowBuffer[CurrentRow],
FRowBuffer[1 - CurrentRow], FBytesPerRow, PixelByteSize);
// log used row pre filters
case AdaptiveFilterMethod of
afmSub:
UsedFilters := UsedFilters + [aafmSub];
afmUp:
UsedFilters := UsedFilters + [aafmUp];
afmAverage:
UsedFilters := UsedFilters + [aafmAverage];
afmPaeth:
UsedFilters := UsedFilters + [aafmPaeth];
end;
// transfer data from row to image
TransferData(@FRowBuffer[CurrentRow][1], ScanLineCallback(Bitmap, Index));
// flip current row
CurrentRow := 1 - CurrentRow;
end;
FHeader.AdaptiveFilterMethods := UsedFilters;
end;
{ TPngNonInterlacedGrayscale1bitDecoder }
procedure TPngNonInterlacedGrayscale1bitDecoder.TransferData(Source: Pointer;
Destination: PColor32);
var
Index: Integer;
Src: PByte absolute Source;
BitIndex: Byte;
begin
BitIndex := 8;
for Index := 0 to FHeader.Width - 1 do
begin
Dec(BitIndex);
PColor32Entry(Destination)^.R := FMappingTable[CGrayScaleTable1Bit[(Src^ shr BitIndex) and $1]];
PColor32Entry(Destination)^.G := PColor32Entry(Destination)^.R;
PColor32Entry(Destination)^.B := PColor32Entry(Destination)^.R;
PColor32Entry(Destination)^.A := 255;
if BitIndex = 0 then
begin
BitIndex := 8;
Inc(Src);
end;
Inc(Destination);
end;
end;
{ TPngNonInterlacedGrayscale2bitDecoder }
procedure TPngNonInterlacedGrayscale2bitDecoder.TransferData(Source: Pointer;
Destination: PColor32);
var
Index: Integer;
Src: PByte absolute Source;
BitIndex: Byte;
begin
BitIndex := 8;
for Index := 0 to FHeader.Width - 1 do
begin
Dec(BitIndex, 2);
PColor32Entry(Destination)^.R := FMappingTable[CGrayScaleTable2Bit[(Src^ shr BitIndex) and $3]];
PColor32Entry(Destination)^.G := PColor32Entry(Destination)^.R;
PColor32Entry(Destination)^.B := PColor32Entry(Destination)^.R;
PColor32Entry(Destination)^.A := 255;
if BitIndex = 0 then
begin
BitIndex := 8;
Inc(Src);
end;
Inc(Destination);
end;
end;
{ TPngNonInterlacedGrayscale4bitDecoder }
procedure TPngNonInterlacedGrayscale4bitDecoder.TransferData(Source: Pointer;
Destination: PColor32);
var
Index: Integer;
Src: PByte absolute Source;
BitIndex: Byte;
begin
BitIndex := 8;
for Index := 0 to FHeader.Width - 1 do
begin
Dec(BitIndex, 4);