CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: BMP Reader and Editor

  1. #1
    Join Date
    Jun 2009
    Posts
    1

    Post BMP Reader and Editor

    I was writing a simple program to read a 24-bit RGB image, convert it into an 8-bit grayscale image and write it back into a file. What's wrong with my code?

    #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();
    }

  2. #2
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: BMP Reader and Editor

    Post in code tags.
    Thanks for your help.

  3. #3
    Join Date
    Sep 2005
    Location
    London
    Posts
    208

    Re: BMP Reader and Editor

    Quote Originally Posted by miteshkarwa View Post
    What's wrong with my code?
    It would be much easier to help you if you specified what problems you are facing.

    Anyway, I found the following problems:

    1. Use
    Code:
    fin=fopen("C:\\Dev-Cpp\\1.bmp","rb+");
    Instead of

    Code:
    fin=fopen("C:\\Dev-Cpp\\1.bmp","rb+");
    2. You are using exit(0); without including cstdlib.

    Code:
    #include <cstdlib>
    3. main() should always return an int.


    Hope it helps
    Doron

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)