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