24-bit bitmap bmp converted to characters

Source: Internet
Author: User

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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.