[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppVclBitmapToAscii
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppVclBitmapToAscii
(C++ VCL Graphics) Bitmap to ASCII
This code converts a bitmap stored in a TImage to an ASCII-art image.//--------------------------------------------------------------------------- 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))
- include <algorithm>
- include <cassert>
- include <Classes.hpp> //For TStringList
- include <boost/shared_ptr.hpp>
- static_cast<double>(image->Height);
Code links
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
