This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
GR32_PortableNetworkGraphic.pas
6150 lines (5197 loc) · 172 KB
/
GR32_PortableNetworkGraphic.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_PortableNetworkGraphic;
(* ***** 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,
{$IFDEF FPC} ZBase, ZDeflate, ZInflate; {$ELSE}
{$IFDEF ZLibEx}ZLibEx, ZLibExApi; {$ELSE}
{$IFDEF COMPILERRX2_UP} System.zlib; {$ELSE} zlib;
{$ENDIF}{$ENDIF}{$ENDIF}
type
{$A1}
TColorType = (
ctGrayscale = 0,
ctTrueColor = 2,
ctIndexedColor = 3,
ctGrayscaleAlpha = 4,
ctTrueColorAlpha = 6
);
TFilterMethod = (
fmAdaptiveFilter = 0
);
TAdaptiveFilterMethod = (
afmNone = 0,
afmSub = 1,
afmUp = 2,
afmAverage = 3,
afmPaeth = 4
);
TAvailableAdaptiveFilterMethod = (aafmSub, aafmUp, aafmAverage, aafmPaeth);
TAvailableAdaptiveFilterMethods = set of TAvailableAdaptiveFilterMethod;
TInterlaceMethod = (
imNone = 0,
imAdam7 = 1
);
TRGB24 = packed record
R, G, B: Byte;
end;
PRGB24 = ^TRGB24;
TRGB24Array = array [0..0] of TRGB24;
PRGB24Array = ^TRGB24Array;
TRGB24Word = packed record
R, G, B : Word;
end;
PRGB24Word = ^TRGB24Word;
TRGB32 = packed record
R, G, B, A: Byte;
end;
PRGB32 = ^TRGB32;
TRGB32Word = packed record
R, G, B, A: Word;
end;
PRGB32Word = ^TRGB32Word;
PByteArray = SysUtils.PByteArray;
TByteArray = SysUtils.TByteArray;
TChunkName = array [0..3] of AnsiChar;
EPngError = class(Exception);
{$IFDEF FPC}
TZStreamRec = z_stream;
{$ENDIF}
{$A4}
TCustomChunk = class(TPersistent)
protected
function GetChunkNameAsString: AnsiString; virtual; abstract;
function GetChunkName: TChunkName; virtual; abstract;
function GetChunkSize: Cardinal; virtual; abstract;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); virtual; abstract;
procedure WriteToStream(Stream: TStream); virtual; abstract;
property ChunkName: TChunkName read GetChunkName;
property ChunkNameAsString: AnsiString read GetChunkNameAsString;
property ChunkSize: Cardinal read GetChunkSize;
end;
TCustomDefinedChunk = class(TCustomChunk)
protected
function GetChunkNameAsString: AnsiString; override;
function GetChunkName: TChunkName; override;
class function GetClassChunkName: TChunkName; virtual; abstract;
public
property ChunkName: TChunkName read GetClassChunkName;
end;
TCustomDefinedChunkClass = class of TCustomDefinedChunk;
TChunkPngImageHeader = class(TCustomDefinedChunk)
private
FWidth : Integer;
FHeight : Integer;
FBitDepth : Byte;
FColorType : TColorType;
FCompressionMethod : Byte;
FFilterMethod : TFilterMethod;
FInterlaceMethod : TInterlaceMethod;
FAdaptiveFilterMethods : TAvailableAdaptiveFilterMethods;
function GetHasPalette: Boolean;
function GetBytesPerRow: Integer;
function GetPixelByteSize: Integer;
procedure SetCompressionMethod(const Value: Byte);
procedure SetFilterMethod(const Value: TFilterMethod);
procedure SetAdaptiveFilterMethods(const Value: TAvailableAdaptiveFilterMethods);
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create; virtual;
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
procedure ResetToDefault; virtual;
property Width: Integer read FWidth write FWidth;
property Height: Integer read FHeight write FHeight;
property BitDepth: Byte read FBitDepth write FBitDepth;
property ColorType: TColorType read FColorType write FColorType;
property CompressionMethod: Byte read FCompressionMethod write SetCompressionMethod;
property AdaptiveFilterMethods: TAvailableAdaptiveFilterMethods read FAdaptiveFilterMethods write SetAdaptiveFilterMethods;
property FilterMethod: TFilterMethod read FFilterMethod write SetFilterMethod;
property InterlaceMethod: TInterlaceMethod read FInterlaceMethod write FInterlaceMethod;
property HasPalette: Boolean read GetHasPalette;
property BytesPerRow: Integer read GetBytesPerRow;
property PixelByteSize: Integer read GetPixelByteSize;
end;
TCustomDefinedChunkWithHeader = class(TCustomDefinedChunk)
protected
FHeader : TChunkPngImageHeader;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(Header: TChunkPngImageHeader); reintroduce; virtual;
procedure HeaderChanged; virtual;
property Header: TChunkPngImageHeader read FHeader;
end;
TCustomDefinedChunkWithHeaderClass = class of TCustomDefinedChunkWithHeader;
TChunkPngImageData = class(TCustomDefinedChunkWithHeader)
private
FData : TMemoryStream;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(Header: TChunkPngImageHeader); override;
destructor Destroy; override;
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property Data: TMemoryStream read FData;
end;
TChunkPngPalette = class(TCustomDefinedChunkWithHeader)
private
FPaletteEntries : array of TRGB24;
function GetPaletteEntry(Index: Cardinal): TRGB24;
function GetCount: Cardinal;
procedure SetCount(const Value: Cardinal);
procedure SetPaletteEntry(Index: Cardinal; const Value: TRGB24);
protected
procedure AssignTo(Dest: TPersistent); override;
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure PaletteEntriesChanged; virtual;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property PaletteEntry[Index: Cardinal]: TRGB24 read GetPaletteEntry write SetPaletteEntry; default;
property Count: Cardinal read GetCount write SetCount;
end;
TChunkPngGamma = class(TCustomDefinedChunkWithHeader)
private
FGamma : Cardinal;
function GetGammaAsSingle: Single;
procedure SetGammaAsSingle(const Value: Single);
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property Gamma: Cardinal read FGamma write FGamma;
property GammaAsSingle: Single read GetGammaAsSingle write SetGammaAsSingle;
end;
TChunkPngStandardColorSpaceRGB = class(TCustomDefinedChunkWithHeader)
private
FRenderingIntent : Byte;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property RenderingIntent: Byte read FRenderingIntent write FRenderingIntent;
end;
TChunkPngPrimaryChromaticities = class(TCustomDefinedChunkWithHeader)
private
FWhiteX : Integer;
FWhiteY : Integer;
FRedX : Integer;
FRedY : Integer;
FGreenX : Integer;
FGreenY : Integer;
FBlueX : Integer;
FBlueY : Integer;
function GetBlueX: Single;
function GetBlueY: Single;
function GetGreenX: Single;
function GetGreenY: Single;
function GetRedX: Single;
function GetRedY: Single;
function GetWhiteX: Single;
function GetWhiteY: Single;
procedure SetBlueX(const Value: Single);
procedure SetBlueY(const Value: Single);
procedure SetGreenX(const Value: Single);
procedure SetGreenY(const Value: Single);
procedure SetRedX(const Value: Single);
procedure SetRedY(const Value: Single);
procedure SetWhiteX(const Value: Single);
procedure SetWhiteY(const Value: Single);
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property WhiteX: Integer read FWhiteX write FWhiteX;
property WhiteY: Integer read FWhiteY write FWhiteY;
property RedX: Integer read FRedX write FRedX;
property RedY: Integer read FRedY write FRedY;
property GreenX: Integer read FGreenX write FGreenX;
property GreenY: Integer read FGreenY write FGreenY;
property BlueX: Integer read FBlueX write FBlueX;
property BlueY: Integer read FBlueY write FBlueY;
property WhiteXAsSingle: Single read GetWhiteX write SetWhiteX;
property WhiteYAsSingle: Single read GetWhiteY write SetWhiteY;
property RedXAsSingle: Single read GetRedX write SetRedX;
property RedYAsSingle: Single read GetRedY write SetRedY;
property GreenXAsSingle: Single read GetGreenX write SetGreenX;
property GreenYAsSingle: Single read GetGreenY write SetGreenY;
property BlueXAsSingle: Single read GetBlueX write SetBlueX;
property BlueYAsSingle: Single read GetBlueY write SetBlueY;
end;
TChunkPngTime = class(TCustomDefinedChunkWithHeader)
private
FYear : Word;
FMonth : Byte;
FDay : Byte;
FHour : Byte;
FMinute : Byte;
FSecond : Byte;
function GetModifiedDateTime: TDateTime;
procedure SetModifiedDateTime(const Value: TDateTime);
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property Year: Word read FYear write FYear;
property Month: Byte read FMonth write FMonth;
property Day: Byte read FDay write FDay;
property Hour: Byte read FHour write FHour;
property Minute: Byte read FMinute write FMinute;
property Second: Byte read FSecond write FSecond;
property ModifiedDateTime: TDateTime read GetModifiedDateTime write SetModifiedDateTime;
end;
TChunkPngEmbeddedIccProfile = class(TCustomDefinedChunkWithHeader)
private
FProfileName : AnsiString;
FCompressionMethod : Byte;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property ProfileName: AnsiString read FProfileName write FProfileName;
property CompressionMethod: Byte read FCompressionMethod write FCompressionMethod;
end;
TCustomPngSignificantBits = class(TPersistent)
protected
class function GetChunkSize: Cardinal; virtual; abstract;
public
constructor Create(BitDepth: Integer = 8); virtual; abstract;
procedure ReadFromStream(Stream: TStream); virtual; abstract;
procedure WriteToStream(Stream: TStream); virtual; abstract;
property ChunkSize: Cardinal read GetChunkSize;
end;
TPngSignificantBitsFormat0 = class(TCustomPngSignificantBits)
private
FGrayBits : Byte;
protected
class function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(BitDepth: Integer = 8); override;
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property GrayBits: Byte read FGrayBits write FGrayBits;
end;
TPngSignificantBitsFormat23 = class(TCustomPngSignificantBits)
private
FRedBits : Byte;
FBlueBits : Byte;
FGreenBits : Byte;
protected
class function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(BitDepth: Integer = 8); override;
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property RedBits: Byte read FRedBits write FRedBits;
property BlueBits: Byte read FBlueBits write FBlueBits;
property GreenBits: Byte read FGreenBits write FGreenBits;
end;
TPngSignificantBitsFormat4 = class(TCustomPngSignificantBits)
private
FGrayBits : Byte;
FAlphaBits : Byte;
protected
class function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(BitDepth: Integer = 8); override;
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property GrayBits: Byte read FGrayBits write FGrayBits;
property AlphaBits: Byte read FAlphaBits write FAlphaBits;
end;
TPngSignificantBitsFormat6 = class(TCustomPngSignificantBits)
private
FRedBits : Byte;
FBlueBits : Byte;
FGreenBits : Byte;
FAlphaBits : Byte;
protected
class function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(BitDepth: Integer = 8); override;
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property RedBits: Byte read FRedBits write FRedBits;
property BlueBits: Byte read FBlueBits write FBlueBits;
property GreenBits: Byte read FGreenBits write FGreenBits;
property AlphaBits: Byte read FAlphaBits write FAlphaBits;
end;
TChunkPngSignificantBits = class(TCustomDefinedChunkWithHeader)
private
FSignificantBits : TCustomPngSignificantBits;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(Header: TChunkPngImageHeader); override;
destructor Destroy; override;
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
procedure HeaderChanged; override;
property SignificantBits: TCustomPngSignificantBits read FSignificantBits;
end;
TCustomPngBackgroundColor = class(TPersistent)
protected
class function GetChunkSize: Cardinal; virtual; abstract;
public
procedure ReadFromStream(Stream: TStream); virtual; abstract;
procedure WriteToStream(Stream: TStream); virtual; abstract;
property ChunkSize: Cardinal read GetChunkSize;
end;
TPngBackgroundColorFormat04 = class(TCustomPngBackgroundColor)
private
FGraySampleValue : Word;
protected
class function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property GraySampleValue: Word read FGraySampleValue write FGraySampleValue;
end;
TPngBackgroundColorFormat26 = class(TCustomPngBackgroundColor)
private
FRedSampleValue : Word;
FBlueSampleValue : Word;
FGreenSampleValue : Word;
protected
class function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property RedSampleValue: Word read FRedSampleValue write FRedSampleValue;
property BlueSampleValue: Word read FBlueSampleValue write FBlueSampleValue;
property GreenSampleValue: Word read FGreenSampleValue write FGreenSampleValue;
end;
TPngBackgroundColorFormat3 = class(TCustomPngBackgroundColor)
private
FIndex : Byte;
protected
class function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property PaletteIndex: Byte read FIndex write FIndex;
end;
TChunkPngBackgroundColor = class(TCustomDefinedChunkWithHeader)
private
FBackground : TCustomPngBackgroundColor;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(Header: TChunkPngImageHeader); override;
destructor Destroy; override;
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
procedure HeaderChanged; override;
property Background: TCustomPngBackgroundColor read FBackground;
end;
TChunkPngImageHistogram = class(TCustomDefinedChunkWithHeader)
private
FHistogram : array of Word;
function GetCount: Cardinal;
function GetFrequency(Index: Cardinal): Word;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property Count: Cardinal read GetCount;
property Frequency[Index: Cardinal]: Word read GetFrequency;
end;
TSuggestedPalette8ByteEntry = record
Red : Byte;
Green : Byte;
Blue : Byte;
Alpha : Byte;
Frequency : Word;
end;
PSuggestedPalette8ByteEntry = ^TSuggestedPalette8ByteEntry;
TSuggestedPalette8ByteArray = array [0..0] of TSuggestedPalette8ByteEntry;
PSuggestedPalette8ByteArray = ^TSuggestedPalette8ByteArray;
TSuggestedPalette16ByteEntry = record
Red : Word;
Green : Word;
Blue : Word;
Alpha : Word;
Frequency : Word;
end;
PSuggestedPalette16ByteEntry = ^TSuggestedPalette16ByteEntry;
TSuggestedPalette16ByteArray = array [0..0] of TSuggestedPalette16ByteEntry;
PSuggestedPalette16ByteArray = ^TSuggestedPalette16ByteArray;
TChunkPngSuggestedPalette = class(TCustomDefinedChunkWithHeader)
private
FPaletteName : AnsiString;
FData : Pointer;
FCount : Cardinal;
FSampleDepth : Byte;
function GetCount: Cardinal;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
public
constructor Create(Header: TChunkPngImageHeader); override;
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property Count: Cardinal read GetCount;
end;
TCustomPngTransparency = class(TPersistent)
protected
function GetChunkSize: Cardinal; virtual; abstract;
public
procedure ReadFromStream(Stream: TStream); virtual; abstract;
procedure WriteToStream(Stream: TStream); virtual; abstract;
property ChunkSize: Cardinal read GetChunkSize;
end;
TPngTransparencyFormat0 = class(TCustomPngTransparency)
private
FGraySampleValue : Word;
protected
procedure AssignTo(Dest: TPersistent); override;
function GetChunkSize: Cardinal; override;
public
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property GraySampleValue: Word read FGraySampleValue write FGraySampleValue;
end;
TPngTransparencyFormat2 = class(TCustomPngTransparency)
private
FRedSampleValue : Word;
FBlueSampleValue : Word;
FGreenSampleValue : Word;
protected
procedure AssignTo(Dest: TPersistent); override;
function GetChunkSize: Cardinal; override;
public
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property RedSampleValue: Word read FRedSampleValue write FRedSampleValue;
property BlueSampleValue: Word read FBlueSampleValue write FBlueSampleValue;
property GreenSampleValue: Word read FGreenSampleValue write FGreenSampleValue;
end;
TPngTransparencyFormat3 = class(TCustomPngTransparency)
private
function GetCount: Cardinal;
function GetTransparency(Index: Cardinal): Byte;
protected
FTransparency : array of Byte;
procedure AssignTo(Dest: TPersistent); override;
function GetChunkSize: Cardinal; override;
public
procedure ReadFromStream(Stream: TStream); override;
procedure WriteToStream(Stream: TStream); override;
property Count: Cardinal read GetCount;
property Transparency[Index: Cardinal]: Byte read GetTransparency;
end;
TChunkPngTransparency = class(TCustomDefinedChunkWithHeader)
protected
FTransparency : TCustomPngTransparency;
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(Header: TChunkPngImageHeader); override;
destructor Destroy; override;
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
procedure HeaderChanged; override;
property Transparency: TCustomPngTransparency read FTransparency;
end;
TChunkPngPhysicalPixelDimensions = class(TCustomDefinedChunkWithHeader)
private
FPixelsPerUnitX : Cardinal;
FPixelsPerUnitY : Cardinal;
FUnit : Byte;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property PixelsPerUnitX: Cardinal read FPixelsPerUnitX write FPixelsPerUnitX;
property PixelsPerUnitY: Cardinal read FPixelsPerUnitY write FPixelsPerUnitY;
property PixelUnit: Byte read FUnit write FUnit;
end;
TChunkPngPhysicalScale = class(TCustomDefinedChunkWithHeader)
private
FUnitSpecifier : Byte;
FUnitsPerPixelX : Single;
FUnitsPerPixelY : Single;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property UnitSpecifier: Byte read FUnitSpecifier write FUnitSpecifier;
property UnitsPerPixelX: Single read FUnitsPerPixelX write FUnitsPerPixelX;
property UnitsPerPixelY: Single read FUnitsPerPixelY write FUnitsPerPixelY;
end;
TChunkPngImageOffset = class(TCustomDefinedChunkWithHeader)
private
FImagePositionX : Integer;
FImagePositionY : Integer;
FUnitSpecifier : Byte;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property UnitSpecifier: Byte read FUnitSpecifier write FUnitSpecifier;
property ImagePositionX: Integer read FImagePositionX write FImagePositionX;
property ImagePositionY: Integer read FImagePositionY write FImagePositionY;
end;
TChunkPngPixelCalibrator = class(TCustomDefinedChunkWithHeader)
private
FCalibratorName : AnsiString;
FOriginalZeroes : array [0..1] of Integer;
FEquationType : Byte;
FNumberOfParams : Byte;
FUnitName : AnsiString;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property CalibratorName: AnsiString read FCalibratorName write FCalibratorName;
property OriginalZeroMin: Integer read FOriginalZeroes[0] write FOriginalZeroes[0];
property OriginalZeroMax: Integer read FOriginalZeroes[1] write FOriginalZeroes[1];
property EquationType: Byte read FEquationType write FEquationType;
property NumberOfParams: Byte read FNumberOfParams write FNumberOfParams;
end;
TCustomChunkPngText = class(TCustomDefinedChunkWithHeader)
private
procedure SetKeyword(const Value: AnsiString);
procedure SetText(const Value: AnsiString);
protected
FKeyword : AnsiString;
FText : AnsiString;
procedure AssignTo(Dest: TPersistent); override;
procedure KeywordChanged; virtual;
procedure TextChanged; virtual;
public
property Keyword: AnsiString read FKeyword write SetKeyword;
property Text: AnsiString read FText write SetText;
end;
TChunkPngText = class(TCustomChunkPngText)
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
end;
TChunkPngCompressedText = class(TCustomChunkPngText)
private
FCompressionMethod : Byte;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property CompressionMethod: Byte read FCompressionMethod write FCompressionMethod;
end;
TChunkPngInternationalText = class(TCustomChunkPngText)
private
FCompressionMethod : Byte;
FCompressionFlag : Byte;
FLanguageString : AnsiString;
FTranslatedKeyword : string;
protected
class function GetClassChunkName: TChunkName; override;
function GetChunkSize: Cardinal; override;
procedure AssignTo(Dest: TPersistent); override;
public
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property CompressionMethod: Byte read FCompressionMethod write FCompressionMethod;
property CompressionFlag: Byte read FCompressionFlag write FCompressionFlag;
property LanguageString: AnsiString read FLanguageString write FLanguageString;
property TranslatedKeyword: string read FTranslatedKeyword write FTranslatedKeyword;
end;
TChunkPngUnknown = class(TCustomChunk)
private
function GetData(index: Integer): Byte;
procedure SetData(index: Integer; const Value: Byte);
protected
FChunkName : TChunkName;
FDataStream : TMemoryStream;
function GetChunkName: TChunkName; override;
function GetChunkNameAsString: AnsiString; override;
function GetChunkSize: Cardinal; override;
function CalculateChecksum: Integer;
procedure AssignTo(Dest: TPersistent); override;
public
constructor Create(ChunkName: TChunkName); virtual;
destructor Destroy; override;
procedure ReadFromStream(Stream: TStream; ChunkSize: Cardinal); override;
procedure WriteToStream(Stream: TStream); override;
property Data[index : Integer]: Byte read GetData write SetData;
property DataStream: TMemoryStream read FDataStream;
end;
TChunkList = class(TPersistent)
private
FChunks : array of TCustomChunk;
function GetCount: Cardinal;
protected
function GetChunk(Index: Integer): TCustomChunk;
procedure AssignTo(Dest: TPersistent); override;
public
destructor Destroy; override;
procedure Add(Item: TCustomChunk);
procedure Clear; virtual;
procedure Delete(Index: Cardinal);
function IndexOf(Item: TCustomChunk): Integer;
procedure Remove(Item: TCustomChunk);
property Count: Cardinal read GetCount;
property Chunks[Index: Integer]: TCustomChunk read GetChunk; default;
end;
TCustomPngCoder = class
protected
FStream : TStream;
FHeader : TChunkPngImageHeader;
FGamma : TChunkPngGamma;
FPalette : TChunkPngPalette;
FTransparency : TCustomPngTransparency;
FRowBuffer : array [0..1] of PByteArray;
FAlphaTable : PByteArray;
FMappingTable : PByteArray;
procedure BuildMappingTables; virtual;
procedure EncodeFilterSub(CurrentRow, PreviousRow, OutputRow: PByteArray; BytesPerRow, PixelByteSize: Integer);
procedure EncodeFilterUp(CurrentRow, PreviousRow, OutputRow: PByteArray; BytesPerRow, PixelByteSize: Integer);
procedure EncodeFilterAverage(CurrentRow, PreviousRow, OutputRow: PByteArray; BytesPerRow, PixelByteSize: Integer);
procedure EncodeFilterPaeth(CurrentRow, PreviousRow, OutputRow: PByteArray; BytesPerRow, PixelByteSize: Integer);
procedure DecodeFilterSub(CurrentRow, PreviousRow: PByteArray; BytesPerRow, PixelByteSize: NativeInt);
procedure DecodeFilterUp(CurrentRow, PreviousRow: PByteArray; BytesPerRow, PixelByteSize: NativeInt);
procedure DecodeFilterAverage(CurrentRow, PreviousRow: PByteArray; BytesPerRow, PixelByteSize: NativeInt);
procedure DecodeFilterPaeth(CurrentRow, PreviousRow: PByteArray; BytesPerRow, PixelByteSize: NativeInt);
procedure EncodeFilterRow(CurrentRow, PreviousRow, OutputRow, TempBuffer: PByteArray; BytesPerRow, PixelByteSize: Integer); virtual; abstract;
procedure DecodeFilterRow(FilterMethod: TAdaptiveFilterMethod; CurrentRow, PreviousRow: PByteArray; BytesPerRow, PixelByteSize: Integer); virtual; abstract;
public
constructor Create(Stream: TStream; Header: TChunkPngImageHeader;
Gamma: TChunkPngGamma = nil; Palette: TChunkPngPalette = nil;
Transparency : TCustomPngTransparency = nil); virtual;
destructor Destroy; override;
end;
TScanLineCallback = function(Bitmap: TObject; Y: Integer): Pointer of object;
TCustomPngDecoder = class(TCustomPngCoder)
protected
procedure EncodeFilterRow(CurrentRow, PreviousRow, OutputRow, TempBuffer: PByteArray; BytesPerRow, PixelByteSize: Integer); override;
procedure DecodeFilterRow(FilterMethod: TAdaptiveFilterMethod; CurrentRow, PreviousRow: PByteArray; BytesPerRow, PixelByteSize: Integer); override;
public
procedure DecodeToScanline(Bitmap: TObject; ScanLineCallback: TScanLineCallback); virtual; abstract;
end;
TCustomPngDecoderClass = class of TCustomPngDecoder;
TCustomPngEncoder = class(TCustomPngCoder)
protected
procedure EncodeFilterRow(CurrentRow, PreviousRow, OutputRow, TempBuffer: PByteArray; BytesPerRow, PixelByteSize: Integer); override;
procedure DecodeFilterRow(FilterMethod: TAdaptiveFilterMethod; CurrentRow, PreviousRow: PByteArray; BytesPerRow, PixelByteSize: Integer); override;
public
procedure EncodeFromScanline(Bitmap: TObject; ScanLineCallback: TScanLineCallback); virtual; abstract;
end;
TCustomPngEncoderClass = class of TCustomPngEncoder;
TCustomPngTranscoder = class(TCustomPngCoder)
protected
procedure EncodeFilterRow(CurrentRow, PreviousRow, OutputRow, TempBuffer: PByteArray; BytesPerRow, PixelByteSize: Integer); override;
procedure DecodeFilterRow(FilterMethod: TAdaptiveFilterMethod; CurrentRow, PreviousRow: PByteArray; BytesPerRow, PixelByteSize: Integer); override;
procedure Transcode; virtual; abstract;
public
constructor Create(Stream: TStream; Header: TChunkPngImageHeader;
Gamma: TChunkPngGamma = nil; Palette: TChunkPngPalette = nil;
Transparency: TCustomPngTransparency = nil); override;
destructor Destroy; override;
end;
TCustomPngTranscoderClass = class of TCustomPngTranscoder;
TPortableNetworkGraphic = class(TInterfacedPersistent, IStreamPersist)
private
FCompressionLevel : Byte;
function GetBitDepth: Byte;
function GetColorType: TColorType;
function GetCompressionMethod: Byte;
function GetFilterMethod: TFilterMethod;
function GetHeight: Integer;
function GetInterlaceMethod: TInterlaceMethod;
function GetPaletteEntry(Index: Integer): TRGB24;
function GetPaletteEntryCount: Integer;
function GetWidth: Integer;
function GetGamma: Single;
function GetModifiedTime: TDateTime;
function GetPixelsPerUnitX: Cardinal;
function GetPixelsPerUnitY: Cardinal;
function GetPixelUnit: Byte;
procedure SetPixelsPerUnitX(const Value: Cardinal);
procedure SetPixelsPerUnitY(const Value: Cardinal);
procedure SetPixelUnit(const Value: Byte);
procedure SetBitDepth(const Value: Byte);
procedure SetChromaChunk(const Value: TChunkPngPrimaryChromaticities);
procedure SetColorType(const Value: TColorType);
procedure SetCompressionMethod(const Value: Byte);
procedure SetCompressionLevel(const Value: Byte);
procedure SetFilterMethods(const Value: TAvailableAdaptiveFilterMethods);
procedure SetFilterMethod(const Value: TFilterMethod);
procedure SetGamma(const Value: Single);
procedure SetModifiedTime(const Value: TDateTime);
procedure SetHeight(const Value: Integer);
procedure SetImageHeader(const Value: TChunkPngImageHeader);
procedure SetInterlaceMethod(const Value: TInterlaceMethod);
procedure SetGammaChunk(const Value: TChunkPngGamma);
procedure SetPaletteChunk(const Value: TChunkPngPalette);
procedure SetTransparencyChunk(const Value: TChunkPngTransparency);
procedure SetPhysicalDimensions(const Value: TChunkPngPhysicalPixelDimensions);
procedure SetSignificantBits(const Value: TChunkPngSignificantBits);
procedure SetTimeChunk(const Value: TChunkPngTime);
procedure SetWidth(const Value: Integer);
function CalculateCRC(Buffer: PByte; Count: Cardinal): Cardinal; overload;
function CalculateCRC(Stream: TStream): Cardinal; overload;
{$IFDEF CheckCRC}
function CheckCRC(Stream: TStream; CRC: Cardinal): Boolean;
{$ENDIF}
procedure ReadImageDataChunk(Stream: TStream; Size: Integer);
procedure ReadUnknownChunk(Stream: TStream; ChunkName: TChunkName; ChunkSize: Integer);
function GetFilterMethods: TAvailableAdaptiveFilterMethods;
procedure SetBackgroundChunk(const Value: TChunkPngBackgroundColor);
protected
FImageHeader : TChunkPngImageHeader;
FPaletteChunk : TChunkPngPalette;
FGammaChunk : TChunkPngGamma;
FTimeChunk : TChunkPngTime;
FSignificantBits : TChunkPngSignificantBits;
FPhysicalDimensions : TChunkPngPhysicalPixelDimensions;
FChromaChunk : TChunkPngPrimaryChromaticities;
FTransparencyChunk : TChunkPngTransparency;
FBackgroundChunk : TChunkPngBackgroundColor;
FDataChunkList : TChunkList;
FAdditionalChunkList : TChunkList;
procedure Clear; virtual;
procedure AssignTo(Dest: TPersistent); override;
procedure CopyImageData(Stream: TStream);
procedure StoreImageData(Stream: TStream);
procedure DecompressImageDataToStream(Stream: TStream);
procedure CompressImageDataFromStream(Stream: TStream);
procedure CompressionLevelChanged; virtual;
procedure AdaptiveFilterMethodsChanged; virtual;
procedure InterlaceMethodChanged; virtual;
property ImageHeader: TChunkPngImageHeader read FImageHeader write SetImageHeader;
property PaletteChunk: TChunkPngPalette read FPaletteChunk write SetPaletteChunk;
property TransparencyChunk: TChunkPngTransparency read FTransparencyChunk write SetTransparencyChunk;
property BackgroundChunk: TChunkPngBackgroundColor read FBackgroundChunk write SetBackgroundChunk;
property GammaChunk: TChunkPngGamma read FGammaChunk write SetGammaChunk;
property TimeChunk: TChunkPngTime read FTimeChunk write SetTimeChunk;
property PhysicalPixelDimensionsChunk: TChunkPngPhysicalPixelDimensions read FPhysicalDimensions write SetPhysicalDimensions;