10 Replies - 354 Views - Last Post: 17 August 2012 - 02:58 AM Rate Topic: -----

#1 adey90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 12

reading image

Posted 16 August 2012 - 10:39 AM

i've written the following code. i want to read a bmp image and write the same..
now the bits per pixel(bpp) and plane are showing wrong in the code..
if i write the whole thing to another file will it be a bmp image or is there anything wrong with the code?
#include<stdio.h>
void main()
{
    char ch[1000],offset[60];long int height;int width;long int size;int r1,r2;long int sz,a,a1,a2,a3,comp;int plane;int bpp1;
    unsigned char blue,green,red;
    FILE *fp=fopen("uu.bmp","r"),*fp1=fopen("aa","w");

    fseek(fp,0,0);
    fread(ch,2,1,fp);
    fprintf(fp1,"%s\n",ch);

       fseek(fp,2,0);
    fread(&size,4,1,fp);
    fprintf(fp1,"%ld\n",size);

    fseek(fp,6,0);
    fread(&r1,2,1,fp);
    fprintf(fp1,"%d\n",r1);

    fseek(fp,8,0);
    fread(&r2,2,1,fp);
    fprintf(fp1,"%d\n",r2);

        fseek(fp,10,0);
    fread(offset,4,1,fp);
    fprintf(fp1,"%s\n",offset);

        fseek(fp,14,0);
    fread(&sz,4,1,fp);
    fprintf(fp1,"%ld\n",sz);

    fseek(fp,18,0);
    fread(&height,4,1,fp);
    fprintf(fp1,"%ld\n",height);

    fseek(fp,22,0);
    fread(&width,4,1,fp);
    fprintf(fp1,"%d\n",width);

    fseek(fp,26,0);
    fread(&plane,2,1,fp);
    fprintf(fp1,"%d\n",plane);

    fseek(fp,28,0);
    fread(&bpp1,2,1,fp);
    fprintf(fp1,"%d\n",bpp1);

        fseek(fp,30,0);
    fread(&comp,4,1,fp);
    fprintf(fp1,"%ld\n",comp);

        fseek(fp,34,0);
    fread(&a,4,1,fp);
    fprintf(fp1,"%ld\n",a);

    fseek(fp,38,0);
    fread(&a1,4,1,fp);
    fprintf(fp1,"%ld\n",a1);

    fseek(fp,42,0);
    fread(&a2,4,1,fp);
    fprintf(fp1,"%ld\n",a2);

    fseek(fp,46,0);
    fread(&a3,4,1,fp);
    fprintf(fp1,"%ld\n",a3);

    printf("height=%d\nwidth=%d\nsize=%d\nreserved=%ld\tresered=%ld\nplane=%ld\nbpp=%d\n",height,width,size,r1,r2,plane,bpp1);
    printf("SOI=%s",ch);
    fseek(fp,54,0);
    while(!feof(fp))
    {
        {
        //fread(&p,sizeof(p),1,image);
        fread(&blue,sizeof(unsigned char),1,fp);
        fread(&green,sizeof(unsigned char),1,fp);
        fread(&red,sizeof(unsigned char),1,fp);
        //pic[i]=p;
        //printf(" %u %u %u \n",blue,green,red);
        fprintf(fp1,"%u %u %u\n",blue,green,red);


    }

    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: reading image

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1929
  • View blog
  • Posts: 5,743
  • Joined: 05-May 12

Re: reading image

Posted 16 August 2012 - 11:22 AM

You are reading the .BMP files binary contents, but you are outputting a file that is essentially a text file.

Can you tell us why you think that the values for the number of color planes and bits per pixel is wrong? What values were you expecting and what are the actual values you are seeing? Are you running on a big-endian CPU?
Was This Post Helpful? 0
  • +
  • -

#3 adey90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 12

Re: reading image

Posted 16 August 2012 - 11:26 AM

now bits per pixel is showing the right value..
but i am unable to open the file as a bmp file..

how to do that.. should the output file be aa.bmp?? but that's not helping either :(
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1929
  • View blog
  • Posts: 5,743
  • Joined: 05-May 12

Re: reading image

Posted 16 August 2012 - 11:49 AM

As I said, the output file aa now contains text data rather than binary data. It's like watching a movie and then writing a movie report. You cannot put the movie report into the projector and expect to see the movie again, do you? It's because the film media is one format, while the text is another format.
Was This Post Helpful? 0
  • +
  • -

#5 adey90  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 08-August 12

Re: reading image

Posted 16 August 2012 - 12:34 PM

so is there anything i can do to see the whole thing as an image !!
Was This Post Helpful? 0
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1929
  • View blog
  • Posts: 5,743
  • Joined: 05-May 12

Re: reading image

Posted 16 August 2012 - 12:48 PM

Yes, copy the source file to the destination byte for byte unchanged.
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 869
  • View blog
  • Posts: 3,996
  • Joined: 09-June 09

Re: reading image

Posted 16 August 2012 - 01:04 PM

Quote

but i am unable to open the file as a bmp file..

so is there anything i can do to see the whole thing as an image


An image is simply binary data, in fact EVERYTHING is binary data, the file type just determines how the binary data is interpretted. If you simply want to copy an image, then read all the data into a buffer, then write that buffer to a new file.

Binary File IO

This post has been edited by jjl: 16 August 2012 - 01:05 PM

Was This Post Helpful? 0
  • +
  • -

#8 sungchoiok  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 38
  • View blog
  • Posts: 131
  • Joined: 18-July 10

Re: reading image

Posted 16 August 2012 - 02:20 PM

Wait... let's get something straight here.

What are you trying to do?

At first, you told us that you want to read a bitmap file and "write the same" (which doesn't make much sense to me), and then, you tell us, "so is there anything i can do to see the whole thing as an image !!".

Give us the whole picture here.

This post has been edited by sungchoiok: 16 August 2012 - 02:24 PM

Was This Post Helpful? 1
  • +
  • -

#9 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 494
  • View blog
  • Posts: 1,543
  • Joined: 20-March 10

Re: reading image

Posted 17 August 2012 - 12:42 AM

Your basic understanding of the file format is what is letting you down.

First of all you should open your files for binary

FILE *fp=fopen("uu.bmp","rb"),*fp1=fopen("aa.bmp","wb");

secondly a .bmp file consists of

a BITMAPFILEHEADER,
a BITMAPINFOHEADER
and the color table and pixel data are then written together as a block.

It was a decent attempt though so I have posted a fixed program in your style.

please study it carefully then study the file format wiki

BMP format

#include<stdio.h>
#include <windows.h>
int main()
{
    WORD ch;int offset;int height;int width;int size;int r1,r2;int sz,a,a1,a2,a3,a4,comp;int plane, bpp1;

    FILE *fp=fopen("uu.bmp","rb"),*fp1=fopen("aa.bmp","wb");

    fseek(fp,0,0);
    fread(&ch,2,1,fp);
    fread(&size,4,1,fp);
    fread(&r1,2,1,fp);
    fread(&r2,2,1,fp);
    fread(&offset,4,1,fp);

    BITMAPFILEHEADER hdr;
    hdr.bfType = ch;
    hdr.bfSize = size;
    hdr.bfReserved1 = r1;
    hdr.bfReserved2 = r2;
    hdr.bfOffBits = offset;

    fwrite((char*)&hdr,sizeof(BITMAPFILEHEADER),1,fp1);

    BITMAPINFOHEADER bih;
    fread(&sz,4,1,fp);
    bih.biSize = sz;




    fread(&width,4,1,fp);
    bih.biWidth = width;
    fread(&height,4,1,fp);
    bih.biHeight = height;



    fread(&plane,2,1,fp);
    bih.biPlanes = 1;


    fread(&bpp1,2,1,fp);
    bih.biBitCount = 24;


    fread(&comp,4,1,fp);
    bih.biCompression = BI_RGB;


    fread(&a,4,1,fp);
    bih.biSizeImage = a;


    fread(&a1,4,1,fp);
    bih.biXPelsPerMeter = 0;


    fread(&a2,4,1,fp);
    bih.biYPelsPerMeter = 0;


    fread(&a3,4,1,fp);
    bih.biClrUsed = 0;

    fread(&a4,4,1,fp);
    bih.biClrImportant = 0;
    // now that we have a bitmapinfoheader we can write it as follows
    fwrite((char*)&bih, (sizeof(BITMAPINFOHEADER) + bih.biClrUsed * sizeof (RGBQUAD)),1,fp1);
    //allocate memory
    LPBYTE lpBits = (LPBYTE) GlobalAlloc(GMEM_FIXED, (bih.biSizeImage));
    int cb = (bih.biSizeImage);
    // copy in the array of color indices
    fread(lpBits,cb,1,fp);
    fwrite((LPSTR)lpBits,cb,1,fp1);

    printf("height=%d\nwidth=%d\nsize=%d\nreserved=%d\treserved=%d\nplane=%d\nbpp=%d\n",height,width,size,r1,r2,plane,bpp1);
    printf("SOI= %d",ch);

    fclose(fp);
    fclose(fp1);
    // Free memory.
	GlobalFree((HGLOBAL)lpBits);
    return 0;
}



I have removed all the fseek's as they are largely unnecessary.

Snoopy.

This post has been edited by snoopy11: 17 August 2012 - 12:55 AM

Was This Post Helpful? 3
  • +
  • -

#10 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1929
  • View blog
  • Posts: 5,743
  • Joined: 05-May 12

Re: reading image

Posted 17 August 2012 - 02:21 AM

I'll ask a silly question... why all the pussyfooting around reading in the BITMAPFILEHEADER and and BITMAPINFOHEADER one piece at a time, but splatting it out to the output as a unit? Why not also read it in as a unit?

Or for that matter why not just copy the file across?
char buffer[4096];
int read;
int error;
:
do
{
    read = fread(buffer, sizeof(buffer[0]), sizeof(buffer), fp);
    error = ferror(fp);
    if (!error)
    {
        fwrite(buffer, sizeof(buffer[0]), read, fp1);
        error = ferror(fp1);
    }
} while (!feof(fp) && !error);
if (error)
{
    // deal with error.
}



A couple notes:
* You don't need the GlobalAlloc() and GlobalFree(). A plain old malloc() and free() will do.
* No need to cast to LPSTR in the fwrite() call because the first parameter is a void *.
Was This Post Helpful? 0
  • +
  • -

#11 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 494
  • View blog
  • Posts: 1,543
  • Joined: 20-March 10

Re: reading image

Posted 17 August 2012 - 02:58 AM

No its not a silly question,

Primarily she has to do it that way.

Her professor also wants her to do it
Without using windows.h.

But in my example it gives you an inkling
Of what to do.

My example was designed to get across
How the file format is actually made up.

Not to be a complete solution

She has to load the pixel data into
a 3 dimensional array dynamically allocated.

But what she has posted suggests
that she doesn't know how to do that yet.

Just block copying and block. Blitting

In this case helps even less than my
Example.

As for globalfree well i allocated it
With globalalloc so globalfree fits well
Enough there.

As for the rest of the stuff yes your right
I have some bad habits I need to
get rid of.

In my defence i was in a rush to get
To work when i wrote it.

Snoopy.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1