replaced libart with new polygon code
[swftools.git] / lib / art / art_rgb_a_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 "config.h"
21 #include "art_rgb_a_affine.h"
22
23 #include <math.h>
24
25 #include "art_affine.h"
26 #include "art_point.h"
27 #include "art_rgb_affine_private.h"
28
29 /* This module handles compositing of affine-transformed alpha only images
30    over rgb pixel buffers. */
31
32 /* Composite the source image over the destination image, applying the
33    affine transform. */
34
35 /**
36  * art_rgb_a_affine: Affine transform source Alpha image and composite.
37  * @dst: Destination image RGB buffer.
38  * @x0: Left coordinate of destination rectangle.
39  * @y0: Top coordinate of destination rectangle.
40  * @x1: Right coordinate of destination rectangle.
41  * @y1: Bottom coordinate of destination rectangle.
42  * @dst_rowstride: Rowstride of @dst buffer.
43  * @src: Source image alpha buffer.
44  * @src_width: Width of source image.
45  * @src_height: Height of source image.
46  * @src_rowstride: Rowstride of @src buffer.
47  * @rgb: RGB foreground color, in 0xRRGGBB.
48  * @affine: Affine transform.
49  * @level: Filter level.
50  * @alphagamma: #ArtAlphaGamma for gamma-correcting the compositing.
51  *
52  * Affine transform the solid color rgb with alpha specified by the
53  * source image stored in @src, compositing over the area of destination
54  * image @dst specified by the rectangle (@x0, @y0) - (@x1, @y1).
55  * As usual in libart, the left and top edges of this rectangle are
56  * included, and the right and bottom edges are 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 art_rgb_a_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                   art_u32 rgb,
72                   const double affine[6],
73                   ArtFilterLevel level,
74                   ArtAlphaGamma *alphagamma)
75 {
76   /* Note: this is a slow implementation, and is missing all filter
77      levels other than NEAREST. It is here for clarity of presentation
78      and to establish the interface. */
79   int x, y;
80   double inv[6];
81   art_u8 *dst_p, *dst_linestart;
82   const art_u8 *src_p;
83   ArtPoint pt, src_pt;
84   int src_x, src_y;
85   int alpha;
86   art_u8 bg_r, bg_g, bg_b;
87   art_u8 fg_r, fg_g, fg_b;
88   int tmp;
89   int run_x0, run_x1;
90   art_u8 r, g, b;
91
92   r = (rgb>>16)&0xff;
93   g = (rgb>>8)&0xff;
94   b = (rgb)&0xff;
95
96   dst_linestart = dst;
97   art_affine_invert (inv, affine);
98   for (y = y0; y < y1; y++)
99     {
100       pt.y = y + 0.5;
101       run_x0 = x0;
102       run_x1 = x1;
103       art_rgb_affine_run (&run_x0, &run_x1, y, src_width, src_height,
104                           inv);
105       dst_p = dst_linestart + (run_x0 - x0) * 3;
106       for (x = run_x0; x < run_x1; x++)
107         {
108           pt.x = x + 0.5;
109           art_affine_point (&src_pt, &pt, inv);
110           src_x = floor (src_pt.x);
111           src_y = floor (src_pt.y);
112           src_p = src + (src_y * src_rowstride) + src_x;
113           if (src_x >= 0 && src_x < src_width &&
114               src_y >= 0 && src_y < src_height)
115             {
116
117           alpha = *src_p;
118           if (alpha)
119             {
120               if (alpha == 255)
121                 {
122                   dst_p[0] = r;
123                   dst_p[1] = g;
124                   dst_p[2] = b;
125                 }
126               else
127                 {
128                   bg_r = dst_p[0];
129                   bg_g = dst_p[1];
130                   bg_b = dst_p[2];
131                   
132                   tmp = (r - bg_r) * alpha;
133                   fg_r = bg_r + ((tmp + (tmp >> 8) + 0x80) >> 8);
134                   tmp = (g - bg_g) * alpha;
135                   fg_g = bg_g + ((tmp + (tmp >> 8) + 0x80) >> 8);
136                   tmp = (b - 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                 }
143             }
144             } else { dst_p[0] = 255; dst_p[1] = 0; dst_p[2] = 0; }
145           dst_p += 3;
146         }
147       dst_linestart += dst_rowstride;
148     }
149 }