-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLImages.pas
189 lines (152 loc) · 5.26 KB
/
GLImages.pas
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
unit GLImages;
interface
uses
SysUtils, DUtils;
type
TGLImage = class
protected
W, H: Integer;
Buf: array of TRGBAColor;
public
bAntialias: Boolean;
constructor Create; virtual;
property Width: Integer read W;
property Height: Integer read H;
procedure Resize(NewWidth, NewHeight: Integer; FillColor: TRGBAColor); virtual;
function GetPixel(X, Y: Integer): TRGBAColor; virtual;
procedure SetPixel(X, Y: Integer; NewColor: TRGBAColor); virtual;
property Pixel[X, Y: Integer]: TRGBAColor read GetPixel write SetPixel;
function GetGreyPixel(X, Y: Integer): Byte; virtual;
procedure SetGreyPixel(X, Y: Integer; NewColor: Byte); virtual;
property GreyPixel[X, Y: Integer]: Byte read GetGreyPixel write SetGreyPixel;
function BakeToA: LongWord; virtual;
function BakeToL: LongWord; virtual;
function BakeToLA: LongWord; virtual;
function BakeToRGBA: LongWord; virtual;
end;
EGLImageError = class(Exception);
implementation
uses
GL, GLU, GLD;
procedure SetAntialias(bAntialias: Boolean);
begin
if bAntialias then begin
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
end
else begin
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
end;
end;
constructor TGLImage.Create;
begin
bAntialias := True;
end;
procedure TGLImage.Resize(NewWidth, NewHeight: Integer; FillColor: TRGBAColor);
var
I: Integer;
begin
//if NewWidth < 0 then raise EGLImageError.Create('Resize to width ' + IntToStr(NewWidth) + ' is invalid.');
//if NewHeight < 0 then raise EGLImageError.Create('Resize to height ' + IntToStr(NewHeight) + ' is invalid.');
SetLength(Buf, 0);
W := NewWidth;
H := NewHeight;
SetLength(Buf, W * H);
for I := 0 to High(Buf) do
Buf[I] := FillColor
;
end;
function TGLImage.GetPixel(X, Y: Integer): TRGBAColor;
begin
if X < 0 then raise EGLImageError.Create('GetPixel X=' + IntToStr(X) + ' is off the image.');
if Y < 0 then raise EGLImageError.Create('GetPixel Y=' + IntToStr(Y) + ' is off the image.');
if X >= W then raise EGLImageError.Create('GetPixel X=' + IntToStr(X) + ' is off the image.');
if Y >= H then raise EGLImageError.Create('GetPixel Y=' + IntToStr(Y) + ' is off the image.');
Result := Buf[X + Y*Width];
end;
procedure TGLImage.SetPixel(X, Y: Integer; NewColor: TRGBAColor);
begin
if X < 0 then raise EGLImageError.Create('SetPixel X=' + IntToStr(X) + ' is off the image.');
if Y < 0 then raise EGLImageError.Create('SetPixel Y=' + IntToStr(Y) + ' is off the image.');
if X >= W then raise EGLImageError.Create('SetPixel X=' + IntToStr(X) + ' is off the image.');
if Y >= H then raise EGLImageError.Create('SetPixel Y=' + IntToStr(Y) + ' is off the image.');
Buf[X + Y*Width] := NewColor;
end;
function TGLImage.GetGreyPixel(X, Y: Integer): Byte;
begin
if X < 0 then raise EGLImageError.Create('GetPixel X=' + IntToStr(X) + ' is off the image.');
if Y < 0 then raise EGLImageError.Create('GetPixel Y=' + IntToStr(Y) + ' is off the image.');
if X >= W then raise EGLImageError.Create('GetPixel X=' + IntToStr(X) + ' is off the image.');
if Y >= H then raise EGLImageError.Create('GetPixel Y=' + IntToStr(Y) + ' is off the image.');
with Buf[X + Y*Width] do
Result := (R + G + B + 1) div 3
;
end;
procedure TGLImage.SetGreyPixel(X, Y: Integer; NewColor: Byte);
begin
if X < 0 then raise EGLImageError.Create('SetPixel X=' + IntToStr(X) + ' is off the image.');
if Y < 0 then raise EGLImageError.Create('SetPixel Y=' + IntToStr(Y) + ' is off the image.');
if X >= W then raise EGLImageError.Create('SetPixel X=' + IntToStr(X) + ' is off the image.');
if Y >= H then raise EGLImageError.Create('SetPixel Y=' + IntToStr(Y) + ' is off the image.');
with Buf[X + Y*Width] do begin
R := NewColor;
G := NewColor;
B := NewColor;
end;
end;
function TGLImage.BakeToA: LongWord;
var
BufRaw: array of Word;
I: Integer;
begin
SetLength(BufRaw, W * H);
for I := 0 to High(BufRaw) do
BufRaw[I] := (Buf[I].A shl 8) or $00FF
;
glGenTextures(1, @Result);
SetBoundTexture(Result);
gluBuild2DMipmaps(GL_TEXTURE_2D, 2, W, H, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, @BufRaw[0]);
SetLength(BufRaw, 0);
SetAntialias(bAntialias);
end;
function TGLImage.BakeToL: LongWord;
var
BufRaw: array of Byte;
I: Integer;
begin
SetLength(BufRaw, W * H);
for I := 0 to High(BufRaw) do
with Buf[I] do
BufRaw[I] := (R + B + G + 1) div 3
;
glGenTextures(1, @Result);
SetBoundTexture(Result);
gluBuild2DMipmaps(GL_TEXTURE_2D, 1, W, H, GL_LUMINANCE, GL_UNSIGNED_BYTE, @BufRaw[0]);
SetLength(BufRaw, 0);
SetAntialias(bAntialias);
end;
function TGLImage.BakeToLA: LongWord;
var
BufRaw: array of Word;
I: Integer;
begin
SetLength(BufRaw, W * H);
for I := 0 to High(BufRaw) do
with Buf[I] do
BufRaw[I] := (A shl 8) or ((R + B + G + 1) div 3)
;
glGenTextures(1, @Result);
SetBoundTexture(Result);
gluBuild2DMipmaps(GL_TEXTURE_2D, 2, W, H, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, @BufRaw[0]);
SetLength(BufRaw, 0);
SetAntialias(bAntialias);
end;
function TGLImage.BakeToRGBA: LongWord;
begin
glGenTextures(1, @Result);
SetBoundTexture(Result);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, W, H, GL_RGBA, GL_UNSIGNED_BYTE, @Buf[0]);
SetAntialias(bAntialias);
end;
end.