Saturday, 23 November 2013

Tutorial: Convert the image to ANSI C++

I do not know how it will be interesting to someone, but recently decided to play around and do the following: Given: Image (eg, BMP) 640 400 8 16 font is necessary: ​​Translate it into ANSI pseudographics in standard mode 80 to 25 characters , characters and background can be any color (true color). Due to the very good comments I updated version: link She obtained about these pictures:



It is worth to mention that "Labour» EGA text mode with 16 colors we do not fit, as will be too ugly because of low resolution and large characters. The second argument: the problem is not worth it to approximate image symbolical representation, since then we could reduce the font, increase the resolution, etc. Task - to make just such a filter as described above. Why am I here publish it: I thought that novice programmers would benefit from practice in the understanding of the algorithm, so I said, this material as a tutorial. To realize we need some library graphics, able getpixel and putpixel, that is considered the color of a pixel on the screen and draw a pixel on the screen with the specified color. I use an old but proven library Allegro (www.allegro.cc), which is easy to use and allows you to work on different operating systems (even in MSDOS!). But enough words, it's time to work.






const  int size_x = 640 ;
 const  int size_y = 400 ;
 const  int ascii_x = 80 ;
 const  int ascii_y = 25 ;


Set constants responsible for picture size and the size of its text representation.
for(int cx=0;cx<ascii_x;cx++)
    for(int cy=0;cy<ascii_y;cy++){
        int fullr=0;
        int fullg=0;
        int fullb=0;
        int pixels=0;

/ / Find background
        
            fullr + = r; fullg + = g; fullb + = b;
            pixels + +;
        }
        fullr / = pixels; fullg / = pixels; fullb / = pixels;


Start the main loop, which consistently find each symbol of 80 by 25 matrix. The next cycle - for each pixel of the picture area that corresponds to this character on the screen of the matrix. As can easily imagine, this area is 640/80 = 8 400/25 = 16 pixels.Here it is useful to remember that this is equal to the size of our font. This second cycle of our task - to find the average color, which we will set the background color of our ANSI character. Make it very simple: to decompose the color R, G, B components and calculate the average for each of them.


/ / Find and ASCII ITS color 
        int RMS = 100000000 ;

        for ( int curch = 1 ; curch < 256 ; curch + +) { / / loop over ASCII table 
            int Charr = 0 ;
             int charg = 0 ;
             int charb = 0 ;

/ / Find char color 
            pixels = 0 ; / / number of pixels for
            

                actual char pixels only
                    charr + = r;
                    charg + = g;
                    charb + = b;
                    pixels + +;
                }
            }
            if (pixels! = 0 ) {
                charr / = pixels; charg / = pixels; charb / = pixels;
            }


Next is more interesting. We have to insert another cycle - for each symbol of the ASCII table (there are 256). I take them out of the black and white pictures with letters, so theoretically, there can be any image 8 of 16 as a character. It is worth noting that at this point we can restrict ourselves to any one region of characters (such as Cyrillic), which greatly affect the outcome. In the above snippet, we find the character color. To do this, we consider the sum as above, but with a slight modification: the pixels are taken on a mask, which is a symbol of the current font. Thus, the second "middle color" matches only the pixels that will be drawn on the character itself. 's also worth noting that the number of pixels can be zero (for example, the symbol 32 - space). With that we should be careful.




/ / Find
            

                pixel
                    rr = charr;
                    gg = charg;
                    bb = charb;
                }
                else { / / Pixel back
                    rr = fullr;
                    gg = fullg;
                    bb = fullb;
                }
                currms + = sqrt ((r-rr) * (r-rr) + (g-gg) * (g-gg) + (b-bb) * (b-bb));
            }

            if (currms <RMS) { / / Find Minimal RMS
                findr = charr;
                findg = charg;
                findb = charb;

                findch = curch;
                rms = currms;
                if (DEBUG) printf ( "!!!% D% D \ n" , currms, curch);
            }
            else 
                if (DEBUG) printf ( "% D% D \ n" , currms, curch);

        } / / Find char


And, finally, came to the most salt. In this piece of code, we have time to pass for a predetermined pixel area of the picture and assume RMS (root mean square deviation) of the three color components of the color of the background or characters (depending on whether the pixel is active on the background or the symbol). Summary and RMS will be the measure of how well this symbol corresponds to the art of the picture. following is output to the screen (in two modes - with and without background ( example )), but in it we shall not dwell, since everything is self-evident. program and the source code can be downloaded here: dimouse.ru / Data / ansiconv.rar (130 Kb). How about draw this thing in text mode? As you can see, the program creates a file with the results (except for the new bmp) - ascii. There for each character is stored in the value ASCII table, its color and its background color. I use text mode pdcurses.sourceforge.net / (write in the comments if you know something better), but this library, though a wrapper over the SDL (anyway, Vindovyh version - exactly) can not show colors more than 16: the difficult legacy of antiquity. As you probably remember, PDCurses originates from the famous Interactive Fiction games Curses . But a friend of mine wrote the improvement of the library and posted here: www.projectpluto.com/win32a.htm Unfortunately, it can not use bitmap font, only the system (but you can choose any) that spoils the experience. But the problem can also be considered completed. program to read ascii can be downloaded here: dimouse.ru / Data / ascii.rar (70 Kb). 

No comments:

Post a Comment