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 */
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"
26 /* Private functions for the rgb affine image compositors - primarily,
27 the determination of runs, eliminating the need for source image
28 bbox calculation in the inner loop. */
30 /* Determine a "run", such that the inverse affine of all pixels from
31 (x0, y) inclusive to (x1, y) exclusive fit within the bounds
32 of the source image.
34 Initial values of x0, x1, and result values stored in first two
35 pointer arguments.
36 */
38 #define EPSILON 1e-6
40 void
41 art_rgb_affine_run (int *p_x0, int *p_x1, int y,
42 int src_width, int src_height,
43 const double affine[6])
44 {
45 int x0, x1;
46 double z;
47 double x_intercept;
48 int xi;
50 x0 = *p_x0;
51 x1 = *p_x1;
53 /* do left and right edges */
54 if (affine[0] > EPSILON)
55 {
56 z = affine[2] * (y + 0.5) + affine[4];
57 x_intercept = -z / affine[0];
58 xi = ceil (x_intercept + EPSILON - 0.5);
59 if (xi > x0)
60 x0 = xi;
61 x_intercept = (-z + src_width) / affine[0];
62 xi = ceil (x_intercept - EPSILON - 0.5);
63 if (xi < x1)
64 x1 = xi;
65 }
66 else if (affine[0] < -EPSILON)
67 {
68 z = affine[2] * (y + 0.5) + affine[4];
69 x_intercept = (-z + src_width) / affine[0];
70 xi = ceil (x_intercept + EPSILON - 0.5);
71 if (xi > x0)
72 x0 = xi;
73 x_intercept = -z / affine[0];
74 xi = ceil (x_intercept - EPSILON - 0.5);
75 if (xi < x1)
76 x1 = xi;
77 }
78 else
79 {
80 z = affine[2] * (y + 0.5) + affine[4];
81 if (z < 0 || z >= src_width)
82 {
83 *p_x1 = *p_x0;
84 return;
85 }
86 }
88 /* do top and bottom edges */
89 if (affine[1] > EPSILON)
90 {
91 z = affine[3] * (y + 0.5) + affine[5];
92 x_intercept = -z / affine[1];
93 xi = ceil (x_intercept + EPSILON - 0.5);
94 if (xi > x0)
95 x0 = xi;
96 x_intercept = (-z + src_height) / affine[1];
97 xi = ceil (x_intercept - EPSILON - 0.5);
98 if (xi < x1)
99 x1 = xi;
100 }
101 else if (affine[1] < -EPSILON)
102 {
103 z = affine[3] * (y + 0.5) + affine[5];
104 x_intercept = (-z + src_height) / affine[1];
105 xi = ceil (x_intercept + EPSILON - 0.5);
106 if (xi > x0)
107 x0 = xi;
108 x_intercept = -z / affine[1];
109 xi = ceil (x_intercept - EPSILON - 0.5);
110 if (xi < x1)
111 x1 = xi;
112 }
113 else
114 {
115 z = affine[3] * (y + 0.5) + affine[5];
116 if (z < 0 || z >= src_height)
117 {
118 *p_x1 = *p_x0;
119 return;
120 }
121 }
123 *p_x0 = x0;
124 *p_x1 = x1;
125 }