void image_filled_rect(image *img, int x, int y, int w, int h, color c) { for(int my = y; my < y+h; my++) { for(int mx = x; mx < x+w; mx++) { image_set_pixel(img, mx, my, c); } } }
static void draw_circle (Image *image, int x0, int y0, int radius, unsigned char value) { int x, y; for (y = -radius; y <= radius; y++) for (x = -radius; x <= radius; x++) if ((x * x) + (y * y) <= (radius * radius)) image_set_pixel (image, x0, y0, x, y, value); }
void capture_depth_image(freenect_device *dev, void *v_depth, uint32_t timestamp) { int x, y; uint16_t *depth = (uint16_t*)v_depth; for (y = 0; y < 480; y++) { for (x = 0; x < 640; x++) { image_set_pixel(depth_image, x, y, depth[y * 640 + x]); } } }
// Bresenham void image_line(image *img, int x0, int y0, int x1, int y1, color c) { int dx = abs(x1 - x0); int sx = (x0 < x1) ? 1 : -1; int dy = abs(y1 - y0); int sy = (y0 < y1) ? 1 : -1; int err = (dx > dy ? dx : -dy) / 2; int e2; while(1) { image_set_pixel(img, x0, y0, c); if(x0 == x1 && y0 == y1) break; e2 = err; if(e2 > -dx) { err -= dy; x0 += sx; } if(e2 < dy) { err += dx; y0 += sy; } } }
int write_image( const Image& image, const char *filename ) { if(std::string(filename).rfind(".png") == std::string::npos && std::string(filename).rfind(".PNG") == std::string::npos ) { printf("writing color image '%s'... failed, not a PNG image.\n", filename); return -1; } // flip de l'image : Y inverse entre GL et BMP Image flip= create_image(image.width, image.height, 4, make_color(0, 0, 0)); for(int y= 0; y < image.height; y++) for(int x= 0; x < image.width; x++) image_set_pixel(flip, x, image.height - y -1, image_pixel(image, x, y)); SDL_Surface *bmp= SDL_CreateRGBSurfaceFrom((void *) &flip.data.front(), image.width, image.height, 32, image.width * 4, #if 0 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF #else 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 #endif ); int code= IMG_SavePNG(bmp, filename); SDL_FreeSurface(bmp); release_image(flip); if(code < 0) printf("writing color image '%s'... failed\n%s\n", filename, SDL_GetError()); else printf("writing color image '%s'...\n", filename); return code; }
/* 画图函数共有6个循环: 1.-r<x<r,-r<y<r(画个大圆<白色>); 2.0<x<r,-r<y<0(在第一象限填充黑色(0); 3.-1/2r<x<0,-r<y<0(在圆心上面画一个中圆<半圆>,黑色); 4.1/2r<x<r,0<y<r(大圆心下面的黑色部分); 5.-1/6r<x<1/6r,1/3r<y<2/3r(大圆心下面的小圆<黑色>) 6.-1/6r<x<1/6r,-1/3r<y<-2/3r(大圆心上面的小圆<白色>) */ static void draw_Taijitu(Image *image,int radius,int value) { int x,y; int rlimit ,llimit; int radius_2 = radius*radius;//半径的平方 r² for(y = -radius;y<radius;y++)//(画个大圆<白色>) 沿着Y轴画一个直径的长度 //以象限来看 是从y轴的对称负数方向开始画,从数值上看是从-300,-299开始 但是因为平方,所以数值不会有影响 { for(x= -radius;x<radius;x++)//每个循环沿着X轴画了一个直径的长度 { // x²+ y² r² if(x*x+y*y <= radius_2)//圆的标准方程 圆心的坐标(a,b) 半径r 所以 (x-a)²+ (y-b)²=r² { image_set_pixel(image,x,y,0xff);//每个像素画一个255(白色) } } } for(y = -radius;y<0;y++)//第一象限画了四分之一的黑色 第一象限开始画半径长度 之所以y = -radius 是因为从数值上看这样是从299,298开始的 因为是平方 所以(x*x)+(y*y)不会有影响 { for(x = 0;x<radius;x++)//每个循环沿着X轴画了一个半径的长度 { // x²+ y² r² if((x*x)+(y*y) <= radius_2)//只要x²+ y² <=r² 就是说明这个点在圆内或者搭到圆边 { image_set_pixel(image,x,y,value); } } } for(y = -radius;y<0;y++)//把第一象限的y轴变成了圆形 画半径长度 { //y² double d_tmp =(double)(-radius*y-y*y); for(x = -(int)sqrt(d_tmp);x<0;x++) { image_set_pixel(image,x,y,value); } } for(y = 0;y<radius;y++)//把第三象限的y轴变成了圆形 至此黑白八卦成型,独缺上下两个小圆 { llimit = (int)sqrt((double)(radius*y - y*y)); rlimit = (int)sqrt((double)(radius_2 - y*y)); for(x = llimit;x<rlimit;x++) { image_set_pixel(image,x,y,value); } } for(y = 2*radius/6;y<4*radius/6;y++)//画出下面的黑色小圆 { rlimit =(int) sqrt((double)(radius*y-y*y-2*radius_2/9)); llimit = -rlimit; for(x = llimit;x<rlimit;x++) { image_set_pixel(image,x,y,value); } } for(y = -4*radius/6;y<-2*radius/6;y++)//画出上面的白色小圆 至此全部完成 { rlimit = sqrt(double(-radius*y-y*y-2*radius_2/9)); llimit = -rlimit; for(x = llimit;x<rlimit;x++) { image_set_pixel(image,x,y,0xff); } } return ; }
static void draw_label (Image *image, int x0, int y0, int x1, int y1, unsigned char value) { int x, y; for (y = y0; y < y1; y++) for (x = x0; x < x1; x++) image_set_pixel (image, x, y, value); }