为什么没有方法将ppm转换为ascii art正常工作? [英] Why doesn't method to convert ppm to ascii art work properly?

查看:72
本文介绍了为什么没有方法将ppm转换为ascii art正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,用于将文件从PPM格式转换为ASCII艺术。通过计算红绿色和蓝色值的平均值((红色+绿色+蓝色)/ 3)将输入图像的每个像素转换为灰度。



升级基本版本是我在一个大小为n * n的窗口中计算RGB的平均值,并将其转换为一个会减小图像大小的字符。因此,如果给定数字n为3,则窗口大小为3 * 3,这意味着您将所有九个红色,绿色和蓝色值相加并将其除以n * n的3倍(在这种情况下为3 * 3)。当程序计算第一个窗口的灰度时,它会移动到另一个窗口,依此类推,直到结束。当有宽度结束时,它会向下跳过n个像素并读取下一行等。



当计算灰度时,它会被转换到一个字符,这个字符被写入输出文件以形成一个ASCII艺术图像。



有代码可以完成这项工作:

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

//将计算出的灰度转换为基于亮度的字符
char method_of_conversion(int greyscale){
if(greyscale> = 230){
return' ;
}否则if(greyscale> = 200&& greyscale< 230){
return'。';;
}否则if(greyscale> = 180&& greyscale< 200){
return'\'';
}否则if(greyscale> = 160&& greyscale< 180){
return':';
}否则if(greyscale> = 130&& greyscale< 160){
return'o';
}否则if(greyscale> = 100&& greyscale< 130){
return'&';
}否则if(greyscale> = 70&& greyscale< 100){
返回'8';
}否则if(greyscale> = 50&& greyscale< 70){
return'#';
}否则if(greyscale< 50){
return'@';
}
}

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

int n; //块的大小

scanf(%s,& ppmFile); //读取输入文件的名称
scanf(%s,& outputFile); //读取输出文件的名称
//您必须转换为ascii艺术字符的像素窗口的大小
scanf(%d,& n);

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

int width,height;
//从ppm文件中读取标题
fscanf(输入,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(输入); // red
array [x] [y] [1] = fgetc(输入); // green
array [x] [y] [2] = fgetc(输入); // blue

int greyscale;
//灰度=(红+绿+蓝)/ 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(输出,%c,c); //将ASCII art写入输出文件
}

}
} fprintf(输出,\ n);
}

free(array);
fclose(输入);
fclose(输出);

返回0;
}





一ppm的输出文件应如下所示:

 ..':: ....'oo&&:.. 
..:'#8 @@ oo#@@@@@@@@@ 8 @ 8& o。& ;○。
ooo:'。'o88'8 @@@@@@@&& 8''@@@@@@'。 。& ..
'@@@ 8&'o8&:8 @@':@ o @#& @@@@@&& & @@ o .o'@ 8 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 8 @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ :'@@@#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@#& ... @@@ o'o:o。@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ 。@
& @@@@@@@@@@#@@@@&& '8o @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@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@''@@ 8o
& @&。 @@@@
@@@@@@@@ @ o @& & @ o @@。
:: o。 o @@@
@@@@@@@@@@'。 。@'@。 '
:@@@@。 & @@
#@@@@@@@@@@。 :。 o
o @@@@@@
@@@@@@@@。 。#'@
@@@@@@@ o
@@@@@@@@@@ 8。@&'
@@@@@@@@@@
。@@@@@#:&。 '@ 8
。@@@@@@@@ 8
@@@@@@:'
o @@@@@@@'
。@@@@ @@ 8:#@ o8'
& @@@@@@
@@@@@。@。& @@@@@@
8 @@@@& amp ;
@@@@ 8 .o @@@@@@@@
@@@@@
。@@@ o @@@@@@@
@@@@
@ 8'@ o#@@ 8
:@@#
o& .o
:@#
:。:
8 @。
'
o#

'



。'
...... ...... ..
8 @
..:'。o#& @@@@@@:o @@@@@@@@@@@@@@@@@@@
':8888&& @@@@ 8 @@@@'8 @@@
@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@ 8。
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@ $ $ $ $ $ $ $
b b b b b b b b b b b b b b =h2_lin>解决方案

在看到这个之前,一切看起来都很好:



  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(输出, %c,c); // 将ASCII art写入输出文件
}
}





此代码与以下内容相同:



 greyscale =(( array  [x] [y] [ 0 ] +  array  [x] [y] [ 1 ] + array [x] [y] [ 2 ])/( 3  *(n * n))); 
char c = method_of_conversion(greyscale);
fprintf(输出, %c,c); // 将ASCII art写入输出文件





这是因为for循环只能执行一次。你说你想要对一个盒装区域内的RGB值求和(例如3 x 3),但上面的循环不求和。您必须先将所有像素读入内存才能汇总。



尝试这样的事情:



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





现在所有像素都在内存中。你的工作可以开始。



  int  greyscale; 
int i,j,* pixel;

for (x = 0 ; x< width; x + = n){
for (y = 0 ; y< height; y + = n ){
greyscale = 0 ;
for (i = 0 ; i< n; i ++){
if (x + i> = width) break ;
for (j = 0 ; j< n; j ++){
if (y + j> = height) break ;
pixel = array [x + i] [y + j];
灰度+ =像素[ 0 ] +像素[ 1 ] +像素[ 2 ]; // RGB
}
}
greyscale / = 3 * N * N;
char c = method_of_conversion(greyscale);
fprintf(输出, %c,c); // 将ASCII art写入输出文件
}
fprintf(输出, \ n);
}





以上代码还处理宽度和高度不是n的倍数的边界条件


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 n*n, 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 3*3, which means that you sum all nine red, green and blue values and divide it by 3 times n*n (3*3 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?

解决方案

Everything looks good until I see this:

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 
    }
}



This code is the same as:

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



This is because the for loop(s) can only execute once. You say you want to sum the RGB values inside a boxed area (eg. 3 x 3) but the loop above does not sum. You must read all the pixels into memory before they can be summed.

Try something like this:

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
    }
}



Now all the pixels are in memory. Your work can begin.

int greyscale;
int i, j, *pixel;

for (x = 0; x < width; x += n) { 
    for (y=0; y < height; y += n) {
        greyscale = 0;
        for (i = 0; i < n; i++) {
            if (x + i >= width) break;
            for (j = 0; j < n; j++) {
                if (y + j >= height) break;
                pixel = array[x + i][y + j];
                greyscale += pixel[0] + pixel[1] + pixel[2]; // RGB
            }
        }
        greyscale /= 3*n*n;
        char c = method_of_conversion(greyscale);
        fprintf(output,"%c",c); // write ASCII art to the output file 
    }
    fprintf(output,"\n");
}



The above code also handles the boundary condition(s) where width and height are not multiples of "n".


这篇关于为什么没有方法将ppm转换为ascii art正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆