~ubuntu-branches/ubuntu/saucy/lazarus/saucy

« back to all changes in this revision

Viewing changes to components/aggpas/image_perspective.dpr

  • Committer: Package Import Robot
  • Author(s): Paul Gevers, Abou Al Montacir, Bart Martens, Paul Gevers
  • Date: 2013-06-08 14:12:17 UTC
  • mfrom: (1.1.9)
  • Revision ID: package-import@ubuntu.com-20130608141217-7k0cy9id8ifcnutc
Tags: 1.0.8+dfsg-1
[ Abou Al Montacir ]
* New upstream major release and multiple maintenace release offering many
  fixes and new features marking a new milestone for the Lazarus development
  and its stability level.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_fixes_branch
* LCL changes:
  - LCL is now a normal package.
      + Platform independent parts of the LCL are now in the package LCLBase
      + LCL is automatically recompiled when switching the target platform,
        unless pre-compiled binaries for this target are already installed.
      + No impact on existing projects.
      + Linker options needed by LCL are no more added to projects that do
        not use the LCL package.
  - Minor changes in LCL basic classes behaviour
      + TCustomForm.Create raises an exception if a form resource is not
        found.
      + TNotebook and TPage: a new implementation of these classes was added.
      + TDBNavigator: It is now possible to have focusable buttons by setting
        Options = [navFocusableButtons] and TabStop = True, useful for
        accessibility and for devices with neither mouse nor touch screen.
      + Names of TControlBorderSpacing.GetSideSpace and GetSpace were swapped
        and are now consistent. GetSideSpace = Around + GetSpace.
      + TForm.WindowState=wsFullscreen was added
      + TCanvas.TextFitInfo was added to calculate how many characters will
        fit into a specified Width. Useful for word-wrapping calculations.
      + TControl.GetColorResolvingParent and
        TControl.GetRGBColorResolvingParent were added, simplifying the work
        to obtain the final color of the control while resolving clDefault
        and the ParentColor.
      + LCLIntf.GetTextExtentExPoint now has a good default implementation
        which works in any platform not providing a specific implementation.
        However, Widgetset specific implementation is better, when available.
      + TTabControl was reorganized. Now it has the correct class hierarchy
        and inherits from TCustomTabControl as it should.
  - New unit in the LCL:
      + lazdialogs.pas: adds non-native versions of various native dialogs,
        for example TLazOpenDialog, TLazSaveDialog, TLazSelectDirectoryDialog.
        It is used by widgetsets which either do not have a native dialog, or
        do not wish to use it because it is limited. These dialogs can also be
        used by user applications directly.
      + lazdeviceapis.pas: offers an interface to more hardware devices such
        as the accelerometer, GPS, etc. See LazDeviceAPIs
      + lazcanvas.pas: provides a TFPImageCanvas descendent implementing
        drawing in a LCL-compatible way, but 100% in Pascal.
      + lazregions.pas. LazRegions is a wholly Pascal implementation of
        regions for canvas clipping, event clipping, finding in which control
        of a region tree one an event should reach, for drawing polygons, etc.
      + customdrawncontrols.pas, customdrawndrawers.pas,
        customdrawn_common.pas, customdrawn_android.pas and
        customdrawn_winxp.pas: are the Lazarus Custom Drawn Controls -controls
        which imitate the standard LCL ones, but with the difference that they
        are non-native and support skinning.
  - New APIs added to the LCL to improve support of accessibility software
    such as screen readers.
* IDE changes:
  - Many improvments.
  - The detailed list of changes can be found here:
    http://wiki.lazarus.freepascal.org/New_IDE_features_since#v1.0_.282012-08-29.29
    http://wiki.lazarus.freepascal.org/Lazarus_1.0_release_notes#IDE_Changes
* Debugger / Editor changes:
  - Added pascal sources and breakpoints to the disassembler
  - Added threads dialog.
* Components changes:
  - TAChart: many fixes and new features
  - CodeTool: support Delphi style generics and new syntax extensions.
  - AggPas: removed to honor free licencing. (Closes: Bug#708695)
[Bart Martens]
* New debian/watch file fixing issues with upstream RC release.
[Abou Al Montacir]
* Avoid changing files in .pc hidden directory, these are used by quilt for
  internal purpose and could lead to surprises during build.
[Paul Gevers]
* Updated get-orig-source target and it compinion script orig-tar.sh so that they
  repack the source file, allowing bug 708695 to be fixed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
{mac_copy:spheres.bmp}
2
 
//
3
 
// AggPas 2.4 RM3 Demo application
4
 
// Note: Press F1 key on run to see more info about this demo
5
 
//
6
 
// Paths: src;src\ctrl;src\svg;src\util;src\platform\win;expat-wrap
7
 
//
8
 
program
9
 
 image_perspective ;
10
 
 
11
 
uses
12
 
 Math ,SysUtils ,
13
 
 
14
 
 agg_basics ,
15
 
 agg_platform_support ,
16
 
 
17
 
 agg_color ,
18
 
 agg_pixfmt ,
19
 
 agg_pixfmt_rgba ,
20
 
 
21
 
 agg_ctrl ,
22
 
 agg_rbox_ctrl ,
23
 
 
24
 
 agg_rendering_buffer ,
25
 
 agg_renderer_base ,
26
 
 agg_renderer_scanline ,
27
 
 agg_rasterizer_scanline_aa ,
28
 
 agg_scanline ,
29
 
 agg_scanline_u ,
30
 
 agg_render_scanlines ,
31
 
 
32
 
 agg_path_storage ,
33
 
 agg_conv_transform ,
34
 
 agg_conv_stroke ,
35
 
 agg_trans_affine ,
36
 
 agg_trans_bilinear ,
37
 
 agg_trans_perspective ,
38
 
 agg_span_interpolator_linear ,
39
 
 agg_span_interpolator_trans ,
40
 
 agg_span_subdiv_adaptor ,
41
 
 agg_span_image_filter ,
42
 
 agg_span_image_filter_rgba ,
43
 
 agg_span_allocator ,
44
 
 agg_image_filters ,
45
 
 agg_gsv_text ,
46
 
 interactive_polygon_ ,
47
 
 file_utils_ ;
48
 
 
49
 
{$I agg_mode.inc }
50
 
 
51
 
const
52
 
 flip_y = true;
53
 
 
54
 
var
55
 
 g_x1 ,
56
 
 g_y1 ,
57
 
 g_x2 ,
58
 
 g_y2 : double;
59
 
 
60
 
 g_rasterizer : rasterizer_scanline_aa;
61
 
 g_scanline   : scanline_u8;
62
 
 
63
 
type
64
 
 the_application = object(platform_support )
65
 
   m_quad       : interactive_polygon;
66
 
   m_trans_type : rbox_ctrl;
67
 
 
68
 
   constructor Construct(format_ : pix_format_e; flip_y_ : boolean );
69
 
   destructor  Destruct;
70
 
 
71
 
   procedure on_init; virtual;
72
 
   procedure on_draw; virtual;
73
 
 
74
 
   procedure on_mouse_move       (x ,y : int; flags : unsigned ); virtual;
75
 
   procedure on_mouse_button_down(x ,y : int; flags : unsigned ); virtual;
76
 
   procedure on_mouse_button_up  (x ,y : int; flags : unsigned ); virtual;
77
 
 
78
 
   procedure on_key(x ,y : int; key ,flags : unsigned ); virtual;
79
 
 
80
 
  end;
81
 
 
82
 
{ CONSTRUCT }
83
 
constructor the_application.Construct;
84
 
begin
85
 
 inherited Construct(format_ ,flip_y_ );
86
 
 
87
 
 m_quad.Construct      (4 ,5.0 );
88
 
 m_trans_type.Construct(420 ,5.0 ,420 + 170.0 ,70.0 ,not flip_y_ );
89
 
 
90
 
 m_trans_type.add_item ('Affine Parallelogram' );
91
 
 m_trans_type.add_item ('Bilinear' );
92
 
 m_trans_type.add_item ('Perspective' );
93
 
 m_trans_type.cur_item_(2 );
94
 
 
95
 
 add_ctrl(@m_trans_type );
96
 
 
97
 
end;
98
 
 
99
 
{ DESTRUCT }
100
 
destructor the_application.Destruct;
101
 
begin
102
 
 inherited Destruct;
103
 
 
104
 
 m_quad.Destruct;
105
 
 m_trans_type.Destruct;
106
 
 
107
 
end;
108
 
 
109
 
{ ON_INIT }
110
 
procedure the_application.on_init;
111
 
var
112
 
 x1 ,y1 ,x2 ,y2 ,dx ,dy : double;
113
 
 
114
 
begin
115
 
 g_x1:=0.0;
116
 
 g_y1:=0.0;
117
 
 g_x2:=rbuf_img(0 )._width;
118
 
 g_y2:=rbuf_img(0 )._height;
119
 
 
120
 
 x1:=g_x1;// * 100.0;
121
 
 y1:=g_y1;// * 100.0;
122
 
 x2:=g_x2;// * 100.0;
123
 
 y2:=g_y2;// * 100.0;
124
 
 
125
 
 dx:=_width  / 2.0 - (x2 - x1 ) / 2.0;
126
 
 dy:=_height / 2.0 - (y2 - y1 ) / 2.0;
127
 
 
128
 
{ m_quad.xn_ptr(0 )^:=Floor(x1 + dx + 50 );
129
 
 m_quad.yn_ptr(0 )^:=Floor(y1 + dy + 50 );
130
 
 m_quad.xn_ptr(1 )^:=Floor(x2 + dx );
131
 
 m_quad.yn_ptr(1 )^:=Floor(y1 + dy );
132
 
 m_quad.xn_ptr(2 )^:=Floor(x2 + dx );
133
 
 m_quad.yn_ptr(2 )^:=Floor(y2 + dy );
134
 
 m_quad.xn_ptr(3 )^:=Floor(x1 + dx );
135
 
 m_quad.yn_ptr(3 )^:=Floor(y2 + dy ); {}
136
 
 
137
 
 m_quad.xn_ptr(0 )^:=100 + 50;
138
 
 m_quad.yn_ptr(0 )^:=100 + 50;
139
 
 m_quad.xn_ptr(1 )^:=_width  - 100;
140
 
 m_quad.yn_ptr(1 )^:=100;
141
 
 m_quad.xn_ptr(2 )^:=_width  - 100;
142
 
 m_quad.yn_ptr(2 )^:=_height - 100;
143
 
 m_quad.xn_ptr(3 )^:=100;
144
 
 m_quad.yn_ptr(3 )^:=_height - 100;
145
 
 
146
 
end;
147
 
 
148
 
{ ON_DRAW }
149
 
procedure the_application.on_draw;
150
 
var
151
 
 pixf ,pixf_pre : pixel_formats;
152
 
 
153
 
 rb ,rb_pre : renderer_base;
154
 
 
155
 
 rgba : aggclr;
156
 
 
157
 
 r  : renderer_scanline_aa_solid;
158
 
 sa : span_allocator;
159
 
 tr : trans_affine;
160
 
 sg : span_image_filter_ptr;
161
 
 ri : renderer_scanline_aa;
162
 
 
163
 
 trb : trans_bilinear;
164
 
 trp : trans_perspective;
165
 
 
166
 
 interpolator   : span_interpolator_linear;
167
 
 interp_trans   : span_interpolator_trans;
168
 
 subdiv_adaptor : span_subdiv_adaptor;
169
 
 
170
 
 filter_kernel : image_filter_hermite;
171
 
 filter        : image_filter_lut;
172
 
 
173
 
 tm  : double;
174
 
 buf : array[0..63 ] of char;
175
 
 t   : gsv_text;
176
 
 pt  : conv_stroke;
177
 
 
178
 
begin
179
 
 sg:=NIL;
180
 
 
181
 
// Initialize structures
182
 
 pixfmt_bgra32    (pixf ,rbuf_window );     
183
 
 pixfmt_bgra32_pre(pixf_pre ,rbuf_window );
184
 
 
185
 
 rb.Construct    (@pixf );
186
 
 rb_pre.Construct(@pixf_pre );
187
 
 r.Construct     (@rb );
188
 
 
189
 
 rgba.ConstrDbl(1 ,1 ,1 );
190
 
 rb.clear      (@rgba );
191
 
 
192
 
 if m_trans_type._cur_item = 0 then
193
 
  begin
194
 
  // For the affine parallelogram transformations we
195
 
  // calculate the 4-th (implicit) point of the parallelogram
196
 
   m_quad.xn_ptr(3 )^:=m_quad.xn(0 ) + (m_quad.xn(2 ) - m_quad.xn(1 ) );
197
 
   m_quad.yn_ptr(3 )^:=m_quad.yn(0 ) + (m_quad.yn(2 ) - m_quad.yn(1 ) );
198
 
 
199
 
  end;
200
 
 
201
 
// Render the "quad" tool
202
 
 g_rasterizer.add_path(@m_quad );
203
 
 
204
 
 rgba.ConstrDbl  (0 ,0.3 ,0.5 ,0.6 );
205
 
 r.color_        (@rgba );
206
 
 render_scanlines(@g_rasterizer ,@g_scanline ,@r );
207
 
 
208
 
// Prepare the polygon to rasterize. Here we need to fill
209
 
// the destination (transformed) polygon.
210
 
 g_rasterizer.clip_box(0 ,0 ,_width ,_height );
211
 
 g_rasterizer.reset;
212
 
 g_rasterizer.move_to_d(m_quad.xn(0 ) ,m_quad.yn(0 ) );
213
 
 g_rasterizer.line_to_d(m_quad.xn(1 ) ,m_quad.yn(1 ) );
214
 
 g_rasterizer.line_to_d(m_quad.xn(2 ) ,m_quad.yn(2 ) );
215
 
 g_rasterizer.line_to_d(m_quad.xn(3 ) ,m_quad.yn(3 ) );
216
 
 
217
 
 sa.Construct;
218
 
 filter_kernel.Construct;
219
 
 filter.Construct(@filter_kernel ,false );
220
 
 
221
 
 rgba.ConstrPre(0 ,0 ,0 ,0 );
222
 
 
223
 
 start_timer;
224
 
 
225
 
 case m_trans_type._cur_item of
226
 
  0 :
227
 
   begin
228
 
   // Note that we consruct an affine matrix that transforms
229
 
   // a parallelogram to a rectangle, i.e., it's inverted.
230
 
   // It's actually the same as:
231
 
   // tr(g_x1, g_y1, g_x2, g_y2, m_triangle.polygon());
232
 
   // tr.invert();
233
 
    tr.Construct(parallelo_ptr(m_quad.polygon ) ,g_x1 ,g_y1 ,g_x2 ,g_y2 );
234
 
 
235
 
   // Also note that we can use the linear interpolator instead of
236
 
   // arbitrary span_interpolator_trans. It works much faster,
237
 
   // but the transformations must be linear and parellel.
238
 
    interpolator.Construct(@tr );
239
 
 
240
 
    sg:=new(
241
 
     span_image_filter_rgba_nn_ptr ,
242
 
     Construct(
243
 
      @sa ,rbuf_img(0 ) ,@rgba ,@interpolator ,bgra_order ) );
244
 
 
245
 
    ri.Construct    (@rb_pre ,sg );
246
 
    render_scanlines(@g_rasterizer ,@g_scanline ,@ri );
247
 
 
248
 
   end;
249
 
 
250
 
  1 :
251
 
   begin
252
 
    trb.Construct(m_quad.polygon ,g_x1 ,g_y1 ,g_x2 ,g_y2 );
253
 
 
254
 
    if trb.is_valid then
255
 
     begin
256
 
      interpolator.Construct(@trb );
257
 
 
258
 
      sg:=new(
259
 
       span_image_filter_rgba_2x2_ptr ,
260
 
       Construct(
261
 
        @sa ,rbuf_img(0 ) ,@rgba ,@interpolator ,@filter ,bgra_order ) );
262
 
 
263
 
      ri.Construct    (@rb_pre ,sg );
264
 
      render_scanlines(@g_rasterizer ,@g_scanline ,@ri );
265
 
 
266
 
     end;
267
 
 
268
 
   end;
269
 
 
270
 
  2 :
271
 
   begin
272
 
    trp.Construct(pointer(m_quad.polygon ) ,g_x1 ,g_y1 ,g_x2 ,g_y2 );
273
 
 
274
 
    if trp.is_valid then
275
 
     begin
276
 
     // interpolator.Construct  (@trp );
277
 
     // subdiv_adaptor.Construct(@interpolator );
278
 
 
279
 
      interp_trans.Construct(@trp );
280
 
 
281
 
      sg:=new(
282
 
       span_image_filter_rgba_2x2_ptr ,
283
 
       Construct(
284
 
        @sa ,rbuf_img(0 ) ,@rgba ,@interp_trans ,@filter ,bgra_order ) );
285
 
 
286
 
      ri.Construct    (@rb_pre ,sg );
287
 
      render_scanlines(@g_rasterizer ,@g_scanline ,@ri );
288
 
 
289
 
     end;
290
 
 
291
 
   end;
292
 
 
293
 
 end;
294
 
 
295
 
// Render Text
296
 
 tm:=elapsed_time;
297
 
 
298
 
 t.Construct;
299
 
 t.size_(10.0 );
300
 
 
301
 
 pt.Construct(@t );
302
 
 pt.width_   (1.5 );
303
 
 
304
 
 sprintf       (@buf[0 ] ,'%3.2f ms' ,tm );
305
 
 t.start_point_(10.0 ,10.0 );
306
 
 t.text_       (@buf[0 ] );
307
 
 
308
 
 g_rasterizer.add_path(@pt );
309
 
 
310
 
 rgba.ConstrDbl  (0 ,0 ,0 );
311
 
 r.color_        (@rgba );
312
 
 render_scanlines(@g_rasterizer ,@g_scanline ,@r );
313
 
 
314
 
// Render the controls
315
 
 render_ctrl(@g_rasterizer ,@g_scanline ,@r ,@m_trans_type );
316
 
 
317
 
// Free AGG resources
318
 
 sa.Destruct;
319
 
 filter.Destruct;
320
 
 
321
 
 if sg <> NIL then
322
 
  dispose(sg ,Destruct );
323
 
 
324
 
 t.Destruct;
325
 
 pt.Destruct; 
326
 
 
327
 
end;
328
 
 
329
 
{ ON_MOUSE_MOVE }
330
 
procedure the_application.on_mouse_move;
331
 
begin
332
 
 if flags and mouse_left <> 0 then
333
 
  if m_quad.on_mouse_move(x ,y ) then
334
 
   force_redraw;
335
 
 
336
 
 if flags and mouse_left = 0 then
337
 
  on_mouse_button_up(x ,y ,flags );
338
 
 
339
 
end;
340
 
 
341
 
{ ON_MOUSE_BUTTON_DOWN }
342
 
procedure the_application.on_mouse_button_down;
343
 
var
344
 
 buf : array[0..99 ] of char;
345
 
 
346
 
begin
347
 
 if flags and mouse_left <> 0 then
348
 
  if m_quad.on_mouse_button_down(x ,y ) then
349
 
   force_redraw;
350
 
 
351
 
 if flags and mouse_right <> 0 then
352
 
  begin
353
 
   sprintf (@buf[0 ]             ,'%d ' ,x );
354
 
   sprintf (@buf[StrLen(@buf ) ] ,'%d'  ,y );
355
 
   message_(@buf[0 ] );
356
 
 
357
 
  end;
358
 
 
359
 
end;
360
 
 
361
 
{ ON_MOUSE_BUTTON_UP }
362
 
procedure the_application.on_mouse_button_up;
363
 
begin
364
 
 if m_quad.on_mouse_button_up(x ,y ) then
365
 
  force_redraw;
366
 
 
367
 
end;
368
 
 
369
 
{ ON_KEY }
370
 
procedure the_application.on_key;
371
 
begin
372
 
 if key = key_f1 then
373
 
  message_(
374
 
   'Image perspective transformations. There are two types of arbitrary quadrangle '#13 +
375
 
   'transformations, Perspective and Bilinear. The image transformer always uses '#13 +
376
 
   'reverse transformations, and there is a problem. The Perspective transformations '#13 +
377
 
   'are perfectly reversible, so they work correctly with images, but the Bilinear '#13 +
378
 
   'transformer behave somehow strange. It can transform a rectangle to a quadrangle, '#13 +
379
 
   'but not vice versa. In this example you can see this effect, when the edges of '#13 +
380
 
   'the image "sag". I''d highly appreciate if someone could help me with math for '#13 +
381
 
   'transformations similar to Bilinear ones, but correctly reversible (i.e., that '#13 +
382
 
   'can transform an arbitrary quadrangle to a rectangle). The bilinear transformations '#13 +
383
 
   'are simple, see agg_trans_bilinear.h and agg_simul_eq.h' +
384
 
   #13#13'Note: F2 key saves current "screenshot" file in this demo''s directory.  ' );
385
 
 
386
 
end;
387
 
 
388
 
VAR
389
 
 app : the_application;
390
 
 buf : array [0..255 ] of char;
391
 
 ext : string[10 ];
392
 
 
393
 
 img_name ,p ,n ,x : shortstring;
394
 
 
395
 
BEGIN
396
 
 g_x1:=0;
397
 
 g_y1:=0;
398
 
 g_x2:=0;
399
 
 g_y2:=0;
400
 
 
401
 
 g_rasterizer.Construct;
402
 
 g_scanline.Construct;
403
 
 
404
 
 app.Construct(pix_format_bgra32 ,flip_y );
405
 
 app.caption_ ('AGG Example. Image Perspective Transformations (F1-Help)' );
406
 
 
407
 
 img_name:='spheres';
408
 
 
409
 
{$IFDEF WIN32 }
410
 
 if ParamCount > 0 then
411
 
  begin
412
 
   spread_name(ParamStr(1 ) ,p ,n ,x );
413
 
 
414
 
   img_name:=fold_name(p ,n ,'' );
415
 
 
416
 
  end;
417
 
 
418
 
{$ENDIF }  
419
 
 
420
 
 if not app.load_img(0 ,img_name ) then
421
 
  begin
422
 
   img_name:=img_name + #0;
423
 
   ext     :=app._img_ext + #0;
424
 
 
425
 
   if img_name = 'spheres'#0 then
426
 
    begin
427
 
     sprintf(@buf[0 ]             ,'File not found: %s' ,ptrcomp(@img_name[1 ] ) );
428
 
     sprintf(@buf[StrLen(@buf ) ] ,'%s. '#13'Download http://www.antigrain.com/' ,ptrcomp(@ext[1 ] ) );
429
 
     sprintf(@buf[StrLen(@buf ) ] ,'%s' ,ptrcomp(@img_name[1 ] ) );
430
 
     sprintf(@buf[StrLen(@buf ) ] ,'%s'#13'or copy it from another directory if available.' ,ptrcomp(@ext[1 ] ) );
431
 
 
432
 
    end
433
 
   else
434
 
    begin
435
 
     sprintf(@buf[0 ]             ,'File not found: %s' ,ptrcomp(@img_name[1 ] ) );
436
 
     sprintf(@buf[StrLen(@buf ) ] ,'%s' ,ptrcomp(@ext[1 ] ) );
437
 
 
438
 
    end;
439
 
 
440
 
   app.message_(@buf[0 ] );
441
 
 
442
 
  end
443
 
 else
444
 
  if app.init(600 ,600 ,window_resize ) then
445
 
   app.run;
446
 
 
447
 
 app.Destruct;
448
 
 
449
 
 g_rasterizer.Destruct;
450
 
 g_scanline.Destruct;
451
 
 
452
 
END.
 
 
b'\\ No newline at end of file'