c - Find gray pixel in BMP image using C -


[edit] can allocate pixel array 2-d array of bmp picture.

but don't know how count red green or blue pixel of picture because need 3 bit store color rgb.

i trying count how many bit of gray pixel in picture using c. have opened bmp file using structure , can read details of bmp file, height, width...

[edited code too]

#include <stdio.h> #include <stdlib.h> #pragma pack(push)  // push current alignment stack #pragma pack(1)     // set alignment 1 byte boundary  typedef struct {     unsigned short int type; /* magic identifier */     unsigned int size; /* file size in bytes */     unsigned short int reserved1;     unsigned short int reserved2;     unsigned int offset; /* offset data (in b)*/ }header; /* -- 14 bytes -- */  typedef struct {    unsigned int size; /* header size in bytes */     int width;     int height; /* width / height of image */     unsigned short int planes; /* number of colour planes */     unsigned short int bits; /* bits per pixel */     unsigned int compression; /* compression type */     unsigned int imagesize; /* image size in bytes */     int xresolution;     int yresolution;/* pixels per meter */     unsigned int colors; /* number of colors */     unsigned int importantcolors;/* important colors */ }infoheader; /* -- 40 bytes -- */     typedef struct     {         unsigned char red;         unsigned char green;         unsigned char blue;     }pixel; #pragma pack(pop)   // restore original alignment stack  int main() {     header data;     infoheader data2;     pixel pixel;      file *file;     file = fopen("lena512.bmp","rb");      fread(&data,sizeof(header),1,file);     //read ib+nfoheader data data2     fread(&data2,sizeof(infoheader),1,file);       printf("height: %d\n",data2.height);     printf("witdh: %d\n" ,data2.width);   //allocate space pixelarray     pixel **pixelarray;      int r=0,c=0,rows=data2.height,columns=data2.width;      pixelarray= malloc(rows*sizeof(pixel *));      for(r=0; r<rows; r++)     {         pixelarray[r]=malloc(columns*sizeof(pixel));     }      //fill pixel array pixel structs     int pixelnum=0;  for( r=0; r<rows; r++ ) { for( c=0; c<columns; c++ )     // read pixel data image {     fread(&pixelarray[r][c].red , 1, sizeof(pixel), file);     fread(&pixelarray[r][c].green , 1, sizeof(pixel), file);     fread(&pixelarray[r][c].blue , 1, sizeof(pixel), file);     if(pixelarray[r][c].red=pixelarray[r][c].green=pixelarray[r][c].blue)         pixelnum++;  } } printf("gray num: %d\n",pixelnum);  fclose(file);  //close files prior exiting } 

[edit] this part found on internet, why put .red after 2-d array?

fread(&pixelarray[r][c].red , 1, sizeof(pixel), file); fread(&pixelarray[r][c].green , 1, sizeof(pixel), file); fread(&pixelarray[r][c].blue , 1, sizeof(pixel), file); 

but don't know how access color pixel level of image anymore. can me this? think should make 1 more struct pixel i've found on forum there red, green , blue no gray value. sorry not knowing anything...


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -