Image Interpolation with Contour Stencils
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
drawline.c
Go to the documentation of this file.
1 
16 #include <math.h>
17 #include "drawline.h"
18 
19 #define PIXEL_STRIDE 3
20 
21 #define SWAP(A,B) \
22  Temp = A; \
23  A = B; \
24  B = Temp;
25 
26 
27 static float fpart(float x)
28 {
29  return (x - (float)floor(x));
30 }
31 
32 
33 static void PlotPixel(uint32_t *Image, int Width, int Height, int x, int y,
34  float Alpha, const float *Color)
35 {
36  if(0 <= x && x < Width && 0 <= y && y < Height)
37  {
38  const float CAlpha = 1 - Alpha;
39 
40  Image += x + Width*y;
41  ((uint8_t *)Image)[0] = (uint8_t)(
42  CAlpha*((uint8_t *)Image)[0] + Alpha*Color[0] + 0.5f);
43  ((uint8_t *)Image)[1] = (uint8_t)(
44  CAlpha*((uint8_t *)Image)[1] + Alpha*Color[1] + 0.5f);
45  ((uint8_t *)Image)[2] = (uint8_t)(
46  CAlpha*((uint8_t *)Image)[2] + Alpha*Color[2] + 0.5f);
47  }
48 }
49 
50 
59 void DrawLine(uint32_t *Image, int Width, int Height,
60  float x1, float y1, float x2, float y2, const float *Color)
61 {
62  float yend, Gap, dx, dy, Gradient, y, Temp;
63  int xend, ix, iy, ix1, iy1, ix2, iy2, Swapped = 0;
64 
65 
66  dx = x2 - x1;
67  dy = y2 - y1;
68 
69  if(fabs(dx) < fabs(dy))
70  {
71  SWAP(x1,y1)
72  SWAP(x2,y2)
73  SWAP(dx,dy)
74  Swapped = 1;
75  }
76 
77  if(x2 < x1)
78  {
79  SWAP(x1,x2)
80  SWAP(y1,y2)
81  }
82 
83  if(dx == 0)
84  return;
85 
86  Gradient = dy/dx;
87 
88  xend = (int)floor(x1 + 0.5f);
89  yend = y1 + Gradient * (xend - x1);
90  y = yend + Gradient;
91  Gap = 1 - fpart(x1 + 0.5f);
92  ix1 = xend;
93  iy1 = (int)floor(yend);
94 
95  if(!Swapped)
96  {
97  PlotPixel(Image, Width, Height, ix1, iy1, (1 - fpart(yend)) * Gap, Color);
98  PlotPixel(Image, Width, Height, ix1, iy1 + 1, fpart(yend) * Gap, Color);
99  }
100  else
101  {
102  PlotPixel(Image, Width, Height, iy1, ix1, (1 - fpart(yend)) * Gap, Color);
103  PlotPixel(Image, Width, Height, iy1 + 1, ix1, fpart(yend) * Gap, Color);
104  }
105 
106  xend = (int)floor(x2 + 0.5f);
107  yend = y2 + Gradient * (xend - x2);
108  Gap = fpart(x2 + 0.5f);
109  ix2 = xend;
110  iy2 = (int)floor(yend);
111 
112  if(!Swapped)
113  {
114  PlotPixel(Image, Width, Height, ix2, iy2, (1 - fpart(yend)) * Gap, Color);
115  PlotPixel(Image, Width, Height, ix2, iy2 + 1, fpart(yend) * Gap, Color);
116 
117  for(ix = ix1 + 1; ix < ix2; ix++)
118  {
119  iy = (int)y;
120  PlotPixel(Image, Width, Height, ix, iy, 1 - fpart(y), Color);
121  PlotPixel(Image, Width, Height, ix, iy + 1, fpart(y), Color);
122  y += Gradient;
123  }
124  }
125  else
126  {
127  PlotPixel(Image, Width, Height, iy2, ix2, (1 - fpart(yend)) * Gap, Color);
128  PlotPixel(Image, Width, Height, iy2 + 1, ix2, fpart(yend) * Gap, Color);
129 
130  for(ix = ix1 + 1; ix < ix2; ix++)
131  {
132  iy = (int)y;
133  PlotPixel(Image, Width, Height, iy, ix, 1 - fpart(y), Color);
134  PlotPixel(Image, Width, Height, iy + 1, ix, fpart(y), Color);
135  y += Gradient;
136  }
137  }
138 }