Click here to Skip to main content
15,867,330 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
JokeRe: C++ code Pin
CPallini4-Jun-09 1:32
mveCPallini4-Jun-09 1:32 
AnswerRe: C++ code Pin
Maximilien4-Jun-09 1:22
Maximilien4-Jun-09 1:22 
GeneralRe: C++ code Pin
shyampasari4-Jun-09 1:25
shyampasari4-Jun-09 1:25 
AnswerRe: C++ code Pin
Cedric Moonen4-Jun-09 1:32
Cedric Moonen4-Jun-09 1:32 
GeneralRe: C++ code Pin
shyampasari4-Jun-09 1:36
shyampasari4-Jun-09 1:36 
GeneralRe: C++ code Pin
Cedric Moonen4-Jun-09 1:54
Cedric Moonen4-Jun-09 1:54 
AnswerRe: C++ code Pin
norish4-Jun-09 4:28
norish4-Jun-09 4:28 
QuestionBMP Reader C Code Pin
Mitesh Karwa4-Jun-09 0:10
Mitesh Karwa4-Jun-09 0:10 
I was developing a simple program to read an 24-bit BMP image, convert it into 8-bit intensity map and then display it as a grayscale image. Please review this code and tell me where I am getting an error...

#include<stdio.h>
#include<conio.h>
#pragma pack(2)

typedef struct
{
        unsigned short int type;
        unsigned int size;
        unsigned short int reserved1,reserved2;
        unsigned int offset;
}HEADER;

typedef struct
{
        unsigned int size;
        int width,height;
        unsigned short int bits;
        unsigned int compression;
        unsigned int imagesize;
        int xresolution,yresolution;
        unsigned int ncolors;
        unsigned int impcolors;
}INFOHEADER;

typedef struct
{
        unsigned int R,G,B;
}PIXEL;


void main()
{
      HEADER headfirst;
      INFOHEADER headsecond;
      PIXEL pix;
      FILE *fin,*fout,*f1;
      char *read;
      int ch=0,i,j,mat[768],R[256],G[256],B[256],Gray[256];
      float Y[256];
      
      //Read Image Header
      fin=fopen("C:/Dev-Cpp/1.bmp","rb+");
      if(fin==NULL)
      {
                   printf("Error");
                   exit(0);
      }
      fread(&headfirst,sizeof(headfirst),1,fin);
      if(headfirst.type!=19778)
      {
                               printf("Not BMP");
                               getch();
                               return(0);
                               }
      printf("Header %x ",headfirst.type);
      printf("%u ",headfirst.size);
      printf("%u %u ",headfirst.reserved1,headfirst.reserved2);
      printf("%u\n",headfirst.offset); 
      fin=fopen("C:/Dev-Cpp/1.bmp","rb+");         
      fread(&headsecond,sizeof(headsecond),1,fin);

      //Read Image Information Directory
      printf("IFD %u",headsecond.size);
      printf("%d %d %u %u %u %d %d %u %u\n",headsecond.width,headsecond.height,headsecond.height,headsecond.bits,headsecond.compression,headsecond.imagesize,headsecond.xresolution,headsecond.yresolution,headsecond.ncolors,headsecond.impcolors);
      
      //Read Image Pixel Values
      fin=fopen("C:/Dev-Cpp/1.bmp","rb+");
      if(fin==0)
      {return(0);
      }
      for(i=0;i<headfirst.offset;i++)
      {
                                     ch=getc(fin);//Remove Offset
      }
      i=0;
      while ((ch=fgetc(fin))!=EOF)
      {
           mat[i]=(int)ch;
           i++;
      }
     //mat[] gives me the whole array of pixels in the image
     //Now I need to convert that into three arrays... 
      fclose(fin);
      printf("Blue Plane\n");
      i=0,j=0;
      while(i<768)
      {
                  B[j]=mat[i];
                  printf("%3d ",mat[i]);
                  i=i+3;
                  j++;
      }
      printf("\n\n");
      
      printf("Green Plane\n");
      i=1,j=0;
      while(i<768)
      {
                  G[j]=mat[i];
                  printf("%3d ",mat[i]);
                  i=i+3;
                  j++;
      }
      printf("\n\n");
      printf("Red Plane\n");
      i=2,j=0;
      while(i<768)
      {
                  R[j]=mat[i];
                  printf("%3d ",mat[i]);
                  i=i+3;
                  j++;
      }
      printf("\n\n Gray Scale Image\n");
      
      //Now I have the three arrays of RGB
      //Now I need to convert to grayscale
      for(i=0;i<256;i++)
      {
      Y[i]=0.2989 * R[i] + 0.5870 * G[i] + 0.1140 * B[i] ;
      G[i]=(int)Y[i];
      printf("%3d ",G[i]); 
      }
      
      //Write back Grayscale Image as bmp file
      fout=fopen("C:/Dev-Cpp/1.bmp","wb+");
      fwrite(&headfirst,sizeof(headfirst),1,fout);
      fout=fopen("C:/Dev-Cpp/1.bmp","wb+");
      fwrite(&headsecond,sizeof(headsecond),1,fout);
      fout=fopen("C:/Dev-Cpp/1.bmp","wb+");
      for(i=0;i<255;i++)
      {
                        read[i]=(char)G[i];
      }
      read[i]='\0';
      for(i=0;i<255;i++)
      {
                        fwrite (read,sizeof(G),1,fout);
      }
      
      getch();
}

NewsRe: BMP Reader C Code Pin
CPallini4-Jun-09 0:30
mveCPallini4-Jun-09 0:30 
GeneralRe: BMP Reader C Code Pin
Mitesh Karwa6-Jun-09 23:40
Mitesh Karwa6-Jun-09 23:40 
GeneralRe: BMP Reader C Code Pin
CPallini7-Jun-09 0:13
mveCPallini7-Jun-09 0:13 
GeneralRe: BMP Reader C Code Pin
Mitesh Karwa7-Jun-09 0:26
Mitesh Karwa7-Jun-09 0:26 
GeneralRe: BMP Reader C Code Pin
CPallini7-Jun-09 3:02
mveCPallini7-Jun-09 3:02 
GeneralRe: BMP Reader C Code Pin
Mitesh Karwa7-Jun-09 23:27
Mitesh Karwa7-Jun-09 23:27 
GeneralRe: BMP Reader C Code Pin
Mitesh Karwa7-Jun-09 0:16
Mitesh Karwa7-Jun-09 0:16 
AnswerRe: BMP Reader C Code Pin
Hamid_RT4-Jun-09 1:07
Hamid_RT4-Jun-09 1:07 
QuestionRe: BMP Reader C Code Pin
Mitesh Karwa6-Jun-09 23:32
Mitesh Karwa6-Jun-09 23:32 
AnswerRe: BMP Reader C Code Pin
KarstenK4-Jun-09 4:09
mveKarstenK4-Jun-09 4:09 
AnswerBitmap is the Native Windows Image Format Pin
Baltoro4-Jun-09 5:59
Baltoro4-Jun-09 5:59 
GeneralRe: Bitmap is the Native Windows Image Format Pin
Mitesh Karwa7-Jun-09 0:47
Mitesh Karwa7-Jun-09 0:47 
AnswerRe: BMP Reader C Code Pin
XcentY1-Jun-17 3:06
XcentY1-Jun-17 3:06 
QuestionIs there iges model/file show UIControl or code? Pin
BegtostudyBoy3-Jun-09 22:53
BegtostudyBoy3-Jun-09 22:53 
QuestionRe: Is there iges model/file show UIControl or code? Pin
CPallini4-Jun-09 0:36
mveCPallini4-Jun-09 0:36 
AnswerRe: Is there iges model/file show UIControl or code? Pin
BegtostudyBoy4-Jun-09 1:21
BegtostudyBoy4-Jun-09 1:21 
QuestionREQUEST FOR HELP Pin
Ramin O3-Jun-09 21:09
Ramin O3-Jun-09 21:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.