将ppm文件转换为ASCII art

将ppm文件转换为ASCII art,c,ppm,ascii-art,C,Ppm,Ascii Art,我正在尝试编写一个代码,用于将ppm图像转换为ASCII art。我已经写了我的代码,但它不能正常工作。事实上,它显示了与原始图像明显不同的东西。我已经阅读了ppm文件,并在文本文件中写入了ASCII艺术字符。有人能帮我吗 这是我的密码: #include <stdio.h> #include <stdlib.h> // convert the calculated greyscale to a character based on brightness char me

我正在尝试编写一个代码,用于将ppm图像转换为ASCII art。我已经写了我的代码,但它不能正常工作。事实上,它显示了与原始图像明显不同的东西。我已经阅读了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;
}
#包括
#包括
//根据亮度将计算出的灰度转换为字符
字符转换方法(整数灰度){
如果(灰度>=230){
返回“”;
}否则如果(灰度>=200){
返回“,”;
}否则如果(灰度>=180){
返回“:”;
}否则如果(灰度>=160){
返回“^”;
}否则如果(灰度>=130){
返回“-”;
}否则如果(灰度>=100){
返回“*”;
}否则如果(灰度>=70){
返回'8';
}否则如果(灰度>=50){
返回“=”;
}否则{
返回“#”;
}
}
int main(){
char-ppmFile[100];
字符输出文件[100];
int n;
scanf(“%s”,ppmFile);//读取输入文件的名称
scanf(“%s”,outputFile);//读取输出文件的名称
//必须转换为ascii艺术字符的像素窗口大小
scanf(“%d”和“&n”);
文件*input=fopen(ppmFile,“r”);
FILE*output=fopen(outputFile,“w”);
int-width,height;//最大像素始终为255
//从ppm文件中读取头文件
printf(“两个文件成功打开”);
fscanf(输入,“P3\n%d%d\n255\n”、&宽度和高度);
printf(“文件读取正确,宽度=%d,高度=%d”,宽度,高度);
//为数组[宽度][长度][3]分配位置
INTA,b;
int***阵列;
数组=malloc(宽度*sizeof(int**));
对于(a=0;a
您从文件中读取单个字符的字符代码。这不是您想要的。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();
int res=fscanf(输入,“%d%d%d”,
&数组[x][y][0],
&数组[x][y][1],
&数组[x][y][2];
如果(res<3)处理_错误();

请阅读如何提供。例如,如果文件加载不是问题,那么我们不需要看到它。您可以删除它,并用更简单的硬编码输入替换它。不要让我们调试您的代码。使用神奇的“数字”P3定义一个普通PPM,其中像素存储为“高度行的光栅,按从上到下的顺序排列。每行由宽度像素组成,按从左到右的顺序排列。每个像素是按该顺序排列的红色、绿色和蓝色样本的三元组。”以及“光栅中的每个样本前后都有空白”(从)。您错误地读取了文件。另外,要“为数组[Width]分配位置”[length][3]“您可以声明
int数组[height][width][3];
(甚至是
short int
)。提示:如果您必须说“它显示的内容与原始图像明显不同”之类的话,那么您还需要显示(a)原始图像(B)您想要的内容(C)它显示的是什么。欢迎使用堆栈溢出!请将您的问题显示给我们您所做的调试类型。我希望您已将代码缩减为a,并使用诸如GDB之类的调试器进行调查。请确保您也启用了一整套编译器警告。这些工具告诉了您什么,它们是什么信息失踪?读埃里克·利珀特的。你明白了。问题解决了。代码正在运行。非常感谢。@SaberDinpashoh如果答案对你有帮助,请投票。如果它解决了你的问题,请将其标记为答案。
int res = fscanf(input, "%d%d%d",
                        &array[x][y][0],
                        &array[x][y][1],
                        &array[x][y][2]);

if (res < 3) handle_error();