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
|
{
This file is part of the Free Pascal run time library.
Copyright (c) 2003 by the Free Pascal development team
XPM writer class.
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.
**********************************************************************}
{$mode objfpc}{$h+}
unit FPWritePNG;
interface
uses sysutils, classes, FPImage, FPImgCmn, PNGComn, ZStream;
type
TGetPixelFunc = function (x,y : LongWord) : TColorData of object;
TColorFormatFunction = function (color:TFPColor) : TColorData of object;
TFPWriterPNG = class (TFPCustomImageWriter)
private
FUsetRNS, FCompressedText, FWordSized, FIndexed,
FUseAlpha, FGrayScale : boolean;
FByteWidth : byte;
FChunk : TChunk;
CFmt : TColorFormat; // format of the colors to convert from
FFmtColor : TColorFormatFunction;
FTransparentColor : TFPColor;
FSwitchLine, FCurrentLine, FPreviousLine : pByteArray;
FPalette : TFPPalette;
OwnsPalette : boolean;
FHeader : THeaderChunk;
FGetPixel : TGetPixelFunc;
FDatalineLength : longword;
ZData : TMemoryStream; // holds uncompressed data until all blocks are written
Compressor : TCompressionStream; // compresses the data
FCompressionLevel : TCompressionLevel;
procedure WriteChunk;
function GetColorPixel (x,y:longword) : TColorData;
function GetPalettePixel (x,y:longword) : TColorData;
function GetColPalPixel (x,y:longword) : TColorData;
procedure InitWriteIDAT;
procedure Gatherdata;
procedure WriteCompressedData;
procedure FinalWriteIDAT;
protected
property Header : THeaderChunk read FHeader;
procedure InternalWrite (Str:TStream; Img:TFPCustomImage); override;
procedure WriteIHDR; virtual;
procedure WritePLTE; virtual;
procedure WritetRNS; virtual;
procedure WriteIDAT; virtual;
procedure WriteTexts; virtual;
procedure WriteIEND; virtual;
function CurrentLine (x:longword) : byte;
function PrevSample (x:longword): byte;
function PreviousLine (x:longword) : byte;
function PrevLinePrevSample (x:longword): byte;
function DoFilter (LineFilter:byte;index:longword; b:byte) : byte; virtual;
procedure SetChunkLength (aValue : longword);
procedure SetChunkType (ct : TChunkTypes);
procedure SetChunkType (ct : TChunkCode);
function DecideGetPixel : TGetPixelFunc; virtual;
procedure DetermineHeader (var AHeader : THeaderChunk); virtual;
function DetermineFilter (Current, Previous:PByteArray; linelength:longword):byte; virtual;
procedure FillScanLine (y : integer; ScanLine : pByteArray); virtual;
function ColorDataGrayB(color:TFPColor) : TColorData;
function ColorDataColorB(color:TFPColor) : TColorData;
function ColorDataGrayW(color:TFPColor) : TColorData;
function ColorDataColorW(color:TFPColor) : TColorData;
function ColorDataGrayAB(color:TFPColor) : TColorData;
function ColorDataColorAB(color:TFPColor) : TColorData;
function ColorDataGrayAW(color:TFPColor) : TColorData;
function ColorDataColorAW(color:TFPColor) : TColorData;
property ChunkDataBuffer : pByteArray read FChunk.data;
property UsetRNS : boolean read FUsetRNS;
property SingleTransparentColor : TFPColor read FTransparentColor;
property ThePalette : TFPPalette read FPalette;
property ColorFormat : TColorformat read CFmt;
property ColorFormatFunc : TColorFormatFunction read FFmtColor;
property byteWidth : byte read FByteWidth;
property DatalineLength : longword read FDatalineLength;
public
constructor create; override;
destructor destroy; override;
property GrayScale : boolean read FGrayscale write FGrayScale;
property Indexed : boolean read FIndexed write FIndexed;
property CompressedText : boolean read FCompressedText write FCompressedText;
property WordSized : boolean read FWordSized write FWordSized;
property UseAlpha : boolean read FUseAlpha write FUseAlpha;
property CompressionLevel : TCompressionLevel read FCompressionLevel write FCompressionLevel;
end;
implementation
constructor TFPWriterPNG.create;
begin
inherited;
Fchunk.acapacity := 0;
Fchunk.data := nil;
FGrayScale := False;
FIndexed := False;
FCompressedText := True;
FWordSized := True;
FUseAlpha := False;
FCompressionLevel:=clDefault;
end;
destructor TFPWriterPNG.destroy;
begin
if OwnsPalette then FreeAndNil(FPalette);
with Fchunk do
if acapacity > 0 then
freemem (data);
inherited;
end;
procedure TFPWriterPNG.WriteChunk;
var chead : TChunkHeader;
c : longword;
begin
with FChunk do
begin
{$IFDEF ENDIAN_LITTLE}
chead.CLength := swap (alength);
{$ELSE}
chead.CLength := alength;
{$ENDIF}
if (ReadType = '') then
if atype <> ctUnknown then
chead.CType := ChunkTypes[aType]
else
raise PNGImageException.create ('Doesn''t have a chunktype to write')
else
chead.CType := ReadType;
c := CalculateCRC (All1Bits, ReadType, sizeOf(ReadType));
c := CalculateCRC (c, data^, alength);
{$IFDEF ENDIAN_LITTLE}
crc := swap(c xor All1Bits);
{$ELSE}
crc := c xor All1Bits;
{$ENDIF}
with TheStream do
begin
Write (chead, sizeof(chead));
Write (data^[0], alength);
Write (crc, sizeof(crc));
end;
end;
end;
procedure TFPWriterPNG.SetChunkLength(aValue : longword);
begin
with Fchunk do
begin
alength := aValue;
if aValue > acapacity then
begin
if acapacity > 0 then
freemem (data);
GetMem (data, alength);
acapacity := alength;
end;
end;
end;
procedure TFPWriterPNG.SetChunkType (ct : TChunkTypes);
begin
with Fchunk do
begin
aType := ct;
ReadType := ChunkTypes[ct];
end;
end;
procedure TFPWriterPNG.SetChunkType (ct : TChunkCode);
begin
with FChunk do
begin
ReadType := ct;
aType := low(TChunkTypes);
while (aType < high(TChunkTypes)) and (ChunkTypes[aType] <> ct) do
inc (aType);
end;
end;
function TFPWriterPNG.CurrentLine(x:longword):byte;
begin
result := FCurrentLine^[x];
end;
function TFPWriterPNG.PrevSample (x:longword): byte;
begin
if x < byteWidth then
result := 0
else
result := FCurrentLine^[x - bytewidth];
end;
function TFPWriterPNG.PreviousLine (x:longword) : byte;
begin
result := FPreviousline^[x];
end;
function TFPWriterPNG.PrevLinePrevSample (x:longword): byte;
begin
if x < byteWidth then
result := 0
else
result := FPreviousLine^[x - bytewidth];
end;
function TFPWriterPNG.DoFilter(LineFilter:byte;index:longword; b:byte) : byte;
var diff : byte;
procedure FilterSub;
begin
diff := PrevSample(index);
end;
procedure FilterUp;
begin
diff := PreviousLine(index);
end;
procedure FilterAverage;
var l, p : word;
begin
l := PrevSample(index);
p := PreviousLine(index);
Diff := (l + p) div 2;
end;
procedure FilterPaeth;
var dl, dp, dlp : word; // index for previous and distances for:
l, p, lp : byte; // r:predictor, Left, Previous, LeftPrevious
r : integer;
begin
l := PrevSample(index);
lp := PrevLinePrevSample(index);
p := PreviousLine(index);
r := l + p - lp;
dl := abs (r - l);
dlp := abs (r - lp);
dp := abs (r - p);
if (dl <= dp) and (dl <= dlp) then
diff := l
else if dp <= dlp then
diff := p
else
diff := lp;
end;
begin
case LineFilter of
0 : diff := 0;
1 : FilterSub;
2 : FilterUp;
3 : FilterAverage;
4 : FilterPaeth;
end;
if diff > b then
result := (b + $100 - diff)
else
result := b - diff;
end;
procedure TFPWriterPNG.DetermineHeader (var AHeader : THeaderChunk);
var c : integer;
function CountAlphas : integer;
var none, half : boolean;
x,y : longint; // warning, checks on <0 !
p : integer;
c : TFPColor;
a : word;
begin
half := false;
none := false;
with TheImage do
if UsePalette then
with Palette do
begin
p := count-1;
FTransparentColor.alpha := alphaOpaque;
while (p >= 0) do
begin
c := color[p];
a := c.Alpha;
if a = alphaTransparent then
begin
none := true;
FTransparentColor := c;
end
else if a <> alphaOpaque then
begin
half := true;
if FtransparentColor.alpha < a then
FtransparentColor := c;
end;
dec (p);
end;
end
else
begin
x := width-1;
y := height-1;
FTransparentColor.alpha := alphaOpaque;
while (y >= 0) and not (half and none) do
begin
c := colors[x,y];
a := c.Alpha;
if a = alphaTransparent then
begin
none := true;
FTransparentColor := c;
end
else if a <> alphaOpaque then
begin
half := true;
if FtransparentColor.alpha < a then
FtransparentColor := c;
end;
dec (x);
if (x < 0) then
begin
dec (y);
x := width-1;
end;
end;
end;
result := 1;
if none then
inc (result);
if half then
inc (result);
end;
procedure DetermineColorFormat;
begin
with AHeader do
case colortype of
0 : if FWordSized then
begin
FFmtColor := @ColorDataGrayW;
FByteWidth := 2;
//CFmt := cfGray16
end
else
begin
FFmtColor := @ColorDataGrayB;
FByteWidth := 1;
//CFmt := cfGray8;
end;
2 : if FWordSized then
begin
FFmtColor := @ColorDataColorW;
FByteWidth := 6;
//CFmt := cfBGR48
end
else
begin
FFmtColor := @ColorDataColorB;
FByteWidth := 3;
//CFmt := cfBGR24;
end;
4 : if FWordSized then
begin
FFmtColor := @ColorDataGrayAW;
FByteWidth := 4;
//CFmt := cfGrayA32
end
else
begin
FFmtColor := @ColorDataGrayAB;
FByteWidth := 2;
//CFmt := cfGrayA16;
end;
6 : if FWordSized then
begin
FFmtColor := @ColorDataColorAW;
FByteWidth := 8;
//CFmt := cfABGR64
end
else
begin
FFmtColor := @ColorDataColorAB;
FByteWidth := 4;
//CFmt := cfABGR32;
end;
end;
end;
begin
with AHeader do
begin
{$IFDEF ENDIAN_LITTLE}
// problem: TheImage has integer width, PNG header longword width.
// Integer Swap can give negative value
Width := swap (longword(TheImage.Width));
height := swap (longword(TheImage.Height));
{$ELSE}
Width := TheImage.Width;
height := TheImage.Height;
{$ENDIF}
if FUseAlpha then
c := CountAlphas
else
c := 0;
if FIndexed then
begin
if OwnsPalette then FreeAndNil(FPalette);
OwnsPalette := not TheImage.UsePalette;
if OwnsPalette then
begin
FPalette := TFPPalette.Create (16);
FPalette.Build (TheImage);
end
else
FPalette := TheImage.Palette;
if ThePalette.count > 256 then
raise PNGImageException.Create ('Too many colors to use indexed PNG color type');
ColorType := 3;
FUsetRNS := C > 1;
BitDepth := 8;
FByteWidth := 1;
end
else
begin
if c = 3 then
ColorType := 4;
FUsetRNS := (c = 2);
if not FGrayScale then
ColorType := ColorType + 2;
if FWordSized then
BitDepth := 16
else
BitDepth := 8;
DetermineColorFormat;
end;
Compression := 0;
Filter := 0;
Interlace := 0;
end;
end;
procedure TFPWriterPNG.WriteIHDR;
begin
// signature for PNG
TheStream.writeBuffer(Signature,sizeof(Signature));
// Determine all settings for filling the header
DetermineHeader (FHeader);
// write the header chunk
SetChunkLength (13); // (sizeof(FHeader)); gives 14 and is wrong !!
move (FHeader, ChunkDataBuffer^, 13); // sizeof(FHeader));
SetChunkType (ctIHDR);
WriteChunk;
end;
{ Color convertions }
function TFPWriterPNG.ColorDataGrayB(color:TFPColor) : TColorData;
var t : word;
begin
t := CalculateGray (color);
result := hi(t);
end;
function TFPWriterPNG.ColorDataGrayW(color:TFPColor) : TColorData;
begin
result := CalculateGray (color);
end;
function TFPWriterPNG.ColorDataGrayAB(color:TFPColor) : TColorData;
begin
result := ColorDataGrayB (color);
result := (result shl 8) and hi(color.Alpha);
end;
function TFPWriterPNG.ColorDataGrayAW(color:TFPColor) : TColorData;
begin
result := ColorDataGrayW (color);
result := (result shl 16) and color.Alpha;
end;
function TFPWriterPNG.ColorDataColorB(color:TFPColor) : TColorData;
begin
with color do
result := hi(red) + (green and $FF00) + (hi(blue) shl 16);
end;
function TFPWriterPNG.ColorDataColorW(color:TFPColor) : TColorData;
begin
with color do
result := red + (green shl 16) + (qword(blue) shl 32);
end;
function TFPWriterPNG.ColorDataColorAB(color:TFPColor) : TColorData;
begin
with color do
result := hi(red) + (green and $FF00) + (hi(blue) shl 16) + (hi(alpha) shl 24);
end;
function TFPWriterPNG.ColorDataColorAW(color:TFPColor) : TColorData;
begin
with color do
result := red + (green shl 16) + (qword(blue) shl 32) + (qword(alpha) shl 48);
end;
{ Data making routines }
function TFPWriterPNG.GetColorPixel (x,y:longword) : TColorData;
begin
result := FFmtColor (TheImage[x,y]);
//result := ConvertColorToData(TheImage.Colors[x,y],CFmt);
end;
function TFPWriterPNG.GetPalettePixel (x,y:longword) : TColorData;
begin
result := TheImage.Pixels[x,y];
end;
function TFPWriterPNG.GetColPalPixel (x,y:longword) : TColorData;
begin
result := ThePalette.IndexOf (TheImage.Colors[x,y]);
end;
function TFPWriterPNG.DecideGetPixel : TGetPixelFunc;
begin
case Fheader.colortype of
3 : if TheImage.UsePalette then
begin
result := @GetPalettePixel;
end
else
begin
result := @GetColPalPixel;
end;
else begin
result := @GetColorPixel;
end
end;
end;
procedure TFPWriterPNG.WritePLTE;
var r,t : integer;
c : TFPColor;
begin
with ThePalette do
begin
SetChunkLength (count*3);
SetChunkType (ctPLTE);
t := 0;
For r := 0 to count-1 do
begin
c := Color[r];
ChunkdataBuffer^[t] := c.red div 256;
inc (t);
ChunkdataBuffer^[t] := c.green div 256;
inc (t);
ChunkdataBuffer^[t] := c.blue div 256;
inc (t);
end;
end;
WriteChunk;
end;
procedure TFPWriterPNG.InitWriteIDAT;
begin
FDatalineLength := TheImage.Width*ByteWidth;
GetMem (FPreviousLine, FDatalineLength);
GetMem (FCurrentLine, FDatalineLength);
fillchar (FCurrentLine^,FDatalineLength,0);
ZData := TMemoryStream.Create;
Compressor := TCompressionStream.Create (FCompressionLevel,ZData);
FGetPixel := DecideGetPixel;
end;
procedure TFPWriterPNG.FinalWriteIDAT;
begin
ZData.Free;
FreeMem (FPreviousLine);
FreeMem (FCurrentLine);
end;
function TFPWriterPNG.DetermineFilter (Current, Previous:PByteArray; linelength:longword) : byte;
begin
result := 0;
end;
procedure TFPWriterPNG.FillScanLine (y : integer; ScanLine : pByteArray);
var r, x : integer;
cd : TColorData;
index : longword;
b : byte;
begin
index := 0;
for x := 0 to pred(TheImage.Width) do
begin
cd := FGetPixel (x,y);
{$IFDEF ENDIAN_BIG}
cd:=swap(cd);
{$ENDIF}
move (cd, ScanLine^[index], FBytewidth);
if WordSized then
begin
r := 1;
while (r < FByteWidth) do
begin
b := Scanline^[index+r];
Scanline^[index+r] := Scanline^[index+r-1];
Scanline^[index+r-1] := b;
inc (r,2);
end;
end;
inc (index, FByteWidth);
end;
end;
procedure TFPWriterPNG.GatherData;
var x,y : integer;
lf : byte;
begin
for y := 0 to pred(TheImage.height) do
begin
FSwitchLine := FCurrentLine;
FCurrentLine := FPreviousLine;
FPreviousLine := FSwitchLine;
FillScanLine (y, FCurrentLine);
lf := DetermineFilter (FCurrentLine, FpreviousLine, FDataLineLength);
for x := 0 to FDatalineLength-1 do
FCurrentLine^[x] := DoFilter (lf, x, FCurrentLine^[x]);
Compressor.Write (lf, sizeof(lf));
Compressor.Write (FCurrentLine^, FDataLineLength);
end;
end;
procedure TFPWriterPNG.WriteCompressedData;
var l : longword;
begin
Compressor.Free; // Close compression and finish the writing in ZData
l := ZData.position;
ZData.position := 0;
SetChunkLength(l);
SetChunkType (ctIDAT);
ZData.Read (ChunkdataBuffer^, l);
WriteChunk;
end;
procedure TFPWriterPNG.WriteIDAT;
begin
InitWriteIDAT;
GatherData;
WriteCompressedData;
FinalWriteIDAT;
end;
procedure TFPWriterPNG.WritetRNS;
procedure PaletteAlpha;
var r : integer;
begin
with TheImage.palette do
begin
// search last palette entry with transparency
r := count;
repeat
dec (r);
until (r < 0) or (color[r].alpha <> alphaOpaque);
if r >= 0 then // there is at least 1 transparent color
begin
// from this color we go to the first palette entry
SetChunkLength (r+1);
repeat
chunkdatabuffer^[r] := (color[r].alpha shr 8);
dec (r);
until (r < 0);
end;
writechunk;
end;
end;
procedure GrayAlpha;
var g : word;
begin
SetChunkLength(2);
if WordSized then
g := CalculateGray (SingleTransparentColor)
else
g := hi (CalculateGray(SingleTransparentColor));
{$IFDEF ENDIAN_LITTLE}
g := swap (g);
{$ENDIF}
move (g,ChunkDataBuffer^[0],2);
WriteChunk;
end;
procedure ColorAlpha;
var g : TFPColor;
begin
SetChunkLength(6);
g := SingleTransparentColor;
with g do
if WordSized then
begin
{$IFDEF ENDIAN_LITTLE}
red := swap (red);
green := swap (green);
blue := swap (blue);
{$ENDIF}
move (g, ChunkDatabuffer^[0], 6);
end
else
begin
ChunkDataBuffer^[0] := 0;
ChunkDataBuffer^[1] := red shr 8;
ChunkDataBuffer^[2] := 0;
ChunkDataBuffer^[3] := green shr 8;
ChunkDataBuffer^[4] := 0;
ChunkDataBuffer^[5] := blue shr 8;
end;
WriteChunk;
end;
begin
SetChunkType (cttRNS);
case fheader.colortype of
6,4 : raise PNGImageException.create ('tRNS chunk forbidden for full alpha channels');
3 : PaletteAlpha;
2 : ColorAlpha;
0 : GrayAlpha;
end;
end;
procedure TFPWriterPNG.WriteTexts;
begin
end;
procedure TFPWriterPNG.WriteIEND;
begin
SetChunkLength(0);
SetChunkType (ctIEND);
WriteChunk;
end;
procedure TFPWriterPNG.InternalWrite (Str:TStream; Img:TFPCustomImage);
begin
WriteIHDR;
if Fheader.colorType = 3 then
WritePLTE;
if FUsetRNS then
WritetRNS;
WriteIDAT;
WriteTexts;
WriteIEND;
end;
initialization
ImageHandlers.RegisterImageWriter ('Portable Network Graphics', 'png', TFPWriterPNG);
end.
|