{
     Apophysis Copyright (C) 2001-2004 Mark Townsend
     Apophysis Copyright (C) 2005-2006 Ronald Hordijk, Piotr Borys, Peter Sdobnov
     Apophysis Copyright (C) 2007-2008 Piotr Borys, Peter Sdobnov
     
     Apophysis "3D hack" Copyright (C) 2007-2008 Peter Sdobnov
     Apophysis "7X" Copyright (C) 2009-2010 Georg Kiehne
     Apophysis AV "Phoenix Edition" Copyright (C) 2021-2022 Alice V. Koryagina

     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
}
unit FormExport;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, ComCtrls, ExtCtrls;

type
  TExportDialog = class(TForm)
    btnOK: TButton;
    btnCancel: TButton;
    GroupBox1: TGroupBox;
    btnBrowse: TSpeedButton;
    txtFilename: TEdit;
    SaveDialog: TSaveDialog;
    GroupBox3: TGroupBox;
    txtOversample: TEdit;
    txtFilterRadius: TEdit;
    txtDensity: TEdit;
    udOversample: TUpDown;
    GroupBox2: TGroupBox;
    chkMaintain: TCheckBox;
    cbWidth: TComboBox;
    cbHeight: TComboBox;
    GroupBox4: TGroupBox;
    cmbDepth: TComboBox;
    chkRender: TCheckBox;
    txtStrips: TEdit;
    udStrips: TUpDown;
    txtEstimator: TEdit;
    txtEstimatorMin: TEdit;
    txtEstimatorCurve: TEdit;
    txtGammaTreshold: TEdit;
    Panel1: TPanel;
    Label6: TLabel;
    Label15: TLabel;
    Label4: TPanel;
    Label5: TPanel;
    Label3: TPanel;
    Label1: TPanel;
    Label2: TPanel;
    Label7: TPanel;
    Label8: TPanel;
    Label9: TPanel;
    Label14: TPanel;
    Label12: TPanel;
    Label11: TPanel;
    Label10: TPanel;
    lblFlam3Link: TLabel;
    procedure Panel1Resize(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure btnBrowseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure txtWidthChange(Sender: TObject);
    procedure chkMaintainClick(Sender: TObject);
    procedure txtHeightChange(Sender: TObject);
    procedure txtDensityChange(Sender: TObject);
    procedure txtFilterRadiusChange(Sender: TObject);
    procedure txtOversampleChange(Sender: TObject);
    procedure txtEstimatorChange(Sender: TObject);
    procedure txtEstimatorMinChange(Sender: TObject);
    procedure txtEstimatorCurveChange(Sender: TObject);
    procedure txtGammaTresholdChange(Sender: TObject);
    procedure lblFlam3LinkClick(Sender: TObject);
  private
    FloatFormatSettings: TFormatSettings;
  public
    Filename: string;
    ImageWidth, ImageHeight, Oversample: Integer;
    Sample_Density, Filter_Radius: double;
    Estimator, EstimatorMin, EstimatorCurve: double;
    GammaTreshold: double;
    // Batches, Jitters: Integer;
  end;

var
  ExportDialog: TExportDialog;
  Ratio: double;

implementation

uses Global, Main, ShellAPI, Translation;

{$R *.DFM}

procedure TExportDialog.btnBrowseClick(Sender: TObject);
begin
  SaveDialog.InitialDir := ExtractFileDir(txtFilename.text);
  SaveDialog.Filename := txtFilename.Text;
  case ExportFileFormat of   // AV: fixed
    1: SaveDialog.DefaultExt := 'jpg';
    2: SaveDialog.DefaultExt := 'ppm';
    3: SaveDialog.DefaultExt := 'png';
  end;
  SaveDialog.FilterIndex := ExportFileFormat;
  if SaveDialog.Execute then
  begin
    case SaveDialog.FilterIndex of
      1: txtFilename.Text := ChangeFileExt(SaveDialog.Filename, '.jpg');
      2: txtFilename.Text := ChangeFileExt(SaveDialog.Filename, '.ppm');
      3: txtFilename.Text := ChangeFileExt(SaveDialog.Filename, '.png');
    end;
    ExportFileFormat := SaveDialog.FilterIndex;
    renderPath := ExtractFilePath(SaveDialog.Filename);
  end;

end;

procedure TExportDialog.FormShow(Sender: TObject);
begin
  txtFilename.Text := Filename;
  cbWidth.Text := IntToStr(MainCp.Width);
  cbHeight.Text := IntToStr(MainCp.Height);
  ImageWidth := MainCp.Width;
  ImageHeight := MainCp.Height;
  txtDensity.text := FloatToStr(Sample_density);
  txtFilterRadius.text := FloatToStr(Filter_Radius);
  txtOversample.text := IntToSTr(Oversample);
  udOversample.Position := Oversample;
  Ratio := ImageWidth / ImageHeight;
  //Batches := 1;
  //Jitters := 1;
  Estimator := 9.0;
  EstimatorMin := 0.0;
  EstimatorCurve := 0.4;
  GammaTreshold := MainCP.gamma_threshold; //0.01;
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, FloatFormatSettings);
  txtEstimator.Text := FloatToStr(Estimator, FloatFormatSettings);
  txtEstimatorMin.Text := FloatToStr(EstimatorMin, FloatFormatSettings);
  txtEstimatorCurve.Text := FloatToStr(EstimatorCurve, FloatFormatSettings);
  txtGammaTreshold.Text := FloatToStr(GammaTreshold, FloatFormatSettings);
end;

procedure TExportDialog.btnOKClick(Sender: TObject);
begin
  Filename := txtFilename.text;
  ImageWidth := StrToInt(cbWidth.Text);
  ImageHeight := StrToInt(cbHeight.Text);
end;

procedure TExportDialog.txtWidthChange(Sender: TObject);
begin
  try
    ImageWidth := StrToInt(cbWidth.Text);
    if chkMaintain.checked and cbWidth.Focused then
    begin
      ImageHeight := Round(ImageWidth / ratio);
      cbHeight.Text := IntToStr(ImageHeight)
    end;
  except
  end;
end;

procedure TExportDialog.chkMaintainClick(Sender: TObject);
begin
  Ratio := ImageWidth / ImageHeight;
end;

procedure TExportDialog.txtHeightChange(Sender: TObject);
begin
  try
    ImageHeight := StrToInt(cbHeight.Text);
    if chkMaintain.checked and cbHeight.Focused then
    begin
      ImageWidth := Round(ImageHeight * ratio);
      cbWidth.Text := IntToStr(ImageWidth)
    end;
  except
  end;
end;

procedure TExportDialog.txtDensityChange(Sender: TObject);
begin
  Sample_Density := StrToFloatDef(txtDensity.Text, 200);
end;

procedure TExportDialog.txtFilterRadiusChange(Sender: TObject);
begin
  Filter_Radius := StrToFloatDef(txtFilterRadius.Text, 0.04);
end;

procedure TExportDialog.txtOversampleChange(Sender: TObject);
begin
  if StrToInt(txtOversample.Text) > udOversample.Max then
    txtOversample.Text := IntToStr(udOversample.Max);
  if StrToInt(txtOversample.Text) < udOversample.Min then
    txtOversample.Text := IntToStr(udOversample.Min);

  Oversample := StrToIntDef(txtOversample.Text, 2);
end;

procedure TExportDialog.txtEstimatorChange(Sender: TObject);
begin
  Estimator := 0;
  Estimator := StrToFloatDef(txtEstimator.Text, 0, FloatFormatSettings);
end;

procedure TExportDialog.txtEstimatorMinChange(Sender: TObject);
begin
  EstimatorMin := 0;
  EstimatorMin := StrToFloatDef(txtEstimatorMin.Text, 0, FloatFormatSettings);
end;

procedure TExportDialog.txtEstimatorCurveChange(Sender: TObject);
begin
  EstimatorCurve := 0;
  EstimatorCurve := StrToFloatDef(txtEstimatorCurve.Text, 0, FloatFormatSettings);
end;

procedure TExportDialog.txtGammaTresholdChange(Sender: TObject);
begin
  GammaTreshold := StrToFloatDef(txtGammaTreshold.Text, 0.01, FloatFormatSettings);
end;

procedure TExportDialog.lblFlam3LinkClick(Sender: TObject);
begin
  ShellExecute(ValidParentForm(Self).Handle, 'open', PChar(TLabel(Sender).Hint),
    nil, nil, SW_SHOWNORMAL);
end;

procedure TExportDialog.FormCreate(Sender: TObject);
begin
  btnOK.Caption := TextByKey('common-ok');
	btnCancel.Caption := TextByKey('common-cancel');
	Label1.Caption := TextByKey('common-width');
	Label2.Caption := TextByKey('common-height');
	GroupBox2.Caption := TextByKey('common-size');
	chkMaintain.Caption := TextByKey('common-keepaspect');
	GroupBox1.Caption := TextByKey('common-destination');
	Label10.Caption := TextByKey('common-filename');
	btnBrowse.Hint := TextByKey('common-browse');
	GroupBox3.Caption := TextByKey('common-quality');
	Label5.Caption := TextByKey('common-filterradius');
	Label4.Caption := TextByKey('common-density');
	Label3.Caption := TextByKey('common-oversample');
	Label14.Caption := TextByKey('common-gammathreshold');
	self.Caption := TextByKey('main-menu-file-exportflame');
	GroupBox4.Caption := TextByKey('export-paramoptions-title');
	Label7.Caption := TextByKey('export-paramoptions-bufferdepth');
	Label8.Caption := TextByKey('export-paramoptions-strips');
	Label9.Caption := TextByKey('export-paramoptions-estimatorradius');
	Label12.Caption := TextByKey('export-paramoptions-estimatorcurve');
	Label11.Caption := TextByKey('export-paramoptions-estimatormin');
	chkRender.Caption := TextByKey('export-paramoptions-dorender');
	Label6.Caption := TextByKey('export-paramoptions-warningtitle');
	Label15.Caption := TextByKey('export-paramoptions-warningtext');
  // AV: fixed the order and moved here here since the filter never changes
  SaveDialog.Filter := Format('%s|*.jpg;*.jpeg|Portable Pixmap (*.ppm)|*.ppm|%s|*.png',
        [TextByKey('common-filter-jpeg'), TextByKey('common-filter-png')]);
end;

procedure TExportDialog.Panel1Resize(Sender: TObject);
begin
  Label15.Top := (Panel1.Height - 30) div 2 - Label15.Height div 2 + 25;
end;

end.

