Changeset 521 for GraphicTest/Packages/bgrabitmap/bgrathumbnail.pas
- Timestamp:
- Apr 17, 2019, 12:58:41 AM (5 years ago)
- Location:
- GraphicTest
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
GraphicTest
- Property svn:ignore
-
old new 8 8 GraphicTest.lps 9 9 GraphicTest.dbg 10 heaptrclog.trc
-
- Property svn:ignore
-
GraphicTest/Packages/bgrabitmap/bgrathumbnail.pas
r494 r521 9 9 Classes, SysUtils, BGRABitmap, BGRABitmapTypes, FPimage; 10 10 11 function GetBitmapThumbnail(ABitmap: TBGRABitmap; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil; AVerticalShrink : single = 1): TBGRABitmap; 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; 12 13 function GetFileThumbnail(AFilenameUTF8: string; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; 13 14 function GetStreamThumbnail(AStream: TStream; AWidth,AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ASuggestedExtensionUTF8: string = ''; ADest: TBGRABitmap= nil): TBGRABitmap; overload; … … 22 23 function GetPaintDotNetThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; 23 24 function GetBmpThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; 24 {$IFDEF BGRABITMAP_USE_LCL}25 25 function GetIcoThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; 26 {$ENDIF} 26 function GetCurThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; 27 27 28 28 function GetPcxThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; … … 34 34 function GetBmpMioMapThumbnail(AStream: TStream; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; 35 35 36 procedure DrawThumbnailCheckers(bmp: TBGRABitmap; ARect: TRect); 36 procedure DrawThumbnailCheckers(bmp: TBGRABitmap; ARect: TRect; AIconCheckers: boolean = false); 37 38 var 39 ImageCheckersColor1,ImageCheckersColor2 : TBGRAPixel; 40 IconCheckersColor1,IconCheckersColor2 : TBGRAPixel; 37 41 38 42 implementation 39 43 40 uses Types, base64, BGRAUTF8, {$IFDEF BGRABITMAP_USE_LCL}Graphics, GraphType,{$ENDIF}44 uses Types, base64, BGRAUTF8, 41 45 DOM, XMLRead, BGRAReadJPEG, BGRAReadPng, BGRAReadGif, BGRAReadBMP, 42 46 BGRAReadPSD, BGRAReadIco, UnzipperExt, BGRAReadLzp; 43 47 44 procedure DrawThumbnailCheckers(bmp: TBGRABitmap; ARect: TRect); 45 begin 46 bmp.DrawCheckers(ARect, BGRA(255,255,255), BGRA(220,220,220)); 47 end; 48 49 function GetBitmapThumbnail(ABitmap: TBGRABitmap; AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap; AVerticalShrink: single 50 ): TBGRABitmap; 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; 51 58 var 52 59 factorX, factorY, factor: single; 53 60 xIcon,yIcon,wIcon,hIcon: Integer; 61 hotspot: TPoint; 54 62 begin 55 63 result := nil; … … 64 72 end else 65 73 result := TBGRABitmap.Create(AWidth,AHeight,ABackColor); 66 factorX := result.Width/ ABitmap.Width;74 factorX := result.Width/(ABitmap.Width*AHorizShrink); 67 75 factorY := result.Height/(ABitmap.Height*AVerticalShrink); 68 76 if factorX < factorY then factor := factorX else factor := factorY; 69 wIcon := round(ABitmap.Width*factor); 77 wIcon := round(ABitmap.Width*AHorizShrink*factor); 78 if wIcon = 0 then wIcon := 1; 70 79 hIcon := round(ABitmap.Height*AVerticalShrink*factor); 80 if hIcon = 0 then hIcon := 1; 71 81 xIcon:= (result.Width-wIcon) div 2; 72 82 yIcon:= (result.Height-hIcon) div 2; 73 if ACheckers then DrawThumbnailCheckers(result,Rect(xIcon,yIcon,xIcon+wIcon,yIcon+hIcon)); 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; 74 94 if (ABackColor.alpha <> 0) or ACheckers then 75 95 result.StretchPutImage(Rect(xIcon,yIcon,xIcon+wIcon,yIcon+hIcon),ABitmap,dmDrawWithTransparency) else 76 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; 77 102 end; 78 103 except 79 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 80 121 end; 81 122 … … 83 124 var stream: TFileStreamUTF8; 84 125 begin 126 result := nil; 85 127 try 86 128 stream := TFileStreamUTF8.Create(AFilenameUTF8,fmOpenRead or fmShareDenyWrite); 87 129 except 88 result := nil;89 130 exit; 90 131 end; … … 99 140 ABackColor: TBGRAPixel; ACheckers: boolean; ASuggestedExtensionUTF8: string; 100 141 ADest: TBGRABitmap): TBGRABitmap; 101 begin 102 case DetectFileFormat(AStream,ASuggestedExtensionUTF8) of 142 var 143 ff: TBGRAImageFormat; 144 reader: TFPCustomImageReader; 145 begin 146 ff := DetectFileFormat(AStream,ASuggestedExtensionUTF8); 147 case ff of 103 148 ifJpeg: result := GetJpegThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest); 104 ifPng: result := GetPngThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);105 ifGif: result := GetGifThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);106 ifBmp: result := GetBmpThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);107 {$IFDEF BGRABITMAP_USE_LCL}108 149 ifIco: result := GetIcoThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest); 109 {$ENDIF} 110 ifPcx: result := GetPcxThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest); 150 ifCur: result := GetCurThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest); 111 151 ifPaintDotNet: result := GetPaintDotNetThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest); 112 152 ifLazPaint: result := GetLazPaintThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest); … … 114 154 ifPhoxo: result := GetPhoxoThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest); 115 155 ifPsd: result := GetPsdThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest); 116 ifTarga: result := GetTargaThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);117 ifTiff: result := GetTiffThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);118 ifXwd: result := GetXwdThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);119 ifXPixMap: result := GetXPixMapThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);120 ifBmpMioMap: result := GetBmpMioMapThumbnail(AStream, AWidth,AHeight, ABackColor, ACheckers, ADest);121 156 else 122 result := nil; 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; 123 172 end; 124 173 end; … … 127 176 AWidth, AHeight: integer; ABackColor: TBGRAPixel; ACheckers: boolean; 128 177 ADest: TBGRABitmap): TBGRABitmap; 129 var bmp: TBGRABitmap; 130 begin 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 131 197 bmp := TBGRABitmap.Create; 132 198 try … … 143 209 end; 144 210 end; 145 146 147 211 148 212 function GetOpenRasterThumbnail(AStream: TStream; AWidth, AHeight: integer; … … 234 298 end; 235 299 236 function GetPngThumbnail(AStream: TStream; AWidth, AHeight: integer 237 ; ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap= nil): TBGRABitmap; 238 var 239 png: TBGRAReaderPNG; 240 bmp: TBGRABitmap; 241 begin 242 png:= TBGRAReaderPNG.Create; 243 bmp := TBGRABitmap.Create; 244 try 245 png.MinifyHeight := AHeight; 246 bmp.LoadFromStream(AStream, png); 247 except 248 FreeAndNil(bmp); 249 end; 250 if bmp = nil then 251 result := nil 252 else 253 begin 254 result := GetBitmapThumbnail(bmp, AWidth, AHeight, ABackColor, ACheckers, ADest, png.OriginalHeight/bmp.Height); 255 bmp.Free; 256 end; 257 png.Free; 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; 258 308 end; 259 309 … … 303 353 if Assigned(pngNode) then 304 354 begin 305 png64 := TStringStream.Create( pngNode.NodeValue);355 png64 := TStringStream.Create(string(pngNode.NodeValue)); 306 356 try 307 357 png64.Position := 0; … … 327 377 var 328 378 bmpFormat: TBGRAReaderBMP; 329 bmp: TBGRABitmap;330 379 begin 331 380 bmpFormat:= TBGRAReaderBMP.Create; 332 bmpFormat.MinifyHeight := AHeight*2; 333 bmp := TBGRABitmap.Create; 334 try 335 bmp.LoadFromStream(AStream, bmpFormat); 336 except 337 FreeAndNil(bmp); 338 end; 339 if bmp = nil then 340 result := nil 341 else 342 begin 343 if bmp.Height <= 0 then 344 result := nil 345 else 346 result := GetBitmapThumbnail(bmp, AWidth, AHeight, ABackColor, ACheckers, ADest, bmpFormat.OriginalHeight/bmp.Height); 347 bmp.Free; 348 end; 381 result:= GetStreamThumbnail(AStream, bmpFormat, AWidth,AHeight, ABackColor, ACheckers, ADest); 349 382 bmpFormat.Free; 350 383 end; 351 384 352 {$IFDEF BGRABITMAP_USE_LCL}353 385 function GetIcoThumbnail(AStream: TStream; AWidth, AHeight: integer; 354 386 ABackColor: TBGRAPixel; ACheckers: boolean; ADest: TBGRABitmap): TBGRABitmap; 355 var ico: TIcon; i,bestIdx: integer; 356 height,width: word; format:TPixelFormat; 357 bestHeight,bestWidth: integer; maxFormat: TPixelFormat; 358 icoBmp: TBGRABitmap; 387 var 388 reader: TBGRAReaderIco; 389 icoBmp: TBGRABitmap; 359 390 begin 360 391 result := nil; 361 ico := TIcon.Create; 362 try 363 ico.LoadFromStream(AStream); 364 except 365 ico.free; 366 exit; 367 end; 368 bestIdx := -1; 369 bestHeight := 0; 370 bestWidth := 0; 371 maxFormat := pfDevice; 372 try 373 for i := 0 to ico.Count-1 do 374 begin 375 ico.GetDescription(i,format,height,width); 376 if (bestIdx = -1) or (abs(height-AHeight)+abs(width-AWidth) < abs(bestHeight-AHeight)+abs(bestWidth-AWidth)) or 377 ((height = bestHeight) or (width = bestWidth) and (format > maxFormat)) then 378 begin 379 bestIdx := i; 380 bestHeight := height; 381 bestWidth := width; 382 maxFormat := format; 383 end; 384 end; 385 if (bestIdx = -1) or (bestWidth = 0) or (bestHeight = 0) then result := nil else 386 begin 387 ico.Current := bestIdx; 388 icoBmp := TBGRABitmap.Create(bestWidth,bestHeight); 389 icoBmp.Assign(ico); 390 result := GetBitmapThumbnail(icoBmp, AWidth, AHeight, ABackColor, ACheckers, ADest); 391 icoBmp.Free; 392 end; 393 except 394 end; 395 ico.Free; 396 end; 397 {$ENDIF} 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; 398 424 399 425 function GetPcxThumbnail(AStream: TStream; AWidth, AHeight: integer; … … 467 493 end; 468 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 469 503 end.
Note:
See TracChangeset
for help on using the changeset viewer.