reading the binary of files

hey guys, so i am in this project right now where i want to be able to take a picture file, lets say a jpeg in this instance, and lower the resolution.

i am pretty sure i know how to do this, logic-wise: take the string of binary numbers given, sample one in every few, and set all the others in that 'group' to that value. (The actual sampling style, like the first pixel, random or center can be figured out later)

thing is, i dont have much experience working with files in c++. is there a way that i can just take an image file, like a jpeg, and screw around with the binary/hexadecimal/whatever represents it?

eventually, i will be applying this to a webcam. well see how it goes.

thanks!

oh and btw, this is in ubuntu 8.10 if that is important. thanks again!
jpeg is a complex compressed image format... just dinking around with the raw binary would likely muck up the image terribly, or corrupt the file so that it can't be properly decompressed.

Your best bet is to use an image library to convert jpeg to a more managable uncompressed bitmap style format where you can modify individual pixels and whatnot. I don't do much work with jpeg so I don't have any good ones to recommend, unfortunately (other than wxWidgets, but if you're not already using wx in your project I wouldn't include it just for jpeg support)
hmm, okay, so a bitmap i take it is just a listing of all the different pixel values of a picture with no sort of advanced encoding? how do you change a jpeg file to a bitmap? and do most webcams spit out jpegs or bitmaps?

thanks, much appreciated!
there are a lot of sources to read about the format of bitmap file
but i read it from charles petzold from microsoft press, chapter 15. it has a very good explanation of the bitmap format. if you want i can post that chapter somewhere for you.
Well I didn't mean the bitmap file format necessarily -- I just meant any pixel array which can be easily modified/used. As for how convert -- you can use an image library. There might be one called "libjpeg" you can try googling. I haven't used any so I don't have any real recommendations as for which lib to use.
ok, great. are there multiple types of pixel arrays? which is the most standard? and how does one actually access the hexadecimal/binary of such a file? for instance, say i wanted to change the very first byte in a bitmap to all black, how would i do so?

thanks!
it depends on the lib

Typically individual pixels are in (A)RGB groups (32-bit XRGB or ARGB, or RGBX/RGBA -- or sometimes even 24-bit RGB). So to make a pixel black you'd just set its R,G, and B components to 0. How these are stored can vary depending on the lib. Some encase them in a struct:

1
2
3
4
struct PIXEL
{
  unsigned char a,r,g,b;
};


Some just use an array, or some use unsigned longs for the whole shebang.

Though if you're asking this kind of question you're starting at the wrong point... you're trying to swallow too much at once. Before worrying about grabbing images from a camera and displaying them (or whatever it is you're trying to do), start with one smaller aspect of what you need to do.

For example if you're intending to draw these images to the user, get a library that does that, and learn how to use it. It may have built-in jpeg support, even, making your life easier. If not, you can look for another library which reads jpegs and learn how to use that. Then "translate" between the output of the jpeg lib and the input of the drawing (if you understand how both work, that shouldn't be hard)

It's really hard to get into specifics as to what code you need without saying what library you're using. You really need to just look into some libraries and pick one (or more) which does what you need. The answer to all your questions lie in the library API -- and without a library you're not going to have any answers.

What specifically are you trying to do? I know it involves webcams somehow, but other than that I'm not really sure. Maybe it might be easier to recommend a library to suit your needs if I knew more about what you're doing -- although I have never done any work with webcams before so I don't know of ways to interface with them. Perhaps try googling "webcam C++ library" or something.

-------------------------------
As for reading binary files

If you're using ifstream, open the stream with ios::binary, and call mystream.read(). See reference page:

http://cplusplus.com/reference/iostream/istream/read.html

If you're using stdio, open the steram with the 'b' flag ("such as "rb" or "r+b"), and use fread(). See reference page:

http://cplusplus.com/reference/clibrary/cstdio/fread.html
Last edited on
Hey! I need to change either jpeg or bitmap image into its binary format.What shd i do?Plz suggest.........
jpeg and bitmap files are already in their binary format =P

If you mean you need to load and display a jpeg or bitmap file you need a library. There are several.

There's wxWidgets ( http://www.wxwidgets.org )-- a crossplatform GUI/widget lib which can display onscreen images on buttons or blitted to the drawing area in windows. It has built-in bmp and jpeg support (as well as other image types)

There's SDL ( http://www.libsdl.org ) -- a crossplatform game lib which can display onscreen images via a single main display window. It comes with built-in .bmp support but jpeg and other image support can be acquired via the "SDL_image" library (bundled seperately).

If you just want raw jpeg decoding without extra features of a display library, there's libjpeg ( http://www.ijg.org ) but beware this is probably significantly harder to use as it is much lower level than what the above libs provide. And all it will do is get you the pixel data, you'll still need another lib to display that pixel data to the user.

If you're on Windows, there might be some way to load jpegs and other images directly into an HBITMAP buried somewhere in WinAPI -- search MSDN's C++ section for "jpeg" and see what hits you get.

And countless others!

Most libs (including all of the above mentioned) provide a way to get and manipulate individual pixels after the file has been loaded, but they all do it differently. There is no one answer to this question. The answer lies in the API of whatever lib you decide to use.
here is what i want the full application to do:

i have an logitech orbit AF webcam. my objective is to first off, using ubuntu linux and 'libwebcam,' to make a framegrabbing application.
from there, my objective is to take the frames that i am receiving and lower their resolution.

i do not really have any experience working with linux. this is one of my first projects, so you can understand that i am having some difficulty even knowing where to get started. honestly, i do not even know what to do with libwebcam after i have downloaded and extracted it.

any help would be appreciated!
Topic archived. No new replies allowed.