Skip to content

Instantly share code, notes, and snippets.

@Al-Muhandis
Last active December 18, 2023 06:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Al-Muhandis/2dcd574b973c4c429d84c9f7470d2cb7 to your computer and use it in GitHub Desktop.
Save Al-Muhandis/2dcd574b973c4c429d84c9f7470d2cb7 to your computer and use it in GitHub Desktop.
Разбивка изображения. FreePascal
unit imageswebutils;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FPimage, FPImgCanv, fgl;
type
TImageCanvasList = specialize TFPGObjectList<TFPImageCanvas>;
procedure CropImages(SourceCanvas: TFPImageCanvas; CropRow, CropCol: Integer;
DestImages: TImageCanvasList);
procedure CropImages(SourceCanvas: TFPImageCanvas; CropCol: Integer;
DestImages: TImageCanvasList; out CropRow: Integer);overload;
implementation
procedure ProposeImage(const Width, Height, ColCount: Integer;
out NewWidth, NewHeight, RowCount: Integer);
var
a: Integer;
begin
a:=Width div ColCount;
RowCount:=Height div a;
NewWidth:=a*ColCount;
NewHeight:=a*RowCount;
end;
procedure CropImages(SourceCanvas: TFPImageCanvas; CropRow, CropCol: Integer;
DestImages: TImageCanvasList);
var
Img: TFPMemoryImage;
DestCanvas: TFPImageCanvas;
W, H, X, Y: Integer;
Col, Row: Integer;
begin
W:=SourceCanvas.Image.Width div CropCol;
H:=SourceCanvas.Image.Height div CropRow;
DestImages.Count:=CropCol*CropRow;
Y:=0;
for Row:=0 to CropRow-1 do
begin
X:=0;
for Col:=0 to CropCol-1 do
begin
Img:=TFPMemoryImage.create(W,H);
DestCanvas:=TFPImageCanvas.create(Img);
DestCanvas.CopyRect(0,0,SourceCanvas,Rect(X, Y, X+W-1, Y+H-1));
DestImages[Col+Row*CropCol]:=DestCanvas;
X+=W;
end;
Y+=H;
end;
end;
procedure CropImages(SourceCanvas: TFPImageCanvas; CropCol: Integer;
DestImages: TImageCanvasList; out CropRow: Integer);
var
Img, BImg: TFPMemoryImage;
BCanvas, DestCanvas: TFPImageCanvas;
W, H, X, Y: Integer;
Col, Row: Integer;
begin
with SourceCanvas.Image do
ProposeImage(Width, Height, CropCol, W, H, CropRow);
BImg:=TFPMemoryImage.create(W, H);
BCanvas:=TFPImageCanvas.create(BImg);
X:=(SourceCanvas.Image.Width-W) div 2;
Y:=(SourceCanvas.Image.Height-H) div 2;
BCanvas.CopyRect(0,0,SourceCanvas,Rect(X,Y,X+W-1,Y+H-1));
W:=BImg.Width div CropCol;
H:=BImg.Height div CropRow;
DestImages.Count:=CropCol*CropRow;
Y:=0;
for Row:=0 to CropRow-1 do
begin
X:=0;
for Col:=0 to CropCol-1 do
begin
Img:=TFPMemoryImage.create(W,H);
DestCanvas:=TFPImageCanvas.create(Img);
DestCanvas.CopyRect(0,0,BCanvas,Rect(X, Y, X+W-1, Y+H-1));
DestImages[Col+Row*CropCol]:=DestCanvas;
X+=W;
end;
Y+=H;
end;
BCanvas.Free;
BImg.Free;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment