【问题标题】:Converting a ppm file into ASCII art [closed]将 ppm 文件转换为 ASCII 艺术 [关闭]
【发布时间】:2018-01-31 13:27:09
【问题描述】:

我正在尝试编写将 ppm 图像转换为 ASCII 艺术的代码。我已经编写了我的代码,但这不能正常工作。事实上,它显示的东西与原始图像明显不同。我已阅读 ppm 文件并将 ASCII 艺术字符写入文本文件。有没有人可以帮助我?

这是我的代码:

#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 ){
        return ',';
    }else if(greyscale >= 180 ){
        return ':';
    }else if(greyscale >= 160 ){
        return '^';
    }else if(greyscale >= 130 ){
        return '-';
    }else if(greyscale >= 100 ){
        return '*';
    }else if(greyscale >= 70 ){
        return '8';
    }else if(greyscale >= 50 ){
        return '=';
    }else {
        return '#';
}
}
int main(){
    char ppmFile[100];
    char outputFile[100];

    int n;

    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, "r");
    FILE *output = fopen(outputFile, "w"); 

    int width, height; // max pixel is always 255
    // read the header from the ppm file
    printf("two files succesfully opened");
    fscanf(input, "P3\n%d %d\n255\n", &width, &height);
    printf("file read correctly that width=%d and height=%d",width ,height);
    // allocate place for 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 (y = 0; y < height;y++){ 
        for(x=0; x < width; x++){
            array[x][y][0] = fgetc(input); //red
            array[x][y][1] = fgetc(input); //green
            array[x][y][2] = fgetc(input); //blue
}}
            int greyscale;
            int i, j;
            int blockx,blocky,sum=0;
            // convert blocks of pixels to a character and write it into output file
            for(j = 0; j < height; j+=n){
                for(i=0; i <width ; i+=n){sum=0;
                    for(blocky=0; blocky < n&&(j+blocky)<height; blocky++){
                    for(blockx = 0; blockx  < n&&(i+blockx)<width; blockx++){
                    // greyscale = (red + green +blue)/3;
                    sum+=(array[blockx+i][blocky+j][0] + array[blockx+i][blocky+j][1] +array[blockx+i][blocky+j][2]);
                    }
                    }
                    greyscale = sum/(3*n*n);
                    char c = method_of_conversion(greyscale);
                    fprintf(output,"%c ",c);
                     // write the ASCII art directly in the output file
                }
            fprintf(output,"\n"); // dont forget to go into a new line
            }   
    free(array);
    fclose(input);
    fclose(output);

    return 0;
}

【问题讨论】:

  • 请阅读如何提供minimal reproducible example。例如,如果文件加载不是问题,那么我们不需要查看它。您可以删除它并用更简单的硬编码输入替换它。不要让我们调试您的代码。
  • 使用神奇的“数字”P3,您可以定义一个普通的 PPM,其中像素存储为 “高度行的栅格,按从上到下的顺序排列。每行由宽度像素组成,在从左到右的顺序。每个像素是红色、绿色和蓝色样本的三元组,按此顺序。""光栅中的每个样本前后都有空白"(来自netpbm.sourceforge.net/doc/ppm.html)。你读错了文件。
  • 另外,要“为数组[宽度][长度][3]分配位置”,您可以声明int array[height][width][3];(甚至是short int)。
  • tip:如果你必须说“它显示的东西与原始图像明显不同”之类的,那么你还需要显示(A)原始图像(B ) 你想要什么 (C) 它正在显示什么。
  • 欢迎来到 Stack Overflow!请edit你的问题告诉我们你做了什么样的调试。例如,我希望您已将代码缩减为 minimal reproducible example 并使用调试器(例如 GDB)进行调查。确保您也启用了全套编译器警告。这些工具告诉了你什么,它们缺少什么信息?并阅读 Eric Lippert 的 How to debug small programs

标签: c ppm ascii-art


【解决方案1】:

这里:

array[x][y][0] = fgetc(input); //red
array[x][y][1] = fgetc(input); //green
array[x][y][2] = fgetc(input); //blue

您从文件中读取单个字符的字符代码。那不是你想要的。 PPM 格式将像素信息存储为 0 到 255 之间的数字。您可以使用 fscanf 来读取它们:

int res = fscanf(input, "%d%d%d",
                        &array[x][y][0],
                        &array[x][y][1],
                        &array[x][y][2]);

if (res < 3) handle_error();

【讨论】:

  • 你明白了。问题解决了。代码现在可以工作了。非常感谢。
  • @SaberDinpashoh 如果答案对您有帮助,请点赞。如果它解决了您的问题,请将其标记为答案。
猜你喜欢
  • 2015-08-04
  • 1970-01-01
  • 2015-10-10
  • 2021-11-28
  • 2011-11-14
  • 1970-01-01
相关资源
最近更新 更多