Ignore:
Timestamp:
Mar 8, 2021, 4:42:28 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Improved LoadGraphicFile function to do not raise and catch exception internally for image file which doesn't exist.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Packages/CevoComponents/ScreenTools.pas

    r295 r296  
    3131procedure EditFrame(ca: TCanvas; p: TRect; const T: TTexture);
    3232function HexStringToColor(S: string): integer;
    33 function LoadGraphicFile(bmp: TBitmap; Path: string; Options: TLoadGraphicFileOptions = []): boolean;
     33function LoadGraphicFile(Bmp: TBitmap; FileName: string; Options: TLoadGraphicFileOptions = []): boolean;
    3434function LoadGraphicSet(const Name: string): integer;
    3535procedure Dump(dst: TBitmap; HGr, xDst, yDst, Width, Height, xGr, yGr: integer);
     
    416416end;
    417417
    418 function LoadGraphicFile(bmp: TBitmap; Path: string; Options: TLoadGraphicFileOptions = []): Boolean;
    419 var
    420   jtex: TJpegImage;
     418function LoadGraphicFile(Bmp: TBitmap; FileName: string; Options:
     419  TLoadGraphicFileOptions = []): Boolean;
     420var
     421  Jpeg: TJpegImage;
    421422  Png: TPortableNetworkGraphic;
    422423begin
    423   Result := True;
    424   if ExtractFileExt(Path) = '' then
    425     Path := Path + '.png';
    426   if ExtractFileExt(Path) = '.jpg' then begin
    427     jtex := TJpegImage.Create;
    428     try
    429       jtex.LoadFromFile(Path);
    430     except
    431       Result := False;
    432     end;
    433     if Result then
    434     begin
    435       if not (gfNoGamma in Options) then
    436         bmp.PixelFormat := pf24bit;
    437       Bmp.SetSize(jtex.Width, jtex.Height);
    438       Bmp.Canvas.Draw(0, 0, jtex);
    439     end;
    440     FreeAndNil(jtex);
    441   end
    442   else
    443   if ExtractFileExt(Path) = '.png' then begin
    444     Png := TPortableNetworkGraphic.Create;
    445     Png.PixelFormat := Bmp.PixelFormat;
    446     try
    447       Png.LoadFromFile(Path);
    448     except
    449       Result := False;
    450     end;
    451     if Result then
    452     begin
    453       if not (gfNoGamma in Options) then
    454         bmp.PixelFormat := pf24bit;
    455       bmp.SetSize(Png.Width, Png.Height);
    456       if (Png.RawImage.Description.Format = ricfGray) then
    457       begin
    458         // LCL doesn't support 8-bit colors properly. Use 24-bit instead.
    459         Bmp.PixelFormat := pf24bit;
    460         CopyGray8BitTo24bitBitmap(Bmp, Png);
    461       end
    462       else
    463         Bmp.Canvas.draw(0, 0, Png);
    464     end;
    465     FreeAndNil(Png);
    466   end
    467   else
    468   if ExtractFileExt(Path) = '.bmp' then begin
    469     try
    470       bmp.LoadFromFile(Path);
    471     except
    472       Result := False;
    473     end;
    474     if Result then begin
    475       if not (gfNoGamma in Options) then
    476         bmp.PixelFormat := pf24bit;
    477     end;
    478   end
    479   else
    480     raise Exception.Create('Unsupported image file type ' + ExtractFileExt(Path));
     424  Result := False;
     425  if ExtractFileExt(FileName) = '' then
     426    FileName := FileName + '.png';
     427
     428  if FileExists(FileName) then begin
     429    if ExtractFileExt(FileName) = '.jpg' then begin
     430      Jpeg := TJpegImage.Create;
     431      try
     432        Jpeg.LoadFromFile(FileName);
     433        if not (gfNoGamma in Options) then
     434          Bmp.PixelFormat := pf24bit;
     435        Bmp.SetSize(Jpeg.Width, Jpeg.Height);
     436        Bmp.Canvas.Draw(0, 0, Jpeg);
     437        Result := True;
     438      except
     439        Result := False;
     440      end;
     441      FreeAndNil(Jpeg);
     442    end else
     443    if ExtractFileExt(FileName) = '.png' then begin
     444      Png := TPortableNetworkGraphic.Create;
     445      try
     446        Png.PixelFormat := Bmp.PixelFormat;
     447        Png.LoadFromFile(FileName);
     448        if not (gfNoGamma in Options) then
     449          Bmp.PixelFormat := pf24bit;
     450        Bmp.SetSize(Png.Width, Png.Height);
     451        if (Png.RawImage.Description.Format = ricfGray) then
     452        begin
     453          // LCL doesn't support 8-bit colors properly. Use 24-bit instead.
     454          Bmp.PixelFormat := pf24bit;
     455          CopyGray8BitTo24bitBitmap(Bmp, Png);
     456        end
     457        else
     458          Bmp.Canvas.Draw(0, 0, Png);
     459        Result := True;
     460      except
     461        Result := False;
     462      end;
     463      FreeAndNil(Png);
     464    end else
     465    if ExtractFileExt(FileName) = '.bmp' then begin
     466      try
     467        Bmp.LoadFromFile(FileName);
     468        if not (gfNoGamma in Options) then
     469          Bmp.PixelFormat := pf24bit;
     470        Result := True;
     471      except
     472        Result := False;
     473      end;
     474    end else
     475      raise Exception.Create('Unsupported image file type ' + ExtractFileExt(FileName));
     476  end;
    481477
    482478  if not Result then begin
    483479    if not (gfNoError in Options) then
    484       raise Exception.Create(Format(Phrases.Lookup('FILENOTFOUND'), [Path]));
     480      raise Exception.Create(Format(Phrases.Lookup('FILENOTFOUND'), [FileName]));
    485481  end;
    486482
Note: See TracChangeset for help on using the changeset viewer.