在C中操作数组

时间:2014-03-18 15:34:21

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

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

1 个答案:

答案 0 :(得分:1)

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

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