00001 #include <cstdlib>
00002
00003 #include "draw.hpp"
00004
00005
00006 static inline void
00007 draw_point_rgb(int x, int y, DrawColor color, DrawImage* img)
00008 {
00009 if(x>=0 && x<img->width && y>=0 && y<img->height) {
00010 uint8_t *pix = &img->data[y*img->stride + x*3];
00011 pix[0] = color.r;
00012 pix[1] = color.g;
00013 pix[2] = color.b;
00014 }
00015 }
00016
00017 DrawImage::DrawImage(int w, int h) :
00018 data(w*h*3),
00019 width(w),
00020 height(h),
00021 stride(width*3)
00022 {}
00023
00024 void
00025 draw_hline_rgb(int x0, int y0, int x1, DrawColor color, DrawImage* img)
00026 {
00027 if(y0 < 0 || y0 >= img->height)
00028 return;
00029
00030 if(x0 > x1)
00031 {
00032 int t = x0;
00033 x0 = x1;
00034 x1 = t;
00035 }
00036
00037 if(x0 < 0)
00038 x0 = 0;
00039 else if (x0 >= img->width)
00040 return;
00041
00042 if(x1 < 0)
00043 return;
00044 else if(x1 >= img->width)
00045 x1 = img->width-1;
00046
00047 uint8_t* pix = &img->data[y0*img->stride + x0*3];
00048 while(x0 <= x1) {
00049 pix[0] = color.r;
00050 pix[1] = color.g;
00051 pix[2] = color.b;
00052 x0++;
00053 pix += 3;
00054 }
00055 }
00056
00057 void
00058 draw_line_rgb(int x0, int y0, int x1, int y1,
00059 DrawColor color, DrawImage* img)
00060 {
00061 int xs;
00062 int ys;
00063
00064 int dx = x1-x0;
00065 if (dx < 0)
00066 {
00067 dx = -dx;
00068 xs = -1;
00069 }
00070 else
00071 xs = 1;
00072
00073 int dy = y1-y0;
00074 if (dy < 0)
00075 {
00076 dy = -dy;
00077 ys = -1;
00078 }
00079 else
00080 ys = 1;
00081
00082 int n = (dx > dy) ? dx : dy;
00083
00084 if (dx == 0) {
00085 for (int i = 0; i < dy; i++) {
00086 draw_point_rgb(x0, y0, color, img);
00087 y0 += ys;
00088 }
00089 } else if (dy == 0) {
00090 draw_hline_rgb(x0, y0, x1, color, img);
00091 } else if (dx > dy) {
00092 n = dx;
00093 dy += dy;
00094 int e = dy - dx;
00095 dx += dx;
00096
00097 for(int i = 0; i < n; i++) {
00098 draw_point_rgb(x0, y0, color, img);
00099 if (e >= 0) {
00100 y0 += ys;
00101 e -= dx;
00102 }
00103 e += dy;
00104 x0 += xs;
00105 }
00106 } else {
00107 n = dy;
00108 dx += dx;
00109 int e = dx - dy;
00110 dy += dy;
00111
00112 for(int i = 0; i < n; i++) {
00113 draw_point_rgb(x0, y0, color, img);
00114 if (e >= 0) {
00115 x0 += xs;
00116 e -= dy;
00117 }
00118 e += dx;
00119 y0 += ys;
00120 }
00121 }
00122 }
00123
00124 void draw_box_rgb(int x0, int y0, int x1, int y1,
00125 DrawColor color, DrawImage* img)
00126 {
00127 for(int y=y0; y<=y1; y++)
00128 draw_hline_rgb(x0, y, x1, color, img);
00129 }
00130
00131 void draw_gray_img_rgb(const uint8_t* gray_img,
00132 int gray_width, int gray_height, int gray_stride,
00133 int dest_x0, int dest_y0,
00134 DrawImage* rgb_img)
00135 {
00136 for(int gray_row=0; gray_row<gray_height; gray_row++) {
00137 uint8_t* rgb_pixel = &rgb_img->data[(gray_row + dest_y0) * rgb_img->stride + dest_x0 * 3];
00138 const uint8_t* gray_pixel = &gray_img[gray_row * gray_stride];
00139 for(int gray_col=0; gray_col<gray_width; gray_col++) {
00140 rgb_pixel[0] = *gray_pixel;
00141 rgb_pixel[1] = *gray_pixel;
00142 rgb_pixel[2] = *gray_pixel;
00143 rgb_pixel += 3;
00144 gray_pixel++;
00145 }
00146 }
00147 }