This is my struct for a pixel
typedef struct pixel{
int couleur[3];//rgb
}pixel;
so the program takes a .pbm file as input I created a test.pbm file to test my program. This is the content of the test.pbm file.
P1
10 4
1111111111
0000000000
1010101010
0101010101
10 being the width and 4 being the height. then 1's being black and 0's being white
my program reads the width and height. then creates a 2d array of pixels. Then using read saves the values from the pbm file into the 2d array of pixels.
pixel** readImage(char* filename,int *width,int *height){
//Reads the magic code
char bufMagic[3];
int fd = open(filename,O_RDONLY);
read(fd,bufMagic,3);
bufMagic[2]='\0';
//if magic is not P1 error
if(strncmp(bufMagic,"P1",2)!=0){
printf("ERREUR:Magic incorrect");
exit(1);
}
printf("BUFMAGIC:%s\n",bufMagic);
char bufResolution[10];
int index = 1;
char resTemp;
//reads full string of resolution
read(fd,&resTemp,sizeof(char));
bufResolution[0]=resTemp;
while(resTemp!='\n'){
read(fd,&resTemp,sizeof(char));
bufResolution[index]=resTemp;
index++;
}
bufResolution[index]='\0';
printf("BUFRES:%s",bufResolution);
//splits the resolution into x and y
char resx[5];
char resy[5];
int i =0;
while(bufResolution[i]!=' '){
resx[i]=bufResolution[i];
i++;
}
i++;
int j =i;
while(bufResolution[i]!='\n'){
resy[i-j]=bufResolution[i];
i++;
}
int y= atoi(resy);
int x = atoi(resx);
//saves the pixels in a table
char temp;
pixel** tab = malloc(sizeof(*tab)*y);
for(int i = 0;i<y;i++){
tab[i]=malloc(sizeof(tab)*x);
}
printf("x:%d y:%d\n",x,y);
for(int i = 0;i<y;i++){
for(int j = 0;j<x;j++){
read(fd,&temp,sizeof(char));
// //so that the read skips a line without actually incrementing j
if(temp=='\n'){
j--;
}
else//1 is black
if(temp=='1'){
tab[i][j].couleur[0]=0;
tab[i][j].couleur[1]=0;
tab[i][j].couleur[2]=0;
// printf("%d ",tab[i][j].couleur[0]/255);
}
if(temp=='0'){
tab[i][j].couleur[0]=255;
tab[i][j].couleur[1]=255;
tab[i][j].couleur[2]=255;
// printf("%d ",tab[i][j].couleur[0]/255);
}
}
// printf("\n");
}
printf("DONE\n");
// //just to test the read
//ISSUE IS HERE
for (int i = 0; i < y; i++)
{
for (int j = 0; j < x; j++)
{
printf("%d", tab[i][j].couleur[1] / 255);
}
printf("\n");
}
//sending the dimensions to main
*width=x;
*height=y;
return tab;
}
My issue is in saving the pixels in a table. the last nested loop is to print the contents of my 2d array of pixels. The expected output would be the content of the test.pbm file but inverted(since my print is printing the pixel color value/255). However the output I am getting is this
0000000011
1111111101
0101010110
1010101010
when I use the printf's in the intial nested loop(the one with the read) this is the output I get.
0000000000
1111111111
0101010101
1010101010
which is exactly what I want the content's of the 2d array of pixels to be. The fact that the output is correct when using the prints in the read function basically proves that there is no mistake in the reading right? So why is it that my 2d array of pixels not correctly retrieving the information of the test.bpm file? Any help would be greatly appreciated.