[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
CppVclBitmapToAscii

(C++ VCL Graphics) Bitmap to ASCII

This code converts a bitmap stored in a TImage to an ASCII-art image.

  1. include <algorithm>
  2. include <cassert>
  3. include <Classes.hpp> //For TStringList
  4. include <boost/shared_ptr.hpp>
//--------------------------------------------------------------------------- std::vector<char> GetChars() { std::vector<char> chars; chars.push_back('M'); chars.push_back('N'); chars.push_back('m'); chars.push_back('d'); chars.push_back('h'); chars.push_back('y'); chars.push_back('s'); chars.push_back('o'); chars.push_back('+'); chars.push_back('/'); chars.push_back(':'); chars.push_back('-'); chars.push_back('.'); chars.push_back('`'); chars.push_back(' '); return chars; } //--------------------------------------------------------------------------- double GetFractionGrey( const TImage* const image, const int& x1, const int& y1, const int& x2, const int& y2) { Assert(image!=0); Assert(image->AutoSize==true); Assert(x1 < image->Width); Assert(y1 < image->Height); Assert(x2 <= image->Width); Assert(y2 <= image->Height); double sumGrey = 0.0; int nPixels = 0; for (int y=y1; y!=y2; ++y) { const unsigned char * line=static_cast<const unsigned char *>(image->Picture->Bitmap->ScanLine[y]); Assert(y < image->Height); for (int x=x1; x!=x2; ++x) { Assert(x < image->Width); const int sumPixel = line[x*3+0] + line[x*3+1] + line[x*3+2]; Assert( sumPixel < 3 * 256 ); const double grey = static_cast<double>(sumPixel) / (3.0 * 255.0); sumGrey+=grey; ++nPixels; } } if (nPixels == 0) { return std::rand()%256; //Random } else { const double fractionGrey = sumGrey / static_cast<double>(nPixels); return fractionGrey; } } //--------------------------------------------------------------------------- boost::shared_ptr<TStringList> ImageToAscii(const TImage* const image, const int& charWidth) { static const std::vector<char> chars(GetChars()); boost::shared_ptr<TStringList> stringList(new TStringList); if (charWidth < 5) return stringList; //Maxy is in proportion with the bitmap const int maxy = (static_cast<double>(charWidth) / static_cast<double>(image->Width))
          • static_cast<double>(image->Height);
assert(charWidth > 0); assert(maxy > 0); const double dX = static_cast<double>(image->Width) / static_cast<double>(charWidth); const double dY = static_cast<double>(image->Height) / static_cast<double>(maxy); assert(dX > 0.0); assert(dY > 0.0); for (int y=0; y!=maxy; ++y) { String myLine = ""; for (int x=0; x!=charWidth; ++x) { const int x1 = std::min( static_cast<double>(x) * dX , image->Width - 1.0) + 0.5; const int y1 = std::min( static_cast<double>(y) * dY , image->Height - 1.0) + 0.5; const int x2 = std::min( (static_cast<double>(x) * dX) + dX , image->Width - 1.0) + 0.5; const int y2 = std::min( (static_cast<double>(y) * dY) + dY , image->Height - 1.0) + 0.5; assert(x1 >= 0); assert(x2 >= 0); assert(y1 >= 0); assert(y2 >= 0); assert(x1 < image->Width); assert(x2 < image->Width); assert(y1 < image->Height); assert(y2 < image->Height); const double fGrey = std::min(std::max(0.0, GetFractionGrey(image,x1,y1,x2,y2)),1.0); assert(fGrey >= 0.0 && fGrey <= 1.0); const double charIndex = fGrey * static_cast<double>(chars.size() - 1); assert(charIndex >= 0); assert(charIndex < static_cast<int>(chars.size())); const char thisChar = chars[charIndex]; myLine+=thisChar; } stringList->Add(myLine); } return stringList; } //---------------------------------------------------------------------------


Code links



last edited (December 18, 2006) by bilderbikkel, Number of views: 2356, Current Rev: 4 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.