English 中文(简体)
How to estimate size of a Jpeg file before saving it with a certain quality factor?
原标题:
  • 时间:2009-11-16 06:37:32
  •  标签:
  • jpeg

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).

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.

最佳回答

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.

问题回答

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..

If you know the Sub Sampling Factor this can be estimated. That information comes from the start of frame marker.

In the same marker right before the width and height so is the bit depth.

If you let

int subSampleFactorH = 2, subSampleFactorV = 1;

Then

int totalImageBytes = (Image.Width / subSampleFactorH) * (Image.Height / subSampleFactorV);

Then you can also optionally add more bytes to account for container data also.

int totalBytes = totalImageBytes + someConstantOverhead;





相关问题
Create a Video Stream (AVI) from a Series of Images

There is an IP web camera that I wrote a .NET class for sometime ago. It s basically a Timer implementation that pings a snapshot CGI script from the camera every five seconds. The camera itself is ...

JPG+Zip File Combination Problem with Zip Format

Hopefully you ve heard of the neat hack that lets you combine a JPG and a Zip file into a single file and it s a valid (or at least readable) file for both formats. Well, I realized that since JPG ...

c# converting HTML to JPG

I am trying to download image from HTTP URL to my computer via c#. Example picture: http://www.hcs.harvard.edu/csharp/Logo1.png I am using cURL to fletch it. Then I am saving it to computer as ...

Scale a .jpg file in WPF

I d like to open a .jpg file in WPF, scale it down to around 50%, then save it back to the file system. What s a good/efficient way to go about doing that?

How to Add Comments to a JPEG File Using C#

Within the property window of a JPEG image, there is a tab called Summary . Within this tab, there is a field called Comments I would like to write some c# code which will add a given string to ...

热门标签