aboutsummaryrefslogtreecommitdiffstats
path: root/Game/Code/MacOSX/Wrapper/Graphics.pas
blob: 39462607c1965a1f6225f412dacbd27fb16d0c8d (plain) (blame)
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
unit Graphics;

{$I switches.inc}

interface

uses
    Classes, SysUtils, Windows, FreeBitmap, FreeImage;

type
    TPixelFormat = (pfDevice, pf1bit, pf4bit, pf8bit, pf15bit, pf16bit, pf24bit, pf32bit, pfCustom);
    TColor = -$7FFFFFFF-1..$7FFFFFFF;

    TCanvas = class
    private
        FImage : TFreeBitmap;
        function GetPixel(x, y: Integer): TColor;
        procedure SetPixel(x, y: Integer; const Value: TColor);
    public
        Constructor Create(const bmp : TFreeBitmap);
        property Pixels[x,y : Integer] : TColor read GetPixel write SetPixel;
    end;

    TBitmap = class
    private
        FCanvas : TCanvas;
        function GetHeight: Integer;
        function GetWidth: Integer;
        procedure SetHeight(const Value: Integer);
        procedure SetWidth(const Value: Integer);
        function GetPixelFormat: TPixelFormat;
        procedure SetPixelFormat(const Value: TPixelFormat);
        function GetScanLine(Line: Integer): Pointer;
    protected
        FImage : TFreeBitmap;
    public
        Constructor Create;
        Destructor Destroy; override;
        Procedure LoadFromStream(const str : TStream);
        Procedure LoadFromFile(const f : String);
        Procedure SaveToFile(const f : String); virtual;
        Procedure Assign(const src : TBitmap);
        property Width : Integer read GetWidth write SetWidth;
        property Height : Integer read GetHeight write SetHeight;
        property PixelFormat : TPixelFormat read GetPixelFormat write SetPixelFormat;
        property ScanLine[Line : Integer] : Pointer read GetScanLine;
        property Canvas : TCanvas read FCanvas;
    end;

implementation

{ TBitmap }

type
    TRealRGB = packed record
        rgbRed: Byte;
        rgbGreen: Byte;
        rgbBlue: Byte;
        rgbReserved: Byte;
    end;

procedure TBitmap.Assign(const src: TBitmap);
begin
    FImage.Assign(src.FImage);
    FCanvas.FImage := FImage;
end;

constructor TBitmap.Create;
begin
    FImage  := TFreeBitmap.Create( FIF_BMP, 4, 4, 24);
    FCanvas := TCanvas.Create(FImage);
end;

destructor TBitmap.Destroy;
begin
    FCanvas.Free;
    FImage.Free;
    inherited;
end;

function TBitmap.GetHeight: Integer;
begin
    Result := FImage.GetHeight;
end;

function TBitmap.GetPixelFormat: TPixelFormat;
begin
    Result := pf24bit;

    case FImage.GetBitsPerPixel of
        1  : Result := pf1bit;
        4  : Result := pf4bit;
        8  : Result := pf8bit;
        15 : Result := pf15bit;
        16 : Result := pf16bit;
        24 : Result := pf24bit;
        32 : Result := pf32bit;
    end;
end;

function TBitmap.GetScanLine(Line: Integer): Pointer;
begin
    Result := FImage.GetScanLine(Height-1-Line);
end;

function TBitmap.GetWidth: Integer;
begin
    Result := FImage.GetWidth;
end;

procedure TBitmap.LoadFromFile(const f: String);
begin
    FreeAndNil(FCanvas);
    FreeAndNil(FImage);
    FImage := TFreeBitmap.Create;
    FImage.Load(f);
    FCanvas := TCanvas.Create(FImage);
end;

procedure TBitmap.LoadFromStream(const str: TStream);
begin
    FreeAndNil(FCanvas);
    FreeAndNil(FImage);
    FImage := TFreeBitmap.Create;
    FImage.LoadFromStream(str);
    FCanvas := TCanvas.Create(FImage);
end;

procedure TBitmap.SaveToFile(const f: String);
begin
    FImage.Save(f);
end;

procedure TBitmap.SetHeight(const Value: Integer);
begin
    if Value <> Height then begin
        FImage.Rescale( Width, Value, FILTER_BILINEAR);
    end;
end;

procedure TBitmap.SetPixelFormat(const Value: TPixelFormat);
begin
    if Value <> PixelFormat then begin
        case Value of
            pf4bit  : FImage.ConvertTo4Bits;
            pf8bit  : FImage.ConvertTo8Bits;
            pf15bit : FImage.ConvertTo16Bits555;
            pf16bit : FImage.ConvertTo16Bits565;
            pf24bit : FImage.ConvertTo24Bits;
            pf32bit : FImage.ConvertTo32Bits;
        end;
    end;
end;

procedure TBitmap.SetWidth(const Value: Integer);
begin
    if Value <> Width then begin
        FImage.Rescale( Value, Height, FILTER_BILINEAR);
    end;
end;

{ TCanvas }

constructor TCanvas.Create(const bmp: TFreeBitmap);
begin
    FImage := bmp;
end;

function TCanvas.GetPixel(x, y: Integer): TColor;
var
    pix : TRGBQuad;
begin
    FImage.GetPixelColor( x, FImage.GetHeight-1-y, @pix);
    Result := TColor(pix);
end;

procedure TCanvas.SetPixel(x, y: Integer; const Value: TColor);
var
    pixRGB : TRealRGB;
    pixBGR : TRGBQuad;
begin
    Move( Value, pixRGB, SizeOf(pixRGB));
    pixBGR.rgbRed      := pixRGB.rgbRed;
    pixBGR.rgbGreen    := pixRGB.rgbGreen;
    pixBGR.rgbBlue     := pixRGB.rgbBlue;
    pixBGR.rgbReserved := pixRGB.rgbReserved;
    FImage.SetPixelColor( x, FImage.GetHeight-1-y, @pixBGR);
end;

end.