How to read embed thumbnail from jpeg images? - c++

I am writing a thumbnail viewer in c++.
I first make use of EXIF information to retrive an image's thumbnail, but the thumbnail in the EXIF is bad, with black bands. So I want to get the jpeg's embedded thumbnail, how can I do this?
Another question:
does jpeg's embeded thumbnail equal to EXIF thumbnial?

If the EXIF thumbnail is bad, you can generate your own from the JPEG itself, without needing to completely decode the JPEG.
Look for the source code for EPEG. It's part of the Enlightenment project on SourceForge, and was part of the old EFL a year ago. You can still dig it up from some old SVN commits or from a source tarball that might be floating around.
Basically, what EPEG does, is it collects the DCT coefficients from the image, and performs a rescaling operation on them. The DCT coefficient is normally used as the base coefficient for an 8x8 block of pixels. You can treat it as one pixel. As a result, you have a (computationally free) thumbnail exactly 1/8th the size of the original image. Rescale it as you would any image data to the desired dimensions.

In the majority of cases where people talk about JPEG files having thumbnails, they mean the file is JPEG-EXIF and the EXIF data contains a thumbnail image.
However, it is true to say that JFIF 1.02 supports a thumbnail image. The thumbnail may be stored in the JFIF APP0 marker or an official JFXX APP0 extension marker; the thumbnail may be uncompressed RGB, JPEG or palletted. See
http://en.wikipedia.org/wiki/JPEG_File_Interchange_Format#JFIF_segment_format for details.

The fastest and recommended ways are libraries.
For example, www.exiv2.org can provide you with the library for embedded thumbnail and EXIF info extraction. There is a lot of alternatives, but I think this one is a good solution.

www.exiv2.org in my eyes is a bad solution. There are just too many deps to other projects. Expat and zlib for instance. If you want to come up with a lightweight solution that you can support on your own, I would recommend to write your own EXIF decoder.

Related

How can I compress jpeg image with compression rate 4 bpp or less?

I am trying to compress my .jpeg image in Photoshop.
WHat is the best way to do this?
I am now calculating the bpp taking the image size in kb, calculating how many bits that is. Then I take the image size in pixel*pixel to get the amount of pixels in the image. After that I divide bits/pixels, to find how many bits per pixel the image has.
But How can I change this number? My guess is to change how many kb the image is, but how do i do this?
Thanks for any help!!
Yes, you can achieve higher compression ratio than 4 bits per pixel. Images with solid color can have rate as low as 0.13bpp.
In fact 4bpp is quite poor compression — it's same as uncompressed 16-color image or half of 256-color image, which even GIF can manage. JPEG can look decent at 1-2bpp.
in general, you cannot "compress" a jpeg image. all you can do is to reduce the image quality further in order to achieve a lower bpp value. jpeg streams are always compressed and they use a lossy compression method. it means that the original image will never ever be reconstructed from a jpeg image. the smaller the file the more information you have lost.
a specific "bpp value" is not, and should never be your target. especially with lossy compression. you should always look at your current image and decide whether it is still good enough or not.
if you still have the original image, try a lossless compression format, like zip-compressed or lzw-compressed tiff or compressed png. i'm sure PhotoShop can handle these formats as well. another softwares like IrfanView (https://www.irfanview.com/) or XnView MP (https://www.xnview.com/en/xnviewmp/) will convert your images too.
if you want manual (eg. full) control over your images, you should use command line utilities, like ImageMagick (https://imagemagick.org/) or NConvert (please find the XnView MP link above)
if you have only the jpeg images do not touch (edit & save) them. with every single save operation you lose another bunch of information. you should always work on file copies.
you should always keep your master image (the very picture you took with your phone or your camera).
of course, these rules of thumb will not answer your original question.

Save a raw image in OpenCV

I'm trying to use OpenCV to read/write images for me. Currently, I have them in a different, non-standard format, and I know how to get them into OpenCV's containers. Here are the requirements:
The pixels are 1, or 3 bands, U8, U16, U32, or F32
The images have metadata, random stuff, like the camera ID that took the images. I would like the metadata to be vi/notepad editable
I want to write as little code as possible when it comes to low level stuff. My experience is that this stuff requires the most maintenance.
I can define the format. It's only to read and write for these programs.
I don't want the pixels to be anything but binary, '0.5873499082' is way too much data for one float.
Is there a way to describe to OpenCV how to read and write image types it doesn't know? Are there image types already available for the types of images I have?
My interim solution is to use boost to serialize the image, and save the metadata in a separate file.
Try using gdal library for reading images and then convert it to IplImage.
OpenCV can't do that for you, you can store the metadata in a separate file, or you can use for example the jpeg exif (that won't be notepad editable though).

c++ read image pixels

I want a c++ Code to read every pixel from an image file. and i want to save the pixels like:
r[]
g[]
b[]
does somebody know how to do this?
The answer depends on the format of the image file. Is it a format which contains raw RGB data (such as uncompressed TGA)? Is it a YUV image? Is it a compressed image such as JPEG or PNG?
There are already plenty of C++ libraries out there which can read a wide variety of image file formats, and then provide the pixel-level access you require. Take a look at Adobe's GIL, or CImg for example.
There are many freely available libraries for reading different image file formats. Since you're using C++ you might want to look at Adobe's Generic Image Library (GIL) or even OpenCV.
This will sort you out, very easy to use and 'low level' image library:
http://easybmp.sourceforge.net/
Two libraries that I've used that jump to mind are:
ImageMagick
libGD
These libraries can handle a wide variety of image formats, depending on what you need.

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.

Does anyone know of a program/method to compress just certain parts of a PNG image w/o slicing it?

Please help! Thanks in advance.
Update: Sorry for the delayed response, but if it is helpful to provide more context here, since I'm not sure what alternative question I should be asking.
I have an image for a website home page that is 300px x 300px. That image has several distinct regions, including two that have graphical copy on top of the regions.
I have compressed the image down as much as I can without compromising the appearance of that text, and those critical regions of the image.
I tried slicing the less critical regions of the image and saving those at lower compressions in order to get the total kbs down, but as gregmac posted, the sections don't look right when rejoined.
I was wondering if there was a piece of software out there, or manual solution for identifying critical regions of an image to "compress less" and could compress other parts of the image more in order to get the file size down, while keeping those elements in the graphic that need to be high resolution sharper.
You cannot - you can only compress an entire PNG file.
You don't need to (I cannot think of a single case where compressing a specific portion of a PNG file would be useful)
Dividing the image in to multiple parts ("slicing") is the only way to compress different portions of a image file, although I'd even recommend again using different compression levels in one "sliced image", as differing compression artefacts joining up will probably look odd
Regarding your update,
identifying critical regions of an image to "compress less" and could compress other parts of the image more in order to get the file size down
This is inherently what image compression does - if there's a bit empty area it will be compressed to a few bytes (using RLE for example), but if there's a very detailed region it will have more bytes "spent" on it.
The problem sounds like the image is too big (in terms of file-size), have you tried other image formats, mainly GIF or JPEG (or the other PNG format, PNG-8 or PNG-24)?
I have compressed the image down as much as I can without compromising the appearance of that text
Perhaps the text could be overlaid using CSS, rather than embedded in the image? Might not be practical, but it would allow you to compress the background more (if the background image is a photo, JPEG might work best, since you no longer have to worry about the text)
Other than that, I'm out of ideas. Is the 300*300px PNG really too big?
It sounds like you are compressing parts of your image using something like JPEG and then pasting those compressed images onto a PNG combined with other images, and the entire PNG is sent to the browser where you split them up.
The problem with this is that the more you compress your JPEG parts the more decompression artifacts you will get. Then when you put these low quality images onto the PNG, which uses deflate compression, you will actually end up increasing the file size because it won't be able to compress well.
So if you are keen on keeping PNG as your file format the best solution would be to not compress the parts using JPEG which you paste onto your PNG - keep everything as sharp as possible.
PNG compresses each row separately unless you have used a "predictor" in the compression.
So it's best to keep your PNG as wide as possible with similar images next to each other horizontally rather than under each other vertically.
Perhaps upload an example of the images you're working with?