The BIG graph update
[rrdtool.git] / libraries / libart_lgpl-2.3.7 / art_rgb_rgba_affine.c
1 /* Libart_LGPL - library of basic graphic primitives
2  * Copyright (C) 1998 Raph Levien
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <math.h>
21 #include "art_misc.h"
22 #include "art_point.h"
23 #include "art_affine.h"
24 #include "art_rgb_affine_private.h"
25 #include "art_rgb_rgba_affine.h"
26
27 /* This module handles compositing of affine-transformed rgba images
28    over rgb pixel buffers. */
29
30 /* Composite the source image over the destination image, applying the
31    affine transform. */
32
33 /**
34  * art_rgb_rgba_affine: Affine transform source RGBA image and composite.
35  * @dst: Destination image RGB buffer.
36  * @x0: Left coordinate of destination rectangle.
37  * @y0: Top coordinate of destination rectangle.
38  * @x1: Right coordinate of destination rectangle.
39  * @y1: Bottom coordinate of destination rectangle.
40  * @dst_rowstride: Rowstride of @dst buffer.
41  * @src: Source image RGBA buffer.
42  * @src_width: Width of source image.
43  * @src_height: Height of source image.
44  * @src_rowstride: Rowstride of @src buffer.
45  * @affine: Affine transform.
46  * @level: Filter level.
47  * @alphagamma: #ArtAlphaGamma for gamma-correcting the compositing.
48  *
49  * Affine transform the source image stored in @src, compositing over
50  * the area of destination image @dst specified by the rectangle
51  * (@x0, @y0) - (@x1, @y1). As usual in libart, the left and top edges
52  * of this rectangle are included, and the right and bottom edges are
53  * excluded.
54  *
55  * The @alphagamma parameter specifies that the alpha compositing be
56  * done in a gamma-corrected color space. In the current
57  * implementation, it is ignored.
58  *
59  * The @level parameter specifies the speed/quality tradeoff of the
60  * image interpolation. Currently, only ART_FILTER_NEAREST is
61  * implemented.
62  **/
63 void
64 art_rgb_rgba_affine (art_u8 *dst,
65                      int x0, int y0, int x1, int y1, int dst_rowstride,
66                      const art_u8 *src,
67                      int src_width, int src_height, int src_rowstride,
68                      const double affine[6],
69                      ArtFilterLevel level,
70                      ArtAlphaGamma *alphagamma)
71 {
72   /* Note: this is a slow implementation, and is missing all filter
73      levels other than NEAREST. It is here for clarity of presentation
74      and to establish the interface. */
75   int x, y;
76   double inv[6];
77   art_u8 *dst_p, *dst_linestart;
78   const art_u8 *src_p;
79   ArtPoint pt, src_pt;
80   int src_x, src_y;
81   int alpha;
82   art_u8 bg_r, bg_g, bg_b;
83   art_u8 fg_r, fg_g, fg_b;
84   int tmp;
85   int run_x0, run_x1;
86
87   dst_linestart = dst;
88   art_affine_invert (inv, affine);
89   for (y = y0; y < y1; y++)
90     {
91       pt.y = y + 0.5;
92       run_x0 = x0;
93       run_x1 = x1;
94       art_rgb_affine_run (&run_x0, &run_x1, y, src_width, src_height,
95                           inv);
96       dst_p = dst_linestart + (run_x0 - x0) * 3;
97       for (x = run_x0; x < run_x1; x++)
98         {
99           pt.x = x + 0.5;
100           art_affine_point (&src_pt, &pt, inv);
101           src_x = floor (src_pt.x);
102           src_y = floor (src_pt.y);
103           src_p = src + (src_y * src_rowstride) + src_x * 4;
104           if (src_x >= 0 && src_x < src_width &&
105               src_y >= 0 && src_y < src_height)
106             {
107
108           alpha = src_p[3];
109           if (alpha)
110             {
111               if (alpha == 255)
112                 {
113                   dst_p[0] = src_p[0];
114                   dst_p[1] = src_p[1];
115                   dst_p[2] = src_p[2];
116                 }
117               else
118                 {
119                   bg_r = dst_p[0];
120                   bg_g = dst_p[1];
121                   bg_b = dst_p[2];
122                   
123                   tmp = (src_p[0] - bg_r) * alpha;
124                   fg_r = bg_r + ((tmp + (tmp >> 8) + 0x80) >> 8);
125                   tmp = (src_p[1] - bg_g) * alpha;
126                   fg_g = bg_g + ((tmp + (tmp >> 8) + 0x80) >> 8);
127                   tmp = (src_p[2] - bg_b) * alpha;
128                   fg_b = bg_b + ((tmp + (tmp >> 8) + 0x80) >> 8);
129                   
130                   dst_p[0] = fg_r;
131                   dst_p[1] = fg_g;
132                   dst_p[2] = fg_b;
133                 }
134             }
135             } else { dst_p[0] = 255; dst_p[1] = 0; dst_p[2] = 0; }
136           dst_p += 3;
137         }
138       dst_linestart += dst_rowstride;
139     }
140 }