forked from bgrabitmap/BGRABitmapDelphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgrareadpng.pas
1406 lines (1317 loc) · 41.3 KB
/
bgrareadpng.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
{
The original file before tweaking is:
$Id: fpreadpng.pp,v 1.10 2003/10/19 21:09:51 luk Exp $
This file is part of the Free Pascal run time library.
Copyright (c) 2003 by the Free Pascal development team
PNG reader implementation
See the file COPYING.FPC, included in this distribution,
for details about the copyright.
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.
**********************************************************************
Optimisations applied:
- using "const" parameter for TColorData
- direct pixel access with TBGRABitmap when possible
- some fixes of hints and of initializations
- vertical shrink option with MinifyHeight, OriginalHeight and VerticalShrinkFactor (useful for thumbnails)
}
{$i bgrabitmap.inc}{$H+}
unit BGRAReadPng;
interface
uses
SysUtils,Classes, BGRATypes, FPImage, FPImgCmn, PNGComn, {$IFNDEF FPC}Types, GraphType,{$ENDIF}
{$IFDEF FPC}ZStream,{$ELSE}ZLib,{$ENDIF}BGRAGraphics, BGRABitmapTypes;
Type
TSetPixelProc = procedure (x,y:integer; const CD : TColordata) of object;
TConvertColorProc = function (const CD:TColorData) : TFPColor of object;
TBGRAConvertColorProc = function (const CD:TColorData) : TBGRAPixel of object;
THandleScanLineProc = procedure (const y : integer; const ScanLine : PByteArray) of object;
{ TBGRAReaderPNG }
TBGRAReaderPNG = class (TBGRAImageReader)
private
FHeader : THeaderChunk;
ZData : TMemoryStream; // holds compressed data until all blocks are read
Decompress : TDeCompressionStream; // decompresses the data
FPltte : boolean; // if palette is used
FCountScanlines : EightLong; //Number of Scanlines to process for each pass
FScanlineLength : EightLong; //Length of Scanline for each pass
FCurrentPass : byte;
ByteWidth : byte; // number of bytes to read for pixel information
BitsUsed : EightLong; // bitmasks to use to split a byte into smaller parts
BitShift : byte; // shift right to do of the bits extracted with BitsUsed for 1 element
CountBitsUsed : byte; // number of bit groups (1 pixel) per byte (when bytewidth = 1)
//CFmt : TColorFormat; // format of the colors to convert from
StartX,StartY, DeltaX,DeltaY, StartPass,EndPass : integer; // number and format of passes
FPalette : TFPPalette;
FSetPixel : TSetPixelProc;
FConvertColor : TConvertColorProc;
FBGRAConvertColor : TBGRAConvertColorProc;
FHandleScanline: THandleScanlineProc;
FVerticalShrinkMask: BGRADWord;
FVerticalShrinkShr: integer;
function GetOriginalHeight: integer;
function GetOriginalWidth: integer;
function GetVerticalShrinkFactor: integer;
procedure ReadChunk;
procedure HandleData;
procedure HandleUnknown;
function ColorGray1 (const CD:TColorData) : TFPColor;
function ColorGray2 (const CD:TColorData) : TFPColor;
function ColorGray4 (const CD:TColorData) : TFPColor;
function ColorGray8 (const CD:TColorData) : TFPColor;
function ColorGray16 (const CD:TColorData) : TFPColor;
function ColorGrayAlpha8 (const CD:TColorData) : TFPColor;
function ColorGrayAlpha16 (const CD:TColorData) : TFPColor;
function ColorColor8 (const CD:TColorData) : TFPColor;
function ColorColor16 (const CD:TColorData) : TFPColor;
function ColorColorAlpha8 (const CD:TColorData) : TFPColor;
function ColorColorAlpha16 (const CD:TColorData) : TFPColor;
function BGRAColorGray1 (const CD:TColorData) : TBGRAPixel;
function BGRAColorGray2 (const CD:TColorData) : TBGRAPixel;
function BGRAColorGray4 (const CD:TColorData) : TBGRAPixel;
function BGRAColorGray8 (const CD:TColorData) : TBGRAPixel;
function BGRAColorGray16 (const CD:TColorData) : TBGRAPixel;
function BGRAColorGrayAlpha8 (const CD:TColorData) : TBGRAPixel;
function BGRAColorGrayAlpha16 (const CD:TColorData) : TBGRAPixel;
function BGRAColorColor8 (const CD:TColorData) : TBGRAPixel;
function BGRAColorColor16 (const CD:TColorData) : TBGRAPixel;
function BGRAColorColorAlpha8 (const CD:TColorData) : TBGRAPixel;
function BGRAColorColorAlpha16 (const CD:TColorData) : TBGRAPixel;
protected
Chunk : TChunk;
UseTransparent, EndOfFile : boolean;
TransparentDataValue : TColorData;
UsingBitGroup : byte;
DataIndex : BGRALongWord;
DataBytes : TColorData;
procedure HandleChunk; virtual;
procedure HandlePalette; virtual;
procedure HandleAlpha; virtual;
function CalcX (relX:integer) : integer;
function CalcY (relY:integer) : integer;
function CalcColor(const Scanline : PByteArray): TColorData;
procedure HandleScanline (const y : integer; const Scanline : PByteArray); virtual;
procedure BGRAHandleScanline(const y: integer; const Scanline: PByteArray);
procedure BGRAHandleScanlineTr(const y: integer; const Scanline: PByteArray);
procedure DoDecompress; virtual;
procedure SetPalettePixel (x,y:integer; const CD : TColordata);
procedure SetPalColPixel (x,y:integer; const CD : TColordata);
procedure SetColorPixel (x,y:integer; const CD : TColordata);
procedure SetColorTrPixel (x,y:integer; const CD : TColordata);
procedure SetBGRAColorPixel (x,y:integer; const CD : TColordata);
procedure SetBGRAColorTrPixel (x,y:integer; const CD : TColordata);
function DecideSetPixel : TSetPixelProc; virtual;
procedure InternalRead ({%H-}Str:TStream; Img:TFPCustomImage); override;
function InternalCheck (Str:TStream) : boolean; override;
//property ColorFormat : TColorformat read CFmt;
property ConvertColor : TConvertColorProc read FConvertColor;
property CurrentPass : byte read FCurrentPass;
property Pltte : boolean read FPltte;
property ThePalette : TFPPalette read FPalette;
property Header : THeaderChunk read FHeader;
property CountScanlines : EightLong read FCountScanlines;
property ScanlineLength : EightLong read FScanlineLength;
public
MinifyHeight: integer;
constructor create; override;
destructor destroy; override;
property VerticalShrinkFactor: integer read GetVerticalShrinkFactor;
property OriginalWidth: integer read GetOriginalWidth;
property OriginalHeight: integer read GetOriginalHeight;
function GetQuickInfo(AStream: TStream): TQuickImageInfo; override;
end;
implementation
uses math;
const StartPoints : array[0..7, 0..1] of BGRAWord =
((0,0),(0,0),(4,0),(0,4),(2,0),(0,2),(1,0),(0,1));
Delta : array[0..7,0..1] of BGRAWord =
((1,1),(8,8),(8,8),(4,8),(4,4),(2,4),(2,2),(1,2));
BitsUsed1Depth : EightLong = ($80,$40,$20,$10,$08,$04,$02,$01);
BitsUsed2Depth : EightLong = ($C0,$30,$0C,$03,0,0,0,0);
BitsUsed4Depth : EightLong = ($F0,$0F,0,0,0,0,0,0);
constructor TBGRAReaderPNG.create;
begin
inherited;
chunk.acapacity := 0;
chunk.data := nil;
UseTransparent := False;
end;
destructor TBGRAReaderPNG.destroy;
begin
with chunk do
if acapacity > 0 then
freemem (data);
inherited;
end;
function TBGRAReaderPNG.GetQuickInfo(AStream: TStream): TQuickImageInfo;
const headerChunkSize = 13;
var
{%H-}FileHeader : packed array[0..7] of byte;
{%H-}ChunkHeader : TChunkHeader;
{%H-}HeaderChunk : THeaderChunk;
begin
fillchar({%H-}result, sizeof(result), 0);
if AStream.Read({%H-}FileHeader, sizeof(FileHeader))<> sizeof(FileHeader) then exit;
{.$IFDEF OBJ} //#
if BGRAQWord(FileHeader) <> BGRAQWord(PNGComn.Signature) then exit;
{.$ENDIF}
if AStream.Read({%H-}ChunkHeader, sizeof(ChunkHeader))<> sizeof(ChunkHeader) then exit;
if ChunkHeader.CType <> ChunkTypes[ctIHDR] then exit;
if {$IFNDEF BDS}BEtoN{$ENDIF}(ChunkHeader.CLength) < headerChunkSize then exit;
if AStream.Read({%H-}HeaderChunk, headerChunkSize) <> headerChunkSize then exit;
result.width:= {$IFNDEF BDS}BEtoN{$ENDIF}(HeaderChunk.Width);
result.height:= {$IFNDEF BDS}BEtoN{$ENDIF}(HeaderChunk.height);
case HeaderChunk.ColorType and 3 of
0,3: {grayscale, palette}
if HeaderChunk.BitDepth > 8 then
result.colorDepth := 8
else
result.colorDepth := HeaderChunk.BitDepth;
2: {color} result.colorDepth := HeaderChunk.BitDepth*3;
end;
if (HeaderChunk.ColorType and 4) = 4 then
result.alphaDepth := HeaderChunk.BitDepth
else
result.alphaDepth := 0;
end;
procedure TBGRAReaderPNG.ReadChunk;
var {%H-}ChunkHeader : TChunkHeader;
readCRC : BGRALongWord;
l : BGRALongWord;
begin
TheStream.Read ({%H-}ChunkHeader,sizeof(ChunkHeader));
with chunk do
begin
// chunk header
with ChunkHeader do
begin
{$IFDEF ENDIAN_LITTLE}
alength := FPImgCmn.swap(CLength);
{$ELSE}
alength := CLength;
{$ENDIF}
ReadType := CType;
end;
aType := low(TChunkTypes);
while (aType < high(TChunkTypes)) and (ChunkTypes[aType] <> ReadType) do
inc (aType);
if alength > MaxChunkLength then
raise PNGImageException.Create ('Invalid chunklength');
if alength > acapacity then
begin
if acapacity > 0 then
freemem (data);
GetMem (data, alength);
acapacity := alength;
end;
l := TheStream.read (data^, alength);
if l <> alength then
raise PNGImageException.Create ('Chunk length exceeds stream length');
readCRC := 0;
TheStream.Read (readCRC, sizeof(ReadCRC));
l := CalculateCRC (All1Bits, ReadType, sizeOf(ReadType));
l := CalculateCRC (l, data^, alength);
{$IFDEF ENDIAN_LITTLE}
l := FPImgCmn.swap(l xor All1Bits);
{$ELSE}
l := l xor All1Bits;
{$ENDIF}
if ReadCRC <> l then
raise PNGImageException.Create ('CRC check failed');
end;
end;
function TBGRAReaderPNG.GetVerticalShrinkFactor: integer;
begin
result := 1 shl FVerticalShrinkShr;
end;
function TBGRAReaderPNG.GetOriginalHeight: integer;
begin
result := Header.height;
end;
function TBGRAReaderPNG.GetOriginalWidth: integer;
begin
result := header.Width;
end;
procedure TBGRAReaderPNG.HandleData;
var OldSize : BGRALongWord;
begin
OldSize := ZData.size;
ZData.Size := OldSize;
ZData.Size := ZData.Size + Chunk.aLength;
ZData.Write (chunk.Data^, chunk.aLength);
end;
procedure TBGRAReaderPNG.HandleAlpha;
procedure PaletteAlpha;
var r : integer;
a : BGRAWord;
c : TFPColor;
begin
with chunk do
begin
if alength > BGRALongWord(ThePalette.count) then
raise PNGImageException.create ('To much alpha values for palette');
for r := 0 to alength-1 do
begin
c := ThePalette[r];
a := data^[r];
c.alpha := (a shl 8) + a;
ThePalette[r] := c;
end;
end;
end;
procedure TransparentGray;
var {%H-}a : BGRAWord;
begin
move (chunk.data^[0], {%H-}a, 2);
{$IFDEF ENDIAN_LITTLE}
a := FPImgCmn.swap (a);
{$ENDIF}
TransparentDataValue := a;
UseTransparent := True;
end;
procedure TransparentColor;
var d : byte;
{%H-}r,{%H-}g,{%H-}b : BGRAWord;
a : TColorData;
begin
with chunk do
begin
move (data^[0], {%H-}r, 2);
move (data^[2], {%H-}g, 2);
move (data^[4], {%H-}b, 2);
end;
{$IFDEF ENDIAN_LITTLE}
r := FPImgCmn.swap (r);
g := FPImgCmn.swap (g);
b := FPImgCmn.swap (b);
{$ENDIF}
d := header.bitdepth;
a := (TColorData(b) shl d) shl d;
a := a + (TColorData(g) shl d) + r;
TransparentDataValue := a;
UseTransparent := True;
end;
begin
case header.ColorType of
3 : PaletteAlpha;
0 : TransparentGray;
2 : TransparentColor;
end;
end;
procedure TBGRAReaderPNG.HandlePalette;
var r : BGRALongWord;
c : TFPColor;
t : BGRAWord;
begin
if header.colortype = 3 then
with chunk do
begin
if TheImage.UsePalette then
FPalette := TheImage.Palette
else
FPalette := TFPPalette.Create(0);
c.Alpha := AlphaOpaque;
if (aLength mod 3) > 0 then
raise PNGImageException.Create ('Impossible length for PLTE-chunk');
r := 0;
ThePalette.count := 0;
while r < alength do
begin
t := data^[r];
c.red := t + (t shl 8);
inc (r);
t := data^[r];
c.green := t + (t shl 8);
inc (r);
t := data^[r];
c.blue := t + (t shl 8);
inc (r);
ThePalette.Add (c);
end;
end;
end;
procedure TBGRAReaderPNG.SetPalettePixel (x,y:integer; const CD : TColordata);
begin // both PNG and palette have palette
TheImage.Pixels[x,y] := CD;
end;
procedure TBGRAReaderPNG.SetPalColPixel (x,y:integer; const CD : TColordata);
begin // PNG with palette, Img without
TheImage.Colors[x,y] := ThePalette[CD];
end;
procedure TBGRAReaderPNG.SetColorPixel (x,y:integer; const CD : TColordata);
var c : TFPColor;
begin // both PNG and Img work without palette, and no transparency colordata
// c := ConvertColor (CD,CFmt);
c := ConvertColor (CD);
TheImage.Colors[x,y] := c;
end;
procedure TBGRAReaderPNG.SetColorTrPixel (x,y:integer; const CD : TColordata);
var c : TFPColor;
begin // both PNG and Img work without palette, and there is a transparency colordata
//c := ConvertColor (CD,CFmt);
c := ConvertColor (CD);
if TransparentDataValue = CD then
c.alpha := alphaTransparent;
TheImage.Colors[x,y] := c;
end;
procedure TBGRAReaderPNG.SetBGRAColorPixel(x, y: integer; const CD: TColordata);
var c: TBGRAPixel;
begin
c := FBGRAConvertColor(CD);
if c.alpha = 0 then TBGRACustomBitmap(TheImage).SetPixel(x,y,BGRAPixelTransparent)
else TBGRACustomBitmap(TheImage).SetPixel(x,y,c);
end;
procedure TBGRAReaderPNG.SetBGRAColorTrPixel(x, y: integer; const CD: TColordata);
var c: TBGRAPixel;
begin
if TransparentDataValue = CD then
TBGRACustomBitmap(TheImage).SetPixel(x,y,BGRAPixelTransparent) else
begin
c := FBGRAConvertColor(CD);
if c.alpha = 0 then TBGRACustomBitmap(TheImage).SetPixel(x,y,BGRAPixelTransparent)
else TBGRACustomBitmap(TheImage).SetPixel(x,y,c);
end;
end;
function TBGRAReaderPNG.DecideSetPixel : TSetPixelProc;
begin
if Pltte then
if TheImage.UsePalette then
result := {$IFDEF OBJ}@{$ENDIF}SetPalettePixel
else
result := {$IFDEF OBJ}@{$ENDIF}SetPalColPixel
else
if UseTransparent then
begin
if TheImage is TBGRACustomBitmap then
result := {$IFDEF OBJ}@{$ENDIF}SetBGRAColorTrPixel
else
result := {$IFDEF OBJ}@{$ENDIF}SetColorTrPixel
end
else
begin
if TheImage is TBGRACustomBitmap then
result := {$IFDEF OBJ}@{$ENDIF}SetBGRAColorPixel
else
result := {$IFDEF OBJ}@{$ENDIF}SetColorPixel
end;
end;
function TBGRAReaderPNG.CalcX (relX:integer) : integer;
begin
result := StartX + (relX * deltaX);
end;
function TBGRAReaderPNG.CalcY (relY:integer) : integer;
begin
result := StartY + (relY * deltaY);
end;
function TBGRAReaderPNG.CalcColor(const ScanLine : PByteArray): TColorData;
var cd : BGRALongWord;
r : BGRAWord;
p : pbyte;
begin
if UsingBitGroup = 0 then
begin
Databytes := 0;
if Header.BitDepth = 16 then
begin
p := @Databytes;
for r:=0 to bytewidth shr 1 - 1 do
begin
p^ := ScanLine^[Dataindex+(r shl 1)+1];
(p+1)^ := ScanLine^[Dataindex+(r shl 1)];
inc(p,2);
end;
end
else move (ScanLine^[DataIndex], Databytes, bytewidth);
{$IFDEF ENDIAN_BIG}
Databytes:=FPImgCmn.swap(Databytes);
{$ENDIF}
inc (DataIndex,bytewidth);
end;
if bytewidth = 1 then
begin
cd := (Databytes and BitsUsed[UsingBitGroup]);
result := cd shr ((CountBitsUsed-UsingBitGroup-1) * BitShift);
inc (UsingBitgroup);
if UsingBitGroup >= CountBitsUsed then
UsingBitGroup := 0;
end
else
result := Databytes;
end;
procedure TBGRAReaderPNG.HandleScanLine (const y : integer; const ScanLine : PByteArray);
var x, rx : integer;
c : TColorData;
begin
UsingBitGroup := 0;
DataIndex := 0;
X := StartX;
if (UsingBitGroup = 0) and (Header.BitDepth <> 16) then
case ByteWidth of
1: if BitsUsed[0] = $ff then
begin
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
FSetPixel (x,y,ScanLine^[DataIndex]);
Inc(X, deltaX);
inc(DataIndex);
end;
exit;
end;
2: begin
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
{$IFDEF ENDIAN_BIG}
FSetPixel (x,y,FPImgCmn.swap(PBGRAWord(@Scanline^[DataIndex])^));
{$ELSE}
FSetPixel (x,y,PBGRAWord(@Scanline^[DataIndex])^);
{$ENDIF}
Inc(X, deltaX);
inc(DataIndex,2);
end;
exit;
end;
4: begin
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
{$IFDEF ENDIAN_BIG}
FSetPixel (x,y,FPImgCmn.swap(PBGRADWord(@Scanline^[DataIndex])^));
{$ELSE}
FSetPixel (x,y,PBGRADWord(@Scanline^[DataIndex])^);
{$ENDIF}
Inc(X, deltaX);
inc(DataIndex,4);
end;
exit;
end;
8: begin
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
{$IFDEF ENDIAN_BIG}
FSetPixel (x,y,FPImgCmn.swap(PBGRAQWord(@Scanline^[DataIndex])^));
{$ELSE}
FSetPixel (x,y,PBGRAQWord(@Scanline^[DataIndex])^);
{$ENDIF}
Inc(X, deltaX);
inc(DataIndex,8);
end;
exit;
end;
end;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
c := CalcColor(ScanLine);
FSetPixel (x,y,c);
Inc(X, deltaX);
end
end;
procedure TBGRAReaderPNG.BGRAHandleScanLine (const y : integer; const ScanLine : PByteArray);
var x, rx : integer;
c : TColorData;
pdest: PBGRAPixel;
begin
UsingBitGroup := 0;
DataIndex := 0;
{$IFDEF FPC}{$PUSH}{$ENDIF}{$RANGECHECKS OFF} //because PByteArray is limited to 32767
if (UsingBitGroup = 0) and (Header.BitDepth <> 16) then
case ByteWidth of
1: if BitsUsed[0] = $ff then
begin
pdest := TBGRACustomBitmap(TheImage).ScanLine[y]+StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
pdest^ := FBGRAConvertColor(ScanLine^[DataIndex]);
if pdest^.alpha = 0 then pdest^ := BGRAPixelTransparent;
Inc(pdest, deltaX);
inc(DataIndex);
end;
exit;
end;
2: begin
pdest := TBGRACustomBitmap(TheImage).ScanLine[y]+StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
pdest^ := FBGRAConvertColor(
{$IFDEF ENDIAN_BIG}
FPImgCmn.swap(PBGRAWord(@Scanline^[DataIndex])^)
{$ELSE}
PBGRAWord(@Scanline^[DataIndex])^
{$ENDIF} );
if pdest^.alpha = 0 then pdest^ := BGRAPixelTransparent;
Inc(pdest, deltaX);
inc(DataIndex,2);
end;
exit;
end;
4: begin
pdest := TBGRACustomBitmap(TheImage).ScanLine[y]+StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
pdest^ := FBGRAConvertColor(
{$IFDEF ENDIAN_BIG}
FPImgCmn.swap(PBGRADWord(@Scanline^[DataIndex])^)
{$ELSE}
PBGRADWord(@Scanline^[DataIndex])^
{$ENDIF} );
if pdest^.alpha = 0 then pdest^ := BGRAPixelTransparent;
Inc(pdest, deltaX);
inc(DataIndex,4);
end;
exit;
end;
8: begin
pdest := TBGRACustomBitmap(TheImage).ScanLine[y]+StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
pdest^ := FBGRAConvertColor(
{$IFDEF ENDIAN_BIG}
FPImgCmn.swap(PBGRAQWord(@Scanline^[DataIndex])^)
{$ELSE}
PBGRAQWord(@Scanline^[DataIndex])^
{$ENDIF} );
if pdest^.alpha = 0 then pdest^ := BGRAPixelTransparent;
Inc(pdest, deltaX);
inc(DataIndex,8);
end;
exit;
end;
end;
{$IFDEF FPC}{$POP}{$ENDIF}
X := StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
c := CalcColor(ScanLine);
FSetPixel (x,y,c);
Inc(X, deltaX);
end
end;
procedure TBGRAReaderPNG.BGRAHandleScanLineTr(const y: integer;
const ScanLine: PByteArray);
var x, rx : integer;
c : TColorData;
pdest: PBGRAPixel;
begin
UsingBitGroup := 0;
DataIndex := 0;
if (UsingBitGroup = 0) and (Header.BitDepth <> 16) then
case ByteWidth of
1: if BitsUsed[0] = $ff then
begin
pdest := TBGRACustomBitmap(TheImage).ScanLine[y]+StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
c := ScanLine^[DataIndex];
if c = TransparentDataValue then
pdest^ := BGRAPixelTransparent else
begin
pdest^ := FBGRAConvertColor(c);
if pdest^.alpha = 0 then pdest^ := BGRAPixelTransparent;
end;
Inc(pdest, deltaX);
inc(DataIndex);
end;
exit;
end;
2: begin
pdest := TBGRACustomBitmap(TheImage).ScanLine[y]+StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
c :=
{$IFDEF ENDIAN_BIG}
FPImgCmn.swap(PBGRAWord(@Scanline^[DataIndex])^)
{$ELSE}
PBGRAWord(@Scanline^[DataIndex])^
{$ENDIF} ;
if c = TransparentDataValue then
pdest^ := BGRAPixelTransparent else
begin
pdest^ := FBGRAConvertColor(c);
if pdest^.alpha = 0 then pdest^ := BGRAPixelTransparent;
end;
Inc(pdest, deltaX);
inc(DataIndex,2);
end;
exit;
end;
4: begin
pdest := TBGRACustomBitmap(TheImage).ScanLine[y]+StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
c :=
{$IFDEF ENDIAN_BIG}
FPImgCmn.swap(PBGRADWord(@Scanline^[DataIndex])^)
{$ELSE}
PBGRADWord(@Scanline^[DataIndex])^
{$ENDIF} ;
if c = TransparentDataValue then
pdest^ := BGRAPixelTransparent else
begin
pdest^ := FBGRAConvertColor(c);
if pdest^.alpha = 0 then pdest^ := BGRAPixelTransparent;
end;
Inc(pdest, deltaX);
inc(DataIndex,4);
end;
exit;
end;
8: begin
pdest := TBGRACustomBitmap(TheImage).ScanLine[y]+StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
c :=
{$IFDEF ENDIAN_BIG}
FPImgCmn.swap(PBGRAQWord(@Scanline^[DataIndex])^)
{$ELSE}
PBGRAQWord(@Scanline^[DataIndex])^
{$ENDIF} ;
if c = TransparentDataValue then
pdest^ := BGRAPixelTransparent else
begin
pdest^ := FBGRAConvertColor(c);
if pdest^.alpha = 0 then pdest^ := BGRAPixelTransparent;
end;
Inc(pdest, deltaX);
inc(DataIndex,8);
end;
exit;
end;
end;
X := StartX;
for rx := 0 to ScanlineLength[CurrentPass]-1 do
begin
c := CalcColor(ScanLine);
FSetPixel (x,y,c);
Inc(X, deltaX);
end
end;
function TBGRAReaderPNG.ColorGray1 (const CD:TColorDAta) : TFPColor;
begin
if CD = 0 then
result := colBlack
else
result := colWhite;
end;
function TBGRAReaderPNG.ColorGray2 (const CD:TColorDAta) : TFPColor;
var c : BGRANativeUInt;
begin
c := CD and 3;
c := c + (c shl 2);
c := c + (c shl 4);
c := c + (c shl 8);
with result do
begin
red := c;
green := c;
blue := c;
alpha := alphaOpaque;
end;
end;
function TBGRAReaderPNG.ColorGray4 (const CD:TColorDAta) : TFPColor;
var c : BGRANativeUInt;
begin
c := CD and $F;
c := c + (c shl 4);
c := c + (c shl 8);
with result do
begin
red := c;
green := c;
blue := c;
alpha := alphaOpaque;
end;
end;
function TBGRAReaderPNG.ColorGray8 (const CD:TColorDAta) : TFPColor;
var c : BGRANativeUInt;
begin
c := CD and $FF;
c := c + (c shl 8);
with result do
begin
red := c;
green := c;
blue := c;
alpha := alphaOpaque;
end;
end;
function TBGRAReaderPNG.ColorGray16 (const CD:TColorDAta) : TFPColor;
var c : BGRANativeUInt;
begin
c := CD and $FFFF;
with result do
begin
red := c;
green := c;
blue := c;
alpha := alphaOpaque;
end;
end;
function TBGRAReaderPNG.ColorGrayAlpha8 (const CD:TColorData) : TFPColor;
var c : BGRANativeUInt;
begin
c := CD and $00FF;
c := c + (c shl 8);
with result do
begin
red := c;
green := c;
blue := c;
c := CD and $FF00;
alpha := c + (c shr 8);
end;
end;
function TBGRAReaderPNG.ColorGrayAlpha16 (const CD:TColorData) : TFPColor;
var c : BGRANativeUInt;
begin
c := CD and $FFFF;
with result do
begin
red := c;
green := c;
blue := c;
alpha := (CD shr 16) and $FFFF;
end;
end;
function TBGRAReaderPNG.ColorColor8 (const CD:TColorData) : TFPColor;
var c : BGRANativeUInt;
begin
with result do
begin
c := CD and $FF;
red := c + (c shl 8);
c := (CD shr 8) and $FF;
green := c + (c shl 8);
c := (CD shr 16) and $FF;
blue := c + (c shl 8);
alpha := alphaOpaque;
end;
end;
function TBGRAReaderPNG.ColorColor16 (const CD:TColorData) : TFPColor;
begin
with result do
begin
red := CD and $FFFF;
green := (CD shr 16) and $FFFF;
blue := (CD shr 32) and $FFFF;
alpha := alphaOpaque;
end;
end;
function TBGRAReaderPNG.ColorColorAlpha8 (const CD:TColorData) : TFPColor;
var c : BGRANativeUInt;
begin
with result do
begin
c := CD and $FF;
red := c + (c shl 8);
c := (CD shr 8) and $FF;
green := c + (c shl 8);
c := (CD shr 16) and $FF;
blue := c + (c shl 8);
c := (CD shr 24) and $FF;
alpha := c + (c shl 8);
end;
end;
function TBGRAReaderPNG.ColorColorAlpha16 (const CD:TColorData) : TFPColor;
{$IFDEF BDS}var _Int64 : BGRAInt64;{$ENDIF}
begin
with result do
begin
red := CD and $FFFF;
green := (CD shr 16) and $FFFF;
blue := (CD shr 32) and $FFFF;
{$IFDEF BDS}
move(CD , _Int64, sizeof(TColorData));
alpha := (_Int64 shr 48) and $FFFF;
{$ELSE}//#
alpha := (CD shr 48) and $FFFF;
{$ENDIF}
end;
end;
function TBGRAReaderPNG.BGRAColorGray1(const CD: TColorData): TBGRAPixel;
begin
if CD = 0 then
result := BGRABlack
else
result := BGRAWhite;
end;
function TBGRAReaderPNG.BGRAColorGray2(const CD: TColorData): TBGRAPixel;
var c : BGRANativeUInt;
begin
c := CD and 3;
c := c + (c shl 2);
c := c + (c shl 4);
result := BGRA(c,c,c);
end;
function TBGRAReaderPNG.BGRAColorGray4(const CD: TColorData): TBGRAPixel;
var c : BGRANativeUInt;
begin
c := CD and $F;
c := c + (c shl 4);
result := BGRA(c,c,c);
end;
function TBGRAReaderPNG.BGRAColorGray8(const CD: TColorData): TBGRAPixel;
var c : BGRANativeUInt;
begin
c := CD and $FF;
result := BGRA(c,c,c);
end;
function TBGRAReaderPNG.BGRAColorGray16(const CD: TColorData): TBGRAPixel;
var c : BGRANativeUInt;
begin
c := (CD shr 8) and $FF;
result := BGRA(c,c,c);
end;
function TBGRAReaderPNG.BGRAColorGrayAlpha8(const CD: TColorData): TBGRAPixel;
var c : BGRANativeUInt;
begin
c := CD and $00FF;
result := BGRA(c,c,c,(CD shr 8) and $FF);
end;
function TBGRAReaderPNG.BGRAColorGrayAlpha16(const CD: TColorData): TBGRAPixel;
var c : BGRANativeUInt;
begin
c := (CD shr 8) and $FF;
result := BGRA(c,c,c,(CD shr 24) and $FF);
end;
function TBGRAReaderPNG.BGRAColorColor8(const CD: TColorData): TBGRAPixel;
begin
result := BGRA(CD and $ff, (CD shr 8) and $ff, (CD shr 16) and $ff);
end;
function TBGRAReaderPNG.BGRAColorColor16(const CD: TColorData): TBGRAPixel;
{$IFDEF BDS}var _Int64 : BGRAInt64;{$ENDIF}
begin
{$IFDEF BDS}
move(CD , _Int64, sizeof(TColorData));
result := BGRA(CD shr 8 and $FF,(CD shr 24) and $FF,(_Int64 shr 40) and $FF);
{$ELSE}//#
result := BGRA(CD shr 8 and $FF,(CD shr 24) and $FF,(CD shr 40) and $FF);
{$ENDIF}
end;
function TBGRAReaderPNG.BGRAColorColorAlpha8(const CD: TColorData): TBGRAPixel;
begin
result := BGRA(CD and $ff, (CD shr 8) and $ff, (CD shr 16) and $ff, CD shr 24);
end;
function TBGRAReaderPNG.BGRAColorColorAlpha16(const CD: TColorData): TBGRAPixel;
{$IFDEF BDS}var _Int64 : BGRAInt64;{$ENDIF}
begin
{$IFDEF BDS}
move(CD , _Int64, sizeof(TColorData));
result := BGRA(CD shr 8 and $FF,(CD shr 24) and $FF,(_Int64 shr 40) and $FF, _Int64 shr 56);
{$ELSE}//#
result := BGRA(CD shr 8 and $FF,(CD shr 24) and $FF,(CD shr 40) and $FF, CD shr 56);
{$ENDIF}
end;
procedure TBGRAReaderPNG.DoDecompress;
procedure initVars;
var r,d : integer;
begin
with Header do
begin
if interlace=0 then
begin
StartPass := 0;
EndPass := 0;
FCountScanlines[0] := Height;
FScanLineLength[0] := Width;
end
else
begin
StartPass := 1;
EndPass := 7;
for r := 1 to 7 do
begin
d := Height div delta[r,1];
if (height mod delta[r,1]) > startpoints[r,1] then
inc (d);
FCountScanlines[r] := d;
d := width div delta[r,0];
if (width mod delta[r,0]) > startpoints[r,0] then
inc (d);
FScanLineLength[r] := d;