1#include <cstdio>2#include <cstring>3#include <stdint.h>4#include <windows.h>5 int32_t width,height;6Rgbquad *pixels;7 BOOLOpenbitmap (Char Const*filename)8 {9FILE *file = fopen (filename,"RB");Ten if(file) One { AWidth=0; -height=0; - Bitmapfileheader BF; the bitmapinfoheader bi; -Fread (&BF,sizeof(BF),1, file); -Fread (&bi,sizeof(BI),1, file); - if(bi.bibitcount!= -) + return false; - if(bi.bicompression!=Bi_rgb) + return false; AWidth=Bi.biwidth; atheight=Bi.biheight; -pixels=Newrgbquad[width*height]; -uint32_t rowsize = (Bi.bibitcount * width + to) / +*4; -uint8_t *line =NewUint8_t[rowsize]; - for(inty =0; Y < height; y++) - { inFread (line, Rowsize,1, file); - for(intx =0; x < width; X + +) to { +uint8_t *color = line + x *3; -Rgbquad *pixel = &pixels[(height-y-1) * width+x]; thePixel->rgbblue = color[0]; *Pixel->rgbgreen = color[1]; $pixel->rgbred = color[2];Panax Notoginseng } - } the Delete[] line; + fclose (file); A return true; the } + return false; - } $Rgbquad GetColor (intXintYintWinth) $ { - intR =0, G =0, B =0; - for(inti =0; i < W; i++) the { - if(i + x >= width)Continue;Wuyi for(intj =0; J < H; J + +) the { - if(j + Y >= height)Continue; WuRgbquadConst& color = pixels[(y + j) * Width + (x +i)]; -R + =color.rgbred; AboutG + =Color.rgbgreen; $B + =Color.rgbblue; - } - } - returnRgbquad{r/(W * h), G/(W * h), b/(W *h)}; A } + CharColortocharacter (RgbquadConst&color) the { - intBrightness = (color.rgbred + color.rgbgreen + color.rgbblue)/3; $ Static Char Const*characters ="qo0v1,:*-."; the intCount =strlen (characters); the intspan =0xFF/count; the intCIDX = Brightness/span; the if(CIDX = =count) -cidx--; in returnCharacters[cidx]; the } the voidOutputascii (Const Char* FileName,intWinth) About { theFILE *file=fopen (filename,"W"); the intx = width/W; the inty = height/h; + for(inti =0; i < height; i + =y) - { the for(intj =0; J < width; J + =x)Bayi { theRgbquad color =GetColor (J, I, X, y); thefprintf (file,"%c", Colortocharacter (color)); - //printf ("%c", Colortocharacter (color)); - } thefprintf (file,"\ n"); the //printf ("\ n"); the } the } - intMain () the { the if(Openbitmap ("a.bmp")) theOutputascii ("a.txt", width/6, height/ A);94 return 0; the}
24-bit bitmap bmp converted to characters