将ppm文件转换为ASCII艺术

时间:2018-01-31 13:27:10

标签: c ppm 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;
}

1 个答案:

答案 0 :(得分: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();