#include "CImg.h"

using namespace cimg_library;

cimg_library::CImg<unsigned char> img("img/level1.bmp");

img.resize(2560,2560);

//img.discard(img); //smaže obrázek

img.save("img/file.bmp");

img.display("My first CImg code");


img.threshold(45,true,true);



img.rotate(4,1,1,1,1,2);

img.transpose();

img.normalize(10, 255);



img.blur(10);

img.blur_median(10);

img.blur_bilateral(10,10,10,10,false);


img.equalize(80,40,120);

img.histogram(640,480,800);


img.blur_bilateral(5,5,5,5,2);


img.quantize(10,true);

img.sharpen(150,false,25,10,30);

img.dilate(5);

img.erode(15);

img.noise(45,1);

img.shift(5,5,5,5,5);


img.exp();

img.cosh();


img.mul(img);


img.pow(img);


img.haar(false,1);


img.cut(0,300);


img.CMYtoRGB();


img.deriche(1,1,1,true);


img.diffusion_tensors(5,5,5,5,false);



cimg_for_insideXY(img,x,y,5) img(x,y) = x+y;

cimg_for_borderXY(img,x,y,5) img(x,y) = x-y;



unsigned char purple[] = { 255,0,255 };

img.draw_text(100,100,"Nějaký text",purple);







img.fill(0);

unsigned char purple[] = { 255,0,255 };

img.draw_text(100,100,"Nějaký text",purple);



img.crop(4,i*20,false);

cimg_usage("Compute a linear fading between two 2D images");

const char *file_i1 = cimg_option("-i1","test.bmp","Input Image 1");

const char *file_i2 = cimg_option("-i2","testt.bmp","Input Image 2");

const char *file_o = cimg_option("-o","test2.bmp","Output Image");

const bool visu = cimg_option("-visu",true,"Visualization mode");

const double pmin = cimg_option("-min",40.0,"Begin of the fade (in %)")/100.0;

const double pmax = cimg_option("-max",60.0,"End of the fade (in %)")/100.0;

const double angle = cimg_option("-angle",0.0,"Fade angle")*cil::cimg::PI/180;

cil::CImg<unsigned char> img1(file_i1), img2(file_i2);

if (!img2.is_sameXYZC(img1)) {

int

dx = cil::cimg::max(img1.width(),img2.width()),

dy = cil::cimg::max(img1.height(),img2.height()),

dz = cil::cimg::max(img1.depth(),img2.depth()),

dv = cil::cimg::max(img1.spectrum(),img2.spectrum());

img1.resize(dx,dy,dz,dv,3);

img2.resize(dx,dy,dz,dv,3);

}

cil::CImg<unsigned char> dest(img1);

const double ca = std::cos(angle), sa = std::sin(angle);

double alpha;

cimg_forXYZC(dest,x,y,z,k)

{

const double X = ((double)x/img1.width() - 0.5)*ca + ((double)y/img1.height() - 0.5)*sa;

if (X+0.5<pmin) alpha = 0; else {

if (X+0.5>pmax) alpha = 1; else

alpha = (X+0.5-pmin)/(pmax-pmin);

}

dest(x,y,z,k) = (unsigned char)((1 - alpha)*img1(x,y,z,k) + alpha*img2(x,y,z,k));

}

if (file_o) dest.save(file_o);

if (visu) dest.display("Image fading");









//console ascii art text

#undef cimg_display

#define cimg_display 0

#include "CImg.h"

using namespace cimg_library;

int main(int argc,char **argv) {

const char *geom = cimg_option("-g","79x40","Output size");

const int alphabet = cimg_option("-a",0,"Alphabet type (0=full, 1=numbers, 2=letters, 3=signs, 4=minimal");

const bool invert = cimg_option("-invert",false,"Invert image intensities");

const float contour = (float)cimg_option("-contour",0.0f,"Use image contours higher than specified threshold");

const float blur = (float)cimg_option("-blur",0.8f,"Image pre-blur");

const float sigma = (float)cimg_option("-sigma",1.5f,"Font pre-blur");

const char *file_i = cimg_argument1(0,"-invert");

int w = 79, h = 40;

std::sscanf(geom,"%d%*c%d",&w,&h);

if (cimg_option("-h",false,0)) std::exit(0);

const CImgList<> font_full = CImgList<>::font(11,false);

const int fw = font_full['A'].width(), fh = font_full['A'].height();

CImgList<> font, font_blur;

CImgList<unsigned char> font_code;

switch (alphabet){case 1:{font_code.insert(CImg<>::vector(' '));for (unsigned char l='0'; l<='9'; l++) font_code.insert(CImg<>::vector(l));}break;

case 2:{font_code.insert(CImg<>::vector(' '));for (unsigned char l='A'; l<='Z'; l++) font_code.insert(CImg<>::vector(l));}break;

case 3:{font_code.insert(CImg<>::vector(' '));font_code.insert(CImg<>::vector('-'));font_code.insert(CImg<>::vector('_'));font_code.insert(CImg<>::vector('|'));

font_code.insert(CImg<>::vector('/'));font_code.insert(CImg<>::vector('\\'));font_code.insert(CImg<>::vector('+'));font_code.insert(CImg<>::vector('.'));

font_code.insert(CImg<>::vector('*'));font_code.insert(CImg<>::vector('='));font_code.insert(CImg<>::vector(']'));font_code.insert(CImg<>::vector('['));

font_code.insert(CImg<>::vector('(')); font_code.insert(CImg<>::vector(')'));font_code.insert(CImg<>::vector('{'));font_code.insert(CImg<>::vector('}'));

font_code.insert(CImg<>::vector('"'));font_code.insert(CImg<>::vector('!'));font_code.insert(CImg<>::vector('$'));}break;

case 4:{font_code.insert(CImg<>::vector(' '));font_code.insert(CImg<>::vector('.'));font_code.insert(CImg<>::vector('/'));font_code.insert(CImg<>::vector('\\'));

font_code.insert(CImg<>::vector('_'));font_code.insert(CImg<>::vector('_'));font_code.insert(CImg<>::vector('|'));}break;

default:{for(unsigned char l=' '; l<='~'; l++) font_code.insert(CImg<>::vector(l)); } break;}

cimglist_for(font_code,l){font.insert(font_full(font_code[l](0)));font_blur.insert(font[l].get_resize(fw,fh,1,1).blur(sigma).normalize(0,255));}

CImg<> img;

if (!file_i) { float white[3] = {255,255,255}; img.assign().draw_text(0,0,"DaNeK",white); }

else img.assign(file_i);

img.norm().resize(fw*w,fh*h);

if (blur) img.blur(blur);

if (contour>0) {

CImgList<> grad = img.get_gradient("xy",4);

img = (grad[0].pow(2) + grad[1].pow(2)).sqrt().normalize(0,100).threshold(contour);

}

img.normalize(0,255);

if (invert) img = 255.0f-img;

CImg<unsigned char> dest(w,h,1,1,0);

CImg<> neigh;

cimg_forY(dest,y) { cimg_forX(dest,x) {

neigh = img.get_crop(x*fw,y*fh,(x+1)*fw,(y+1)*fh);

float scoremin = 2e28f;

unsigned int best = 0;

cimglist_for(font_code,l) {

const CImg<>& letter = font_blur[l];

const float score = (float)((letter-neigh).pow(2).sum());

if (score<scoremin) { scoremin = score; best = l; }

}

dest(x,y) = best;

std::fprintf(stdout,"%c",font_code[dest(x,y)](0));

}

std::fprintf(stdout,"\n");

}

std::exit(0);

return 0;

}