summaryrefslogtreecommitdiff
path: root/src/corelib/fpg_imgfmt_png.pas
blob: e4a46a7dcc81cc5e62b9bc54e07630bc935ddd9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
{
    fpGUI  -  Free Pascal GUI Toolkit

    Copyright (C) 2006 - 2012 See the file AUTHORS.txt, included in this
    distribution, for details of the copyright.

    See the file COPYING.modifiedLGPL, included in this distribution,
    for details about redistributing fpGUI.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

    Description:
      PNG image loading using the Free Pascal library: fcl-image
}

unit fpg_imgfmt_png;

{$mode objfpc}{$H+}

interface

uses
  SysUtils,
  Classes,
  fpg_base,
  fpg_main,
  FPImage,
  FPReadPNG;

function LoadImage_PNG(const AFileName: TfpgString): TfpgImage;
function LoadImage_PNGcrop(const AMaxWidth, AMaxHeight: integer; const AFileName: TfpgString): TfpgImage;


implementation

uses
  fpg_utils;

function LoadImage_PNG(const AFileName: TfpgString): 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 not fpgFileExists(AFileName) then
    Exit; //==>

  imga := TFPMemoryImage.Create(0, 0);
  try
    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 $FF00) shl 8);
        imgb.Colors[j, i] := colorB;
      end;
    imgb.UpdateImage;
  end;
  imga.Free;
  Result := imgb;
end;

function LoadImage_PNGcrop(const AMaxWidth, AMaxHeight: integer; const AFileName: TfpgString): 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 not fpgFileExists(AFileName) then
    Exit; //==>

          // Maximum image size of AMaxWidth by AMaxHeight.
          // Actual image imga.Width (AMaxWidth) and imga.Height (AMaxHeight).
          // Calculated to fit the image within required size: xlocal, ylocal
  imga := TFPMemoryImage.Create(0, 0);
  try
    imga.LoadFromFile(AFileName, TFPReaderPNG.Create); // auto size image
  except
    imga := nil;
    imgb := nil;
  end;
  if imga <> nil then
  begin
    if imga.Width > AMaxWidth then
      xlocal := AMaxWidth
    else
      xlocal := imga.Width;
    if imga.Height > AMaxHeight then
      ylocal := AMaxHeight
    else
      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 $FF00) shl 8);
        imgb.Colors[j, i] := colorB;
      end;
    imgb.UpdateImage;
  end;
  imga.Free;
  Result := imgb;
end;


end.