Resource

Resource Basics

Resource Name Filter By Ratio
Resource Version 1.1
Category Filter Script
Downloads 832
Release Date 6/27/2019 9:47:44 AM
 

Resource Details

Description This script will allow you to filter a set of images based on its ratio
Installation Instructions No need to install
Usage Instructions Select the script from the FILTER button dropdown...there select: From Repository->Filter by Ratio
Changes in this version Added 2x1 ratio option
Special Requirements
 

Author Information

Author Name IDimager Support
 

Available Attachments

Attachment #1 filter_by_ratio.png
 

Script

(*
  Author: Hert

  Version: 1.0
  Initial Date: 27 jun 2019

  Version 1.1;
    2019-09-25;
      - added 2x1 ratio
  Version 1.0;
    Initial version
*)

const
  cPrecision = 1;

var
  ARatioNames: TTntStringList;
  ARatioIndex: Integer;
  ARatioLandscape, ARatioPortrait: Boolean;
  ARatioValue: Double;

  ACombo: TvxPopupBox;
  ALandscape: TvxCheckBox;
  APortrait: TvxCheckBox;

function BuildForm: TvxSceneForm;
var
  ALabel: TvxLabel;
  i: Integer;
begin
  result := TvxSceneForm.Create(nil);
  result.Caption  := 'Filter by Ratio';
  result.Position := poScreenCenter;
  result.Height := Round(150 * result.DPIFactor);

  ALabel := TvxLabel.Create(result.Root);
  result.Root.AddObject(ALabel);
  ALabel.Text := 'Select a ratio';
  ALabel.TextAlign := vgTextAlignNear;
  ALabel.Position.Point := vgPoint(20, 20);

  ACombo := TvxPopupBox.Create(result.Root);
  result.Root.AddObject(ACombo);
  //ACombo.Style := csDropDownList;
  ACombo.Items.Clear;
  for i := 0 to ARatioNames.Count - 1 do
    ACombo.Items.Add(ARatioNames.Names[i]);
  ACombo.ItemIndex := ARatioIndex;
  ACombo.Position.Point := vgPoint(20, 40);
  ACombo.Width := 150;
  ACombo.TextAlign := vgTextAlignNear;

  ALandscape := TvxCheckBox.Create(result.Root);
  result.Root.AddObject(ALandscape);
  ALandscape.Text := 'Landscape';
  ALandscape.IsChecked := ARatioLandscape;
  ALandscape.Position.Point := vgPoint(20, 70);

  APortrait := TvxCheckBox.Create(result.Root);
  result.Root.AddObject(APortrait);
  APortrait.Text := 'Portrait';
  APortrait.IsChecked := ARatioPortrait;
  APortrait.Position.Point := vgPoint(20, 90);

end;

function Initialize: Boolean;
var
  AForm: TvxSceneForm;
begin
  result := True;

  ARatioNames := TTntStringList.Create;

  ARatioNames.Values[Translate('Square')] := FloatToStr(RoundToPrecision(1/1, cPrecision));
  ARatioNames.Values[Translate('2x1')]    := FloatToStr(RoundToPrecision(2/1, cPrecision));
  ARatioNames.Values[Translate('3x2')]    := FloatToStr(RoundToPrecision(3/2, cPrecision));
  ARatioNames.Values[Translate('4x3')]    := FloatToStr(RoundToPrecision(4/3, cPrecision));
  ARatioNames.Values[Translate('7x5')]    := FloatToStr(RoundToPrecision(7/5, cPrecision));
  ARatioNames.Values[Translate('16x9')]   := FloatToStr(RoundToPrecision(16/9, cPrecision));

  ARatioIndex     := ReadFromRegistry ('Scripts', 'FilterRatio_Index', 2);
  ARatioLandscape := ReadFromRegistry ('Scripts', 'FilterRatio_Landscape', 1) = 1;
  ARatioPortrait  := ReadFromRegistry ('Scripts', 'FilterRatio_Portrait', 1) = 1;
  ARatioValue     := 1.0;

  AForm := BuildForm;
  AForm.PrepareModal([mbOK, mbCancel]);
  AForm.ShowModal;
  result := (AForm.ModalResult = mrOk); 
  if result then
  begin
    ARatioIndex     := ACombo.ItemIndex;
    ARatioLandscape := ALandscape.IsChecked;
    ARatioPortrait  := APortrait.IsChecked;
    ARatioValue     := StrToFloat(ARatioNames.ValueFromIndex[ARatioIndex]);

    WriteToRegistry ('Scripts', 'FilterRatio_Index', ARatioIndex);
    WriteToRegistry ('Scripts', 'FilterRatio_Landscape', iif(ARatioLandscape, 1, 0));
    WriteToRegistry ('Scripts', 'FilterRatio_Portrait', iif(ARatioPortrait, 1, 0));
  end;
  AForm.Free;

  ARatioNames.Free;
end;

procedure Finalize;
begin
end;

function Filter(AItem: TImageItem): Boolean;
var
  AWidthStr, AHeightStr: WideString;
  AWidth, AHeight: Integer;
  ARatio: Double;

  AMs: TMemoryStream;
  ATif: TTif;
begin
  result := False;

  if not ARatioLandscape and not ARatioPortrait then 
    exit;

  AWidth := 0; AHeight := 0;
  AMs := TMemoryStream.Create;
  if Thumbnailer.LoadThumbStream(AItem.GUID, AMs) then
  begin
    ATif := TTif.Create(nil);
    AMs.Position := 0;
    ATif.JpegDynamicDimensionsFromStream(AMs, AWidth, AHeight);
    ATif.Free;
  end
  else
  begin
    AWidthStr := '0';
    AHeightStr := '0';
    if not PublicCatalog.FindXMPCachedTagValueForImage(AItem, 'exif:PixelXDimension', AWidthStr) then
      AWidthStr  := AItem.ParseMacros('%xmp:exif:pixelxdimension', nil);
    if not PublicCatalog.FindXMPCachedTagValueForImage(AItem, 'exif:PixelYDimension', AHeightStr) then
      AHeightStr := AItem.ParseMacros('%xmp:exif:pixelydimension', nil);
    if IsValidIntNumberString(AWidthStr, False) and IsValidIntNumberString(AHeightStr, False) then
    begin
      AWidth := StrToInt(AWidthStr);
      AHeight := StrToInt(AHeightStr);
    end;
  end;
  AMs.Free;

  if (AWidth * AHeight) > 0 then  // don't accept 0 values
  begin
    ARatio := RoundToPrecision(Max(AWidth, AHeight) / Min(AWidth, AHeight), cPrecision)

    if ARatio = ARatioValue then
    begin
      if (ARatioLandscape and (AWidth >= AHeight)) or
         (ARatioPortrait and (AWidth <= AHeight))
      then
        result := True;
    end;
  end;
end;