C - PPM双下标数组错误



我正在为我的学校项目做一个程序。它应该读取ppm图像并返回梯度作为输出。但我有一个大的问题,如420 x 360的图像。它可以很好地处理两边低于300像素的图像。谁能告诉我我的代码出了什么问题?谢谢。哦,它在c。

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

struct imageHeader{
    char code[3];
    char startComment;
    char comment[1000];
    int width;
    int height;
    int offset;
} header;
struct imagePixel{
    int red;
    int green;
    int blue;
    int total;
};
int main(){
    FILE *image;
    FILE *imageOut;

    image = fopen("sunset.ppm", "rb");
    imageOut = fopen("output.ppm", "wb");
    fseek(image, 0, SEEK_SET);
    fseek(imageOut, 0, SEEK_SET);
    // ********* Get the right Code *************
    fgets(header.code, sizeof(header.code), image);

    if(strcmp(header.code, "P3")){
        puts("Not suitable file format!");
    }
    fputs(header.code, imageOut);
    fputs("n", imageOut);
    //*********** SKIP THE COMMENT IF EXIST *****************
    header.startComment = fgetc(image);
    if(header.startComment = '#'){
        fgets(header.comment, sizeof(header.comment), image);
    }
    fputs(header.comment, imageOut);
    //**************** Get the Width of the picture ***************
    fscanf(image, "%d", &header.width);
    fprintf(imageOut, "%d ", header.width);
    //**************** Get the Height of the picture **************
    fscanf(image, "%d", &header.height);
    fprintf(imageOut, "%dn", header.height);

    //***************** Get the offset color of the picture ************
    fscanf(image, "%d", &header.offset);
    fprintf(imageOut, "%dn", header.offset);
    struct imagePixel *ptrOne;

ptrOne = (struct imagePixel*) malloc(sizeof(ptrOne) * header.height *header.width);
if (ptrOne == NULL){
    printf("Error! Run out of memory!");
    return 1;
}
struct imagePixel number[header.height][header.width];
    //**************read the image*****************
    for(int i = 0; i <= header.height - 1; i++){
        for(int j = 0; j <= header.width - 1; j++){
            fscanf(image, "%d", &number[i][j].red);
            fscanf(image, "%d", &number[i][j].green);
            fscanf(image, "%d", &number[i][j].blue);
            number[i][j].total = (0.2126 * number[i][j].red) + (0.7152 * number[i][j].green) + (0.0722 * number[i][j].blue);
            //number[i][j].total = (number[i][j].red) + (number[i][j].green) + (number[i][j].blue);
        }
    }

    for(int i = 0; i <= header.height - 1; i++){
        for(int j = 0; j <= header.width - 1; j++){
            int axisXswap = i;
            int axisYswap = j;
            for(int x = i; x <= header.height - 1; x++){
                for(int y = j; y <= header.width - 1; y++){
                    if(number[x][y].total > number[axisXswap][axisYswap].total){
                        axisXswap = x;
                        axisYswap = y;
                    }
                }
            }
            struct imagePixel temp;
            temp.red = number[i][j].red;
            temp.green = number[i][j].green;
            temp.blue = number[i][j].blue;
            temp.total = number[i][j].total;
            number[i][j].red = number[axisXswap][axisYswap].red;
            number[i][j].green = number[axisXswap][axisYswap].green;
            number[i][j].blue = number[axisXswap][axisYswap].blue;
            number[i][j].total = number[axisXswap][axisYswap].total;
            number[axisXswap][axisYswap].red = temp.red;
            number[axisXswap][axisYswap].green = temp.green;
            number[axisXswap][axisYswap].blue = temp.blue;
            number[axisXswap][axisYswap].total = temp.total;
        }
    }
    for(int i = 0; i <= header.height-1; i++){
        for(int j = 0; j <= header.width-1; j++){
            fprintf(imageOut, "%d %d %dn", number[i][j].red, number[i][j].green, number[i][j].blue);
        }
    }

    fclose(image);
    fclose(imageOut);

}

不应该在堆栈上分配,而应该使用堆:

struct imagePixel **number; // two dimensional array of pixels
// allocate rows
number = malloc(sizeof(struct imagePixel *) * header.height);
for (i = 0; i < header.height; ++i) {
    // allocate columns for each row
    number[0] = malloc(sizeof(struct imagePixel) * header.width);
}
// do free() in reverse as above

最新更新