![]() |
|
#1
|
|||
|
|||
How to get the pixels' value of a bmp image? (Pelles C)Could anyone help me that how I can get the value of each pixels in a bmp image?
I would like to do image encryption using Chaos in Pelles C, could anyone help me about this topic?? |
|||
|
#2
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Quote:
From my point of view, you need to decode the BMP image first to get the raw image data. What do you mean by image encryption ? |
|
#3
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Quote:
The answer is: That depends. (Of course, that is the answer to most questions, isn't it?) In this case, it depends on the kind of bitmap file. Some examples for bitmap files without compression: Bitmaps with 8-bit color information (256 colors) have a color table after the headers and before the pixel data. The color table usually is 1024 bytes (four bytes for each color: Green, Blue, Red intensity and a zero byte), but it could be smaller, depending on how many colors are actually used. Each byte of the pixel data is used to index into the color table to be able to access the Red, Green, and Blue byte values for that pixel. For bitmaps with fewer colors, each pixel data byte is packed with 4-bit or 2-bit or 1-bit color information for multiple pixels. The color information for each pixel used to index into the color table (color tables are smaller for these cases). Bitmaps with 24-bit color information ("Millions of colors") have no color table. The pixel data starts immediately after the headers. Each pixel has its color data in three successive bytes (The byte values are for Green, Blue, and Red intensity, in that order.) There are several compression methods that are sometimes used to make the bitmap files smaller (and to make your task a little more elaborate). Bottom line: If you know what kind of bitmap file you have (How many colors? Is compression used?) you may be able to write a fairly straightforward program to extract the color information. If you are trying to write a program to handle any kind of bitmap file then it's a little more "interesting". Here are a couple of links. There are about a million others---maybe more. Find one that you like. wotsit Dr. Dobb's and, of course Don't forget Microsoft; they invented it! Regards, Dave Footnote: I have seen applications (including some apparently popular source code on the web) that create non-standard bitmaps that may or may not render correctly with other applications. (Things like omitting the color table from 8-bit grayscale files.) An understanding, not only of the "standard" bitmap format, but also an understanding of where the bitmap file came from---and what you intend to do with the results---is important. |
|
#4
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Quote:
m.. I want to encrypt an image by using Chaos mapping, such as Baker and Standard. |
|
#5
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Quote:
Really thank you for your warm reply!!! I just start my program and I want to do 8-bit color bmp first(without compression). However, I am a beginner of Pelles C... I do not know how to write those code actually.. do you have any examples/source code about this ? Thank you again. |
|
#6
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Quote:
What compiler are you using? The reason that I ask is that some of the implementation-dependent parameters (sizes of shorts, ints and longs, for example) require some definitions to be a little different at the beginning of the project. Instead of giving you code with a bunch of conditional definitions, I can just tailor it to a particular setup, in hopes of making it a little easier to get started. Did you look at the Dr. Dobb's link? There is some source code there. (Not exactly the same as I might have done some of the things, but I think it can be made to work.) Note that I have written a number of simple bitmap applications in C (not C++). They serve my purpose(s) OK, but are not necessarily the best examples for teaching (and, in particular, C++ program organization around a BitMap class would organize things a somehwhat differently). But, yes, I can give you some example code for reading in and interpreting the headers. Regards, Dave |
|
#7
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Quote:
Thanks for your reply again!!! I am using this one I have read those link that you gave to me, however, I understand some of them only... do not know how to start my program.. www.apl.jhu.edu / Notes / Corrigan / lena.BMP this bmp is the one that I want to read its pixel first.... someone told me that doing Chaos mapping, first I need to know each value of its pixels first, and then change their position and so on.... it is very difficult to me.. as i am a beginner of C... I am very happy of your reply and you help me lots!! Thank you! |
|
#8
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Quote:
I don't have that one, but I think that it is recent enough to make it easy to get you started with the kind of notation that I use when it is important to know the sizes of various integer data types. (This is supported by the C99 Standard C language specification. Not all compilers---Microsoft for example---have the <stdint.h> header.) I will show a header that you can use with the couple of examples that I will post (next time) to read the information from bmp file headers. The way that I do things: I read the bmp headers directly into structs that let me look at the bitmap parameters. I have simplified the files a little, with a some loss of generality (won't work on big-endian architectures like PowerPC Macintoshes and certain Sun workstations.) Since you are using Windows, they should be OK. If you were planning to distribute source to other people who might be running on different platforms, things might be a little more complicated (not very much, but some). My header file uses a pragma to make sure that the header structs that I define will be the correct size. If the program doesn't work with your compiler, we (meaning you) will have to do a little detective work. Anyhow, paste the following into two files in your working directory. Run the program and tell me what the output is. The following will be used in all of our bitmap programs CPP / C++ / C Code:
Here is a test program that is only used at first, to make sure that everything after this will compile as required. CPP / C++ / C Code:
The output should be: Code:
Regards, Dave Last edited by davekw7x : 05-Oct-2007 at 09:50.
|
|
#9
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Well it looks like this thread has stalled... With all the questions about image manipulation lately I thought I'd try to get a bit more familiar with it... and a .bmp seemed like a good place to start I guess.
So using MS paint (don't laugh) I began placing a couple of dots around to see what I could. I began with a monochrome format which made a much smaller file than the default 24 bit format. I soon found that the data for the bottom row of the image was at the beginning of the file AFTER what appeared to be the file header area. I also got the feel for how one byte of data was used for eight pixels in that format. I was viewing the file using a hexdump feature my editor (textpad)will do. Then made my own little program to organize and view the data: CPP / C++ / C Code:
NOT to my suprise I found that the header areas of both were indeed laid out as you indicated in your structures! Code:
Code:
illustration like this might help others like me to understand... Anyhow the next step was to implement your structures which work very nicely: CPP / C++ / C Code:
Code:
How would YOU print the two characters of fheader.type which are in a uint16_t ? I kinda cheated there... So yes, the functions compile, what's next Dave? ++Howard; |
|
#10
|
|||
|
|||
Re: How to get the pixels' value of a bmp image?(Pelles C)Quote:
Suppose the width is "WIDTH" and the height is "HEIGHT" There are HEIGHT rows of pixel data. Each row consists of WIDTH triads of color intensity data. A triad consists of three bytes. The values of the bytes give color intensity for Blue, Green, and Red colors, in that order. See Footnote 1. So, each row contains 3 * WIDTH bytes of pixel data. If the total number of bytes on the row is not a multiple of four, extra padding bytes (value zero) are appended. There is no newline or other separator between rows. For other people who didn't look carefully at your commented output: Note, also, that the first row contains pixel data for the bottom-most line of the picture. Since your example has a width of 400 pixels, the total number of pixel data bytes on each row is 1200, and no padding bytes are necessary. Exercise for the student: suppose it had a width of 399 pixels, how many padding bytes would be required for each row? How about 398? 397? 396? Quote:
The first two bytes in a bmp file are ascii 'M' and 'B' in that order. Think of it as a "little-endian 'B','M'. Since I defined the header struct as uint16_t, and the program (as written) is restricted run on little-endian machines, the value of fheader.type will be 0x4d42, as you can verify by CPP / C++ / C Code:
CPP / C++ / C Code:
Quote:
Let's do something simple: Convert the 24-bit color bitmap to a 24-bit grayscale bitmap. (Seems kind of a waste, but it is pretty easy to get something that you can look at to see the results of your manipulation.) First of all, the headers will be exactly the same for the grayscale file as for the original, right. (Can't get much easier than that.) So, write them. Now, make two loops (for rows and columns) that go through and replace each three bytes with three bytes having the average value of those three. So our output file will have the green, blue, and red bytes with equal values for each triad. See Footnote 2. For this experiment, you don't have to store them in an array, just read 3, calculate average, write 3. Run the output file through your program that prints header values. Finally, open the file with paint (or whatever... I use GIMP on my Linux platform) Make sure that the output file looks like a grayscale version of the input. Next step: Dynamically allocate a 2-D array of uint_8 to hold the grayscale pixel values. Read all of the pixels. Then use the values from the array to write out the pixel triads. The next part could be to write an 8-bit (256-color) grayscale bitmap file instead of a 24-bit grayscale. (Size is about 1/3 of the other, and total information is the same.) The next part could be to process the grayscale values in some way. Maybe make it brighter or darker. Maybe to increase or decrease contrast. Maybe to perform the 2-D Sobel convolution to emphasize the edges in the picture. Maybe... Regards, Dave Footnote 1: I call the three color bytes a "triad." I just kind of made that up. Some people define a struct that holds the bytes in the same order as they appear in the file: CPP / C++ / C Code:
I didn't see any need for such a thing, but I put it here as a reminder that the order is blue, green, red. (We talk about "rgb" colors, but the order in the file is gbr.) Footnote 2: If you just use the average rgb values for grayscale values of a well color-balanced photograph you will (probably) perceive that it's kind of flat. Especially if it has human skin tones. Some programs use a weighted average of the rgb values. Typically they use relative weights of {0.11, 0.59, 0.3} to give a more natural-looking luminance to the grayscale Last edited by davekw7x : 09-Oct-2007 at 11:18.
|
Recent GIDBlog
Toyota - 2009 May Promotion by Nihal
| 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