c++ - libjpeg:如何获得正确的图像解压大小?

标签 c++ jpeg compression libjpeg

我有这样一个常用的代码:

struct jpeg_decompress_struct cinfo;
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);

cinfo.scale_num = ?;
cinfo.scale_denom = ?;

needed_width = cinfo.output_width;
needed_height = cinfo.output_height;

如何选择 scale_numscale_denum 参数来将图像缩放到所需的尺寸。例如,我想将其缩小一倍。

如果我设置scale_num = 1 , scale_denom = 2 。结果是:(1802 x 1237)到(258 x 194)

文档说:

Scale the image by the fraction scale_num/scale_denom.  Default is
1/1, or no scaling.  Currently, the only supported scaling ratios
are 1/1, 1/2, 1/4, and 1/8.

但是当我设置这样的比例时,我没有得到所需的结果。

所以问题是:如何设置scale_numscale_denom获取与所需尺寸最大相似的图像。

最佳答案

您应该调用jpeg_calc_output_dimensions(cinfo)来获取正确的output_width和output_height值。

为了计算系数scale_denom,我通常会这样做:

unsigned int intlog2(unsigned int val) {
 int targetlevel = 0;
 while (val >>= 1) ++targetlevel;
 return targetlevel;
}

// ....
// for example, 
const int width = 5184;
const int height = 3456;

unsigned int needed_width = 640;
unsigned int needed_height = 480;

unsigned int wdeg = intlog2(width / needed_width);
unsigned int hdeg = intlog2(height / needed_height);

unsigned int scale_den = std::min(1 << (std::min)(wdeg, hdeg), 8);

unsigned int result_width = width / scale_den;
unsigned int result_height = height / scale_den;

关于c++ - libjpeg:如何获得正确的图像解压大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12957543/

相关文章:

c++ - 没有合适的默认构造函数可用 - 迭代器?

c++ - 在 Ubuntu 上的 eclipse 中开始对 c++ 进行单元测试

c++ - SAMD21如何使用TCC设置PWM?

actionscript-3 - 如何将 BitmapData 保存到 Bitmap *.bmp 文件,或者更快的 JPE 编码方法

delphi - 如何使用 FireDAC 在 Firebird 3.0 上启用 WireCompression

c++ - Orwell Dev-C++ 中的 Unicode 字符串

html - 有没有办法告诉浏览器尊重 jpeg exif 方向?

php - imagejpeg 保存文件不起作用

索引/随机访问 7zip .7z 文件

http - Google 按图片 "image_content"格式搜索?