我有这样一个常用的代码:
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_num 和 scale_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_num
和scale_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/