Changeset 73 for trunk/ScreenTools.pas


Ignore:
Timestamp:
Jan 15, 2017, 4:12:10 PM (7 years ago)
Author:
chronos
Message:
  • Modified: All graphics images converted from BMP to PNG.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ScreenTools.pas

    r72 r73  
    99  {$ENDIF}
    1010  StringTables, LCLIntf, LCLType, SysUtils, Classes, Graphics, Controls,
    11   Forms, Menus;
     11  Forms, Menus, GraphType;
    1212
    1313type
     
    4242    procedure SetXY(X, Y: Integer); inline; // Set pixel position relative to base
    4343    procedure SetX(X: Integer); inline; // Set horizontal pixel position relative to base
    44     procedure Init(Bitmap: TBitmap; BaseX: Integer = 0; BaseY: Integer = 0); inline;
     44    procedure Init(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0); inline;
    4545  end;
    4646  PPixelPointer = ^TPixelPointer;
     
    188188  gfNoError = $01;
    189189  gfNoGamma = $02;
    190   gfJPG = $04;
    191190
    192191type
     
    411410end;
    412411
     412procedure ApplyGammaToBitmap(Bitmap: TBitmap);
     413var
     414  PixelPtr: TPixelPointer;
     415  X, Y: Integer;
     416begin
     417  Bitmap.BeginUpdate;
     418  PixelPtr.Init(Bitmap);
     419  for Y := 0 to Bitmap.Height - 1 do begin
     420    for X := 0 to Bitmap.Width - 1 do begin
     421      PixelPtr.Pixel^.B := GammaLUT[PixelPtr.Pixel^.B];
     422      PixelPtr.Pixel^.G := GammaLUT[PixelPtr.Pixel^.G];
     423      PixelPtr.Pixel^.R := GammaLUT[PixelPtr.Pixel^.R];
     424      PixelPtr.NextPixel;
     425    end;
     426    PixelPtr.NextLine;
     427  end;
     428  Bitmap.EndUpdate;
     429end;
     430
     431procedure CopyGray8BitTo24bitBitmap(Dst, Src: TRasterImage);
     432var
     433  SrcPtr, DstPtr: TPixelPointer;
     434  X, Y: Integer;
     435begin
     436  //Dst.SetSize(Src.Width, Src.Height);
     437  SrcPtr.Init(Src);
     438  DstPtr.Init(Dst);
     439  for Y := 0 to Src.Height - 1 do begin
     440    for X := 0 to Src.Width - 1 do begin
     441      DstPtr.Pixel^.B := SrcPtr.Pixel^.B;
     442      DstPtr.Pixel^.G := SrcPtr.Pixel^.B;
     443      DstPtr.Pixel^.R := SrcPtr.Pixel^.B;
     444      SrcPtr.NextPixel;
     445      DstPtr.NextPixel;
     446    end;
     447    SrcPtr.NextLine;
     448    DstPtr.NextLine;
     449  end;
     450end;
     451
    413452function LoadGraphicFile(bmp: TBitmap; Path: string; Options: integer): boolean;
    414453var
    415   PixelPtr: TPixelPointer;
    416454  jtex: tjpegimage;
    417   X, Y: Integer;
    418 begin
    419   result := true;
    420   if Options and gfJPG <> 0 then
    421   begin
     455  Png: TPortableNetworkGraphic;
     456begin
     457  Result := True;
     458  if ExtractFileExt(Path) = '.jpg' then begin
    422459    jtex := tjpegimage.create;
    423460    try
    424       jtex.loadfromfile(Path + '.jpg');
     461      jtex.LoadFromFile(Path);
    425462    except
    426       result := false;
    427     end;
    428     if result then
    429     begin
    430       if Options and gfNoGamma = 0 then
    431         bmp.PixelFormat := pf24bit;
    432       bmp.Width := jtex.Width;
    433       bmp.Height := jtex.Height;
    434       bmp.Canvas.draw(0, 0, jtex);
     463      Result := False;
     464    end;
     465    if result then begin
     466      if Options and gfNoGamma = 0 then bmp.PixelFormat := pf24bit;
     467      Bmp.SetSize(jtex.Width, jtex.Height);
     468      Bmp.Canvas.Draw(0, 0, jtex);
    435469    end;
    436470    jtex.Free;
    437   end
    438   else
    439   begin
     471  end else
     472  if ExtractFileExt(Path) = '.png' then begin
     473    Png := TPortableNetworkGraphic.Create;
     474    Png.PixelFormat := Bmp.PixelFormat;
    440475    try
    441       bmp.loadfromfile(Path + '.bmp');
     476      Png.LoadFromFile(Path);
    442477    except
    443       result := false;
    444     end;
    445     if result then
    446     begin
     478      Result := False;
     479    end;
     480    if Result then begin
     481      if Options and gfNoGamma = 0 then bmp.PixelFormat := pf24bit;
     482      bmp.SetSize(Png.Width, Png.Height);
     483      if (Png.RawImage.Description.Format = ricfGray) then begin
     484        // LCL doesn't support 8-bit colors properly. Use 24-bit instead.
     485        Bmp.PixelFormat := pf24bit;
     486        CopyGray8BitTo24bitBitmap(Bmp, Png)
     487      end else Bmp.Canvas.draw(0, 0, Png);
     488    end;
     489    Png.Free;
     490  end else
     491  if ExtractFileExt(Path) = '.bmp' then begin
     492    try
     493      bmp.LoadFromFile(Path);
     494    except
     495      Result := False;
     496    end;
     497    if Result then begin
    447498      if Options and gfNoGamma = 0 then
    448499        bmp.PixelFormat := pf24bit;
    449500    end
    450   end;
    451   if not result then
    452   begin
     501  end else
     502    raise Exception.Create('Unsupported image file type ' + ExtractFileExt(Path));
     503
     504  if not Result then begin
    453505    if Options and gfNoError = 0 then
    454506      Application.MessageBox(PChar(Format(Phrases.Lookup('FILENOTFOUND'),
    455507        [Path])), 'C-evo', 0);
    456     exit;
    457   end;
     508    Exit;
     509  end;
     510
    458511  if (Options and gfNoGamma = 0) and (Gamma <> 100) then
    459   begin
    460     Bmp.BeginUpdate;
    461     PixelPtr.Init(bmp);
    462     for Y := 0 to Bmp.Height - 1 do begin
    463       for X := 0 to Bmp.Width - 1 do begin
    464         PixelPtr.Pixel^.B := GammaLUT[PixelPtr.Pixel^.B];
    465         PixelPtr.Pixel^.G := GammaLUT[PixelPtr.Pixel^.G];
    466         PixelPtr.Pixel^.R := GammaLUT[PixelPtr.Pixel^.R];
    467         PixelPtr.NextPixel;
    468       end;
    469       PixelPtr.NextLine;
    470     end;
    471     Bmp.EndUpdate;
    472   end
     512    ApplyGammaToBitmap(Bmp);
    473513end;
    474514
     
    484524    inc(i);
    485525  result := i;
    486   if i = nGrExt then
    487   begin
    488     FileName := HomeDir + 'Graphics' + DirectorySeparator + Name + '.bmp';
     526  if i = nGrExt then begin
    489527    Source := TBitmap.Create;
    490     try
    491       Source.LoadFromFile(FileName)
    492     except
     528    Source.PixelFormat := pf24bit;
     529    FileName := HomeDir + 'Graphics' + DirectorySeparator + Name;
     530    if not LoadGraphicFile(Source, FileName) then begin
    493531      Result := -1;
    494       Application.MessageBox(PChar(Format(Phrases.Lookup('FILENOTFOUND'),
    495         [FileName])), 'C-evo', 0);
    496       exit;
     532      Exit;
    497533    end;
    498534
     
    501537
    502538    xmax := Source.Width - 1; // allows 4-byte access even for last pixel
    503     if xmax > 970 then
    504       xmax := 970;
     539    if xmax > 970 then xmax := 970;
    505540
    506541    GrExt[nGrExt].Data := Source;
     
    13961431          MainTextureAge := Age;
    13971432          LoadGraphicFile(Image, HomeDir + 'Graphics' + DirectorySeparator + 'Texture' +
    1398             IntToStr(Age + 1), gfJPG);
     1433            IntToStr(Age + 1) + '.jpg');
    13991434          clBevelLight := Colors.Canvas.Pixels[clkAge0 + Age, cliBevelLight];
    14001435          clBevelShade := Colors.Canvas.Pixels[clkAge0 + Age, cliBevelShade];
     
    14421477end;
    14431478
    1444 procedure TPixelPointer.Init(Bitmap: TBitmap; BaseX: Integer = 0; BaseY: Integer = 0); inline;
     1479procedure TPixelPointer.Init(Bitmap: TRasterImage; BaseX: Integer = 0; BaseY: Integer = 0); inline;
    14451480begin
    14461481  BytesPerLine := Bitmap.RawImage.Description.BytesPerLine;
     
    15711606
    15721607  nGrExt := 0;
    1573   HGrSystem := LoadGraphicSet('System');
    1574   HGrSystem2 := LoadGraphicSet('System2');
    1575   Templates := TBitmap.create;
    1576   LoadGraphicFile(Templates, HomeDir + 'Graphics' + DirectorySeparator + 'Templates', gfNoGamma);
     1608  HGrSystem := LoadGraphicSet('System.png');
     1609  HGrSystem2 := LoadGraphicSet('System2.png');
     1610  Templates := TBitmap.Create;
    15771611  Templates.PixelFormat := pf24bit;
    1578   Colors := TBitmap.create;
    1579   LoadGraphicFile(Colors, HomeDir + 'Graphics' + DirectorySeparator + 'Colors');
    1580   Paper := TBitmap.create;
    1581   LoadGraphicFile(Paper, HomeDir + 'Graphics' + DirectorySeparator + 'Paper', gfJPG);
    1582   BigImp := TBitmap.create;
    1583   LoadGraphicFile(BigImp, HomeDir + 'Graphics' + DirectorySeparator + 'Icons');
    1584   MainTexture.Image := TBitmap.create;
     1612  LoadGraphicFile(Templates, HomeDir + 'Graphics' + DirectorySeparator + 'Templates.png', gfNoGamma);
     1613  Colors := TBitmap.Create;
     1614  Colors.PixelFormat := pf24bit;
     1615  LoadGraphicFile(Colors, HomeDir + 'Graphics' + DirectorySeparator + 'Colors.png');
     1616  Paper := TBitmap.Create;
     1617  Paper.PixelFormat := pf24bit;
     1618  LoadGraphicFile(Paper, HomeDir + 'Graphics' + DirectorySeparator + 'Paper.jpg');
     1619  BigImp := TBitmap.Create;
     1620  BigImp.PixelFormat := pf24bit;
     1621  LoadGraphicFile(BigImp, HomeDir + 'Graphics' + DirectorySeparator + 'Icons.png');
     1622  MainTexture.Image := TBitmap.Create;
    15851623  MainTextureAge := -2;
    15861624  ClickFrameColor := GrExt[HGrSystem].Data.Canvas.Pixels[187, 175];
    1587   InitOrnamentDone := false;
    1588   GenerateNames := true;
     1625  InitOrnamentDone := False;
     1626  GenerateNames := True;
    15891627end;
    15901628
Note: See TracChangeset for help on using the changeset viewer.