Image Interpolation with Contour Stencils

drawline.c

Go to the documentation of this file.
00001 
00016 #include <math.h>
00017 #include "drawline.h"
00018 
00019 #define PIXEL_STRIDE    3
00020 
00021 #define SWAP(A,B)       \
00022     Temp = A; \
00023     A = B; \
00024     B = Temp;
00025 
00026     
00027 static float fpart(float x)
00028 {
00029     return (x - (float)floor(x));
00030 }
00031 
00032 
00033 static void PlotPixel(uint32_t *Image, int Width, int Height, int x, int y,
00034     float Alpha, const float *Color)
00035 {
00036     if(0 <= x && x < Width && 0 <= y && y < Height)
00037     {
00038         const float CAlpha = 1 - Alpha;
00039         
00040         Image += x + Width*y;
00041         ((uint8_t *)Image)[0] = (uint8_t)(
00042             CAlpha*((uint8_t *)Image)[0] + Alpha*Color[0] + 0.5f);
00043         ((uint8_t *)Image)[1] = (uint8_t)(
00044             CAlpha*((uint8_t *)Image)[1] + Alpha*Color[1] + 0.5f);
00045         ((uint8_t *)Image)[2] = (uint8_t)(
00046             CAlpha*((uint8_t *)Image)[2] + Alpha*Color[2] + 0.5f);        
00047     }
00048 }
00049 
00050 
00059 void DrawLine(uint32_t *Image, int Width, int Height,
00060     float x1, float y1, float x2, float y2, const float *Color)
00061 {
00062     float yend, Gap, dx, dy, Gradient, y, Temp;
00063     int xend, ix, iy, ix1, iy1, ix2, iy2, Swapped = 0;
00064        
00065     
00066     dx = x2 - x1;
00067     dy = y2 - y1;
00068     
00069     if(fabs(dx) < fabs(dy))
00070     {
00071         SWAP(x1,y1)
00072         SWAP(x2,y2)
00073         SWAP(dx,dy)
00074         Swapped = 1;
00075     }
00076         
00077     if(x2 < x1)
00078     {
00079         SWAP(x1,x2)
00080         SWAP(y1,y2)
00081     }
00082     
00083     if(dx == 0)
00084         return;
00085     
00086     Gradient = dy/dx;
00087     
00088     xend = (int)floor(x1 + 0.5f);
00089     yend = y1 + Gradient * (xend - x1);
00090     y = yend + Gradient;
00091     Gap = 1 - fpart(x1 + 0.5f);
00092     ix1 = xend;
00093     iy1 = (int)floor(yend);
00094     
00095     if(!Swapped)
00096     {
00097         PlotPixel(Image, Width, Height, ix1, iy1, (1 - fpart(yend)) * Gap, Color);
00098         PlotPixel(Image, Width, Height, ix1, iy1 + 1, fpart(yend) * Gap, Color);
00099     }
00100     else
00101     {
00102         PlotPixel(Image, Width, Height, iy1, ix1, (1 - fpart(yend)) * Gap, Color);
00103         PlotPixel(Image, Width, Height, iy1 + 1, ix1, fpart(yend) * Gap, Color);
00104     }
00105         
00106     xend = (int)floor(x2 + 0.5f);
00107     yend = y2 + Gradient * (xend - x2);
00108     Gap = fpart(x2 + 0.5f);
00109     ix2 = xend;
00110     iy2 = (int)floor(yend);
00111     
00112     if(!Swapped)
00113     {
00114         PlotPixel(Image, Width, Height, ix2, iy2, (1 - fpart(yend)) * Gap, Color);
00115         PlotPixel(Image, Width, Height, ix2, iy2 + 1, fpart(yend) * Gap, Color);
00116         
00117         for(ix = ix1 + 1; ix < ix2; ix++)
00118         {       
00119             iy = (int)y;            
00120             PlotPixel(Image, Width, Height, ix, iy, 1 - fpart(y), Color);
00121             PlotPixel(Image, Width, Height, ix, iy + 1, fpart(y), Color);            
00122             y += Gradient;
00123         }
00124     }
00125     else
00126     {
00127         PlotPixel(Image, Width, Height, iy2, ix2, (1 - fpart(yend)) * Gap, Color);
00128         PlotPixel(Image, Width, Height, iy2 + 1, ix2, fpart(yend) * Gap, Color);
00129         
00130         for(ix = ix1 + 1; ix < ix2; ix++)
00131         {            
00132             iy = (int)y;
00133             PlotPixel(Image, Width, Height, iy, ix, 1 - fpart(y), Color);
00134             PlotPixel(Image, Width, Height, iy + 1, ix, fpart(y), Color);
00135             y += Gradient;
00136         }
00137     }
00138 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines