Send Iplimage data as jpeg - c++

I am trying to send Iplimage to another program as a array of bytes in c++/cli. The program needs to save the Iplimage as jpeg image. Let's say the Iplimage is img which I am obtaing by cvQyeryFrame from an avi video file, I am returning img->imagedata to the other program. Does img->imagedata contain the header for the jpeg image to be saved or does it only contain the data. If it only contains the data, how can I include the header? I can save the image using cvSaveImage and then read it but there should be a more direct way (maybe cvEncodeImage? )
thanks.

The data in the Iplimage is already decoded into own format of openCV (typically 8bit B,G,R) once it has been read from disk.
The newer versions of opencv can encode/decode an image in memory, see imencode.

If you need the entire JPEG data, you might want to consider reading the entire file from the disk with fread() instead of cvLoadImage().
If that's not going to work for you, consider Martin's answer.

Related

Compress Raw buffer data of video (C++)

I have raw buffer video data which I need to stream over the net as and when requested by the clients. This data is very huge. So I will need to compress and save it at my server location.
One way I understand of doing this convert the data into a video file ".avi" and save it and then stream it frame by frame as and when requested.
I have used the VideoWriter function of OpenCV for converting the buffer to mat and then to ".avi" file with MPEG4 encoding.
However I want to know if there is any way I can compress the raw buffer data and save it in a file. Can anyone suggest me if this is possible?
Thank You.

Read RGB triplets of JPEG files in C

to read bmp files we may use this http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx
as the header file and then get rgb triplets. How to get the rgb triplets of jpeg file, is there any such header file available. Please share the link if any.
The JPEG file format does not store the rgb triplets directly but it uses some sort of image compression. The file actually contains blocks of 64 (if I remember correctly) pixels which are attributed with a cosine pattern defining the actual colors.
You really should use a library (libjpeg, imagemagick, gd, ... e.g., depending on your use case) to read and decode the files and generate the rgb triplets in memory.
According to the answer to this question on MSDN, you could use the GDI+ component, which can load not only BMP, but JPG and other image formats too. From it, you will get a memory bitmap.
Here is an example on how to do that.
Check this library: libjpeg. This library implements JPEG image encoding, decoding,
and transcoding.

Custom avi/MP4 file writer

I am writing some video files under Windows from a camera.
I need the data unaltered - not MP4's 'uncompressed' ie. no YUV, no color interpolation - just the raw camera sensor bytestream.
At the moment I am writing this direct to disk and re-reading it later to recode into a usable video. But with no header I have to keep track of image size, frame rate, color balance etc separately.
I could add a custom header but even if the actual video data is unreadable by anything other than my app, using an AVI file would at least give me a relatively standard header to store all the camera parameters and also means that resolution, length etc would show up in explorer.
Is there an easy way of generating an AVI header/footer without sending all the data through directshow or vfw? The data is coming in at >250MB/s and I can't lose any frames so I don't have time to do much more than dump each frame to disk.
edit: Perhaps MP4 would be better I have a lot of metadata about the camera config that isn't in the AVI standard
Well, after figuring out what 'reasonable' AVI headers would be for your stream (e.g. if you use a custom codec fourcc, no application would probably be able to do useful things with it -- so why bother with AVI?)
you could just write a prebuild RIFF-AVI header at the beginning of your file. It's not too hard to figure out the values.
Each frame then has to be enclosed in its own RIFF chunk (4 Byte type: "00db" + 4 byte length + your data).
After the fact you have to fix the num_frames and some length fields in the header. And for files >2GB don't forget the OpenDML extension for the header.
Martin, since you are proficient in OpenCV, couldn't you just use cvCreateVideoWriter() for creating an uncompressed .avi?
CvVideoWriter* cvCreateVideoWriter(const char* filename, int fourcc, double fps, CvSize frame_size, int is_color=1)
Regarding the foucc param, the documentation states:
Under Win32 if 0 is passed while using an avi filename it will create a video writer that creates an uncompressed avi file.
It sounds like you could really benefit from using opencv, it could probably handle alot of this nicely for you. Take a look and see if it suits your needs: http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html#videocapture
You can use OpenCV to read and write avi file.
see http://opencv.willowgarage.com/documentation/cpp/reading_and_writing_images_and_video.html
Note that OpenCV can also be used to grab images from a camera.

Is there any sample code to read thumbnail from Jpeg exif header?

I am writing application using c++, in windows.
I want to get a thumbnail from jpeg, without decoding the whole image.
How Can I read thumbnail from jpeg exif header?
Can any one offer me a some sample code?
Many thanks!
Unsurprisingly the library is called libexif has win32 port, and there is sample code for reading thubnail from file
Don't bother. You can create tumbnails very fast from JPEGs. They are compressed using DCTs on 8x8 pixel blocks. So, get the DC component (i.e. 0,0) of each block and you have an 1/64th thumbnail without decoding. Further scaling should be fast since there are hardly any pixels left.

How do you place EXIF tags into a JPG, having the raw jpeg buffer in C++?

I am having a bit of a problem.
I get a RAW char* buffer from a camera and I need to add this tags before I can save it to disk. Writing the file to disk and reading it back again is not an option, as this will happen thousands of times.
The buffer data I receive from the camera does not contain any EXIF information, apart from the Width, Height and Pixels per Inch.
Any ideas? (C++)
Look at this PDF, on page 20 you have a diagram showing you were to place or modify your exif information. What is the difference with a file on disk ?
Does the JPEG buffer of your camera contain an EXIF section already ?
What's the difference? Why would doing it to a file on the disk be any different from doing it in memory?
Just do whatever it is you do after you read the file from the disk..
As far as I know EXIF data in JPEG is continuous subpart of file.
So
prepare EXIF data in memory
write part of JPEG file upto EXIF
write prepared EXIF
write rest of JPEG file
You might want to take a look into Exiv2 library. I know it can work on files but I suppose it also has functions to work on memory buffers.