-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdwsGraphicLibrary.pas
More file actions
438 lines (398 loc) · 14.3 KB
/
Copy pathdwsGraphicLibrary.pas
File metadata and controls
438 lines (398 loc) · 14.3 KB
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
{**********************************************************************}
{ }
{ "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. }
{ }
{ Copyright Creative IT. }
{ Current maintainer: Eric Grange }
{ }
{**********************************************************************}
unit dwsGraphicLibrary;
{$I dws.inc}
{$define USE_LIB_JPEG}
interface
uses
Classes, SysUtils, Graphics,
{$ifdef USE_LIB_JPEG}
dwsJPEGEncoder,
{$else}
JPEG,
{$endif}
PNGImage,
dwsXPlatform, dwsUtils, dwsStrings,
dwsFunctions, dwsSymbols, dwsExprs, dwsCoreExprs, dwsExprList, dwsUnitSymbols,
dwsConstExprs, dwsMagicExprs, dwsDataContext;
const
SYS_PIXMAP = 'TPixmap';
SYS_TJPEGOption = 'TJPEGOption';
SYS_TJPEGOptions = 'TJPEGOptions';
type
TPixmapToJPEGDataFunc = class(TInternalMagicStringFunction)
procedure DoEvalAsString(const args : TExprBaseListExec; var Result : UnicodeString); override;
end;
TPNGDataToPixmapFunc = class(TInternalMagicVariantFunction)
procedure DoEvalAsVariant(const args : TExprBaseListExec; var result : Variant); override;
end;
TPixmapToPNGDataFunc = class(TInternalMagicStringFunction)
procedure DoEvalAsString(const args : TExprBaseListExec; var Result : UnicodeString); override;
end;
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
implementation
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
type
TRGB24 = record r, g, b : Byte; end;
PRGB24 = ^TRGB24;
TRGB32 = record r, g, b, a : Byte; end;
PRGB32 = ^TRGB32;
// RegisterGraphicsTypes
//
procedure RegisterGraphicsTypes(systemTable : TSystemSymbolTable; unitSyms : TUnitMainSymbols;
unitTable : TSymbolTable);
var
typPixmap : TDynamicArraySymbol;
jpgOption : TEnumerationSymbol;
jpgOptions : TSetOfSymbol;
begin
if unitTable.FindLocal(SYS_PIXMAP)<>nil then exit;
typPixmap:=TDynamicArraySymbol.Create(SYS_PIXMAP, systemTable.TypInteger, systemTable.TypInteger);
unitTable.AddSymbol(typPixmap);
jpgOption:=TEnumerationSymbol.Create(SYS_TJPEGOption, systemTable.TypInteger, enumScoped);
unitTable.AddSymbol(jpgOption);
jpgOption.AddElement(TElementSymbol.Create('Optimize', jpgOption, Ord(jpgoOptimize), False));
jpgOption.AddElement(TElementSymbol.Create('NoJFIFHeader', jpgOption, Ord(jpgoNoJFIFHeader), False));
jpgOption.AddElement(TElementSymbol.Create('Progressive', jpgOption, Ord(jpgoProgressive), False));
jpgOptions:=TSetOfSymbol.Create(SYS_TJPEGOptions, jpgOption, jpgOption.LowBound, jpgOption.HighBound);
unitTable.AddSymbol(jpgOptions);
end;
// ------------------
// ------------------ TPixmapToJPEGDataFunc ------------------
// ------------------
// DoEvalAsString
//
procedure TPixmapToJPEGDataFunc.DoEvalAsString(const args : TExprBaseListExec; var Result : UnicodeString);
var
w, h, quality : Integer;
jpegOptions : TJPEGOptions;
pixmap : IScriptDynArray;
{$ifdef USE_LIB_JPEG}
procedure CompressWithLibJPEG(var Result : UnicodeString);
var
buf : array of TRGB24;
i, x, y, c : Integer;
outBuf : RawByteString;
begin
SetLength(buf, w*h*3);
i:=0;
for y:=0 to h-1 do begin
for x:=0 to w-1 do begin
c:=pixmap.AsInteger[i];
buf[i]:=PRGB24(@c)^;
Inc(i);
end;
end;
outBuf:=CompressJPEG(Pointer(buf), w, h, quality, jpegOptions);
SetLength(buf, 0);
Result:=RawByteStringToScriptString(outBuf);
end;
{$else}
procedure CompressWithTJPEGImage(var Result : UnicodeString);
var
i, x, y, c : Integer;
bmp : TBitmap;
jpg : TJPEGImage;
scanLine : PRGB24;
buf : TWriteOnlyBlockStream;
begin
bmp:=TBitmap.Create;
try
bmp.PixelFormat:=pf24bit;
bmp.Width:=w;
bmp.Height:=h;
i:=0;
for y:=0 to h-1 do begin
scanLine:=bmp.ScanLine[y];
for x:=0 to w-1 do begin
c:=dynArray.AsInteger[i];
scanLine^:=PRGB24(@c)^;
Inc(i);
Inc(scanLine);
end;
end;
jpg:=TJPEGImage.Create;
buf:=TWriteOnlyBlockStream.AllocFromPool;
try
jpg.Assign(bmp);
jpg.CompressionQuality:=quality;
if jpgoOp
jpg.SaveToStream(buf);
Result:=RawByteStringToScriptString(buf.ToRawBytes);
finally
buf.ReturnToPool;
jpg.Free;
end;
finally
bmp.Free;
end;
end;
{$endif}
var
opts : Integer;
jpegOption : TJPEGOption;
begin
args.ExprBase[0].EvalAsScriptDynArray(args.Exec, pixmap);
w:=args.AsInteger[1];
h:=args.AsInteger[2];
if w*h>pixmap.ArrayLength then
raise Exception.Create('Not enough data in pixmap');
quality:=args.AsInteger[3];
opts:=StrToIntDef(args.AsString[4], 0);
jpegOptions:=[];
for jpegOption:=Low(TJPEGOption) to High(TJPEGOption) do
if (opts and (1 shl Ord(jpegOption)))<>0 then
Include(jpegOptions, jpegOption);
{$ifdef USE_LIB_JPEG}
CompressWithLibJPEG(Result);
{$else}
CompressWithTJPEGImage(Result);
{$endif}
end;
// ------------------
// ------------------ TPNGDataToPixmapFunc ------------------
// ------------------
// DoEvalAsVariant
//
procedure TPNGDataToPixmapFunc.DoEvalAsVariant(const args : TExprBaseListExec; var result : Variant);
procedure CopyRGBtoBGR(pixmap : PVarData; pngScan : PRGB24); inline;
begin
pixmap.VType:=varInt64;
PRGB24(@pixmap.VInt64).r:=pngScan.b;
PRGB24(@pixmap.VInt64).g:=pngScan.g;
PRGB24(@pixmap.VInt64).b:=pngScan.r;
end;
procedure CopyRowDefault(pixmap : PVarData; png : TPngImage; y : Integer);
var
x : Integer;
begin
for x:=0 to png.Width-1 do begin
pixmap.VType:=varInt64;
pixmap.VInt64:=png.Pixels[x, y];
PByte(@pixmap.VInt64)[3]:=255;
Inc(pixmap);
end;
end;
procedure CopyRowDefaultBitAlpha(pixmap : PVarData; png : TPngImage; y : Integer);
var
x : Integer;
col, trCol : TColor;
begin
trCol:=png.TransparentColor;
for x:=0 to png.Width-1 do begin
pixmap.VType:=varInt64;
col:=png.Pixels[x, y];
pixmap.VInt64:=col;
if col=trCol then
PByte(@pixmap.VInt64)[3]:=0
else PByte(@pixmap.VInt64)[3]:=255;
Inc(pixmap);
end;
end;
procedure CopyRowPalettedPartialAlpha(pixmap : PVarData; png : TPngImage; y : Integer);
var
x : Integer;
alphaScan : PByteArray;
trns : TChunktRNS;
begin
trns:=png.Chunks.ItemFromClass(TChunkTRNS) as TChunktRNS;
alphaScan:=png.AlphaScanline[y];
if alphaScan=nil then
alphaScan:=png.Scanline[y];
for x:=0 to png.Width-1 do begin
pixmap.VType:=varInt64;
pixmap.VInt64:=png.Pixels[x, y];
PByte(@pixmap.VInt64)[3]:=trns.PaletteValues[alphaScan[x]];
Inc(pixmap);
end;
end;
procedure CopyRow24(pixmap : PVarData; pngScan : PRGB24; n : Integer);
var
x : Integer;
begin
for x:=1 to n do begin
CopyRGBtoBGR(pixmap, pngScan);
PByte(@pixmap.VInt64)[3]:=255;
Inc(pixmap);
Inc(pngScan);
end;
end;
procedure CopyRow32(pixmap : PVarData; pngScan : PRGB24; alphaScan : PByte; n : Integer);
var
x : Integer;
begin
for x:=0 to n-1 do begin
CopyRGBtoBGR(pixmap, pngScan);
PByte(@pixmap^.VInt64)[3]:=alphaScan[x];
Inc(pixmap);
Inc(pngScan);
end;
end;
var
data : RawByteString;
stream : TMemoryStream;
png : TPngImage;
pixmapData : TData;
pixmap : IScriptDynArray;
y, w, h : Integer;
withAlpha : Boolean;
begin
data:=args.AsDataString[0];
stream:=TMemoryStream.Create;
try
stream.Write(Pointer(data)^, Length(data));
stream.Position:=0;
data:='';
png:=TPngImage.Create;
try
png.LoadFromStream(stream);
stream.Clear;
w:=png.Width;
h:=png.Height;
SetLength(pixmapData, w*h);
withAlpha:=args.AsBoolean[1];
case png.Header.ColorType of
COLOR_RGBALPHA : begin
if withAlpha then begin
png.CreateAlpha;
for y:=0 to h-1 do
CopyRow32(@pixmapData[y*w], png.Scanline[y], PByte(png.AlphaScanline[y]), w);
end else begin
for y:=0 to h-1 do
CopyRow24(@pixmapData[y*w], png.Scanline[y], w);
end;
end;
else
if withAlpha and (png.TransparencyMode<>ptmNone) then begin
if png.TransparencyMode=ptmBit then begin
for y:=0 to h-1 do
CopyRowDefaultBitAlpha(@pixmapData[y*w], png, y);
end else begin
for y:=0 to h-1 do
CopyRowPalettedPartialAlpha(@pixmapData[y*w], png, y);
end;
end else begin
for y:=0 to h-1 do
CopyRowDefault(@pixmapData[y*w], png, y);
end;
end;
pixmap:=TScriptDynamicArray.CreateNew((args.Exec as TdwsProgramExecution).CompilerContext.TypInteger);
pixmap.ReplaceData(pixmapData);
finally
png.Free;
end;
finally
stream.Free;
end;
args.AsInteger[2]:=w;
args.AsInteger[3]:=h;
result:=pixmap;
end;
// ------------------
// ------------------ TPixmapToPNGDataFunc ------------------
// ------------------
// DoEvalAsString
//
procedure TPixmapToPNGDataFunc.DoEvalAsString(const args : TExprBaseListExec; var Result : UnicodeString);
var
w, h : Integer;
pixmap : IScriptDynArray;
procedure CompressWithPNGImage(var Result : UnicodeString; withAlpha : Boolean);
var
i, x, y : Integer;
bmp : TBitmap;
scanLine24 : PRGB24;
c : TRGB32;
alphaChannel : array of Byte;
alphaPtr : PByte;
buf : TWriteOnlyBlockStream;
png : TPngImage;
begin
buf:=TWriteOnlyBlockStream.AllocFromPool;
try
png:=TPngImage.Create;
try
bmp:=TBitmap.Create;
try
bmp.PixelFormat:=pf24bit;
bmp.Width:=w;
bmp.Height:=h;
if withAlpha then
SetLength(alphaChannel, w*h);
alphaPtr:=PByte(alphaChannel);
i:=0;
for y:=0 to h-1 do begin
scanLine24:=bmp.ScanLine[y];
for x:=0 to w-1 do begin
PInteger(@c)^:=pixmap.AsInteger[i];
scanLine24^.r:=c.b;
scanLine24^.g:=c.g;
scanLine24^.b:=c.r;
Inc(i);
Inc(scanLine24);
if withAlpha then begin
alphaPtr^:=c.a;
Inc(alphaPtr);
end;
end;
end;
png.Assign(bmp);
finally
bmp.Free;
end;
if withAlpha then begin
png.CreateAlpha;
for y:=0 to h-1 do
System.Move(alphaChannel[y*w], png.AlphaScanline[y]^, w);
end;
png.SaveToStream(buf);
finally
png.Free;
end;
Result:=RawByteStringToScriptString(buf.ToRawBytes);
finally
buf.ReturnToPool;
end;
end;
begin
args.ExprBase[0].EvalAsScriptDynArray(args.Exec, pixmap);
w:=args.AsInteger[1];
h:=args.AsInteger[2];
if w*h>pixmap.ArrayLength then
raise Exception.Create('Not enough data in pixmap');
CompressWithPNGImage(Result, args.AsBoolean[3]);
end;
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
initialization
// ------------------------------------------------------------------
// ------------------------------------------------------------------
// ------------------------------------------------------------------
dwsInternalUnit.AddSymbolsRegistrationProc(RegisterGraphicsTypes);
RegisterInternalStringFunction(TPixmapToJPEGDataFunc, 'PixmapToJPEGData',
['pixmap', SYS_PIXMAP, 'width', SYS_INTEGER, 'height', SYS_INTEGER,
'quality=90', SYS_INTEGER, 'options=', SYS_TJPEGOptions], []);
RegisterInternalFunction(TPNGDataToPixmapFunc, 'PNGDataToPixmap',
['pngData', SYS_STRING, 'withAlpha=False', SYS_BOOLEAN, '@width', SYS_INTEGER, '@height', SYS_INTEGER], SYS_PIXMAP, []);
RegisterInternalStringFunction(TPixmapToPNGDataFunc, 'PixmapToPNGData',
['pixmap', SYS_PIXMAP, 'width', SYS_INTEGER, 'height', SYS_INTEGER, 'withAlpha=False', SYS_BOOLEAN], []);
end.