1//
2// AggPas 2.4 RM3 Demo application
3// Note: Press F1 key on run to see more info about this demo
4//
5// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
6//
7program
8 gamma_ctrl_ ;
9
10{DEFINE AGG_GRAY8 }
11{$DEFINE AGG_BGR24 }
12{DEFINE AGG_RGB24 }
13{DEFINE AGG_BGRA32 }
14{DEFINE AGG_RGBA32 }
15{DEFINE AGG_ARGB32 }
16{DEFINE AGG_ABGR32 }
17{DEFINE AGG_RGB565 }
18{DEFINE AGG_RGB555 }
19
20uses
21 agg_basics ,
22 agg_platform_support ,
23
24 agg_ctrl ,
25 agg_gamma_ctrl ,
26
27 agg_rasterizer_scanline_aa ,
28 agg_scanline ,
29 agg_scanline_p ,
30
31 agg_renderer_base ,
32 agg_renderer_scanline ,
33 agg_render_scanlines ,
34
35 agg_gsv_text ,
36 agg_conv_stroke ,
37 agg_conv_transform ,
38 agg_path_storage ,
39 agg_ellipse ,
40 agg_trans_affine
41
42{$I pixel_formats.inc }
43{$I agg_mode.inc }
44{$I- }
45const
46 flip_y = true;
47
48var
49 g_ctrl : gamma_ctrl;
50
51type
52 the_application = object(platform_support )
53   constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
54   destructor  Destruct;
55
56   procedure on_init; virtual;
57   procedure on_draw; virtual;
58
59   procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
60
61  end;
62
63{ READ_GAMMA }
64procedure read_gamma(fname : shortstring );
65var
66 fd : text;
67
68 kx1 ,ky1 ,kx2 ,ky2 : double;
69
70begin
71 assignfile(fd ,fname );
72 reset     (fd );
73
74 readln(fd ,kx1 );
75 readln(fd ,ky1 );
76 readln(fd ,kx2 );
77 readln(fd ,ky2 );
78
79 g_ctrl.values(kx1 ,ky1 ,kx2 ,ky2 );
80
81 close(fd );
82
83end;
84
85{ WRITE_GAMMA_BIN }
86procedure write_gamma_bin(fname : shortstring );
87var
88 fd : file;
89
90 gamma : char_ptr;
91
92begin
93 gamma:=g_ctrl.gamma;
94
95 assignfile(fd ,fname );
96 rewrite   (fd ,1 );
97 blockwrite(fd ,gamma^ ,256 );
98 close     (fd );
99
100end;
101
102{ WRITE_GAMMA_TXT }
103procedure write_gamma_txt(fname : shortstring );
104var
105 fd : text;
106
107 gamma : char_ptr;
108 i ,j  : int;
109
110 kx1 ,ky1 ,kx2 ,ky2 : double;
111
112begin
113 gamma:=g_ctrl.gamma;
114
115 assignfile(fd ,fname );
116 rewrite   (fd );
117
118 g_ctrl.values(@kx1 ,@ky1 ,@kx2 ,@ky2 );
119
120 writeln(fd ,kx1:1:3 );
121 writeln(fd ,ky1:1:3 );
122 writeln(fd ,kx2:1:3 );
123 writeln(fd ,ky2:1:3 );
124
125 for i:=0 to 15 do
126  begin
127   for j:= 0 to 15 do
128    write(fd ,int8u_ptr(ptrcomp(gamma ) + i * 16 + j )^:3 ,',' );
129
130   writeln(fd );
131
132  end;
133
134 close(fd );
135
136end;
137
138{ CONSTRUCT }
139constructor the_application.Construct;
140begin
141 inherited Construct(format_ ,flip_y_ );
142
143 add_ctrl(@g_ctrl );
144
145end;
146
147{ DESTRUCT }
148destructor the_application.Destruct;
149begin
150 inherited Destruct;
151
152 write_gamma_txt('gamma.txt' );
153 write_gamma_bin('gamma.bin' );
154
155end;
156
157{ ON_INIT }
158procedure the_application.on_init;
159begin
160 read_gamma('gamma.txt' );
161
162end;
163
164{ ON_DRAW }
165procedure the_application.on_draw;
166var
167 ewidth ,ecenter : double;
168
169 i : int;
170
171 pixf : pixel_formats;
172
173 rb : renderer_base;
174 r  : renderer_scanline_aa_solid;
175
176 ras : rasterizer_scanline_aa;
177 sl  : scanline_p8;
178
179 rgba  : aggclr;
180 elli  : ellipse;
181 poly  : conv_stroke;
182 tpoly : conv_transform;
183
184 mtx : trans_affine;
185 tas : trans_affine_skewing;
186 tar : trans_affine_rotation;
187 tat : trans_affine_translation;
188
189 text  : gsv_text;
190 text1 : gsv_text_outline;
191 path  : path_storage;
192 trans : conv_transform;
193
194begin
195// Initialize structures
196 ewidth :=_initial_width / 2 - 10;
197 ecenter:=_initial_width / 2;
198
199 pixfmt(pixf ,rbuf_window );
200
201 rb.Construct(@pixf );
202 r.Construct (@rb );
203
204 rgba.ConstrDbl(1 ,1 ,1 );
205 rb.clear      (@rgba );
206
207 g_ctrl.text_size_(10.0 ,12.0 );
208
209 ras.Construct;
210 sl.Construct;
211
212// Render the controls
213 render_ctrl(@ras ,@sl ,@r ,@g_ctrl );
214
215 ras.gamma(@g_ctrl );
216
217// Ellipses
218 elli.Construct;
219 poly.Construct (@elli );
220 tpoly.Construct(@poly ,_trans_affine_resizing );
221
222 rgba.ConstrInt(0 ,0 ,0 );
223 r.color_      (@rgba );
224
225 elli.init   (ecenter ,220 ,ewidth ,15 ,100 );
226 poly.width_ (2.0 );
227 ras.add_path(@tpoly ,0 );
228
229 render_scanlines(@ras ,@sl ,@r );
230
231 elli.init   (ecenter ,220 ,11 ,11 ,100 );
232 poly.width_ (2.0 );
233 ras.add_path(@tpoly ,0 );
234
235 render_scanlines(@ras ,@sl ,@r );
236
237 rgba.ConstrInt(127 ,127 ,127 );
238 r.color_      (@rgba );
239
240 elli.init   (ecenter ,260 ,ewidth ,15 ,100 );
241 poly.width_ (2.0 );
242 ras.add_path(@tpoly ,0 );
243
244 render_scanlines(@ras ,@sl ,@r );
245
246 elli.init   (ecenter ,260 ,11 ,11 ,100 );
247 poly.width_ (2.0 );
248 ras.add_path(@tpoly ,0 );
249
250 render_scanlines(@ras ,@sl ,@r );
251
252 rgba.ConstrInt(192 ,192 ,192 );
253 r.color_      (@rgba );
254
255 elli.init   (ecenter ,300 ,ewidth ,15 ,100 );
256 poly.width_ (2.0 );
257 ras.add_path(@tpoly ,0 );
258
259 render_scanlines(@ras ,@sl ,@r );
260
261 elli.init   (ecenter ,300 ,11 ,11 ,100 );
262 poly.width_ (2.0 );
263 ras.add_path(@tpoly ,0 );
264
265 render_scanlines(@ras ,@sl ,@r );
266
267 rgba.ConstrDbl(0.0 ,0.0 ,0.4 );
268 r.color_      (@rgba );
269
270 elli.init   (ecenter ,340 ,ewidth ,15.5 ,100 );
271 poly.width_ (1.0 );
272 ras.add_path(@tpoly ,0 );
273
274 render_scanlines(@ras ,@sl ,@r );
275
276 elli.init   (ecenter ,340 ,10.5 ,10.5 ,100 );
277 poly.width_ (1.0 );
278 ras.add_path(@tpoly ,0 );
279
280 render_scanlines(@ras ,@sl ,@r );
281
282 elli.init   (ecenter ,380 ,ewidth ,15.5 ,100 );
283 poly.width_ (0.4 );
284 ras.add_path(@tpoly ,0 );
285
286 render_scanlines(@ras ,@sl ,@r );
287
288 elli.init   (ecenter ,380 ,10.5 ,10.5 ,100 );
289 poly.width_ (0.4 );
290 ras.add_path(@tpoly ,0 );
291
292 render_scanlines(@ras ,@sl ,@r );
293
294 elli.init   (ecenter ,420 ,ewidth ,15.5 ,100 );
295 poly.width_ (0.1 );
296 ras.add_path(@tpoly ,0 );
297
298 render_scanlines(@ras ,@sl ,@r );
299
300 elli.init   (ecenter ,420 ,10.5 ,10.5 ,100 );
301 poly.width_ (0.1 );
302 ras.add_path(@tpoly ,0 );
303
304 render_scanlines(@ras ,@sl ,@r );
305
306// Text
307 mtx.Construct;
308 tas.Construct(0.15 ,0.0 );
309 mtx.Multiply (@tas );
310 mtx.Multiply (_trans_affine_resizing );
311
312 text.Construct;
313 text1.Construct(@text ,@mtx );
314
315 text.text_       ('Text 2345' );
316 text.size_       (50 ,20 );
317 text1.width_     (2.0 );
318 text.start_point_(320 ,10 );
319
320 rgba.ConstrDbl(0.0 ,0.5 ,0.0 );
321 r.color_      (@rgba );
322
323 ras.add_path    (@text1 ,0 );
324 render_scanlines(@ras ,@sl ,@r );
325
326// Triangled circle
327 rgba.ConstrDbl(0.5 ,0.0 ,0.0 );
328 r.color_      (@rgba );
329
330 path.Construct;
331 path.move_to(30 ,-1.0 );
332 path.line_to(60 ,0.0 );
333 path.line_to(30 ,1.0 );
334
335 path.move_to(27 ,-1.0 );
336 path.line_to(10 ,0.0 );
337 path.line_to(27 ,1.0 );
338
339 trans.Construct(@path ,@mtx );
340
341 for i:=0 to 34 do
342  begin
343   tar.Construct(i / 35.0 * pi * 2.0 );
344   tat.construct(400 ,130 );
345
346   mtx.reset;
347   mtx.multiply(@tar );
348   mtx.multiply(@tat );
349   mtx.multiply(_trans_affine_resizing );
350
351   ras.add_path    (@trans ,0 );
352   render_scanlines(@ras ,@sl ,@r );
353
354  end;
355
356// Free AGG resources
357 ras.Destruct;
358 sl.Destruct;
359
360 poly.Destruct;
361 text.Destruct;
362 text1.Destruct;
363 path.Destruct;
364
365end;
366
367{ ON_KEY }
368procedure the_application.on_key;
369begin
370 if key = key_f1 then
371  message_(
372   'This is another experiment with gamma correction. See also Gamma Correction. '#13 +
373   'I presumed that we can do better than with a traditional power function. '#13 +
374   'So, I created a special control to have an arbitrary gamma function. '#13 +
375   'The conclusion is that we can really achieve a better visual result with '#13 +
376   'this control, but still, in practice, the traditional power function is good '#13 +
377   'enough too.'#13#13 +
378   'How to play with:'#13#13 +
379   'Feel free to change the gamma curve. The shape you''ll set up, will be'#13 +
380   'stored in external file "gamma.txt".' +
381   #13#13'Note: F2 key saves current "screenshot" file in this demo''s directory.  ' );
382
383end;
384
385VAR
386 app : the_application;
387
388BEGIN
389 g_ctrl.Construct(10.0 ,10.0 ,300.0 ,200.0 ,not flip_y );
390
391 app.Construct(pix_format ,flip_y );
392 app.caption_ ('Anti-Aliasing Gamma Correction (F1-Help)' );
393
394 if app.init(500 ,400 ,window_resize ) then
395  app.run;
396
397 app.Destruct;
398 g_ctrl.Destruct;
399
400END.