summaryrefslogtreecommitdiff
path: root/img/fpimg.pas
blob: 9c782a04cdd53dafa198876a7782c4e1bed9dda9 (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
{
    fpGUI  -  Free Pascal GUI Library
    
    fpIMG interface declarations. This is the main glue code between 
    different fpIMG submodules such as ImageIO and fpGFX.
    
    Copyright (C) 2000 - 2006 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.
}


unit fpImg;

{$IFDEF Debug}
{$ASSERTIONS On}
{$ENDIF}

interface

uses
  Classes
  ,GFXBase
  ,ImageIO
  ;


function CreateImageFromFile(AScreen: TGfxScreen; AReader: TImageReaderClass;
  const AFilename: String): TGfxImage;

function CreateImageFromStream(AScreen: TGfxScreen; AReader: TImageReaderClass;
  AStream: TStream): TGfxImage;



implementation


function CreateImageFromFile(AScreen: TGfxScreen; AReader: TImageReaderClass;
  const AFilename: String): TGfxImage;
var
  Stream: TFileStream;
begin
  Stream := TFileStream.Create(AFilename, fmOpenRead);
  try
    Result := CreateImageFromStream(AScreen, AReader, Stream);
  finally
    Stream.Free;
  end;
end;


function CreateImageFromStream(AScreen: TGfxScreen; AReader: TImageReaderClass;
  AStream: TStream): TGfxImage;
var
  Reader: TImageReader;
  Data: Pointer;
  Stride: LongWord;
  Palette: TGfxPalette;
begin
  Reader := AReader.Create;
  try
    Reader.ProcessHeaderData(AStream);
    Result := AScreen.Display.CreateImage(Reader.Width, Reader.Height, Reader.PixelFormat);
    if Reader.PaletteSize > 0 then
    begin
      Palette := AScreen.CreatePalette(Reader.PaletteSize, Reader.Palette);
      try
        Result.Palette := Palette;
      finally
        Palette.Release;
      end;
    end;
    try
      Result.Lock(Data, Stride);
      try
        Reader.SetImageSegmentBuffer(Data, Stride, Reader.Height);
        Reader.ProcessImageData(AStream);
      finally
        Result.Unlock;
      end;
    except
      Result.Free;
      raise;
    end;
  finally
    Reader.Free;
  end;
end;

end.