0

Problem with converting colour image to greyscale

The following code is to accept an colour image as input and write a greyscale image but it didn't work as planned. #include<stdio.h> #include<stdint.h> #include<math.h> typedef uint8_t Byte; typedef struct { Byte rgbtBlue; Byte rgbtGreen; Byte rgbtRed; }rgbtriple; int greyscale(int width,int height); int main() { greyscale(1920,1280); } int greyscale(int width,int height) { rgbtriple sample[height][width]; FILE *fp=fopen("/content/sample_1920×1280.bmp","rb"); FILE *fp1=fopen("/content/newimage1.bmp","wb"); if(fp!=NULL && fp1!=NULL) { int i=0,j=0; Byte average=0;; for(i=0;i<height;i++) { for(j=0;j<width;j++) { fread(&sample[i][j], sizeof(sample[i][j]),1,fp); average=(sample[i][j].rgbtRed+sample[i][j].rgbtGreen+sample[i][j].rgbtBlue)/3; sample[i][j].rgbtRed=sample[i][j].rgbtGreen=sample[i][j].rgbtBlue=average; fwrite(&sample[i][j], sizeof(sample[i][j]),1,fp1); } }} else{printf("Can'topenfile");}}

24th Mar 2023, 1:55 PM
Vennimalai raja
1 Answer
+ 1
If the file is a standard bmp file then it has a header section that comes before the pixel data. See https://en.wikipedia.org/wiki/BMP_file_format.
24th Mar 2023, 2:28 PM
Brian
Brian - avatar