1 | unit BGRAThumbnail;
|
---|
2 |
|
---|
3 | {$mode objfpc}{$H+}
|
---|
4 | {$i bgrabitmap.inc}
|
---|
5 |
|
---|
6 | interface
|
---|
7 |
|
---|
8 | uses
|
---|
9 | Classes, SysUtils, BGRABitmap, BGRABitmapTypes, FPimage;
|
---|
10 |
|
---|
11 | function GetBitmapThumbnail(ABitmap: TBGRACustomBitmap; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil; AVerticalShrink: single = 1; AHorizShrink: single = 1): TBGRABitmap; overload;
|
---|
12 | function GetBitmapThumbnail(ABitmap: TBGRACustomBitmap; AFormat: TBGRAImageFormat; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil; AVerticalShrink: single = 1; AHorizShrink: single = 1): TBGRABitmap; overload;
|
---|
13 | function GetFileThumbnail(AFilenameUTF8: string; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
14 | function GetStreamThumbnail(AStream: TStream; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ASuggestedExtensionUTF8: string = ''; ADest: TBGRABitmap= nil): TBGRABitmap; overload;
|
---|
15 | function GetStreamThumbnail(AStream: TStream; AReader: TFPCustomImageReader; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; overload;
|
---|
16 |
|
---|
17 | function GetOpenRasterThumbnail(AStream: TStream; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
18 | function GetLazPaintThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
19 | function GetPhoxoThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
20 | function GetJpegThumbnail(AStream: TStream; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
21 | function GetPsdThumbnail(AStream: TStream; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
22 | function GetPngThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
23 | function GetPaintDotNetThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
24 | function GetBmpThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
25 | function GetIcoThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
26 | function GetCurThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
27 |
|
---|
28 | function GetPcxThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
29 | function GetTargaThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
30 | function GetTiffThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
31 | function GetGifThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
32 | function GetXwdThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
33 | function GetXPixMapThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
34 | function GetBmpMioMapThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
35 |
|
---|
36 | procedure DrawThumbnailCheckers(bmp: TBGRABitmap; ARect: TRect; AIconCheckers: boolean = false);
|
---|
37 |
|
---|
38 | var
|
---|
39 | ImageCheckersColor1,ImageCheckersColor2 : TBGRAPixel;
|
---|
40 | IconCheckersColor1,IconCheckersColor2 : TBGRAPixel;
|
---|
41 |
|
---|
42 | implementation
|
---|
43 |
|
---|
44 | uses Types, base64, BGRAUTF8,
|
---|
45 | DOM, XMLRead, BGRAReadJPEG, BGRAReadPng, BGRAReadGif, BGRAReadBMP,
|
---|
46 | BGRAReadPSD, BGRAReadIco, UnzipperExt, BGRAReadLzp;
|
---|
47 |
|
---|
48 | procedure DrawThumbnailCheckers(bmp: TBGRABitmap; ARect: TRect; AIconCheckers: boolean);
|
---|
49 | begin
|
---|
50 | if AIconCheckers then
|
---|
51 | bmp.DrawCheckers(ARect, IconCheckersColor1, IconCheckersColor2)
|
---|
52 | else
|
---|
53 | bmp.DrawCheckers(ARect, ImageCheckersColor1, ImageCheckersColor2);
|
---|
54 | end;
|
---|
55 |
|
---|
56 | function InternalGetBitmapThumbnail(ABitmap: TBGRACustomBitmap; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean;
|
---|
57 | ADest: TBGRABitmap; AVerticalShrink: single = 1; AHorizShrink: single = 1; AShowHotSpot: boolean = false; ADarkCheckers: boolean = false): TBGRABitmap;
|
---|
58 | var
|
---|
59 | factorX, factorY, factor: single;
|
---|
60 | xIcon,yIcon,wIcon,hIcon: Integer;
|
---|
61 | hotspot: TPoint;
|
---|
62 | begin
|
---|
63 | result := nil;
|
---|
64 | try
|
---|
65 | if (ABitmap <> nil) and (ABitmap.Width <> 0) and (ABitmap.Height <> 0) then
|
---|
66 | begin
|
---|
67 | If Assigned(ADest) then
|
---|
68 | begin
|
---|
69 | result := ADest;
|
---|
70 | result.SetSize(AWidth,AHeight);
|
---|
71 | result.Fill(ABackColor);
|
---|
72 | end else
|
---|
73 | result := TBGRABitmap.Create(AWidth,AHeight,ABackColor);
|
---|
74 | factorX := result.Width/(ABitmap.Width*AHorizShrink);
|
---|
75 | factorY := result.Height/(ABitmap.Height*AVerticalShrink);
|
---|
76 | if factorX < factorY then factor := factorX else factor := factorY;
|
---|
77 | wIcon := round(ABitmap.Width*AHorizShrink*factor);
|
---|
78 | if wIcon = 0 then wIcon := 1;
|
---|
79 | hIcon := round(ABitmap.Height*AVerticalShrink*factor);
|
---|
80 | if hIcon = 0 then hIcon := 1;
|
---|
81 | xIcon:= (result.Width-wIcon) div 2;
|
---|
82 | yIcon:= (result.Height-hIcon) div 2;
|
---|
83 | if ACheckers then DrawThumbnailCheckers(result,Rect(xIcon,yIcon,xIcon+wIcon,yIcon+hIcon),ADarkCheckers);
|
---|
84 | if AShowHotSpot and (wIcon > 0) and (hIcon > 0) then
|
---|
85 | begin
|
---|
86 | hotspot := Point(xIcon+ABitmap.HotSpot.X*wIcon div ABitmap.Width,yIcon+ABitmap.HotSpot.Y*hIcon div ABitmap.Height);
|
---|
87 | result.HorizLine(xIcon,hotspot.y-1,xIcon+wIcon-1,CSSLime,dmDrawWithTransparency);
|
---|
88 | result.HorizLine(xIcon,hotspot.y,xIcon+wIcon-1,CSSLime,dmDrawWithTransparency);
|
---|
89 | result.HorizLine(xIcon,hotspot.y+1,xIcon+wIcon-1,CSSLime,dmDrawWithTransparency);
|
---|
90 | result.VertLine(hotspot.x-1,yIcon,yIcon+hIcon-1,CSSLime,dmDrawWithTransparency);
|
---|
91 | result.VertLine(hotspot.x,yIcon,yIcon+hIcon-1,CSSLime,dmDrawWithTransparency);
|
---|
92 | result.VertLine(hotspot.x+1,yIcon,yIcon+hIcon-1,CSSLime,dmDrawWithTransparency);
|
---|
93 | end;
|
---|
94 | if (ABackColor.alpha <> 0) or ACheckers then
|
---|
95 | result.StretchPutImage(Rect(xIcon,yIcon,xIcon+wIcon,yIcon+hIcon),ABitmap,dmDrawWithTransparency) else
|
---|
96 | result.StretchPutImage(Rect(xIcon,yIcon,xIcon+wIcon,yIcon+hIcon),ABitmap,dmSet);
|
---|
97 | if AShowHotSpot and (wIcon > 0) and (hIcon > 0) then
|
---|
98 | begin
|
---|
99 | result.HorizLine(xIcon,yIcon+ABitmap.HotSpot.Y*hIcon div ABitmap.Height,xIcon+wIcon-1,BGRA(255,0,255,96),dmDrawWithTransparency);
|
---|
100 | result.VertLine(xIcon+ABitmap.HotSpot.X*wIcon div ABitmap.Width,yIcon,yIcon+hIcon-1,BGRA(255,0,255,96),dmDrawWithTransparency);
|
---|
101 | end;
|
---|
102 | end;
|
---|
103 | except
|
---|
104 | end;
|
---|
105 | end;
|
---|
106 |
|
---|
107 | function GetBitmapThumbnail(ABitmap: TBGRACustomBitmap; AWidth, AHeight: integer;
|
---|
108 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap = nil;
|
---|
109 | AVerticalShrink: single = 1; AHorizShrink: single = 1): TBGRABitmap;
|
---|
110 | begin
|
---|
111 | result := InternalGetBitmapThumbnail(ABitmap,AWidth,AHeight,ABackColor,ACheckers,ADest,AVerticalShrink,AHorizShrink,
|
---|
112 | false,false);
|
---|
113 | end;
|
---|
114 |
|
---|
115 | function GetBitmapThumbnail(ABitmap: TBGRACustomBitmap; AFormat: TBGRAImageFormat; AWidth, AHeight: integer;
|
---|
116 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap = nil; AVerticalShrink: single = 1; AHorizShrink: single = 1): TBGRABitmap;
|
---|
117 | begin
|
---|
118 | result := InternalGetBitmapThumbnail(ABitmap,AWidth,AHeight,ABackColor,ACheckers,ADest,AVerticalShrink,AHorizShrink,
|
---|
119 | AFormat = ifCur, AFormat in[ifCur,ifIco]);
|
---|
120 |
|
---|
121 | end;
|
---|
122 |
|
---|
123 | function GetFileThumbnail(AFilenameUTF8: string; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
124 | var stream: TFileStreamUTF8;
|
---|
125 | begin
|
---|
126 | result := nil;
|
---|
127 | try
|
---|
128 | stream := TFileStreamUTF8.Create(AFilenameUTF8,fmOpenRead or fmShareDenyWrite);
|
---|
129 | except
|
---|
130 | exit;
|
---|
131 | end;
|
---|
132 | try
|
---|
133 | result := GetStreamThumbnail(stream, AWidth,AHeight,ABackColor,ACheckers,ExtractFileExt(AFilenameUTF8),ADest);
|
---|
134 | finally
|
---|
135 | stream.free;
|
---|
136 | end;
|
---|
137 | end;
|
---|
138 |
|
---|
139 | function GetStreamThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
140 | ABackColor: TBGRAPixel; ACheckers: boolean; ASuggestedExtensionUTF8: string;
|
---|
141 | ADest: TBGRABitmap): TBGRABitmap;
|
---|
142 | var
|
---|
143 | ff: TBGRAImageFormat;
|
---|
144 | reader: TFPCustomImageReader;
|
---|
145 | begin
|
---|
146 | ff := DetectFileFormat(AStream,ASuggestedExtensionUTF8);
|
---|
147 | case ff of
|
---|
148 | ifJpeg: result := GetJpegThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
149 | ifIco: result := GetIcoThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
150 | ifCur: result := GetCurThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
151 | ifPaintDotNet: result := GetPaintDotNetThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
152 | ifLazPaint: result := GetLazPaintThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
153 | ifOpenRaster: result := GetOpenRasterThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
154 | ifPhoxo: result := GetPhoxoThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
155 | ifPsd: result := GetPsdThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
156 | else
|
---|
157 | begin
|
---|
158 | if (ff = ifUnknown) or (DefaultBGRAImageReader[ff] = nil) then
|
---|
159 | result := nil
|
---|
160 | else
|
---|
161 | begin
|
---|
162 | result := nil;
|
---|
163 | reader := nil;
|
---|
164 | try
|
---|
165 | reader := CreateBGRAImageReader(ff);
|
---|
166 | result := GetStreamThumbnail(AStream, reader, AWidth, AHeight, ABackColor, ACheckers, ADest);
|
---|
167 | finally
|
---|
168 | reader.Free;
|
---|
169 | end;
|
---|
170 | end;
|
---|
171 | end;
|
---|
172 | end;
|
---|
173 | end;
|
---|
174 |
|
---|
175 | function GetStreamThumbnail(AStream: TStream; AReader: TFPCustomImageReader;
|
---|
176 | AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean;
|
---|
177 | ADest: TBGRABitmap): TBGRABitmap;
|
---|
178 | var
|
---|
179 | bmp: TBGRACustomBitmap;
|
---|
180 | AOriginalWidth, AOriginalHeight: integer;
|
---|
181 | begin
|
---|
182 | if AReader is TBGRAImageReader then
|
---|
183 | begin
|
---|
184 | bmp := nil;
|
---|
185 | try
|
---|
186 | bmp := TBGRAImageReader(AReader).GetBitmapDraft(AStream, AWidth,AHeight, AOriginalWidth,AOriginalHeight);
|
---|
187 | if Assigned(bmp) and (bmp.Height <> 0) and (bmp.Width <> 0) then
|
---|
188 | result := GetBitmapThumbnail(bmp, AWidth, AHeight, ABackColor, ACheckers, ADest,
|
---|
189 | AOriginalHeight/bmp.Height, AOriginalWidth/bmp.Width);
|
---|
190 | except
|
---|
191 | result := nil;
|
---|
192 | end;
|
---|
193 | bmp.free;
|
---|
194 | exit;
|
---|
195 | end;
|
---|
196 |
|
---|
197 | bmp := TBGRABitmap.Create;
|
---|
198 | try
|
---|
199 | bmp.LoadFromStream(AStream, AReader);
|
---|
200 | except
|
---|
201 | FreeAndNil(bmp);
|
---|
202 | end;
|
---|
203 | if bmp = nil then
|
---|
204 | result := nil
|
---|
205 | else
|
---|
206 | begin
|
---|
207 | result := GetBitmapThumbnail(bmp, AWidth, AHeight, ABackColor, ACheckers, ADest);
|
---|
208 | bmp.Free;
|
---|
209 | end;
|
---|
210 | end;
|
---|
211 |
|
---|
212 | function GetOpenRasterThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
213 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
214 | var
|
---|
215 | unzip: TUnzipperStreamUtf8;
|
---|
216 | png: TMemoryStream;
|
---|
217 | begin
|
---|
218 | result := nil;
|
---|
219 | unzip := TUnzipperStreamUtf8.Create;
|
---|
220 | try
|
---|
221 | unzip.InputStream := AStream;
|
---|
222 | png := TMemoryStream.Create;
|
---|
223 | try
|
---|
224 | if unzip.UnzipFileToStream('Thumbnails\thumbnail.png', png, False) then
|
---|
225 | begin
|
---|
226 | png.Position:= 0;
|
---|
227 | result := GetPngThumbnail(png,AWidth,AHeight,ABackColor,ACheckers,ADest);
|
---|
228 | end;
|
---|
229 | finally
|
---|
230 | png.Free;
|
---|
231 | end;
|
---|
232 | except
|
---|
233 | end;
|
---|
234 | unzip.Free;
|
---|
235 | end;
|
---|
236 |
|
---|
237 | function GetLazPaintThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
238 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
239 | var
|
---|
240 | reader: TBGRAReaderLazPaint;
|
---|
241 | begin
|
---|
242 | reader:= TBGRAReaderLazPaint.Create;
|
---|
243 | reader.WantThumbnail := true;
|
---|
244 | result := GetStreamThumbnail(AStream,reader,AWidth,AHeight,ABackColor,ACheckers,ADest);
|
---|
245 | reader.Free;
|
---|
246 | end;
|
---|
247 |
|
---|
248 | function GetPhoxoThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
249 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
250 | var
|
---|
251 | reader: TFPCustomImageReader;
|
---|
252 | begin
|
---|
253 | if DefaultBGRAImageReader[ifPhoxo] = nil then
|
---|
254 | result := nil
|
---|
255 | else
|
---|
256 | begin
|
---|
257 | reader := CreateBGRAImageReader(ifPhoxo);
|
---|
258 | result := GetStreamThumbnail(AStream, reader, AWidth,AHeight,ABackColor,ACheckers,ADest);
|
---|
259 | reader.Free;
|
---|
260 | end;
|
---|
261 | end;
|
---|
262 |
|
---|
263 | function GetJpegThumbnail(AStream: TStream; AWidth, AHeight: integer
|
---|
264 | ; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
265 | var
|
---|
266 | jpeg: TBGRAReaderJpeg;
|
---|
267 | begin
|
---|
268 | jpeg := TBGRAReaderJpeg.Create;
|
---|
269 | jpeg.Performance := jpBestSpeed;
|
---|
270 | jpeg.MinWidth := AWidth;
|
---|
271 | jpeg.MinHeight := AHeight;
|
---|
272 | result := GetStreamThumbnail(AStream, jpeg, AWidth,AHeight,ABackColor,ACheckers,ADest);
|
---|
273 | jpeg.Free;
|
---|
274 | end;
|
---|
275 |
|
---|
276 | function GetPsdThumbnail(AStream: TStream; AWidth, AHeight: integer
|
---|
277 | ; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
278 | var
|
---|
279 | psd: TBGRAReaderPSD;
|
---|
280 | bmp: TBGRABitmap;
|
---|
281 | begin
|
---|
282 | psd:= TBGRAReaderPSD.Create;
|
---|
283 | psd.MinifyHeight:= AHeight;
|
---|
284 | bmp := TBGRABitmap.Create;
|
---|
285 | try
|
---|
286 | bmp.LoadFromStream(AStream, psd);
|
---|
287 | except
|
---|
288 | FreeAndNil(bmp);
|
---|
289 | end;
|
---|
290 | if bmp = nil then
|
---|
291 | result := nil
|
---|
292 | else
|
---|
293 | begin
|
---|
294 | result := GetBitmapThumbnail(bmp, AWidth, AHeight, ABackColor, ACheckers, ADest, psd.Height/bmp.Height);
|
---|
295 | bmp.Free;
|
---|
296 | end;
|
---|
297 | psd.Free;
|
---|
298 | end;
|
---|
299 |
|
---|
300 | function GetPngThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
301 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap;
|
---|
302 | var
|
---|
303 | pngFormat: TBGRAReaderPNG;
|
---|
304 | begin
|
---|
305 | pngFormat:= TBGRAReaderPNG.Create;
|
---|
306 | result:= GetStreamThumbnail(AStream, pngFormat, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
307 | pngFormat.Free;
|
---|
308 | end;
|
---|
309 |
|
---|
310 | function GetPaintDotNetThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
311 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
312 | var
|
---|
313 | {%H-}magic: packed array[0..6] of byte;
|
---|
314 | xmlHeader: TMemoryStream;
|
---|
315 | xmlHeaderSize: longint;
|
---|
316 | doc: TXMLDocument;
|
---|
317 | custom,thumb,pngNode: TDOMNode;
|
---|
318 | png64: TStringStream;
|
---|
319 | decode64: TBase64DecodingStream;
|
---|
320 | begin
|
---|
321 | result := nil;
|
---|
322 | if AStream.Read({%H-}magic,sizeof(magic)) <> sizeof(magic) then exit;
|
---|
323 | if chr(magic[0])+chr(magic[1])+chr(magic[2])+chr(magic[3]) <> 'PDN3' then exit;
|
---|
324 | xmlHeaderSize := magic[4] + (magic[5] shl 8) + (magic[6] shl 16);
|
---|
325 | if xmlHeaderSize >= 10*1024*1024 then exit;
|
---|
326 | xmlHeader:= TMemoryStream.Create;
|
---|
327 | try
|
---|
328 | if xmlHeader.CopyFrom(AStream,xmlHeaderSize) <> xmlHeaderSize then
|
---|
329 | begin
|
---|
330 | xmlHeader.Free;
|
---|
331 | exit;
|
---|
332 | end;
|
---|
333 | except
|
---|
334 | xmlHeader.Free;
|
---|
335 | exit;
|
---|
336 | end;
|
---|
337 | xmlHeader.Position := 0;
|
---|
338 | try
|
---|
339 | XMLRead.ReadXMLFile(doc, xmlHeader);
|
---|
340 | except
|
---|
341 | xmlHeader.Free;
|
---|
342 | exit;
|
---|
343 | end;
|
---|
344 | xmlHeader.Free;
|
---|
345 | try
|
---|
346 | custom := doc.DocumentElement.FindNode('custom');
|
---|
347 | if Assigned(custom) then
|
---|
348 | begin
|
---|
349 | thumb := custom.FindNode('thumb');
|
---|
350 | if Assigned(thumb) then
|
---|
351 | begin
|
---|
352 | pngNode := thumb.Attributes.GetNamedItem('png');
|
---|
353 | if Assigned(pngNode) then
|
---|
354 | begin
|
---|
355 | png64 := TStringStream.Create(string(pngNode.NodeValue));
|
---|
356 | try
|
---|
357 | png64.Position := 0;
|
---|
358 | decode64 := TBase64DecodingStream.Create(png64);
|
---|
359 | try
|
---|
360 | result := GetPngThumbnail(decode64,AWidth,AHeight,ABackColor,ACheckers, ADest);
|
---|
361 | finally
|
---|
362 | decode64.Free;
|
---|
363 | end;
|
---|
364 | finally
|
---|
365 | png64.free;
|
---|
366 | end;
|
---|
367 | end;
|
---|
368 | end;
|
---|
369 | end;
|
---|
370 | except
|
---|
371 | end;
|
---|
372 | doc.Free;
|
---|
373 | end;
|
---|
374 |
|
---|
375 | function GetBmpThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
376 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
377 | var
|
---|
378 | bmpFormat: TBGRAReaderBMP;
|
---|
379 | begin
|
---|
380 | bmpFormat:= TBGRAReaderBMP.Create;
|
---|
381 | result:= GetStreamThumbnail(AStream, bmpFormat, AWidth,AHeight, ABackColor, ACheckers, ADest);
|
---|
382 | bmpFormat.Free;
|
---|
383 | end;
|
---|
384 |
|
---|
385 | function GetIcoThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
386 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
387 | var
|
---|
388 | reader: TBGRAReaderIco;
|
---|
389 | icoBmp: TBGRABitmap;
|
---|
390 | begin
|
---|
391 | result := nil;
|
---|
392 | reader := TBGRAReaderIco.Create;
|
---|
393 | reader.WantedWidth:= AWidth;
|
---|
394 | reader.WantedHeight:= AHeight;
|
---|
395 | icoBmp := TBGRABitmap.Create;
|
---|
396 | try
|
---|
397 | icoBmp.LoadFromStream(AStream, reader);
|
---|
398 | result := GetBitmapThumbnail(icoBmp, ifIco, AWidth, AHeight, ABackColor, ACheckers, ADest);
|
---|
399 | except
|
---|
400 | end;
|
---|
401 | icoBmp.Free;
|
---|
402 | reader.Free;
|
---|
403 | end;
|
---|
404 |
|
---|
405 | function GetCurThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
406 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
407 | var
|
---|
408 | reader: TBGRAReaderCur;
|
---|
409 | icoBmp: TBGRABitmap;
|
---|
410 | begin
|
---|
411 | result := nil;
|
---|
412 | reader := TBGRAReaderCur.Create;
|
---|
413 | reader.WantedWidth:= AWidth;
|
---|
414 | reader.WantedHeight:= AHeight;
|
---|
415 | icoBmp := TBGRABitmap.Create;
|
---|
416 | try
|
---|
417 | icoBmp.LoadFromStream(AStream, reader);
|
---|
418 | result := GetBitmapThumbnail(icoBmp, ifCur, AWidth, AHeight, ABackColor, ACheckers, ADest);
|
---|
419 | except
|
---|
420 | end;
|
---|
421 | icoBmp.Free;
|
---|
422 | reader.Free;
|
---|
423 | end;
|
---|
424 |
|
---|
425 | function GetPcxThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
426 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
427 | var
|
---|
428 | reader: TFPCustomImageReader;
|
---|
429 | begin
|
---|
430 | reader:= CreateBGRAImageReader(ifPcx);
|
---|
431 | result := GetStreamThumbnail(AStream,reader,AWidth,AHeight,ABackColor,ACheckers,ADest);
|
---|
432 | reader.Free;
|
---|
433 | end;
|
---|
434 |
|
---|
435 | function GetTargaThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
436 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
437 | var
|
---|
438 | reader: TFPCustomImageReader;
|
---|
439 | begin
|
---|
440 | reader:= CreateBGRAImageReader(ifTarga);
|
---|
441 | result := GetStreamThumbnail(AStream,reader,AWidth,AHeight,ABackColor,ACheckers, ADest);
|
---|
442 | reader.Free;
|
---|
443 | end;
|
---|
444 |
|
---|
445 | function GetTiffThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
446 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
447 | var
|
---|
448 | reader: TFPCustomImageReader;
|
---|
449 | begin
|
---|
450 | reader:= CreateBGRAImageReader(ifTiff);
|
---|
451 | result := GetStreamThumbnail(AStream,reader,AWidth,AHeight,ABackColor,ACheckers,ADest);
|
---|
452 | reader.Free;
|
---|
453 | end;
|
---|
454 |
|
---|
455 | function GetGifThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
456 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
457 | var
|
---|
458 | reader: TFPCustomImageReader;
|
---|
459 | begin
|
---|
460 | reader:= CreateBGRAImageReader(ifGif);
|
---|
461 | result := GetStreamThumbnail(AStream,reader,AWidth,AHeight,ABackColor,ACheckers, ADest);
|
---|
462 | reader.Free;
|
---|
463 | end;
|
---|
464 |
|
---|
465 | function GetXwdThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
466 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
467 | var
|
---|
468 | reader: TFPCustomImageReader;
|
---|
469 | begin
|
---|
470 | reader:= CreateBGRAImageReader(ifXwd);
|
---|
471 | result := GetStreamThumbnail(AStream,reader,AWidth,AHeight,ABackColor,ACheckers, ADest);
|
---|
472 | reader.Free;
|
---|
473 | end;
|
---|
474 |
|
---|
475 | function GetXPixMapThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
476 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
477 | var
|
---|
478 | reader: TFPCustomImageReader;
|
---|
479 | begin
|
---|
480 | reader:= CreateBGRAImageReader(ifXPixMap);
|
---|
481 | result := GetStreamThumbnail(AStream,reader,AWidth,AHeight,ABackColor,ACheckers,ADest);
|
---|
482 | reader.Free;
|
---|
483 | end;
|
---|
484 |
|
---|
485 | function GetBmpMioMapThumbnail(AStream: TStream; AWidth, AHeight: integer;
|
---|
486 | ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap;
|
---|
487 | var
|
---|
488 | reader: TFPCustomImageReader;
|
---|
489 | begin
|
---|
490 | reader:= CreateBGRAImageReader(ifBmpMioMap);
|
---|
491 | result := GetStreamThumbnail(AStream,reader,AWidth,AHeight,ABackColor,ACheckers,ADest);
|
---|
492 | reader.Free;
|
---|
493 | end;
|
---|
494 |
|
---|
495 | initialization
|
---|
496 |
|
---|
497 | IconCheckersColor1 := BGRA(140,180,180);
|
---|
498 | IconCheckersColor2 := BGRA(80,140,140);
|
---|
499 |
|
---|
500 | ImageCheckersColor1 := BGRA(255,255,255);
|
---|
501 | ImageCheckersColor2 := BGRA(220,220,220);
|
---|
502 |
|
---|
503 | end.
|
---|