root/trunk/include/mapnik/graphics.hpp @ 1681

Revision 1681, 13.0 kB (checked in by dane, 6 months ago)

+ use rint(v) instead of int(round(v)) + add rint implementation - msvc hasn't got one + minor cleanups

Line 
1/*****************************************************************************
2 *
3 * This file is part of Mapnik (c++ mapping toolkit)
4 *
5 * Copyright (C) 2006 Artem Pavlenko
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 *
21 *****************************************************************************/
22
23//$Id: graphics.hpp 39 2005-04-10 20:39:53Z pavlenko $
24
25#ifndef GRAPHICS_HPP
26#define GRAPHICS_HPP
27// mapnik
28#include <mapnik/color.hpp>
29#include <mapnik/gamma.hpp>
30#include <mapnik/image_data.hpp>
31#include <mapnik/box2d.hpp>
32#include <mapnik/image_view.hpp>
33#include <mapnik/global.hpp>
34
35// stl
36#include <cmath>
37#include <string>
38#include <cassert>
39
40// cairo
41#ifdef HAVE_CAIRO
42#include <cairomm/surface.h>
43#endif
44
45namespace mapnik
46{
47
48struct Multiply
49{
50    inline static void mergeRGB(unsigned const &r0, unsigned const &g0, unsigned const &b0,
51                                unsigned &r1, unsigned &g1, unsigned &b1)
52    {
53        r1 = r1*r0/255;
54        g1 = g1*g0/255;
55        b1 = b1*b0/255;
56    }
57};
58struct Multiply2
59{
60    inline static void mergeRGB(unsigned const &r0, unsigned const &g0, unsigned const &b0,
61                                unsigned &r1, unsigned &g1, unsigned &b1)
62    {
63        r1 = r1*r0/128;
64        if (r1>255) r1=255;
65        g1 = g1*g0/128;
66        if (g1>255) g1=255;
67        b1 = b1*b0/128;
68        if (b1>255) b1=255;
69    }
70};
71struct Divide
72{
73    inline static void mergeRGB(unsigned const &r0, unsigned const &g0, unsigned const &b0,
74                                unsigned &r1, unsigned &g1, unsigned &b1)
75    {
76        r1 = r0*256/(r1+1);
77        g1 = g0*256/(g1+1);
78        b1 = b0*256/(b1+1);
79    }
80};
81struct Divide2
82{
83    inline static void mergeRGB(unsigned const &r0, unsigned const &g0, unsigned const &b0,
84                                unsigned &r1, unsigned &g1, unsigned &b1)
85    {
86        r1 = r0*128/(r1+1);
87        g1 = g0*128/(g1+1);
88        b1 = b0*128/(b1+1);
89    }
90};
91struct Screen
92{
93    inline static void mergeRGB(unsigned const &r0, unsigned const &g0, unsigned const &b0,
94                                unsigned &r1, unsigned &g1, unsigned &b1)
95    {
96        r1 = 255 - (255-r0)*(255-r1)/255;
97        g1 = 255 - (255-g0)*(255-g1)/255;
98        b1 = 255 - (255-b0)*(255-b1)/255;
99    }
100};
101struct HardLight
102{
103    inline static void mergeRGB(unsigned const &r0, unsigned const &g0, unsigned const &b0,
104                                unsigned &r1, unsigned &g1, unsigned &b1)
105    {
106        r1 = (r1>128)?255-(255-r0)*(255-2*(r1-128))/256:r0*r1*2/256;
107        g1 = (g1>128)?255-(255-g0)*(255-2*(g1-128))/256:g0*g1*2/256;
108        b1 = (b1>128)?255-(255-b0)*(255-2*(b1-128))/256:b0*b1*2/256;
109    }
110};
111struct MergeGrain
112{
113    inline static void mergeRGB(unsigned const &r0, unsigned const &g0, unsigned const &b0,
114                                unsigned &r1, unsigned &g1, unsigned &b1)
115    {
116        r1 = (r1+r0>128)?r1+r0-128:0;
117        if (r1>255) r1=255;
118        g1 = (g1+g0>128)?g1+g0-128:0;
119        if (g1>255) g1=255;
120        b1 = (b1+b0>128)?b1+b0-128:0;
121        if (b1>255) b1=255;
122    }
123};
124struct MergeGrain2
125{
126    inline static void mergeRGB(unsigned const &r0, unsigned const &g0, unsigned const &b0,
127                                unsigned &r1, unsigned &g1, unsigned &b1)
128    {
129        r1 = (2*r1+r0>256)?2*r1+r0-256:0;
130        if (r1>255) r1=255;
131        g1 = (2*g1+g0>256)?2*g1+g0-256:0;
132        if (g1>255) g1=255;
133        b1 = (2*b1+b0>256)?2*b1+b0-256:0;
134        if (b1>255) b1=255;
135    }
136};
137
138class MAPNIK_DECL image_32
139{
140private:
141    unsigned width_;
142    unsigned height_;
143    color background_;
144    image_data_32 data_;
145public:
146    image_32(int width,int height);
147    image_32(image_32 const& rhs);
148#ifdef HAVE_CAIRO
149    image_32(Cairo::RefPtr<Cairo::ImageSurface> rhs);
150#endif
151    ~image_32();
152    void set_background(color const& background);
153    const color& get_background() const;
154    const image_data_32& data() const;
155
156    inline image_data_32& data()
157    {
158        return data_;
159    }
160
161    inline const unsigned char* raw_data() const
162    {
163        return data_.getBytes();
164    }
165
166    inline unsigned char* raw_data()
167    {
168        return data_.getBytes();
169    }
170
171    inline image_view<image_data_32> get_view(unsigned x,unsigned y, unsigned w,unsigned h)
172    {
173        return image_view<image_data_32>(x,y,w,h,data_);
174    }
175
176private:
177
178    inline bool checkBounds(unsigned x, unsigned y) const
179    {
180        return (x < width_ && y < height_);
181    }
182
183public:
184    inline void setPixel(int x,int y,unsigned int rgba)
185    {
186        if (checkBounds(x,y))
187        {
188            data_(x,y)=rgba;
189        }
190    }
191    inline void blendPixel(int x,int y,unsigned int rgba1,int t)
192    {
193        blendPixel2(x,y,rgba1,t,1.0);  // do not change opacity
194    }
195
196    inline void blendPixel2(int x,int y,unsigned int rgba1,int t,double opacity)
197    {
198        if (checkBounds(x,y))
199        {
200            unsigned rgba0 = data_(x,y);
201#ifdef MAPNIK_BIG_ENDIAN
202            unsigned a1 = (int)((rgba1 & 0xff) * opacity) & 0xff; // adjust for desired opacity
203            a1 = (t*a1) / 255;
204            if (a1 == 0) return;
205            unsigned r1 = (rgba1 >> 24) & 0xff;
206            unsigned g1 = (rgba1 >> 16 ) & 0xff;
207            unsigned b1 = (rgba1 >> 8) & 0xff;
208
209            unsigned a0 = (rgba0 & 0xff);
210            unsigned r0 = ((rgba0 >> 24 ) & 0xff) * a0;
211            unsigned g0 = ((rgba0 >> 16 ) & 0xff) * a0;
212            unsigned b0 = ((rgba0 >> 8) & 0xff) * a0;
213
214            a0 = ((a1 + a0) << 8) - a0*a1;
215
216            r0 = ((((r1 << 8) - r0) * a1 + (r0 << 8)) / a0);
217            g0 = ((((g1 << 8) - g0) * a1 + (g0 << 8)) / a0);
218            b0 = ((((b1 << 8) - b0) * a1 + (b0 << 8)) / a0);
219            a0 = a0 >> 8;
220            data_(x,y)= (a0)| (b0 << 8) |  (g0 << 16) | (r0 << 24) ;
221#else
222            unsigned a1 = (int)(((rgba1 >> 24) & 0xff) * opacity) & 0xff; // adjust for desired opacity
223            a1 = (t*a1) / 255;
224            if (a1 == 0) return;
225            unsigned r1 = rgba1 & 0xff;
226            unsigned g1 = (rgba1 >> 8 ) & 0xff;
227            unsigned b1 = (rgba1 >> 16) & 0xff;
228
229            unsigned a0 = (rgba0 >> 24) & 0xff;
230            unsigned r0 = (rgba0 & 0xff) * a0;
231            unsigned g0 = ((rgba0 >> 8 ) & 0xff) * a0;
232            unsigned b0 = ((rgba0 >> 16) & 0xff) * a0;
233
234            a0 = ((a1 + a0) << 8) - a0*a1;
235
236            r0 = ((((r1 << 8) - r0) * a1 + (r0 << 8)) / a0);
237            g0 = ((((g1 << 8) - g0) * a1 + (g0 << 8)) / a0);
238            b0 = ((((b1 << 8) - b0) * a1 + (b0 << 8)) / a0);
239            a0 = a0 >> 8;
240            data_(x,y)= (a0 << 24)| (b0 << 16) |  (g0 << 8) | (r0) ;
241#endif
242        }
243    }
244
245    inline unsigned width() const
246    {
247        return width_;
248    }
249
250    inline unsigned height() const
251    {
252        return height_;
253    }
254
255    inline void set_rectangle(int x0,int y0,image_data_32 const& data)
256    {
257        box2d<int> ext0(0,0,width_,height_);
258        box2d<int> ext1(x0,y0,x0+data.width(),y0+data.height());
259
260        if (ext0.intersects(ext1))
261        {
262            box2d<int> box = ext0.intersect(ext1);
263            for (int y = box.miny(); y < box.maxy(); ++y)
264            {
265                unsigned int* row_to =  data_.getRow(y);
266                unsigned int const * row_from = data.getRow(y-y0);
267
268                for (int x = box.minx(); x < box.maxx(); ++x)
269                {
270#ifdef MAPNIK_BIG_ENDIAN
271                    row_to[x] = row_from[x-x0];
272#else
273                    if (row_from[x-x0] & 0xff000000)
274                    {
275                        row_to[x] = row_from[x-x0];
276                    }
277#endif
278                }
279            }
280        }
281    }
282
283    inline void set_rectangle_alpha(int x0,int y0,const image_data_32& data)
284    {
285        box2d<int> ext0(0,0,width_,height_);
286        box2d<int> ext1(x0,y0,x0 + data.width(),y0 + data.height());
287
288        if (ext0.intersects(ext1))
289        {
290            box2d<int> box = ext0.intersect(ext1);
291            for (int y = box.miny(); y < box.maxy(); ++y)
292            {
293                unsigned int* row_to =  data_.getRow(y);
294                unsigned int const * row_from = data.getRow(y-y0);
295                for (int x = box.minx(); x < box.maxx(); ++x)
296                {
297                    unsigned rgba0 = row_to[x];
298                    unsigned rgba1 = row_from[x-x0];
299
300#ifdef MAPNIK_BIG_ENDIAN
301                    unsigned a1 = rgba1 & 0xff;
302                    if (a1 == 0) continue;
303                    unsigned r1 = (rgba1 >> 24) & 0xff;
304                    unsigned g1 = (rgba1 >> 16 ) & 0xff;
305                    unsigned b1 = (rgba1 >> 8) & 0xff;
306
307                    unsigned a0 = rgba0 & 0xff;
308                    unsigned r0 = ((rgba0 >> 24) & 0xff) * a0;
309                    unsigned g0 = ((rgba0 >> 16 ) & 0xff) * a0;
310                    unsigned b0 = ((rgba0 >> 8) & 0xff) * a0;
311
312                    a0 = ((a1 + a0) << 8) - a0*a1;
313
314                    r0 = ((((r1 << 8) - r0) * a1 + (r0 << 8)) / a0);
315                    g0 = ((((g1 << 8) - g0) * a1 + (g0 << 8)) / a0);
316                    b0 = ((((b1 << 8) - b0) * a1 + (b0 << 8)) / a0);
317                    a0 = a0 >> 8;
318                    row_to[x] = (a0) | (b0 << 8) |  (g0 << 16) | (r0 << 24) ;
319#else
320                    unsigned a1 = (rgba1 >> 24) & 0xff;
321                    if (a1 == 0) continue;
322                    unsigned r1 = rgba1 & 0xff;
323                    unsigned g1 = (rgba1 >> 8 ) & 0xff;
324                    unsigned b1 = (rgba1 >> 16) & 0xff;
325
326                    unsigned a0 = (rgba0 >> 24) & 0xff;
327                    unsigned r0 = (rgba0 & 0xff) * a0;
328                    unsigned g0 = ((rgba0 >> 8 ) & 0xff) * a0;
329                    unsigned b0 = ((rgba0 >> 16) & 0xff) * a0;
330
331                    a0 = ((a1 + a0) << 8) - a0*a1;
332
333                    r0 = ((((r1 << 8) - r0) * a1 + (r0 << 8)) / a0);
334                    g0 = ((((g1 << 8) - g0) * a1 + (g0 << 8)) / a0);
335                    b0 = ((((b1 << 8) - b0) * a1 + (b0 << 8)) / a0);
336                    a0 = a0 >> 8;
337                    row_to[x] = (a0 << 24)| (b0 << 16) |  (g0 << 8) | (r0) ;
338#endif
339                }
340            }
341        }
342    }
343
344    inline void set_rectangle_alpha2(image_data_32 const& data, unsigned x0, unsigned y0, float opacity)
345    {
346        box2d<int> ext0(0,0,width_,height_);
347        box2d<int> ext1(x0,y0,x0 + data.width(),y0 + data.height());
348
349        if (ext0.intersects(ext1))
350        {
351            box2d<int> box = ext0.intersect(ext1);
352            for (int y = box.miny(); y < box.maxy(); ++y)
353            {
354                unsigned int* row_to =  data_.getRow(y);
355                unsigned int const * row_from = data.getRow(y-y0);
356                for (int x = box.minx(); x < box.maxx(); ++x)
357                {
358                    unsigned rgba0 = row_to[x];
359                    unsigned rgba1 = row_from[x-x0];
360#ifdef MAPNIK_BIG_ENDIAN
361                    unsigned a1 = int( (rgba1 & 0xff) * opacity );
362                    if (a1 == 0) continue;
363                    unsigned r1 = (rgba1 >> 24) & 0xff;
364                    unsigned g1 = (rgba1 >> 16 ) & 0xff;
365                    unsigned b1 = (rgba1 >> 8) & 0xff;
366
367                    unsigned a0 = rgba0 & 0xff;
368                    unsigned r0 = (rgba0 >> 24) & 0xff ;
369                    unsigned g0 = (rgba0 >> 16 ) & 0xff;
370                    unsigned b0 = (rgba0 >> 8) & 0xff;
371
372                    r0 = byte(((r1 - r0) * a1 + (r0 << 8)) >> 8);
373                    g0 = byte(((g1 - g0) * a1 + (g0 << 8)) >> 8);
374                    b0 = byte(((b1 - b0) * a1 + (b0 << 8)) >> 8);
375                    a0 = byte((a1 + a0) - ((a1 * a0 + 255) >> 8));
376
377                    row_to[x] = (a0)| (b0 << 8) |  (g0 << 16) | (r0 << 24) ;
378#else
379                    unsigned a1 = int( ((rgba1 >> 24) & 0xff) * opacity );
380                    if (a1 == 0) continue;
381                    unsigned r1 = rgba1 & 0xff;
382                    unsigned g1 = (rgba1 >> 8 ) & 0xff;
383                    unsigned b1 = (rgba1 >> 16) & 0xff;
384
385                    unsigned a0 = (rgba0 >> 24) & 0xff;
386                    unsigned r0 = rgba0 & 0xff ;
387                    unsigned g0 = (rgba0 >> 8 ) & 0xff;
388                    unsigned b0 = (rgba0 >> 16) & 0xff;
389
390                    r0 = byte(((r1 - r0) * a1 + (r0 << 8)) >> 8);
391                    g0 = byte(((g1 - g0) * a1 + (g0 << 8)) >> 8);
392                    b0 = byte(((b1 - b0) * a1 + (b0 << 8)) >> 8);
393                    a0 = byte((a1 + a0) - ((a1 * a0 + 255) >> 8));
394                   
395                    row_to[x] = (a0 << 24)| (b0 << 16) |  (g0 << 8) | (r0) ;
396#endif
397                }
398            }
399        }
400    }
401
402    template <typename MergeMethod>
403        inline void merge_rectangle(image_data_32 const& data, unsigned x0, unsigned y0, float opacity)
404    {
405        box2d<int> ext0(0,0,width_,height_);
406        box2d<int> ext1(x0,y0,x0 + data.width(),y0 + data.height());
407
408        if (ext0.intersects(ext1))
409        {
410            box2d<int> box = ext0.intersect(ext1);
411            for (int y = box.miny(); y < box.maxy(); ++y)
412            {
413                unsigned int* row_to =  data_.getRow(y);
414                unsigned int const * row_from = data.getRow(y-y0);
415                for (int x = box.minx(); x < box.maxx(); ++x)
416                {
417                    unsigned rgba0 = row_to[x];
418                    unsigned rgba1 = row_from[x-x0];
419#ifdef MAPNIK_BIG_ENDIAN
420                    unsigned a1 = int( (rgba1 & 0xff) * opacity );
421                    if (a1 == 0) continue;
422                    unsigned r1 = (rgba1 >> 24)& 0xff;
423                    unsigned g1 = (rgba1 >> 16 ) & 0xff;
424                    unsigned b1 = (rgba1 >> 8) & 0xff;
425
426                    unsigned a0 = rgba0 & 0xff;
427                    unsigned r0 = (rgba0 >> 24) & 0xff ;
428                    unsigned g0 = (rgba0 >> 16 ) & 0xff;
429                    unsigned b0 = (rgba0 >> 8) & 0xff;
430
431                    unsigned a = (a1 * 255 + (255 - a1) * a0 + 127)/255;
432
433                    MergeMethod::mergeRGB(r0,g0,b0,r1,g1,b1);
434
435                    r0 = (r1*a1 + (((255 - a1) * a0 + 127)/255) * r0 + 127)/a;
436                    g0 = (g1*a1 + (((255 - a1) * a0 + 127)/255) * g0 + 127)/a;
437                    b0 = (b1*a1 + (((255 - a1) * a0 + 127)/255) * b0 + 127)/a;
438
439                    row_to[x] = (a)| (b0 << 8) |  (g0 << 16) | (r0 << 24) ;
440#else
441                    unsigned a1 = int( ((rgba1 >> 24) & 0xff) * opacity );
442                    if (a1 == 0) continue;
443                    unsigned r1 = rgba1 & 0xff;
444                    unsigned g1 = (rgba1 >> 8 ) & 0xff;
445                    unsigned b1 = (rgba1 >> 16) & 0xff;
446
447                    unsigned a0 = (rgba0 >> 24) & 0xff;
448                    unsigned r0 = rgba0 & 0xff ;
449                    unsigned g0 = (rgba0 >> 8 ) & 0xff;
450                    unsigned b0 = (rgba0 >> 16) & 0xff;
451
452                    unsigned a = (a1 * 255 + (255 - a1) * a0 + 127)/255;
453
454                    MergeMethod::mergeRGB(r0,g0,b0,r1,g1,b1);
455
456                    r0 = (r1*a1 + (((255 - a1) * a0 + 127)/255) * r0 + 127)/a;
457                    g0 = (g1*a1 + (((255 - a1) * a0 + 127)/255) * g0 + 127)/a;
458                    b0 = (b1*a1 + (((255 - a1) * a0 + 127)/255) * b0 + 127)/a;
459
460                    row_to[x] = (a << 24)| (b0 << 16) |  (g0 << 8) | (r0) ;
461#endif
462                }
463            }
464        }
465    }
466};
467}
468#endif //GRAPHICS_HPP
Note: See TracBrowser for help on using the browser.