program project1;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Interfaces, // this includes the LCL widgetset
Forms, // Unit1,
{ you can add units after this }
sysutils, Graphics, LCLType, IntfGraphics;
{$R *.res}
type
TImgRotation = (
irError, irNormal, irMirrorHor, irRotate180, irMirrorVert,
irMirrorHorRot270, irRotate90, irMirrorHorRot90, irRotate270
); // all angle are clockwise
procedure RotateBitmap(const ABitmap: TBitmap; Angle: TImgRotation);
Var
bmp: TBitmap;
srcImg, dstImg: TLazIntfImage;
imgHandle, imgMaskHandle: HBitmap;
i, j: integer;
w1, h1: Integer; // Input bitmap width and height diminished by 1
Begin
Assert(ABitmap <> nil, 'RotateBitmap: Input bitmap is expected not to be nil.');
if (Angle = irError) or (Angle = irNormal) then
exit;
w1 := ABitmap.Width - 1;
h1 := ABitmap.Height - 1;
srcImg := TLazIntfImage.Create(0, 0);
try
srcImg.LoadFromBitmap(ABitmap.Handle, ABitmap.MaskHandle);
bmp := TBitmap.Create;
try
bmp.PixelFormat := pf32Bit; // added due to reply #19
dstImg := TLazIntfImage.Create(0, 0);
try
if Angle in [irRotate90, irRotate270, irMirrorHorRot90, irMirrorHorRot270] then
begin
bmp.SetSize(ABitmap.Height, ABitmap.Width);
dstImg.LoadFromBitmap(bmp.Handle, bmp.MaskHandle);
case Angle of
irRotate90:
for i:=0 to w1 do
for j:=0 to h1 do
dstImg.Colors[h1-j, i] := srcImg.Colors[i, j];
irRotate270:
for i:=0 to w1 do
for j:=0 to h1 do
dstImg.Colors[j, w1-i] := srcImg.Colors[i, j];
irMirrorHorRot90:
for i:=0 to w1 do
for j:=0 to h1 do
dstImg.Colors[h1-j, w1-i] := srcImg.Colors[i, j];
irMirrorHorRot270:
for i:=0 to w1 do
for j:=0 to h1 do
dstImg.Colors[j, i] := srcImg.Colors[i, j];
end;
end else
if Angle in [irRotate180, irMirrorHor, irMirrorVert] then
begin
bmp.SetSize(ABitmap.Width, ABitmap.Height);
dstImg.LoadFromBitmap(bmp.Handle, bmp.MaskHandle);
case Angle of
irRotate180:
for i:=0 to w1 do
for j:=0 to h1 do
dstImg.Colors[w1-i, h1-j] := srcImg.Colors[i, j];
irMirrorHor:
for j:=0 to h1 do
for i:=0 to w1 do
dstImg.Colors[w1-i, j] := srcImg.Colors[i, j];
irMirrorVert:
for i:=0 to w1 do
for j:=0 to h1 do
dstImg.Colors[i, h1-j] := srcImg.Colors[i, j];
end;
end;
dstImg.CreateBitmaps(imgHandle, imgMaskHandle, false);
bmp.Handle := ImgHandle;
bmp.MaskHandle := ImgMaskHandle;
finally
dstImg.Free;
end;
ABitmap.Assign(bmp);
finally
bmp.Free;
end;
finally
srcImg.Free;
end;
end; {RotateBitmap}
procedure convert(fspecIn,fspecOut: string; rot: TImgRotation);
{converts Graphic file 'fspecIn' to 'fspecOut' and does rotation by 'rot'}
var PT: TPicture;
begin
PT:=TPicture.Create;
PT.LoadFromFile(fspecIn);
RotateBitmap(PT.Bitmap,rot);
PT.SaveToFile(fspecOut);
PT.Free;
writeln('done');
end; {convert}
begin {main}
if ParamCount <> 2 then
begin
writeln('Usage: <inputfile> <outputfile>');
exit;
end;
if not FileExists(ParamStr(1)) then
begin
writeln('Inputfile not found!');
exit;
end;
convert(ParamStr(1),ParamStr(2),irRotate180); // use 90/180/270°
end.