gpt4 book ai didi

c - 在 C 中操作数组

转载 作者:行者123 更新时间:2023-11-30 21:26:02 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com