将 ppm 文件转换为 ASCII 艺术

Converting a ppm file into ASCII art

我正在尝试编写将 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;
}

这里:

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