I wrote a program for converting a file from PPM format to ASCII art. Every pixel of the input image is converted to greyscale by calculating the average of red green and blue values ((red + green +blue)/3).

The upgrade of the basic version is that I calculate the average of RGB in a window of size nn, and convert it to a character which would decrease the size of the image. So if the given number n is 3, the window is of size 33, which means that you sum all nine red, green and blue values and divide it by 3 times nn (33 in this case). When the program counts the greyscale for the first window, it moves to the other one and so on until the end. When there is the end of width, it jumps down by the n number of pixels and reads the next line, etc.

When the greyscale is calculated it is then converted to a character and this character is being written to the output file to form an ASCII art image.

There is the code that should do this work:

#include <stdio.h>
#include <stdlib.h>

// convert the calculated greyscale to a character based on brightness
char method_of_conversion(int greyscale){
    if(greyscale >= 230){
        return ' ';
    }else if(greyscale >= 200 && greyscale < 230){
        return '.';
    }else if(greyscale >= 180 && greyscale < 200){
        return '\'';
    }else if(greyscale >= 160 && greyscale < 180){
        return ':';
    }else if(greyscale >= 130 && greyscale < 160){
        return 'o';
    }else if(greyscale >= 100 && greyscale < 130){
        return '&';
    }else if(greyscale >= 70 && greyscale < 100){
        return '8';
    }else if(greyscale >= 50 && greyscale < 70){
        return '#';
    }else if(greyscale < 50){
        return '@';
    }
}

int main(){
    char ppmFile[100];
    char outputFile[100];

    int n; // the size of the block

    scanf("%s", &ppmFile); //read the name of input file
    scanf("%s", &outputFile); //read the name of output file 
    // the size of a window of pixels you have to convert to ascii art character
    scanf("%d", &n); 

    FILE *input = fopen(ppmFile, "rb");
    FILE *output = fopen(outputFile, "w"); 

    int width, height; 
    // read header from ppm file
    fscanf(input, "P6\n%d %d\n255\n", &width, &height);

    // array[width][length][3]
    int a, b;
    int ***array;
    array = malloc(width*sizeof(int **));
    for(a = 0; a < width; a++){
        array[a] = malloc(height*sizeof(int *));
        for(b = 0; b < height; b++){
            array[a][b] = malloc(3*sizeof(int));
        }
    }

    int x, y;
    for (x = 0; x < width; x++){ 
        for(y=0; y < height; y++){
            array[x][y][0] = fgetc(input); //red
            array[x][y][1] = fgetc(input); //green
            array[x][y][2] = fgetc(input); //blue

            int greyscale;
            // greyscale = (red + green +blue)/3;

            int blockx, blocky;
            for(blockx = 0; blockx < n; blockx+=n){
                for(blocky=0; blocky < n; blocky+=n){
                    greyscale = ((array[x][y][0] + array[x][y][1] +array[x][y][2])/(3*(n*n)));
                    char c = method_of_conversion(greyscale);
                    fprintf(output,"%c",c); // write ASCII art to the output file 
                }

            }   
        }fprintf(output,"\n");
    }

    free(array);
    fclose(input);
    fclose(output);

    return 0;
}

The output file for one ppm should look like this:

                    ..'::....'oo&&:..                                           
               ..:'#8@@oo#@@@@@@@@@8      .8&o              .&o.                
            ooo:'.'o88'    8@@@@@@@&                8'    '@@@@@@' .  .&..      
   '@@@8&'o8&:8@@':@o@#   &@@@@@&         &@@o    .o'@8@@@@@@@@@@@@@@@@@@#o:&o
:& 8@@@@@@@@@@@@@@@@8&.&@o  #@@'   #8     :@@8@@o@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   &@@#o8@@@@@@@@@@   '@o.   :'          @@@ #@@@@@@@@@@@@@@@@@@@@@@@@@@@@8:8&' 
    ..    &@@@@@@@@#&..@@@o           'o  :o.@@@@@@@@@@@@@@@@@@@@@@@@@&   .@    
           &@@@@@@@@@@#@@@@&          '8o@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8        
            8@@@@@@@@@@@@8 &           &@@@@@@@@@@#:@@@@@@@@@@@@@@@@@@@.        
            #@@@@@@@@@@@:             8##:&'@@'..@&&@@@@@@@@@@@@@@@@@& &        
            '@@@@@@@@@@.              @@   '. @@@@@ @@@@@@@@@@@@@@: &  :        
             :@@@@@@@@&               8@@@&    .@@@@@@@@@@@@@@@@@@@  &.         
              :@@@@:.8               :@@@@@@@@@@@@@@@@@@@@@@@@@@@@@             
               '@@.                 :@@@@@@@@@@#8@@&8''@@@@@@@@@@@:             
                '@&.o '.'           @@@@@@@@@@@@:@@@@  .@@@''@@8o               
                  &@&.              @@@@@@@@@@@@@o@&    &@   o@@  .             
                     : :o.          o@@@@@@@@@@@@@'.    .@    '@.  '            
                      :@@@@.         &@@#@@@@@@@@@@      .    :  . o            
                      o@@@@@@             @@@@@@@@.          .# '@              
                      @@@@@@@@o           @@@@@@@             8oo@o .8&'        
                      @@@@@@@@@@.         .@@@@@#              :&.    '@8       
                      .@@@@@@@@8           @@@@@@                    : '        
                       o@@@@@@@'          .@@@@@@ 8                :#@o8'       
                        &@@@@@@            @@@@@ .@              .&@@@@@@       
                        8@@@@&             @@@@8 .o              @@@@@@@@@      
                        @@@@@              .@@@                  o@@@@@@@@      
                        @@@@                @8                   '@o  #@@8      
                       :@@#                                            o&     .o
                       :@#                                              :    .: 
                       8@.                                                   '  
                       o#                                                       
                        '                                                       

                         .'                        ..       ...... . ..         
                        8@               ..:'.o#&@@@@@@:o@@@@@@@@@@@@@@@@@#o.   
         ':8888&&@@@@8@@@@'         8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8.  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

But my program outputs it like this:

                    ..'::....'oo&&:..   

               ..:'#8@@oo#@@@@@@@@@8    
  .8&o              .&o.                
            ooo:'.'o88'    8@@@@@@@&    
            8'    '@@@@@@' .  .&..      
   '@@@8&'o8&:8@@':@o@#   &@@@@@&     
    &@@o    .o'@8@@@@@@@@@@@@@@@@@@#o:&o
:& 8@@@@@@@@@@@@@@@@8&.&@o  #@@'   #8   
  :@@8@@o@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
   &@@#o8@@@@@@@@@@   '@o.   :'         
 @@@ #@@@@@@@@@@@@@@@@@@@@@@@@@@@@8:8&' 
    ..    &@@@@@@@@#&..@@@o           'o
  :o.@@@@@@@@@@@@@@@@@@@@@@@@@&   .@    
           &@@@@@@@@@@#@@@@&          '8
o@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8        
            8@@@@@@@@@@@@8 &           &
@@@@@@@@@@#:@@@@@@@@@@@@@@@@@@@.        
            #@@@@@@@@@@@:             8#
#:&'@@'..@&&@@@@@@@@@@@@@@@@@& &        
            '@@@@@@@@@@.              @@
   '. @@@@@ @@@@@@@@@@@@@@: &  :        
             :@@@@@@@@&               8@
@@&    .@@@@@@@@@@@@@@@@@@@  &.         
              :@@@@:.8               :@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@             
               '@@.                 :@@@
@@@@@@@#8@@&8''@@@@@@@@@@@:             
                '@&.o '.'           @@@@
@@@@@@@@:@@@@  .@@@''@@8o               
                  &@&.              @@@@
@@@@@@@@@o@&    &@   o@@  .             
                     : :o.          o@@@
@@@@@@@@@@'.    .@    '@.  '            
                      :@@@@.         &@@
#@@@@@@@@@@      .    :  . o            
                      o@@@@@@           
  @@@@@@@@.          .# '@              
                      @@@@@@@@o         
  @@@@@@@             8oo@o .8&'        
                      @@@@@@@@@@.       
  .@@@@@#              :&.    '@8       
                      .@@@@@@@@8        
   @@@@@@                    : '        
                       o@@@@@@@'        
  .@@@@@@ 8                :#@o8'       
                        &@@@@@@         
   @@@@@ .@              .&@@@@@@       
                        8@@@@&          
   @@@@8 .o              @@@@@@@@@      
                        @@@@@           
   .@@@                  o@@@@@@@@      
                        @@@@            
    @8                   '@o  #@@8      
                       :@@#             
                               o&     .o
                       :@#              
                                :    .: 
                       8@.              
                                     '  
                       o#               

                        '               



                         .'             
           ..       ...... . ..         
                        8@              
 ..:'.o#&@@@@@@:o@@@@@@@@@@@@@@@@@#o.   
         ':8888&&@@@@8@@@@'         8@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@8.  
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Can anyone tell me what am I doing wrong?

Recommended Answers

All 3 Replies

You're putting an extra line-ending in the middle of every line.

Which you could have done (I haven't checked your code) by getting width and height mixed up somewhere; your picture is 78x39, but it's meant to be 39x78.

As usual, Moschops hits the nail on the head!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.