Wikipedia:ASCII art conversion tool

related topics
{system, computer, user}
{math, number, function}
{work, book, publish}
{law, state, case}
{@card@, make, design}
{style, bgcolor, rowspan}
{day, year, event}

Given the filename of a monochrome bitmap in argv[1], this program will produce an ASCII art representation of the bitmap on stdout. It uses the Allegro library.

/*

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();


Output of ascii wikilogo.bmp, where wikilogo.bmp contains an earlier Wikipedia logo:

                                                           ,M., a. a  , ,.                                                          
                                              a   d.(M.  (b."'"'`"""' "'V'`"MMM'M.a.,., a                                           
                                       a. dbM'"'  `     ,a                  `  "     "'""M)aa                                       
                                    (M""'     a.    ,   (Mba a.aa.   aa a.a   a ,           ""'`a.                                 
                                      ,da ,  (P"V'M)    () M V' ""   V) "b. ,aMb`VbM) aaa.a ,  , ` ,                                
                                  ,ad.d VP   `"  ,                                    " " ( ,a `Vaa                                 
                               ,.""'"'                        (M                             "  `'""MMa                            
                           aM              ,  a a.Vb.d)M'V) M)(M  VM)VP'   (M"Vb `M.aa aa             `  ,a                        
                        ,`"     dMa.a.Ma   VMaM.d.,M)MMa.`" "'`"""""'""    `M)`'VbMb "MP"bPMMM',aaa.  d   `" ,                     
                           ,  M'M)dM" `"            `' `                   `"'             " ` "'V)M  V)aa    M)                   
                        ("M  `"'                      (M                 M)   `     (M                `"","'aa                      
                       `                   aaa.aa  db.(M.d)      `M.MM.V'MP"M.da ,   Mba.                   `"M                     
                       ,    ,a   ,M V"V)MP M M)(MaadM)(M `VMaa    `M)VM  M) M)(MPM.  M)(M   M)VM.   da  (                           
                     ,adba/'V   ,dM)M.V'"'""'"'`"'"'` `     `"'             ` `" `"' "'`"   V" ("' ,MM. (PM',aa.                    
                    dMaaP"' "                   ,a             ,a.                                      `""VP"M) ,.                 
                   ` `                (M        (M        ,.    M),                                            ` `V                 
                                aaa   M)     VP'(M"M) `VM MP'   M)"M)Vb/"    "VM)   MPVb.a.a.dM.a.  a.a                             
                  (a.a  (PMPMP"MP MbM d),    db (M M)aaMM Vb    Mba`  VP    ,MMM)   M) `"M)  d. "VbM"""b ,M'VM ,aa                  
                 `\MM)  (.d)""'"'"" """'""                           ,/             MP    "' `'  "'""' "'`V. ,.dMPM ,               
                 `"""                  ,                       ,a                   `                            """"               
                         (b   (M       M'                   ,. (M.                            `            (.                       
                    aa   d)aaad)aaa.MM.M"M)`M"   (b MP"M.   MP'(M"VbadPM)   dP"",d'"b.MPVb.MP M.dPMba.aa.aad   ,.aa ,               
                  (MM) (aaMM) (.aM)MP  M d) M.   (M M) M)   Mb (M (MPMa.    Mba "M ,M)M. M)M. d.() dMb.MadMb.  ,MPVMM
                  `'""  ""'`" `'""\P"V"" "' `    `' "' "'    " `" `"  "'     `"   `'  "' " `' "'"' " `""""""'  """"""               
                            ,      V                  ,a    ,.                                                                      
                            ('    d        `          `M'   M)                                     `             ,                  
                     (MM"ba V)a/b M' "M)MP'M.a/"M),`M. MPMb.M) a.a.   a.VM.a.aa.MPMb.a.a.dMP  MbMM'V)a/"aMPM  ("VM                  
                     (MM.MMadPMa  M.a.M)Ma () MP" ,.Mb.M. M'd)MM)     (bP' M)   M)(MPM)  (M ,aMbMM ()"ba"M.d  `\"V                  
                                  `   "  " `',P"Vb""""'"""  "" """    V""M.`""  "'`" `"' `"  "'                                     
                   (              a       ,   "                       "'             ,          a           (     ,.                
                   `MPMb.aa a ,   M       VM                                         V)         (.       ,. Mba`MMM                 
                    VbdaMM (PbMbP'V.dPM.a VMaaPM.a.aa      ,.a.,a.aa , a. aaa.,.a.   M)ad'   MP'M"VbdP"  "Mb.MPb"'                  
                          `' `'"' ()Mb.`MadM)M)" M)       (MP"  `Mb (M.  MP' VM)  Mb d) "M.  M.aM.d'Vb     `                        
                                         `' ,M"Vb "" `V)   """ `"'"" """ `""  ""' `"""'                       `                     
                         ,     ()            `                                                           a ,ad                      
                          VPaMb Mba ,a   a                               ,`                    ,a.aa.dPVPMbdMP                      
                             Va d.dMM"'VM"V'VbaMM. MPVb., " aaa.   aa a. M)    `M. MPMb"M)"  ,M  adMbM ('""                         
                                  ` "" `"'V'dMPba  M.(MPMa (M)     M) (P'M.   a.M)aM d) V"    "" `                                  
                                                         "'  "'     `'   "    `        (                                            
                                                         a.                                                                         
                                                    aaaa ()  ,   ,a     a ,. aa.,aa.                                                
                                                    (M V'MbMMP"',aM)"Mb.M)dM)M) Mb  ,.                                              
                                                    (M)  `" `"' """"  " """"""   `                                                  
                                                                                                                                    
 ,.   ,a.aa. ,aa.a. aaaaa.aaa                                                                                           aa.         
 "VMMMM"""VMMMM"""MMM""""MMM"                                                                                          dMMM.        
   (MMM)   `MMM) ,MM'   (MM'  aaaaaa  aaaaaa  ,aaaaa ,aaaaaa ,aaaaaaa     aaaaaaaaaaa   aaaaaaaa.        aaaaaa       ,MMMMM.       
    VMMM)   VMMMbMM'   ,MM)   `VMMP'  "MMMP'   MMP"'  "MMMP'  VMMM""VMMa. `VMMP""""MM)  "MMMP""""MMba    `VMMM"       dMPMMMb       
     VMMM.   VMMMM'    dMP     (MM)    MMM)  aMP'      MMM)   (MMM   VMMb  (MM)     "'   MMM)     "MMM.   (MMM       (M) (MMM)      
      MMMb    VMMM    dMP      (MM)    MMM)aMP'        MMM)   `MMM   (MMP  (MM)   ,a     MMM)      `MMM)  (MMM      ,MP   VMMM.     
      `MMMb   dMMMb  ,MM'      (MM)    MMMMMb.         MMM)    MMM ,aMM"'  (MMMMMMMM     MMM)       MMM)  (MMM      MMMaaaMMMMb     
       `MMM) dMPMMMb.MM)       (MM)    MMMPMMMb.       MMM)    MMM"""'     (MM)   VM     MMM)       MMM)  (MMM     dMP     `MMM)    
        VMMMMM) `MMMMMP        (MM)    MMM) "MMMa      MMM)   (MMM         (MM)          MMM)      (MMP'  (MMM    (MM'      VMMM.   
         VMMMP   `MMMP         (MM)    MMM)  `VMMMa    MMM)   (MMM         (MM)     ,MP  MMM)    ,dMM"    (MMM   ,MM)        MMMM.  
         `MMP     VMM'        dMMMMb  dMMMMb   `"MMMbaMMMMMb aMMMMM.      dMMMMMMMMMMM' dMMMMMMMP""'     aMMMMbaMMMMMM.    ,MMMMMMM 
                                                                                                                                    
    ,aaaaaaaa.db               aa. aa                     ,aa.aa.                          db                         (M. aa        
        MM    MM               MM                          M)                              MM                         ,M)           
        MM    MM""M) aP"Mb     MMbaM  MMP"'aP"M. dP"Mb     MMad)  MMP"Mb ,d""V"MM (M',d'""'MM ,dP"Vb.`MP"VM. dP"M. aP'"M) Mb ,d"Mb  
        MM    MM  M)(M)`"      MM     MM  (M) " dMP""      M)     (M) MM M)    `Mb/  M)    MM MM   M) M)  M)MM' " MM.  M) MM  ,.MM  
       ,MM.   MM.,Mb VMaa     ,MM.    MM.  Vbaa  Mbaa     ,Mbaaad dM) MM."Mba.  VM'  "Mba. MM.`VbadP' Mb.aP'`Mbaa `MbadM) MM.MbaMM. 
                                                                               ,d                     M)                            
                                                                               "'                    `""                           

See also: ASCII art

Full article ▸

related documents
Andrew Tridgell
ABC (programming language)
ANSI C
Advogato
Data stream
Grid network
Longitudinal redundancy check
UMSDOS
Dia (software)
Java Development Kit
Common Intermediate Language
Applet
Name server
David A. Huffman
Automatic data processing
Input
Typed link
Pico (text editor)
A-law algorithm
Wikipedia:Federal Standard 1037C terms/telecommunications encryption terms
Small-C
PowerBASIC
Netwide Assembler
GNUstep
Escape character
Yacc
Incremental reading
Bill Atkinson
Common Language Runtime
Data processing