aboutsummaryrefslogtreecommitdiffstats
path: root/libart_lgpl/art_rgb_affine_private.c
blob: 9f2d12026f7c9ceee3e40338c5632e9fff4829a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* Libart_LGPL - library of basic graphic primitives
 * Copyright (C) 1998 Raph Levien
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "art_rgb_affine_private.h"

#include <math.h>
#include "art_misc.h"
#include "art_point.h"
#include "art_affine.h"

/* Private functions for the rgb affine image compositors - primarily,
   the determination of runs, eliminating the need for source image
   bbox calculation in the inner loop. */

/* Determine a "run", such that the inverse affine of all pixels from
   (x0, y) inclusive to (x1, y) exclusive fit within the bounds
   of the source image.

   Initial values of x0, x1, and result values stored in first two
   pointer arguments.
*/

#define EPSILON 1e-6

void
art_rgb_affine_run (gint *p_x0, gint *p_x1, gint y,
            gint src_width, gint src_height,
            const gdouble affine[6])
{
  gint x0, x1;
  gdouble z;
  gdouble x_intercept;
  gint xi;

  x0 = *p_x0;
  x1 = *p_x1;

  /* do left and right edges */
  if (affine[0] > EPSILON)
    {
      z = affine[2] * (y + 0.5) + affine[4];
      x_intercept = -z / affine[0];
      xi = ceil (x_intercept + EPSILON - 0.5);
      if (xi > x0)
    x0 = xi;
      x_intercept = (-z + src_width) / affine[0];
      xi = ceil (x_intercept - EPSILON - 0.5);
      if (xi < x1)
    x1 = xi;
    }
  else if (affine[0] < -EPSILON)
    {
      z = affine[2] * (y + 0.5) + affine[4];
      x_intercept = (-z + src_width) / affine[0];
      xi = ceil (x_intercept + EPSILON - 0.5);
      if (xi > x0)
    x0 = xi;
      x_intercept = -z / affine[0];
      xi = ceil (x_intercept - EPSILON - 0.5);
      if (xi < x1)
    x1 = xi;
    }
  else
    {
      z = affine[2] * (y + 0.5) + affine[4];
      if (z < 0 || z >= src_width)
    {
      *p_x1 = *p_x0;
      return;
    }
    }

  /* do top and bottom edges */
  if (affine[1] > EPSILON)
    {
      z = affine[3] * (y + 0.5) + affine[5];
      x_intercept = -z / affine[1];
      xi = ceil (x_intercept + EPSILON - 0.5);
      if (xi > x0)
    x0 = xi;
      x_intercept = (-z + src_height) / affine[1];
      xi = ceil (x_intercept - EPSILON - 0.5);
      if (xi < x1)
    x1 = xi;
    }
  else if (affine[1] < -EPSILON)
    {
      z = affine[3] * (y + 0.5) + affine[5];
      x_intercept = (-z + src_height) / affine[1];
      xi = ceil (x_intercept + EPSILON - 0.5);
      if (xi > x0)
    x0 = xi;
      x_intercept = -z / affine[1];
      xi = ceil (x_intercept - EPSILON - 0.5);
      if (xi < x1)
    x1 = xi;
    }
  else
    {
      z = affine[3] * (y + 0.5) + affine[5];
      if (z < 0 || z >= src_height)
    {
      *p_x1 = *p_x0;
      return;
    }
    }

  *p_x0 = x0;
  *p_x1 = x1;
}