<commit_before>unit KM_IoGraphicUtils;
{$I KaM_Remake.inc}
interface
uses
  KM_CommonTypes
  {$IFDEF MSWindows}, Windows {$ENDIF}
  {$IFDEF WDC}, Graphics, JPEG, PNGImage {$ENDIF} //Lazarus doesn't have JPEG library yet -> FPReadJPEG?
  ;

  procedure SavePixelDataToFile(const aFilePath: string; aImageType: TKMImageType; aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);

implementation
uses
  SysUtils
{$IFDEF WDC}, Dialogs, KM_Log{$ENDIF}
  ;


procedure SavePixelDataToFile(const aFilePath: string; aImageType: TKMImageType; aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);
{$IFDEF WDC}
var
  filePath: string;
  mkbmp: TBitmap;
  jpg: TJpegImage;
  png: TPngImage;
{$ENDIF}
begin
{$IFDEF WDC}
  ForceDirectories(ExtractFilePath(aFilePath));
  filePath := ChangeFileExt(aFilePath, IMAGE_TYPE_EXT[aImageType]);

  mkbmp := TBitmap.Create;
  try
    try
      mkbmp.Handle := CreateBitmap(aWidth, aHeight, 1, 32, @aPixelData[0]);

      case aImageType of
        itJpeg: begin
                  jpg := TJpegImage.Create;
                  try
                    jpg.assign(mkbmp);
                    jpg.ProgressiveEncoding := True;
                    jpg.ProgressiveDisplay  := True;
                    jpg.Performance         := jpBestQuality;
                    jpg.CompressionQuality  := 90;
                    jpg.Compress;
                    jpg.SaveToFile(filePath);
                  finally
                    jpg.Free;
                  end;
                end;
        itPng:  begin
                  png := TPngImage.CreateBlank(COLOR_RGBALPHA, 16, aWidth, aHeight);
                  try
                    png.Assign(mkbmp);
                    png.SaveToFile(filePath);
                  finally
                    png.Free;
                  end;
                end;
        itBmp:  mkbmp.SaveToFile(filePath);
      end;
    except
      on E: Exception do
      begin
        gLog.AddTime('Error while saving image: ' + E.ClassName + ': ' + E.Message);

        ShowMessage('Error saving image. Try to reduce max image size or choose other image format');

        if FileExists(filePath) then
          DeleteFile(filePath);
      end;

    end;
  finally
    mkbmp.Free;
  end;
{$ENDIF}
end;


end.<commit_msg>Minor formatting tweaks<commit_after>unit KM_IoGraphicUtils;
{$I KaM_Remake.inc}
interface
uses
  KM_CommonTypes
  {$IFDEF MSWindows}, Windows {$ENDIF}
  {$IFDEF WDC}, Graphics, JPEG, PNGImage {$ENDIF} //Lazarus doesn't have JPEG library yet -> FPReadJPEG?
  ;

  procedure SavePixelDataToFile(const aFilePath: string; aImageType: TKMImageType; aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);

implementation
uses
  SysUtils
{$IFDEF WDC}, Dialogs, KM_Log{$ENDIF}
  ;


procedure SavePixelDataToFile(const aFilePath: string; aImageType: TKMImageType; aWidth, aHeight: Integer; var aPixelData: TKMCardinalArray);
{$IFDEF WDC}
var
  filePath: string;
  mkbmp: TBitmap;
  jpg: TJpegImage;
  png: TPngImage;
{$ENDIF}
begin
{$IFDEF WDC}
  ForceDirectories(ExtractFilePath(aFilePath));
  filePath := ChangeFileExt(aFilePath, IMAGE_TYPE_EXT[aImageType]);

  mkbmp := TBitmap.Create;
  try
    try
      mkbmp.Handle := CreateBitmap(aWidth, aHeight, 1, 32, @aPixelData[0]);

      case aImageType of
        itJpeg: begin
                  jpg := TJpegImage.Create;
                  try
                    jpg.Assign(mkbmp);
                    jpg.ProgressiveEncoding := True;
                    jpg.ProgressiveDisplay  := True;
                    jpg.Performance         := jpBestQuality;
                    jpg.CompressionQuality  := 90;
                    jpg.Compress;
                    jpg.SaveToFile(filePath);
                  finally
                    jpg.Free;
                  end;
                end;
        itPng:  begin
                  png := TPngImage.CreateBlank(COLOR_RGBALPHA, 16, aWidth, aHeight);
                  try
                    png.Assign(mkbmp);
                    png.SaveToFile(filePath);
                  finally
                    png.Free;
                  end;
                end;
        itBmp:  mkbmp.SaveToFile(filePath);
      end;
    except
      on E: Exception do
      begin
        gLog.AddTime('Error while saving image: ' + E.ClassName + ': ' + E.Message);

        ShowMessage('Error saving image. Try to reduce max image size or choose other image format');

        if FileExists(filePath) then
          DeleteFile(filePath);
      end;
    end;
  finally
    mkbmp.Free;
  end;
{$ENDIF}
end;


end.
{'fraction_non_alphanumeric': 0.09192874422696283, 'fraction_numerical': 0.003518803606773697, 'mean_word_length': 1.9570871261378413, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5305898', 'n_tokens_mistral': 1492, 'n_tokens_neox': 1351, 'n_words': 325}