2017-09-30 01:13:05 +02:00
|
|
|
(*
|
|
|
|
****************************************************************
|
|
|
|
Delphi-OpenCV Demo
|
|
|
|
Copyright (C) 2013 Project Delphi-OpenCV
|
|
|
|
****************************************************************
|
|
|
|
Contributor:
|
|
|
|
Laentir Valetov
|
|
|
|
email:laex@bk.ru
|
|
|
|
****************************************************************
|
|
|
|
You may retrieve the latest version of this file at the GitHub,
|
|
|
|
located at git://github.com/Laex/Delphi-OpenCV.git
|
|
|
|
****************************************************************
|
|
|
|
The contents of this file are used with permission, subject to
|
|
|
|
the Mozilla Public License Version 1.1 (the "License"); you may
|
|
|
|
not use this file except in compliance with the License. You may
|
|
|
|
obtain a copy of the License at
|
|
|
|
http://www.mozilla.org/MPL/MPL-1_1Final.html
|
|
|
|
|
|
|
|
Software distributed under the License is distributed on an
|
|
|
|
"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
|
|
|
|
implied. See the License for the specific language governing
|
|
|
|
rights and limitations under the License.
|
|
|
|
****************************************************************
|
|
|
|
*)
|
|
|
|
|
2015-08-26 19:35:51 +02:00
|
|
|
unit ocv.fmxutils;
|
|
|
|
|
2017-09-30 01:13:05 +02:00
|
|
|
{$I OpenCV.inc}
|
|
|
|
|
2015-08-26 19:35:51 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
Uses
|
2017-09-15 11:02:59 +02:00
|
|
|
ocv.core.types_c
|
|
|
|
{$IFDEF DELPHIXE5_UP}
|
2019-03-18 17:10:15 +01:00
|
|
|
, FMX.Graphics
|
2017-09-15 11:02:59 +02:00
|
|
|
{$IFEND}
|
2019-03-18 17:10:15 +01:00
|
|
|
;
|
2015-08-26 19:35:51 +02:00
|
|
|
|
2017-09-15 11:02:59 +02:00
|
|
|
{$IFDEF DELPHIXE5_UP}
|
2019-03-18 17:10:15 +01:00
|
|
|
procedure IPLImageToFMXBitmap(const IpImage: pIplImage; const FMXBitmap: TBitmap;
|
|
|
|
const WithROI: Boolean = true); inline;
|
2017-09-15 11:02:59 +02:00
|
|
|
{$IFEND}
|
2015-08-26 19:35:51 +02:00
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2017-09-30 01:13:05 +02:00
|
|
|
{$IFDEF DELPHIXE5_UP}
|
|
|
|
|
2015-08-26 19:35:51 +02:00
|
|
|
Uses FMX.Types;
|
|
|
|
|
2019-03-18 17:10:15 +01:00
|
|
|
procedure IPLImageToFMXBitmap(const IpImage: pIplImage; const FMXBitmap: TBitmap; const WithROI: Boolean); inline;
|
2015-08-26 19:35:51 +02:00
|
|
|
Var
|
2017-05-24 17:27:25 +02:00
|
|
|
BitmapData: TBitmapData;
|
2019-03-18 17:10:15 +01:00
|
|
|
i, j, src_index, dst_index: Integer;
|
2015-08-26 19:35:51 +02:00
|
|
|
SrcData, DestData: pByte;
|
|
|
|
nC: Integer;
|
2017-05-24 17:27:25 +02:00
|
|
|
pf: Integer;
|
2015-08-26 19:35:51 +02:00
|
|
|
begin
|
2017-09-07 12:38:44 +02:00
|
|
|
SrcData := nil;
|
2015-08-26 19:35:51 +02:00
|
|
|
Assert(Assigned(IpImage) and Assigned(FMXBitmap));
|
2017-05-24 17:27:25 +02:00
|
|
|
if (IpImage^.Width > 0) and (IpImage^.Height > 0) and Assigned(IpImage^.imageData) then
|
2015-08-26 19:35:51 +02:00
|
|
|
try
|
|
|
|
nC := IpImage^.nChannels;
|
2017-05-24 17:27:25 +02:00
|
|
|
With IpImage^ do
|
2015-08-26 19:35:51 +02:00
|
|
|
begin
|
2017-05-24 17:27:25 +02:00
|
|
|
SrcData := AllocMem(Width * Height * nC);
|
|
|
|
Move(imageData^, SrcData^, Width * Height * nC);
|
2015-08-26 19:35:51 +02:00
|
|
|
end;
|
2019-03-18 17:10:15 +01:00
|
|
|
|
|
|
|
if WithROI and Assigned(IpImage^.roi) then
|
|
|
|
begin
|
|
|
|
if (FMXBitmap.Width <> IpImage^.roi^.Width) or (FMXBitmap.Height <> IpImage^.roi^.Height) then
|
|
|
|
FMXBitmap.SetSize(IpImage^.roi^.Width, IpImage^.roi^.Height);
|
|
|
|
end
|
|
|
|
else if (FMXBitmap.Width <> IpImage^.Width) or (FMXBitmap.Height <> IpImage^.Height) then
|
2017-05-24 17:27:25 +02:00
|
|
|
FMXBitmap.SetSize(IpImage^.Width, IpImage^.Height);
|
2019-03-18 17:10:15 +01:00
|
|
|
|
2017-05-24 17:27:25 +02:00
|
|
|
if FMXBitmap.Map(TMapAccess.Write, BitmapData) then
|
|
|
|
try
|
|
|
|
DestData := pByte(BitmapData.Data);
|
|
|
|
pf := PixelFormatBytes[FMXBitmap.PixelFormat];
|
2019-03-18 17:10:15 +01:00
|
|
|
|
|
|
|
if WithROI and Assigned(IpImage^.roi) then
|
2017-05-24 17:27:25 +02:00
|
|
|
begin
|
2019-03-18 17:10:15 +01:00
|
|
|
for j := 0 to BitmapData.Height - 1 do
|
|
|
|
for i := 0 to BitmapData.Width - 1 do
|
|
|
|
begin
|
|
|
|
src_index := ((j + IpImage^.roi^.yOffset) * IpImage^.Width + (i + IpImage^.roi^.xOffset)) * nC;
|
|
|
|
dst_index := (j * BitmapData.Width + i) * pf;
|
|
|
|
DestData[dst_index + 0] := SrcData[src_index + 0];
|
|
|
|
DestData[dst_index + 1] := SrcData[src_index + 1];
|
|
|
|
DestData[dst_index + 2] := SrcData[src_index + 2];
|
|
|
|
DestData[dst_index + 3] := $FF;
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for i := 0 to BitmapData.Width * BitmapData.Height - 1 do
|
|
|
|
begin
|
|
|
|
DestData[i * pf + 0] := SrcData[i * nC + 0];
|
|
|
|
DestData[i * pf + 1] := SrcData[i * nC + 1];
|
|
|
|
DestData[i * pf + 2] := SrcData[i * nC + 2];
|
|
|
|
DestData[i * pf + 3] := $FF;
|
|
|
|
end;
|
2017-05-24 17:27:25 +02:00
|
|
|
finally
|
|
|
|
FMXBitmap.Unmap(BitmapData);
|
|
|
|
end;
|
2015-08-26 19:35:51 +02:00
|
|
|
finally
|
2017-09-07 12:38:44 +02:00
|
|
|
if Assigned(SrcData) then
|
|
|
|
FreeMem(SrcData);
|
2015-08-26 19:35:51 +02:00
|
|
|
end;
|
|
|
|
end;
|
2017-09-15 11:02:59 +02:00
|
|
|
{$IFEND}
|
2015-08-26 19:35:51 +02:00
|
|
|
|
|
|
|
end.
|