标签:
Cimg代码初探#include "CImg_demo.h"
// Include CImg library header.
#include "CImg.h"
using namespace cimg_library;
#undef min
#undef max
void main()
{
cimg::info();
//各类RGB定值
const unsigned char
white[] = { 255, 255, 255 }, black[] = { 0, 0, 0 }, red[] = { 120, 50, 80 },
yellow[] = { 200, 155, 0 }, green[] = { 30, 200, 70 }, purple[] = { 175, 32, 186 },
blue[] = { 55, 140, 185 }, grey[] = { 127, 127, 127 };
float
rx = 0, ry = 0, t = 0, gamma = 0, vgamma = 0, T = 0.9f,
nrx = (float)(2*cimg::crand()),
nry = (float)(2*cimg::crand());
int y0 = 2*13;
//图像显示
CImg<unsigned char> back(1,2,1,3,10), fore, text, img;//back fore text img都是界面背景
back.fillC(0,1,0,10,10,235).resize(640,480,1,3,3).get_shared_channel(2).noise(10,1).draw_plasma();//绿宝石界面
fore.assign(back.width(),50,1,1,0).draw_text(20,y0-5,"** CImg %u.%u.%u Samples **",grey,0,1,24,
cimg_version/100,(cimg_version/10)%10,cimg_version%10);
(fore+=fore.get_dilate(3).dilate(3)).resize(-100,-100,1,3);
cimg_forXY(fore,x,y)
if (fore(x,y)==127) fore(x,y,0) = fore(x,y,1) = fore(x,y,2) = 1;
else if (fore(x,y)) {
const float val = cimg::min(255.0f,7.0f*(y-3));
fore(x,y,0) = (unsigned char)(val/1.5f);
fore(x,y,1) = (unsigned char)val;
fore(x,y,2) = (unsigned char)(val/1.1f);
}
fore.resize(back,0).draw_image(20,y0+2*13,text|=text.get_dilate(3)>>4);
//创建显示的窗体
CImgDisplay disp(back,"CImg Library Samples",0,false,true);
disp.move((disp.screen_width()-disp.window_width())/2,(disp.screen_height()-disp.window_height())/2);
img = back; back*=0.15f;
for (y0+=2*13; !disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC(); ) {
while ( !disp.is_closed() && !disp.is_keyQ() && !disp.is_keyESC()) {
img*=0.85f; img+=back;
for (int i = 0; i<60; ++i) {
const float
mx = (float)(img.width()/2+(img.width()/2-30)*((1-gamma)*std::cos(3*t+rx*i*18.0f*cimg::PI/180) +
gamma*std::cos(3*t+nrx*i*18.0f*cimg::PI/180))),
my = (float)(img.height()/2+(img.height()/2-30)*((1-gamma)*std::sin(4*t+ry*i*18.0f*cimg::PI/180) +
gamma*std::sin(4*t+nry*i*18.0f*cimg::PI/180))),
mz = (float)(1.3f + 1.2f*((1-gamma)*std::sin(2*t+(rx+ry)*i*20*cimg::PI/180) +
gamma*std::sin(2*t+(nrx+nry)*i*20*cimg::PI/180)));
const int j = i%5;
//具体画圆
img.draw_circle((int)mx,(int)my,(int)(10*mz),j!=0?(j!=1?(j!=2?(j!=3?green:red):yellow):purple):blue,0.2f).
draw_circle((int)(mx+4*mz),(int)(my-4),(int)(3*mz),white,0.1f).
draw_circle((int)mx,(int)my,(int)(10*mz),black,0.2f,~0U);
}
const unsigned char *ptrs = fore.data();
cimg_for(img,ptrd,unsigned char)
{
const unsigned char val = *(ptrs++);
if (val) *ptrd = val;
}
//显示菜单条
int y = disp.mouse_y();
if (y>=y0 && y<y0+27*13) {
y = (y/13)*13+7;
for (int yy = y-7; yy<=y+6; ++yy) img.draw_rectangle(0,yy,0,1,img.width()-1,yy,0,1,(unsigned char)(130-15*cimg::abs(yy-y)));
img.draw_triangle(2,y-4,2,y+4,8,y,yellow).draw_triangle(img.width()-2,y-4,img.width()-2,y+4,img.width()-8,y,yellow);
}
gamma+=vgamma;
if (gamma>1)
{
gamma = vgamma = 0;
rx = nrx;
ry = nry;
nrx=(float)(2*cimg::crand());
nry=(float)(2*cimg::crand());
}
t+=0.006f; T+=0.005f; if (T>1) { T-=(float)(1+cimg::crand()); vgamma = 0.03f; }
disp.resize(disp,false).display(img).wait(25);
}
}
getchar();
}
标签:
原文地址:http://www.cnblogs.com/jsxyhelu/p/5493372.html