GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #21  
Old 04-Nov-2007, 21:57
Soundzz Soundzz is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 14
Soundzz is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by davekw7x
Am I missing something?

I can't find a single line of code that you have posted. Since we have absolutely no way of knowing what you have tried, how could you reasonably expect anyone to be able to help???

1. Show your code. If you can trim it down to a small subset that shows your specific problems, that would be a Good Thing.

2. Tell us what exactly happened when you tried to compile and/or execute the code. (Be specific.)

3. Tell us what exactly you don't understand. (Be specific.)

Regards,

Dave


CPP / C++ / C Code:
#include <stdio.h>
#include "bmp_headers.h"

int main(void)
{
  FILE *fp;
  char *cptr;
  unsigned char c;
  
  unsigned int i, j, linect, endline, pixwidth;
  char filename[] = "lena512.bmp\0\0\0\0\0\0\0\0\0\0\0\0";

  BmpFileHeader fheader;
  BmpInfoHeader iheader;

  printf("sizeof: fheader: %d   iheader: %d \n", sizeof(fheader), sizeof(iheader) );
  /* looks good  14 and 40*/

  if( (fp = fopen(filename, "r")) == NULL)
  {
    printf("%s failed to open", filename);
    return 1;
  };
  fread(&fheader, sizeof(fheader), 1, fp );
  fread(&iheader, sizeof(iheader), 1, fp );
  cptr = (void*)&fheader;
  cptr++;
  printf("           fheader.type= %c%c \n", fheader.type, *cptr );
  printf("           fheader.size= %u   \n", fheader.size );
  printf("      fheader.reserved1= %u   \n", (int)fheader.reserved1 );
  printf("      fheader.reserved2= %u   \n", (int)fheader.reserved2 );
  printf("         fheader.offset= %u   \n", fheader.offset );
  printf("           iheader.size= %u   \n", iheader.size );
  printf("          iheader.width= %u   \n", iheader.width );
  printf("         iheader.height= %u   \n", iheader.height );
  printf("         iheader.planes= %u   \n", (int)iheader.planes );
  printf("           iheader.bits= %u   \n", (int)iheader.bits );
  printf("    iheader.compression= %u   \n", iheader.compression );
  printf("      iheader.imagesize= %u   \n", iheader.imagesize );
  printf("    iheader.xresolution= %u   \n", iheader.xresolution );
  printf("    iheader.yresolution= %u   \n", iheader.yresolution );
  printf("        iheader.ncolors= %u   \n", iheader.ncolors );
  printf("iheader.importantcolors= %u   \n", iheader.importantcolors );

  fseek(fp, (long)fheader.offset, 0);   /* ensure fp is at right place */

  unsigned int databuff[512][512][3];
  int k=0;

  fread(databuff,1,iheader.width*iheader.height*3,fp);
  for(i=0;i<iheader.width;i++)
 {	for(j=0;j<iheader.height;j++)
		{	for(k=0;k<3;k++)
			printf("%c",databuff[i][j][k]);
			}
		}


  fclose(fp);

  return 0;

Here is my latest verison code(based on your bmp_headers.h and Howard_L's code), the result shows that the compiler tells me "Stack overflow"... I cannot solve this problem.

Regards,
Soundzz
  #22  
Old 04-Nov-2007, 22:29
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by Soundzz
...

Did you get any output from the program before the crash? Give us as much information as you can so that we can know what you are seeing.

Regards,

Dave
  #23  
Old 04-Nov-2007, 22:34
Soundzz Soundzz is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 14
Soundzz is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by davekw7x
Did you get any output from the program before the crash? Give us as much information as you can so that we can know what you are seeing.

Regards,

Dave
Ar.. really sorry, I solved the problems just right now, by changing the unsigned int into unsigned char.

however, another new problem,
CPP / C++ / C Code:
...
  unsigned char databuff[512][512];
 
  for(i=0;i<iheader.width-499;i++)
 {	for(j=0;j<iheader.height-500;j++)
		{	
			databuff[i][j]=getc(fp);
			printf("%02x ",databuff[i][j]);
			}
		}
...

I print some of the data to ensure whether I get the right data, but after about 100 data, the values all gone into ff, ff, ff, ff, ff....

Do you know why it means? did i get the wrong data?

Regards

P.S. Thank you very much for your help
  #24  
Old 04-Nov-2007, 22:42
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by Soundzz
however, another new problem,

I hate to repeat myself, but
Quote:
Originally Posted by davekw7x
Give us as much information as you can so that we can know what you are seeing.

You aren't using any of the information from the header. Where did you get the loop limits? What is the size of the bit map in the file? What is the size of your file? Are the header contents that you printed out consistent with whatever it is that you know about the file? How many pixels do you think you are reading? Have you read all about the bitmap file format?

Regards,

Dave
  #25  
Old 04-Nov-2007, 22:53
Soundzz Soundzz is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 14
Soundzz is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by davekw7x
I hate to repeat myself, but


You aren't using any of the information from the header. Where did you get the loop limits? What is the size of the bit map in the file? What is the size of your file? Are the header contents that you printed out consistent with whatever it is that you know about the file? How many pixels do you think you are reading? Have you read all about the bitmap file format?

Regards,

Dave

Really sorry...

I am using this bmp

width= 512 pixels, height=512pixels, imagesize = 786432, 24bits color

I use a software called UltraEdit to check the value of each pixels of the bitmap.

the values of each pixels (start after the 54 bytes, shown by my compiler):
37 51 9E 3E 58 A5.... 1C(the 127th values) and then after that (start from 128th values), all becomes ff, ff, ff...

Regards
  #26  
Old 04-Nov-2007, 23:09
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by Soundzz
...

I am going to try this one more time:
Quote:
Originally Posted by davekw7x
Give us as much information as you can so that we can know what you are seeing.

What I mean is this: What the heck did your program print out? What was in the header information. Paste it into your post. Just the header information.

Regards,

Dave
  #27  
Old 04-Nov-2007, 23:15
Soundzz Soundzz is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 14
Soundzz is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by davekw7x
I am going to try this one more time:


What I mean is this: What the heck did your program print out? What was in the header information. Paste it into your post. Just the header information.

Regards,

Dave

sizeof: fileheader:14 infoheader:40
fileheader.type=BM
fileheader.size=786486
fileheader.reserved1=0
fileheader.reserved2=0
fileheader.offset=54
infoheader.size=40
infoheader.width=512
infoheader.height=512
infoheader.planes=1
infoheader.bits=24
infoheader.compression=0
infoheader.imagesize=786432
infoheader.xresolution=3790
infoheader.yresolution=3800
infoheader.ncolors=0
infoheader.importantcolors=0

pixels values:
37 51 9E 3E 58 A5..... (start from 128th) ff ff ff ff ff ff ff ff ff ff ....

Regards
  #28  
Old 04-Nov-2007, 23:46
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by Soundzz
...
pixels values:
37 51 9E 3E 58 A5..... (start from 128th) ff ff ff ff ff ff ff ff ff ff ....

It's not clear to me what exactly your present version of software contains. I will say that the first few bytes from your bitmap look as I would expect.

You didn't include any tests on your reading to see whether it actually read the number of bytes that you wanted.

Using my header and Howard's example for starters, you should be pretty close. Instead of prolonging the agony, how about I just show a little more about what I mean?

I have reworked Howard's excellent example very slightly to make a couple of points. In general, unless you know that you are always going to be using bitmaps with these exact characteristics (height, width, etc.) the program should not be locked to a particular geometry. If it does depend on certain properties, then the program absolutely should test to make sure the file is as it should be.

I read a row at a time instead of reading the whole bitmap. It's not important for your particular example (512 pixels on each row), but is extremely important for a general program to read bitmaps since, if each row ends up not having the number of pixel bytes that is a multiple of 4 extra bytes are put there that are not part of the displayed pixels.

Anyhow:

CPP / C++ / C Code:
#include <stdio.h>
#include "bmp_headers.h"

int main()
{
    FILE *fp;

    int i, j, k;
    char *filename = "lena512.bmp";

    unsigned char databuff[512][512][3];

    BmpFileHeader fheader;
    BmpInfoHeader iheader;

    printf("sizeof: fheader: %d   iheader: %d\n\n", sizeof(fheader),
           sizeof(iheader));
    if ((sizeof(fheader) != 14) || (sizeof(iheader) != 40)) {
        printf("Header structs are not properly packed\n");
        return 0;
    }

    if ((fp = fopen(filename, "r")) == NULL) {
        printf("Coulsn't open file %s for reading.\n", filename);
        return 1;
    };
    if (fread(&fheader, sizeof(fheader), 1, fp) != 1) {
        printf("Couldn't read fheader.\n");
        return 0;
    }
    if (fread(&iheader, sizeof(iheader), 1, fp) != 1) {
        printf("Couldn't read iheader.\n");
        return 0;
    }
    printf("           fheader.type = %04x \n", fheader.type);
    printf("           fheader.size = %u   \n", fheader.size);
    printf("      fheader.reserved1 = %u   \n", fheader.reserved1);
    printf("      fheader.reserved2 = %u   \n", fheader.reserved2);
    printf("         fheader.offset = %u   \n", fheader.offset);
    printf("           iheader.size = %u   \n", iheader.size);
    printf("          iheader.width = %u   \n", iheader.width);
    printf("         iheader.height = %u   \n", iheader.height);
    printf("         iheader.planes = %u   \n", iheader.planes);
    printf("           iheader.bits = %u   \n", iheader.bits);
    printf("    iheader.compression = %u   \n", iheader.compression);
    printf("      iheader.imagesize = %u   \n", iheader.imagesize);
    printf("    iheader.xresolution = %u   \n", iheader.xresolution);
    printf("    iheader.yresolution = %u   \n", iheader.yresolution);
    printf("        iheader.ncolors = %u   \n", iheader.ncolors);
    printf("iheader.importantcolors = %u   \n", iheader.importantcolors);
    printf("\n");


    if ((iheader.height != 512) || (iheader.width != 512) || (iheader.bits != 24)) {
        printf("This only works for 512x512 24-color bitmaps\n");
        return 0;
    }
    if (fheader.offset != 54) {
        printf("This only works if the offset is equal to 54\n");
        return 0;
    }

    for (i = 0; i < iheader.height; i++) {
        for (j = 0; j < iheader.width; j++) {
            if (fread(&databuff[i][j][0], 3, 1, fp) != 1) {
                printf("Couldn't read colors for element [%d][%d]\n", i, j);
                return 0;
            }
        }
    }
    for (i = 0; i < 12; i++) { /* look at the first 12 rows */
        printf("Row %2d:\n", i);
        for (j = 0; j < 12; j++) { /* look at the first 12 on each row */
            printf("  ");
            for (k = 0; k < 3; k++) {
                printf("%02x ", databuff[i][j][k]);
            }
            printf("\n");
        }
        printf("\n\n");
    }

    fclose(fp);

    return 0;
}

Output from the bitmap that you are using:
Code:
sizeof: fheader: 14 iheader: 40 fheader.type= 4d42 fheader.size= 786486 fheader.reserved1= 0 fheader.reserved2= 0 fheader.offset= 54 iheader.size= 40 iheader.width= 512 iheader.height= 512 iheader.planes= 1 iheader.bits= 24 iheader.compression= 0 iheader.imagesize= 786432 iheader.xresolution= 3790 iheader.yresolution= 3800 iheader.ncolors= 0 iheader.importantcolors= 0 Row 0: 37 51 9e 3e 58 a5 3e 5a a7 4a 66 b3 52 70 bd 52 72 be 57 79 c5 60 82 ce 4a 6f bf 4e 73 c3 53 78 c8 4f 74 c4 Row 1: 3d 56 a6 3e 59 a9 40 5b ab 47 65 b4 4d 6c bb 4b 6a b9 4d 6e bd 57 78 c7 49 6e be 4e 73 c3 55 7a ca 4d 72 c2 Row 2: 3d 58 a8 3f 5a aa 41 5f ae 46 64 b3 4c 6b ba 4a 6b ba 4a 6b ba 52 76 c4 4a 6f bf 4e 73 c3 55 7a ca 4e 73 c3 Row 3: 38 53 a4 3e 59 aa 43 60 b1 45 63 b4 4c 6d bd 52 73 c3 4f 72 c2 57 7a ca 4c 71 c1 4e 73 c3 53 78 c8 50 75 c5 Row 4: 3b 56 a7 42 5f b0 4a 67 b8 45 63 b4 49 6a ba 4f 72 c2 4a 6d bd 4e 71 c1 4e 73 c3 4e 73 c3 4f 74 c4 54 79 c9 Row 5: 36 51 a2 3f 5c ad 49 66 b7 42 60 b1 46 67 b7 50 73 c3 4a 6d bd 49 6e be 50 75 c5 4e 73 c3 4c 71 c1 56 7b cb Row 6: 3a 58 a5 40 5e ab 45 65 b1 46 67 b6 4a 6b ba 4e 71 c1 4f 74 c4 4e 73 c3 51 76 ca 4b 70 c4 4f 74 c8 50 76 c8 Row 7: 38 56 a3 40 5e ab 46 66 b2 49 68 b7 4a 6b ba 4d 70 c0 4f 74 c4 4f 74 c4 55 79 cd 54 78 cc 4e 73 c5 52 77 c9 Row 8: 37 55 a2 40 5e ab 47 65 b2 49 68 b7 49 6a b9 4c 6f bf 51 74 c4 50 75 c5 56 7a ce 56 7b cd 4f 74 c6 51 76 c8 Row 9: 38 54 a1 42 5e ab 48 66 b3 49 68 b7 49 6a b9 4d 6e be 50 73 c3 53 76 c6 53 78 ca 4f 74 c6 53 78 ca 51 76 c6 Row 10: 39 55 a2 42 5e ab 47 65 b2 4a 68 b7 4b 6a b9 4d 6e be 50 73 c3 51 74 c4 51 73 c6 50 72 c5 57 7a ca 55 78 c8 Row 11: 3e 58 a5 42 5e ab 48 64 b1 49 67 b6 4b 6a b9 4e 6f bf 51 72 c2 51 72 c2 4f 72 c2 56 79 c9 54 77 c7 56 79 c9

Footnote: I didn't put in tests for some of the parameters: planes must always be 1; if compression is not equal to zero, you must apply whatever decompression algorithm is indicates (so, just make sure it is 0). The xresolution and yresolution are irrelevant for your purposes. Also ncolors and importantcolors can be ignored for your case.
  #29  
Old 05-Nov-2007, 01:31
Soundzz Soundzz is offline
Awaiting Email Confirmation
 
Join Date: Oct 2007
Posts: 14
Soundzz is on a distinguished road

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by davekw7x
skip

Thank you for your warm reply!!

However, I copy whole the program and run again, I found that Row 0 is shown only, Row1-12 show all 00 00 00(i.e. Before the last Row 0's elements (c4), all the program output is same as yours, including all the file/info header)

it shows "Couldn't read colors for element[0][42]" and after this element all couldn't be read.

Same program codes, and same photo... so I am wondering that .. may be is my compiler not good at all,

May I ask you that which compiler you are using for this program?^^

P.S if (fread(&databuff[i][j][0], 3, 1, fp) != 1) {....} <<< what does it mean of not equal to 1??
Regards

Soundz
  #30  
Old 05-Nov-2007, 09:04
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: How to get the pixels' value of a bmp image? (Pelles C)


Quote:
Originally Posted by Soundzz
Same program codes, and same photo... so I am wondering that .. may be is my compiler not good at all...May I ask you that which compiler you are using for this program?^^

I just pasted your code into a file and made a few minor changes before testing.

Since I tested it with GNU gcc on my Linux workstation I carelessly neglected to notice that you did not open the file in binary mode. ("Binary" mode is meaningless for the Linux I/O library functions, but means something very special in many Windows-based compilers as well as for certain other workstations.)

Here's the next step for you (don't throw your compiler away just yet):

Change the fopen statement to read the file in binary mode:
CPP / C++ / C Code:
    if ((fp = fopen(filename, "rb")) == NULL) {
Note the "rb" instead of the "r" that I previously showed.

For other people wanting to try it. See footnote.


Quote:
Originally Posted by Soundzz

P.S if (fread(&databuff[i][j][0], 3, 1, fp) != 1) {....} <<< what does it mean of not equal to 1??
It means that you should look up the functionality of things that you are using in your program.
For example from http://www-ccs.ucsd.edu/c/. Click on <stdio.h>. Then scroll down to fread() and read all about it.

To answer your question directly: since I asked it to read a block of three bytes each time, the fread() function must return a value of 1 unless it couldn't read the three bytes at that point in the file. Notice that for the headers, I required fread to return 1 each time, and if it couldn't read each entire header in turn , the program would bail out.

By reading one three-byte block at a time and testing to make sure all was OK, the program itself told you exactly where the problem lies. There is a byte with hex value 0x1a at the place in the file (byte number 127 after the end of the headers). For historical reasons, many C libraries for Windows compilers consider this to be an end-of-file marker unless the file was opened in binary mode.

Whether any of this makes sense or not, one important point (and one that I forgot in the test program, since it worked on the platform that I was using at the time) is: When dealing with a binary file, open it in binary mode.

The other important point is: always (yes, always) test to see that file I/O is successful. For example, always test the return value of fread as well as other functions. Make error messages verbose enough to allow you to tell exactly where the failure occurred.

Regards,

Dave

Footnote:
The header file that I used has #include <stdint.h>, which, apparently is available on the compiler used by Soundzz (as it is with current Borland compilers and all recent GNU compilers---Linux and Windows). It is not available with Microsoft compilers and older Borland compilers.

If anyone wants to compile with a compiler that does not have <stdint.h>, then, change that part of the header file to
CPP / C++ / C Code:
/*
 * You can change the "#if 0" to #if 1" if your compiler
 * has <stdint.h>
 */
#if 0
#include <stdint.h>
#else
typedef unsigned int uint32_t;
typedef          int int32_t;
typedef unsigned short uint16_t;
#endif

With the original header, the program compiled and executed successfully with Borland bcc32 version 5.82 and with GNU/cygwin gcc version 3.4.4 on my Windows XP platform and with various versions of GNU gcc 3.4.x and 4.x.x on Linux platforms. With the above change in the header file, it compiled and executed OK with Microsoft Visual C++ version 6 and Microsoft Visual Studio Express version 2008 and with older Borland Compilers.
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about locking surfaces to directly access pixels, SDL. george89 C++ Forum 0 18-Jun-2006 22:16
Pixels dan_ielle20 C++ Forum 0 23-Mar-2005 09:54
Checking source codes of image, audio and video files onauc C Programming Language 5 26-Feb-2005 22:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:54.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.