1

I am trying to print separate text files for R, G and B in an image.

Here is my code:

void main () {
  int pix_x=300,pix_y=300;    // image dimns in pixels
  int image[pix_x][pix_y][3]; // first [] number here is total pixels of each color in
                              // my image, 3 is for //RGB values
  FILE *streamIn;
  FILE *num_img_r,*num_img_g,*num_img_b;//separate files for R,G and B components

  //opening 24bit image
  streamIn = fopen("starcolor.bmp", "r"); // a bigger star in black and a smaller
                                          // star in blue (refer figure attached)
  num_img_r= fopen("redpixels.txt","w");
  num_img_g= fopen("greenpixels.txt","w");
  num_img_b= fopen("bluepixels.txt","w");

  int byte;
  int i,j;
  for(i=0;i<54;i++) {
    byte = fgetc(streamIn);  // strip out BMP header-> for //24bit bmp image
  }

  // initiating with new "i" different from above

  for(i=0;i<pix_y;i++) {
    for(j=0;j<pix_x;j++) {
      image[i][j][2] = fgetc(streamIn);  // use BMP 24bit with no alpha channel
      image[i][j][1] = fgetc(streamIn);  // BMP uses BGR but we want RGB, grab //byte-by-byte
      image[i][j][0] = fgetc(streamIn);  // reverse-order array indexing fixes //RGB issue...
      // printing R, G and B components separately in a .txt format
      if ((j+1)==pix_x) {
        // incorporating nextline ('\n') to ensure alignment for edge pixels
        fprintf(num_img_r,"%d \n",image[i][j][0]); // print R component
        fprintf(num_img_g,"%d \n",image[i][j][1]); // print G component
        fprintf(num_img_b,"%d \n",image[i][j][2]); // print B component
      }
      else {
        // for inner pixels, nextline(\n) not required
        fprintf(num_img_r,"%d ",image[i][j][0]); // R component
        fprintf(num_img_g,"%d ",image[i][j][1]); // G component
        fprintf(num_img_b,"%d ",image[i][j][2]); // B component
      }
    }
  }
}

Actual image:with colored and black star:

Actual image:with colored and black star

Screenshot: star shape(actually black) in 0's and background as 255(Note: No traces of colored star in this!) obtained by opening text file in excel-zoomed out table

Screenshot: star shape(actually black) in 0's and background as 255(Note: No traces of colored star in this!) obtained by opening text file in excel-zoomed out table

How can I get color info also printed in the text files i.e., print the blue colored star too in the text file? Can anyone trace mistake I am doing in reading bmp image?

Reference: Getting the pixel value of BMP file

Community
  • 1
  • 1
  • why using `int` to store components in `image`? 24bit color is 8+8+8 (R, G, B) so why not using `char` or `uint8_t`? – hexasoft Nov 12 '15 at 10:27
  • You are reading the bitmap char by char and then trying to save it in a text file? You need to read the bitmap, interpret it and convert it. read this post: http://stackoverflow.com/questions/14279242/read-bitmap-file-into-structure or this: http://stackoverflow.com/questions/1531268/getting-rgb-values-for-each-pixel-from-a-24bpp-bitmap-in-c – terence hill Nov 12 '15 at 11:03
  • Don't forget that an image consists of scanlines, and a scanline of pixels - but scnalines are aligned on word boundaries, so the last bytes of a scanline may not be used (so you shouldn't process them). You must change your loops to process scanline-by-scanline and then of each scanline pixel-by pixel. – Paul Ogilvie Nov 12 '15 at 11:17
  • 1
    I'm not sure that you interpret the results correctly. Black is all channels zero and white is all channels 255. There's also a trace of the small star visible in the lower left corner. Which of the three result files are we looking at here? – M Oehm Nov 12 '15 at 11:21
  • Thank you @ M Oehm. I am sorry for failing to trace it.. – Sai Krishna Nov 12 '15 at 11:22
  • I hope, my problem is solved ! Thank u everyone for giving alternative ideas. – Sai Krishna Nov 12 '15 at 11:25

0 Answers0