![]() |
|
#21
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
CPP / C++ / C Code:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
however, another new problem, CPP / C++ / C Code:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
I hate to repeat myself, but Quote:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
I am going to try this one more time: Quote:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
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:
Output from the bitmap that you are using: Code:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
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
|
|||
|
|||
Re: How to get the pixels' value of a bmp image? (Pelles C)Quote:
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:
For other people wanting to try it. See footnote. Quote:
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:
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 GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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