Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 105,764 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,637 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!




.bmp to ASCII art converter

Converts a 24-bit .bmp image into ASCII art. Looks really cool.

Submitted By: WolfCoder
Actions:
Rating:
Views: 4,992

Language: C++

Last Modified: March 17, 2008
Instructions: Compile and run. Have a file named source.bmp in the working directory of the program. For best results, use an image that has a width divisible by 8 and is no bigger than 320 pixels in width (height does not matter). A file called art.txt will appear when the program is run. Try viewing with 5-point terminal font if you can't easily see the image.

For adding shades with letters, simply add one to the array near the top and increment the MAX_SHADES macro to fit.

Snippet


  1. // Primitive BMP to ASCII art generator
  2. // Reads source.bmp and outputs art.txt
  3. // Source must be 24-bit .bmp
  4.  
  5. #include <iostream.h>
  6. #include <windows.h>
  7.  
  8. #define MAX_SHADES 10
  9.  
  10. BITMAPFILEHEADER bfh;
  11. BITMAPINFOHEADER bih;
  12. RGBTRIPLE *image;
  13. DWORD written;
  14. HANDLE hfile;
  15. int imagesize;
  16. char shades[MAX_SHADES] = {'#','$','O','=','+','|','-','^','.',' '};
  17. char return1 = 0x0D;
  18. char return2 = 0x0A;
  19. int needle = 0;
  20. int average_color = 0;
  21.  
  22. int main()
  23. {
  24.      // Open a channel to source file
  25.      hfile = CreateFile("source.bmp",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,NULL,NULL);
  26.      // Read header
  27.      ReadFile(hfile,&bfh,sizeof(bfh),&written,NULL);
  28.      ReadFile(hfile,&bih,sizeof(bih),&written,NULL);
  29.      // Read image
  30.      imagesize = bih.biSizeImage;
  31.      image = new RGBTRIPLE[imagesize];
  32.      ReadFile(hfile,image,imagesize*sizeof(RGBTRIPLE),&written,NULL);
  33.      // Close source file
  34.      CloseHandle(hfile);
  35.      // Open channel to output
  36.      hfile = CreateFile("art.txt",GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  37.      // Keeping in mind the image is upside down, convert and write it
  38.      // It down-samples the image a bit in res too
  39.      for(int y = bih.biHeight-1;y >= 0;y--)
  40.      {
  41.           for(int x = 0;x < bih.biWidth;x++)
  42.           {
  43.                // Get the average color
  44.                average_color = (image[x+y*bih.biWidth].rgbtBlue+image[x+y*bih.biWidth].rgbtRed+image[x+y*bih.biWidth].rgbtGreen)/3;
  45.                // Convert to a shade of 8
  46.                average_color /= (256/MAX_SHADES);
  47.                if(average_color >= MAX_SHADES)
  48.                     average_color -= 1;
  49.                // Output
  50.                WriteFile(hfile,&shades[average_color],1,&written,NULL);
  51.                //WriteFile(hfile,&shades[average_color],1,&written,NULL);
  52.           }
  53.           WriteFile(hfile,&return1,1,&written,NULL);
  54.           WriteFile(hfile,&return2,1,&written,NULL);
  55.      }
  56.      // Close handle to output
  57.      CloseHandle(hfile);
  58.      return 0;
  59. }

Copy & Paste


Comments


imamkomc 2007-11-02 03:42:34

REally Better, Why you not write code to convert ASCII to *.bmp ? || convert all extention of picture like *.jpg, *tiff, *jpeg, *.gif etc to Ascii.

WolfCoder 2008-07-23 15:36:35

I don't know how to do that, but, it would be interesting for you to try that as a programming exercise (my excuse^^)


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month