File: gamma_correction.dpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (323 lines) | stat: -rw-r--r-- 6,578 bytes parent folder | download | duplicates (6)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
//
// AggPas 2.4 RM3 Demo application
// Note: Press F1 key on run to see more info about this demo
//
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
//
program
 gamma_correction ;

{$DEFINE AGG_BGR24 }
{DEFINE AGG_RGB24 }
{DEFINE AGG_RGB565 }
{DEFINE AGG_RGB555 }

uses
 agg_basics ,
 agg_platform_support ,

 agg_ctrl ,
 agg_slider_ctrl ,

 agg_rasterizer_scanline_aa ,
 agg_scanline ,
 agg_scanline_u ,

 agg_renderer_base ,
 agg_renderer_scanline ,
 agg_render_scanlines ,

 agg_gamma_lut ,
 agg_gamma_functions ,
 agg_path_storage ,
 agg_conv_stroke ,
 agg_ellipse

{$I pixel_formats.inc } 
{$I agg_mode.inc }

const
 flip_y = true;

type
 the_application = object(platform_support )
   m_thickness ,
   m_gamma     ,
   m_contrast  : slider_ctrl;

   m_rx ,
   m_ry : double;

   constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
   destructor  Destruct;

   procedure on_init; virtual;
   procedure on_draw; virtual;

   procedure on_mouse_move       (x ,y : int; flags : unsigned ); virtual;
   procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;

   procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;

  end;

{ CONSTRUCT }
constructor the_application.Construct;
begin
 inherited Construct(format_ ,flip_y_ );

 m_thickness.Construct(5 ,5 ,400 - 5 ,11 ,not flip_y_ );
 m_gamma.Construct    (5 ,5 + 15 ,400 - 5 ,11 + 15 ,not flip_y_ );
 m_contrast.Construct (5 ,5 + 30 ,400 - 5 ,11 + 30 ,not flip_y_ );

 add_ctrl(@m_thickness );
 add_ctrl(@m_gamma );
 add_ctrl(@m_contrast );

 m_thickness.label_('Thickness=%3.2f' );
 m_gamma.label_    ('Gamma=%3.2f' );
 m_contrast.label_ ('Contrast' );

 m_thickness.range_(0.0 ,3.0 );
 m_gamma.range_    (0.5 ,3.0 );
 m_contrast.range_ (0.0 ,1.0 );

 m_thickness.value_(1.0 );
 m_gamma.value_    (1.0 );
 m_contrast.value_ (1.0 );

end;

{ DESTRUCT }
destructor the_application.Destruct;
begin
 inherited Destruct;

 m_thickness.Destruct;
 m_gamma.Destruct;
 m_contrast.Destruct;

end;

{ ON_INIT }
procedure the_application.on_init;
begin
 m_rx:=_width / 3.0;
 m_ry:=_height / 3.0;

end;

{ ON_DRAW }
procedure the_application.on_draw;
var
 g ,dark ,light ,x ,y ,v ,gval ,dy : double;

 i : unsigned;

 gamma : gamma_lut;
 pixf  : pixel_formats;

 renb : renderer_base;
 ren  : renderer_scanline_aa_solid;

 ras : rasterizer_scanline_aa;
 sl  : scanline_u8;

 rgba : aggclr;
 path : path_storage;

 gm_pw : gamma_power;
 gpoly ,
 poly  : conv_stroke;
 ell   : ellipse;

begin
// Initialize structures
 g:=m_gamma._value;

 gamma.Construct(g );

 pixfmt_gamma(pixf ,rbuf_window ,@gamma );

 renb.Construct(@pixf );
 ren.Construct (@renb );

 rgba.ConstrDbl(1 ,1 ,1 );
 renb.clear    (@rgba );

 dark :=1.0 - m_contrast._value;
 light:=m_contrast._value;

 rgba.ConstrDbl(dark ,dark ,dark );
 renb.copy_bar (0 ,0 ,trunc(_width ) div 2 ,trunc(_height ) ,@rgba );

 rgba.ConstrDbl(light ,light ,light );
 renb.copy_bar (trunc(_width ) div 2 + 1 ,0 ,trunc(_width ) ,trunc(_height ) ,@rgba );

 rgba.ConstrDbl(1.0 ,dark ,dark );
 renb.copy_bar (0 ,trunc(_height ) div 2 + 1 ,trunc(_width ) ,trunc(_height ) ,@rgba );

 ras.Construct;
 sl.Construct;

// Graph line
 path.Construct;

 x:=(_width - 256.0 ) / 2.0;
 y:=50.0;

 for i:=0 to 255 do
  begin
   v:=i / 255.0;

   gm_pw.Construct(g );

   gval:=gm_pw.func_operator_gamma(v );
   dy  :=gval * 255.0;

   if i = 0 then
    path.move_to(x + i ,y + dy )
   else
    path.line_to(x + i ,y + dy );

  end;

 gpoly.Construct(@path );
 gpoly.width_   (2.0 );

 ras.reset;
 ras.add_path(@gpoly );

 rgba.ConstrInt(80 ,127 ,80 );
 ren.color_    (@rgba );

 render_scanlines(@ras ,@sl ,@ren );

// Ellipses
 ell.Construct (_width / 2 ,_height / 2 ,m_rx ,m_ry ,150 );
 poly.Construct(@ell );
 poly.width_   (m_thickness._value );

 ras.reset;
 ras.add_path(@poly );

 rgba.ConstrInt(255 ,0 ,0 );
 ren.color_    (@rgba );

 render_scanlines(@ras ,@sl ,@ren );


 ell.init(_width / 2 ,_height / 2 ,m_rx - 5.0 ,m_ry - 5.0 ,150 );

 ras.reset;
 ras.add_path(@poly );

 rgba.ConstrInt(0 ,255 ,0 );
 ren.color_    (@rgba );

 render_scanlines(@ras ,@sl ,@ren );


 ell.init(_width / 2 ,_height / 2 ,m_rx - 10.0 ,m_ry - 10.0 ,150 );

 ras.reset;
 ras.add_path(@poly );

 rgba.ConstrInt(0 ,0 ,255 );
 ren.color_    (@rgba );

 render_scanlines(@ras ,@sl ,@ren );


 ell.init(_width / 2 ,_height / 2 ,m_rx - 15.0 ,m_ry - 15.0 ,150 );

 ras.reset;
 ras.add_path(@poly );

 rgba.ConstrInt(0 ,0 ,0 );
 ren.color_    (@rgba );

 render_scanlines(@ras ,@sl ,@ren );


 ell.init(_width / 2 ,_height / 2 ,m_rx - 20.0 ,m_ry - 20.0 ,150 );

 ras.reset;
 ras.add_path(@poly );

 rgba.ConstrInt(255 ,255 ,255 );
 ren.color_    (@rgba );

 render_scanlines(@ras ,@sl ,@ren );

// Render the controls
 render_ctrl(@ras ,@sl ,@ren ,@m_thickness );
 render_ctrl(@ras ,@sl ,@ren ,@m_gamma );
 render_ctrl(@ras ,@sl ,@ren ,@m_contrast );

// Free AGG resources
 ras.Destruct;
 sl.Destruct;
 path.Destruct;

 gamma.Destruct;
 gpoly.Destruct;
 poly.Destruct;

end;

{ ON_MOUSE_MOVE }
procedure the_application.on_mouse_move;
begin
 on_mouse_button_down(x ,y ,flags );

end;

{ ON_MOUSE_BUTTON_DOWN }
procedure the_application.on_mouse_button_down;
begin
 if flags and mouse_left <> 0 then
  begin
   m_rx:=Abs(_width / 2 - x );
   m_ry:=Abs(_height / 2 - y );

   force_redraw;

  end;

end;

{ ON_KEY }
procedure the_application.on_key;
begin
 if key = key_f1 then
  message_(
   'Anti-Aliasing is very tricky because everything depends. Particularly, having '#13 +
   'straight linear dependence "pixel coverage" -> "brightness" may be not the best. '#13 +
   'It depends on the type of display (CRT, LCD), contrast, black-on-white vs '#13 +
   'white-on-black, it even depends on your personal vision. There are no linear '#13 +
   'dependencies in this World. This example demonstrates the importance of so called '#13 +
   'Gamma Correction in Anti-Aliasing. There a traditional power function is used, '#13 +
   'in terms of C++ it''s brighness = pow(brighness, gamma). Note, that if you improve '#13 +
   'the quality on the white side, it becomes worse on the black side and vice versa.'#13 +
   #13#13 +
   'How to play with:'#13#13 +
   'Change "Gamma" and see how the quality changes.'#13 +
   'Use the left mouse button to resize the circles.' +
   #13#13'Note: F2 key saves current "screenshot" file in this demo''s directory.  ' );

end;

VAR
 app : the_application;

BEGIN
 app.Construct(pix_format ,flip_y );
 app.caption_ ('AGG Example. Thin red ellipse (F1-Help)' );

 if app.init(400 ,320 ,0 ) then
  app.run;

 app.Destruct;

END.