forked from e-delphi/VCL2FMX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.pas
113 lines (97 loc) · 2.9 KB
/
Image.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
{**********************************************}
{ }
{ Eduardo Rodrigues }
{ 18/09/2019 }
{ }
{**********************************************}
unit Image;
interface
uses
Vcl.Imaging.PngImage;
function CreateGlyphPng(const AData: String; ANumGlyphs: Integer): TPngImage;
function EncodePicture(APng: TPngImage; const APad: String): String;
function ConvertPicture(const AData, APad: String; out AWidth, AHeight: Integer): String;
implementation
uses
System.Classes, System.SysUtils, Vcl.Graphics, Vcl.Imaging.Jpeg, Vcl.Imaging.GIFImg, PatchLib;
type
TGraphicAccess = class(Vcl.Graphics.TGraphic)
end;
function CreateGlyphPng(const AData: String; ANumGlyphs: Integer): TPngImage;
const
StreamLenghtFieldLen = 4;
var
GlyphBmp: TBitmap;
Stream: TMemoryStream;
begin
GlyphBmp := nil;
Stream := nil;
try
GlyphBmp := TBitmap.Create;
Stream := TMemoryStream.Create;
HexToStream(AData, Stream);
Stream.Position := StreamLenghtFieldLen;
GlyphBmp.LoadFromStream(Stream);
Result := TPngImage.CreateBlank(COLOR_RGB, 8, GlyphBmp.Width div ANumGlyphs, GlyphBmp.Height);
Result.Canvas.Draw(0, 0, GlyphBmp);
finally
GlyphBmp.Free;
Stream.Free;
end;
end;
function EncodePicture(APng: TPngImage; const APad: String): String;
var
Stream: TMemoryStream;
begin
Stream := TMemoryStream.Create;
try
APng.SaveToStream(Stream);
Result := '{' + BreakIntoLines(StreamToHex(Stream), APad) + '}';
finally
Stream.Free;
end;
end;
function ConvertPicture(const AData, APad: String; out AWidth, AHeight: Integer): String;
var
Stream: TMemoryStream;
GraphClassName: ShortString;
Graphic: TGraphic;
Png: TPngImage;
begin
Graphic := nil;
Png := nil;
Stream := TMemoryStream.Create;
try
HexToStream(AData, Stream);
GraphClassName := PShortString(Stream.Memory)^;
Graphic := TGraphicClass(FindClass(UTF8ToString(GraphClassName))).Create;
Stream.Position := 1 + Length(GraphClassName);
TGraphicAccess(Graphic).ReadData(Stream);
if (Graphic is TPngImage) or (Graphic is TBitmap) or (Graphic is TWICImage) then
begin
Png := TPngImage.Create;
Png.Assign(Graphic);
end
else
begin
Png := TPngImage.CreateBlank(COLOR_RGB, 8, Graphic.Width, Graphic.Height);
Png.Canvas.Draw(0, 0, Graphic, 255);
end;
Result := EncodePicture(Png, APad);
AHeight := Graphic.Height;
AWidth := Graphic.Width;
finally
Stream.Free;
Png.Free;
Graphic.Free;
end;
end;
initialization
System.Classes.RegisterClass(TMetafile);
System.Classes.RegisterClass(TIcon);
System.Classes.RegisterClass(TBitmap);
System.Classes.RegisterClass(TWICImage);
System.Classes.RegisterClass(TJpegImage);
System.Classes.RegisterClass(TGifImage);
System.Classes.RegisterClass(TPngImage);
end.