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.
Set constants responsible for picture size and the size of its text representation.
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.
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.
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