unit uImageConvert;
{
This code is part of the TiCodEd Application, designed to develop for the
TI-99/4a Homecomputer in a modern, structured way.

BSD License
Copyright (c) 2026, Stefan Bauch, TiCodEd <at> lizardware <dot> de

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

}
{$mode ObjFPC}{$H+}

interface

uses
  Classes, SysUtils,math;

procedure ImageConvert(conv :  Integer);

implementation

uses uMain;

procedure ImageConvert(conv: Integer);
var i,j : Integer;
    t : packed array[0..127,0..63] of boolean;
    maxX, MaxY :  Integer;

begin
  case Form1.rgCharSize.ItemIndex of
    0 : begin maxX:=7;  maxY:=7;  end;
    1 : begin maxX:=15; maxY:=15; end;
    2,3: begin
            maxX := form1.cbCol.ItemIndex*8+7;
            maxY := form1.cbRow.ItemIndex*8+7;
            // make sure a square area is selected for rotation
            if (conv=3) or (conv=4) then begin
              maxX := min(maxX,MaxY);
              maxY := min(maxX,MaxY);
            end;
         end;
  end;

  // fill temporary matrix t according to conv value
  for i:=0 to maxX do
    for j:=0 to maxY do
      case conv of
        1 : t[i,maxY-j] := bt[i,j];  // flip vertical
        2 : t[maxX-i,j] := bt[i,j];  // flip horizontal
        3 : t[i,j] := bt[maxX-j,i];  // rotate left
        4 : t[i,j] := bt[j,maxY-i];  // rotate right
        5 : t[i,j] := not(bt[i,j]);  // invert picture
      end; // CASE

  // update the original bt matrix
  for i:=0 to maxX do
    for j:=0 to maxY do
      bt[i,j] := t[i,j];
end;


end.

