views:

368

answers:

2

I'v got a bitmap 24bits, I am writing application in c++, MFC, I am using libjpeg for encoding the bitmap into jpeg file 24bits.

When this bitmap's width is M, and height is N.

How to estimate jpeg file size before saving it with certain quality factor N (0-100).

How can I do it ? Many thanks!

Is it possible to do this?

For example. I want to implement a slide bar, which represent save a current bitmap with certain quality factor N. A label is beside it. shows the approximate file size when decode the bitmap with this quality factor. When user move the slide bar. He can have a approximate preview of the filesize of the tobe saved jpeg file.

A: 

It also depends on the compression you are using and to be more specific how many bits per color pixel are you using.

The quality factor wont help you here as a quality factor of 100 can range (in most cases) from 6 bits per color pixel to ~10 bits per color pixel, maybe even more (Not sure).

so once you know that its really straight forward from there..

Red Serpent
I want to save it as 24bits jpeg file
what you should be most interested in is the compression ratio and not the bit depth of the image. in other words mostly all images nowadays uses a 24 bit depth, translated into R = 8bits, G = 8 bits and B = 8 bits (RGB). now to calculate an image size first you'll need the size in Mega pixels MP which is basically the width multiplied by the height of the image. then to get the size in bytes you'll multiply the size in MP by the bit depth and divide by 8.. in your case that is (N * M * 24)/8. This will give you the raw size of the image which is the same as that of a raw BMP.
Red Serpent
You'll have to divide that by the compression ratio. for example if your compression ration is 4:1 this means that you'll divide by 4 making the equation looks like this:((N * M * 24)/8)/4
Red Serpent
and bare in mind that JPEG conversion is not lossless so there has to be a conversion somewhere and its probably determined by the quality factor.
Red Serpent
+1  A: 

In libjpeg, you can write a custom destination manager that doesn't actually call fwrite, but just counts the number of bytes written.

Start with the stdio destination manager in jdatadst.c, and have a look at the documentation in libjpeg.doc.

Your init_destination and term_destination methods will be very minimal (just alloc/dealloc), and your empty_output_buffer method will do the actual counting. Once you have completed the JPEG writing, you'll have to read the count value out of your custom structure. Make sure you do this before term_destination is called.

Adam Goode
Many thanks Adam. I see. I am using FreeImage lib which is based on libjpeg , It have build-in API. I will use it