summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGraeme Geldenhuys <graeme@mastermaths.co.za>2012-08-31 18:27:48 +0100
committerGraeme Geldenhuys <graeme@mastermaths.co.za>2012-08-31 18:27:48 +0100
commit4602a4ee9c0ed69cdeb39895e7b190e711dcb163 (patch)
treea860843364f9e2757435f81475831dcdf6f9fa31 /src
parent8f35c22bf68b81bd8ac14890067e1703030dfb5f (diff)
downloadfpGUI-4602a4ee9c0ed69cdeb39895e7b190e711dcb163.tar.xz
Refactored the png loading code.
Extracted the code that does the conversion from FPC's FPImage to fpGUI's TfpgImage. Thanks to Dibo for this patch.
Diffstat (limited to 'src')
-rw-r--r--src/corelib/fpg_imgfmt_png.pas42
1 files changed, 24 insertions, 18 deletions
diff --git a/src/corelib/fpg_imgfmt_png.pas b/src/corelib/fpg_imgfmt_png.pas
index 49f3e12c..19f6b8cc 100644
--- a/src/corelib/fpg_imgfmt_png.pas
+++ b/src/corelib/fpg_imgfmt_png.pas
@@ -38,16 +38,35 @@ implementation
uses
fpg_utils;
-function LoadImage_PNG(const AFileName: TfpgString): TfpgImage;
+function FPImageToFPG(ASource: TFPCustomImage): TfpgImage;
var
i, j: integer;
colorA: TFPColor; // struct Red, Green, Blue, Alpha: word
colorB: TfpgColor; // ONE long 32-bit-word
- imga: TFPCustomImage;
- imgb: TfpgImage;
xlocal, ylocal: integer;
begin
Result := nil;
+ if ASource=nil then Exit;
+
+ xlocal := ASource.Width;
+ ylocal := ASource.Height;
+ Result := TfpgImage.Create;
+ Result.AllocateImage(32, xlocal, ylocal); // 32=colordepth
+ for i := 0 to ylocal - 1 do
+ for j := 0 to xlocal - 1 do
+ begin
+ colorA := ASource.Colors[j, i];
+ colorB := (colorA.Blue shr 8) or (colorA.Green and $FF00) or ((colorA.Red and $FF) shl 16) or ((colorA.Alpha and $FF) shl 24);
+ Result.Colors[j, i] := colorB;
+ end;
+ Result.UpdateImage;
+end;
+
+function LoadImage_PNG(const AFileName: TfpgString): TfpgImage;
+var
+ imga: TFPCustomImage;
+begin
+ Result := nil;
if not fpgFileExists(AFileName) then
Exit; //==>
@@ -56,25 +75,12 @@ begin
imga.LoadFromFile(AFileName, TFPReaderPNG.Create); // auto size image
except
imga := nil;
- imgb := nil;
end;
if imga <> nil then
begin
- xlocal := imga.Width;
- ylocal := imga.Height;
- imgb := TfpgImage.Create;
- imgb.AllocateImage(32, xlocal, ylocal); // 32=colordepth
- for i := 0 to ylocal - 1 do
- for j := 0 to xlocal - 1 do
- begin
- colorA := imga.Colors[j, i];
- colorB := (colorA.Blue shr 8) or (colorA.Green and $FF00) or ((colorA.Red and $FF) shl 16) or ((colorA.Alpha and $FF) shl 24);
- imgb.Colors[j, i] := colorB;
- end;
- imgb.UpdateImage;
+ Result := FPImageToFPG(imga);
+ imga.Free;
end;
- imga.Free;
- Result := imgb;
end;
function LoadImage_PNGcrop(const AMaxWidth, AMaxHeight: integer; const AFileName: TfpgString): TfpgImage;