1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| /*
ascii art conversion utility Copyright 2001 Damian Yerrick
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ /*
ascii.c converts a bitmap (in .bmp or .pcx format) to ASCII art. It treats the least significant bitplane as a monochrome image, making 0 dark and 1 light. Each character sent to stdout represents a 2x2 cell in the bitmap.
It uses the Allegro library to read the bitmap. Allegro is a multimedia library that has been ported to POSIX+X11, DOS, Windows, BeOS, and Mac OS. There are library extensions that allow Allegro to handle other file formats such as PNG. http://alleg.sourceforge.net/allegro/
Because of the simple design of the program, it is also extremely easy to port to other imaging APIs.
Compilation on DOS: gcc -Wall -O ascii.c -lalleg -o ascii.exe Compilation on Linux: gcc -Wall -O ascii.c `allegro-config --libs` -o ascii
*/
#include <allegro.h> #include <stdio.h> #include <stdlib.h>
/* the following characters' glyphs represent 2x2 pixel tiles */ const char tiles[16] = { 'M', 'P', 'V', '"', 'b', ')', '\\','\'', 'd', '/', '(', '`', 'a', '.', ',', ' ' };
int main(int argc, char **argv) { BITMAP *bmp; int y;
install_allegro(SYSTEM_NONE, &errno, atexit);
if(argc != 2) { allegro_message("usage: ascii foo.bmp\n"); return EXIT_FAILURE; }
/* if you've installed Allegro library extensions for other bitmap formats, register their functions here */
bmp = load_bitmap(argv[1], NULL); if(bmp == NULL) { allegro_exit();
fprintf(stderr, "ascii Error: could not load bitmap `%s'\n", argv[1]); return EXIT_FAILURE; }
for(y = 0; y < bmp->h; y += 2) { int x; for(x = 0; x < bmp->w; x += 2) { int c = 0; if(getpixel(bmp, x, y ) & 1) c |= 8; if(getpixel(bmp, x+1, y ) & 1) c |= 4; if(getpixel(bmp, x, y+1) & 1) c |= 2; if(getpixel(bmp, x+1, y+1) & 1) c |= 1; putchar(tiles[c]); } putchar('\n'); } destroy_bitmap(bmp);
return EXIT_SUCCESS; } END_OF_MAIN();
|