-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcompress2png.pas
92 lines (75 loc) · 2.13 KB
/
compress2png.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
unit compress2png;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics, GraphType, IntfGraphics, FPImage, zstream;
function CompressToPNG(Data: PByteArray; DataSize: Integer; OutputFile: ansistring; MaxWidth: Integer = 4096): Boolean;
implementation
const
ByteDepth = 1;
function CompressToPNG(Data: PByteArray; DataSize: Integer; OutputFile: ansistring; MaxWidth: Integer): Boolean;
var
i, width, height: Integer;
c: TFPColor;
pic: TLazIntfImage;
lRawImage: TRawImage;
pngWriter: TLazWriterPNG;
function feek(input: Byte): Word;
begin
result:=input * $100;
end;
begin
result:=False;
height:=1;
while DataSize div Height > MaxWidth do height:=Height + 1;
width:=DataSize div Height;
while DataSize > width*height do Inc(Height);
lRawImage.Init;
lRawImage.Description.Init_BPP24_B8G8R8_M1_BIO_TTB (width, Height);
lRawImage.Description.BitsPerPixel:=32;
lRawImage.Description.AlphaPrec:=8;
lRawImage.Description.BluePrec:=8;
lRawImage.Description.RedPrec:=8;
lRawImage.Description.BluePrec:=8;
lRawImage.CreateData(True);
pic:=TLazIntfImage.Create(0, 0);
pic.SetRawImage(lRawImage);
for i:=0 to DataSize - 1 do
begin
//for j:=ByteDepth-1 downto 0 do
//cc:=cc*$100 + data[i * ByteDepth + j];
c.red :=feek(data^[i * ByteDepth + 0]);
c.green :=feek(data^[i * ByteDepth + 0]);
c.blue :=feek(data^[i * ByteDepth + 0]);
c.alpha :=feek(255);
pic.Colors[i mod width, i div width]:=c;
end;
c:=TColorToFPColor(0);
c.red :=feek(255);
c.green :=feek(255);
c.blue :=feek(255);
c.alpha :=feek(255);
for i:=DataSize to Width*height -1 do
begin
pic.Colors[i mod width, i div width]:=c;
end;
pngWriter:=TLazWriterPNG.create;
pngWriter.CompressionLevel:=clmax;
pngWriter.UseAlpha:=False;
pngWriter.Indexed:=False;
pngWriter.WordSized:=False;
pngWriter.GrayScale:=True;
pic.SaveToFile(OutputFile, pngWriter);
pngWriter.Free;
(*
with TPortableNetworkGraphic.Create do begin
try
LoadFromIntfImage(Pic);
SaveToFile(OutputFile);
finally
Free;
end;
end; *)
pic.Free;
end;
end.