c - 在 C 中操作数组

标签 c arrays

int image[128][256][3];
main()
{
int a=0;
int b=0;
int c=0;
int d=0;
int x=0;
int y=0;

//code for opening and reading each file and saving it as an array for image1

while(a== 0) //while loops used so all files can be read and written at the same time
{ 
    FILE *pfile = NULL;
    pfile = fopen("image01.red", "r");
    for(y = 0;y<HEIGHT;y++) //reads the file from the top row down until the array is full to specified height
    {
        for(x = 0;x<WIDTH;x++) //reads the file from left to right until the array is full to specified width
        {
            fscanf(pfile,"%d", &image[y][x][0]); //reads formatted input as a stream. Red file assigned to positon 0 in the 3-D array as it must be the first file in the array or colour distortion happens on image
        }
    }
    fclose(pfile);  //closes the file after reading the file
    a = 1; 
    break; //stops the loop (breaks the loop)
}
while(b== 0) //while loop done again for the blue array
{       
    FILE *pfile = NULL;
    pfile = fopen("image01.blue", "r");
    for(y = 0;y<HEIGHT;y++)
    {
        for(x = 0;x<WIDTH;x++)
        {
            fscanf(pfile,"%d", &image[y][x][2]); //blue file assigned to position 2 in the 3-D array as it must be the last file in the array or colour distortion happens on image
        }
    }
    fclose(pfile);
    b = 1;
    break;
}
while(c== 0) //while loop done again for the green array
{
    FILE *pfile = NULL;
    pfile = fopen("image01.green", "r");
    for(y = 0;y<HEIGHT;y++)
    {
        for(x = 0;x<WIDTH;x++)
        {
            fscanf(pfile,"%d", &image[y][x][1]);//green file assigned to positon 1 in the 3-D array as it must be the second file in the array or colour distortion happens on image
        }
    }
    fclose(pfile);
    c = 1;
    break;
}
while(d== 0) //finally while loop done for writing the three colour arrays to make a single image array 3 deep
{       //code for opening and writing the images to save them as a .ppm file to open in Irfanview
    FILE *pfile = NULL;
    pfile = fopen("ppm1.ppm", "w"); //Opens a file and calls it .ppm
    fprintf(pfile,"P3\n"); //First line of new file reads P3
    fprintf(pfile,"%d %d \n", WIDTH, HEIGHT); //Second lines defines the size of the 3-D array
    fprintf(pfile,"255\n"); //Defines maximum component colour value
    for(y = 0;y<HEIGHT;y++)
    {
        for(x = 0;x< WIDTH;x++)
        {
            fprintf(pfile,"%d %d %d ", image[y][x][0], image[y][x][1], image[y][x][2]); //image files arrays are written together to produce the overlapped image of 3 deep
        }
    }
    fclose(pfile);
    d = 1;
    break;
}
}  

上面的代码将 3 个 .ppm 文件读取为 .txt,并将整数存储在数组中,然后将数组写入新的 .ppm 文件以在图像查看器(此处为 Irfanview)中打开。 3 .ppm 是图像的 RGB 分量,代码旨在将它们组合在一起以生成最终图像。唯一的问题是红色分量被反转,因此当生成最终图像时,红色 channel 被颠倒并水平翻转。我将如何操作数组以便重新排列红色值来修复此错误?希望这是有道理的。谢谢

最佳答案

所以只需“上下颠倒并水平翻转”来阅读:

fscanf(pfile,"%d", &image[HEIGHT - y - 1][WIDTH - x - 1][0]);

或以这种方式存储:

fprintf(pfile,"%d %d %d ", 
  image[HEIGHT - y - 1][WIDTH - x - 1][0], 
  image[y][x][1], 
  image[y][x][2]
);

关于c - 在 C 中操作数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22483984/

相关文章:

c - scanf导致C程序崩溃

C - char* 为空?

c - 增加数组大小时出现段错误

java - 字符串数组到带有格式的字符串

javascript - 获取所有 css 类背景和颜色属性值

c - GCC 裸机内联汇编 SI 寄存器与指针不能很好地配合

复制内部包含空值的字符串

代码不允许我输入并立即结束

php - 使用array_map调用数组中对象的方法?

arrays - ILMath 函数在给定开始、结束和步长的情况下排列值?