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, "%d\n", header.height);


    //***************** Get the offset color of the picture ************
    fscanf(image, "%d", &header.offset);
    fprintf(imageOut, "%d\n", 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 %d\n", 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

错误的数组下标错误

来自分类Dev

数组下标错误

来自分类Dev

Swift数组下标索引编制中的错误?

来自分类Dev

PostgreSQL:错误:数组下标超出范围

来自分类Dev

错误:数组下标的类型无效

来自分类Dev

错误:为数组指定的下标太少

来自分类Dev

ActivePerl ppm命令错误

来自分类Dev

在bash中拆分字符串时数组下标错误

来自分类Dev

错误:下标的值不是内核中的数组,指针或向量

来自分类Dev

数组中的VBA随机元素,下标超出范围错误

来自分类Dev

C错误:数组下标无效的类型'double [int]'

来自分类Dev

数组中的VBA随机元素,下标超出范围错误

来自分类Dev

C错误:数组下标无效的类型'double [int]'

来自分类Dev

编译错误-数组下标的类型'char [int]'无效

来自分类Dev

在bash中拆分字符串时数组下标错误

来自分类Dev

指定数组循环范围的下标超出范围错误

来自分类Dev

尝试递增关联数组元素时出现错误的数组下标错误

来自分类Dev

向关联数组添加条目时出现“错误数组下标”错误

来自分类Dev

数组下标不是整数

来自分类Dev

使用枚举类值索引数组时出现“数组下标不是整数”错误

来自分类Dev

将变量分配给数组时,为什么出现数组下标超出范围错误?

来自分类Dev

二维数组错误:下标值既不是数组,也不是指针,也不是矢量

来自分类Dev

用C ++编写二维数组会产生“数组下标类型无效”错误

来自分类Dev

C ++ 2D数组-数组下标的错误无效类型'int [int]'

来自分类Dev

错误下标

来自分类Dev

在数组中排序负数的错误(双精度)

来自分类Dev

在数组中排序负数的错误(双精度)

来自分类Dev

矩阵乘积导致错误:下标值既不是数组也不是指针也不是向量

来自分类Dev

C错误:行梯形形式的“下标值既不是数组也不是指针”

Related 相关文章

  1. 1

    错误的数组下标错误

  2. 2

    数组下标错误

  3. 3

    Swift数组下标索引编制中的错误?

  4. 4

    PostgreSQL:错误:数组下标超出范围

  5. 5

    错误:数组下标的类型无效

  6. 6

    错误:为数组指定的下标太少

  7. 7

    ActivePerl ppm命令错误

  8. 8

    在bash中拆分字符串时数组下标错误

  9. 9

    错误:下标的值不是内核中的数组,指针或向量

  10. 10

    数组中的VBA随机元素,下标超出范围错误

  11. 11

    C错误:数组下标无效的类型'double [int]'

  12. 12

    数组中的VBA随机元素,下标超出范围错误

  13. 13

    C错误:数组下标无效的类型'double [int]'

  14. 14

    编译错误-数组下标的类型'char [int]'无效

  15. 15

    在bash中拆分字符串时数组下标错误

  16. 16

    指定数组循环范围的下标超出范围错误

  17. 17

    尝试递增关联数组元素时出现错误的数组下标错误

  18. 18

    向关联数组添加条目时出现“错误数组下标”错误

  19. 19

    数组下标不是整数

  20. 20

    使用枚举类值索引数组时出现“数组下标不是整数”错误

  21. 21

    将变量分配给数组时,为什么出现数组下标超出范围错误?

  22. 22

    二维数组错误:下标值既不是数组,也不是指针,也不是矢量

  23. 23

    用C ++编写二维数组会产生“数组下标类型无效”错误

  24. 24

    C ++ 2D数组-数组下标的错误无效类型'int [int]'

  25. 25

    错误下标

  26. 26

    在数组中排序负数的错误(双精度)

  27. 27

    在数组中排序负数的错误(双精度)

  28. 28

    矩阵乘积导致错误:下标值既不是数组也不是指针也不是向量

  29. 29

    C错误:行梯形形式的“下标值既不是数组也不是指针”

热门标签

归档