File: gradient_focal.cpp

package info (click to toggle)
agg 2.5%2Bdfsg1-8
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 5,640 kB
  • sloc: cpp: 64,972; ansic: 11,272; makefile: 3,645; sh: 31
file content (257 lines) | stat: -rw-r--r-- 8,419 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
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
#include "agg_rendering_buffer.h"
#include "agg_rasterizer_scanline_aa.h"
#include "agg_scanline_u.h"
#include "agg_renderer_scanline.h"
#include "agg_span_allocator.h"
#include "agg_span_gradient.h"
#include "agg_gradient_lut.h"
#include "agg_gamma_lut.h"
#include "agg_span_interpolator_linear.h"
#include "ctrl/agg_slider_ctrl.h"
#include "platform/agg_platform_support.h"


#include "agg_pixfmt_rgb.h"
#define pix_format agg::pix_format_bgr24
typedef agg::pixfmt_bgr24 pixfmt;
typedef agg::rgba8 color_type;
typedef agg::order_bgr component_order;


enum { flip_y = true };


class the_application : public agg::platform_support
{
    typedef agg::renderer_base<pixfmt> renderer_base;
    typedef agg::renderer_scanline_aa_solid<renderer_base> renderer_solid;
    typedef agg::gamma_lut<agg::int8u, agg::int8u> gamma_lut_type;
    typedef agg::gradient_radial_focus gradient_func_type;
    typedef agg::gradient_reflect_adaptor<gradient_func_type> gradient_adaptor_type;
    typedef agg::gradient_lut<agg::color_interpolator<agg::rgba8>, 1024> color_func_type;
    typedef agg::span_interpolator_linear<> interpolator_type;
    typedef agg::span_allocator<color_type> span_allocator_type;
    typedef agg::span_gradient<color_type, 
                               interpolator_type, 
                               gradient_adaptor_type, 
                               color_func_type> span_gradient_type;
    
    agg::slider_ctrl<agg::rgba8>    m_gamma;

    agg::scanline_u8                m_scanline;
    agg::rasterizer_scanline_aa<>   m_rasterizer;
    span_allocator_type             m_alloc;
    color_func_type                 m_gradient_lut;
    gamma_lut_type                  m_gamma_lut;

    double m_mouse_x, m_mouse_y;
    double m_old_gamma;


public:
    the_application(agg::pix_format_e format, bool flip_y) :
        agg::platform_support(format, flip_y),
        m_gamma(5.0, 5.0, 340.0, 12.0, !flip_y),
        m_mouse_x(200), m_mouse_y(200)
    {
        m_gamma.range(0.5, 2.5);
        m_gamma.value(1.8);
        m_gamma.label("Gamma = %.3f");
        add_ctrl(m_gamma);
        m_gamma.no_transform();

        m_gamma_lut.gamma(m_gamma.value());
        m_old_gamma = m_gamma.value();

        build_gradient_lut();
    }

    virtual void on_init()
    {
        m_mouse_y = initial_height() / 2;
        m_mouse_x = initial_width() / 2;
    }

    void build_gradient_lut()
    {
        m_gradient_lut.remove_all();

        m_gradient_lut.add_color(0.0, agg::rgba8_gamma_dir(agg::rgba8(0, 255, 0),   m_gamma_lut));
        m_gradient_lut.add_color(0.2, agg::rgba8_gamma_dir(agg::rgba8(120, 0, 0),   m_gamma_lut));
        m_gradient_lut.add_color(0.7, agg::rgba8_gamma_dir(agg::rgba8(120, 120, 0), m_gamma_lut));
        m_gradient_lut.add_color(1.0, agg::rgba8_gamma_dir(agg::rgba8(0, 0, 255),   m_gamma_lut));

        //m_gradient_lut.add_color(0.0, agg::rgba8::from_wavelength(380, m_gamma.value()));
        //m_gradient_lut.add_color(0.1, agg::rgba8::from_wavelength(420, m_gamma.value()));
        //m_gradient_lut.add_color(0.2, agg::rgba8::from_wavelength(460, m_gamma.value()));
        //m_gradient_lut.add_color(0.3, agg::rgba8::from_wavelength(500, m_gamma.value()));
        //m_gradient_lut.add_color(0.4, agg::rgba8::from_wavelength(540, m_gamma.value()));
        //m_gradient_lut.add_color(0.5, agg::rgba8::from_wavelength(580, m_gamma.value()));
        //m_gradient_lut.add_color(0.6, agg::rgba8::from_wavelength(620, m_gamma.value()));
        //m_gradient_lut.add_color(0.7, agg::rgba8::from_wavelength(660, m_gamma.value()));
        //m_gradient_lut.add_color(0.8, agg::rgba8::from_wavelength(700, m_gamma.value()));
        //m_gradient_lut.add_color(0.9, agg::rgba8::from_wavelength(740, m_gamma.value()));
        //m_gradient_lut.add_color(1.0, agg::rgba8::from_wavelength(780, m_gamma.value()));

        m_gradient_lut.build_lut();
    }

    virtual void on_draw()
    {
        pixfmt pixf(rbuf_window());
        renderer_base rb(pixf);
        renderer_solid rs(rb);
        rb.clear(agg::rgba(1, 1, 1));

        // When Gamma changes rebuild the gamma and gradient LUTs 
        //------------------
        if(m_old_gamma != m_gamma.value())
        {
            m_gamma_lut.gamma(m_gamma.value());
            build_gradient_lut();
            m_old_gamma = m_gamma.value();
        }


        // Gradient center. All gradient functions assume the 
        // center being in the origin (0,0) and you can't 
        // change it. But you can apply arbitrary transformations
        // to the gradient (see below).
        //------------------
        double cx = initial_width()  / 2;
        double cy = initial_height() / 2;
        double r = 100;

        // Focal center. Defined in the gradient coordinates, 
        // that is, with respect to the origin (0,0)
        //------------------
        double fx = m_mouse_x - cx;
        double fy = m_mouse_y - cy;

        gradient_func_type    gradient_func(r, fx, fy);
        gradient_adaptor_type gradient_adaptor(gradient_func);
        agg::trans_affine     gradient_mtx;
        
        // Making the affine matrix. Move to (cx,cy), 
        // apply the resizing transformations and invert
        // the matrix. Gradients and images always assume the
        // inverse transformations.
        //------------------
        gradient_mtx.translate(cx, cy);
        gradient_mtx *= trans_affine_resizing();
        gradient_mtx.invert();

        interpolator_type     span_interpolator(gradient_mtx);
        span_gradient_type    span_gradient(span_interpolator, 
                                          gradient_adaptor, 
                                          m_gradient_lut, 
                                          0, r);

        // Form the simple rectangle 
        //------------------
        m_rasterizer.reset();
        m_rasterizer.move_to_d(0,0);
        m_rasterizer.line_to_d(width(), 0);
        m_rasterizer.line_to_d(width(), height());
        m_rasterizer.line_to_d(0, height());

        // Render the gradient to the whole screen and measure the time
        //------------------
        start_timer();
        agg::render_scanlines_aa(m_rasterizer, m_scanline, rb, m_alloc, span_gradient);
        double tm = elapsed_time();

        // Draw the transformed circle that shows the gradient boundary
        //------------------
        agg::ellipse e(cx, cy, r, r);
        agg::conv_stroke<agg::ellipse> estr(e);
        agg::conv_transform<
            agg::conv_stroke<
                agg::ellipse> > etrans(estr, trans_affine_resizing());

        m_rasterizer.add_path(etrans);
        agg::render_scanlines_aa_solid(m_rasterizer, m_scanline, rb, agg::rgba(1,1,1));

        // Show the gradient time
        //------------------
        char buf[64]; 
        agg::gsv_text t;
        t.size(10.0);
        agg::conv_stroke<agg::gsv_text> pt(t);
        pt.width(1.5);
        sprintf(buf, "%3.2f ms", tm);
        t.start_point(10.0, 35.0);
        t.text(buf);
        m_rasterizer.add_path(pt);
        agg::render_scanlines_aa_solid(m_rasterizer, m_scanline, rb, agg::rgba(0,0,0));

        // Show the controls
        //------------------
        agg::render_ctrl(m_rasterizer, m_scanline, rb, m_gamma);

        // Apply the inverse gamma to the whole buffer 
        // (transform the colors to the perceptually uniform space)
        //------------------
        pixf.apply_gamma_inv(m_gamma_lut);
    }




    virtual void on_mouse_move(int x, int y, unsigned flags)
    {
        if(flags & agg::mouse_left)
        {
            m_mouse_x = x;
            m_mouse_y = y;
            trans_affine_resizing().inverse_transform(&m_mouse_x, &m_mouse_y);
            force_redraw();
        }
    }


    virtual void on_mouse_button_down(int x, int y, unsigned flags)
    {
        if(flags & agg::mouse_left)
        {
            m_mouse_x = x;
            m_mouse_y = y;
            trans_affine_resizing().inverse_transform(&m_mouse_x, &m_mouse_y);
            force_redraw();
        }
    }







};











int agg_main(int argc, char* argv[])
{
    the_application app(pix_format, flip_y);
    app.caption("AGG Example. PDF linear and radial gradients");

    if(app.init(600, 400, agg::window_resize))
    {
        return app.run();
    }
    return 1;
}