我有这样一个通常的代码:
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
以获得与所需尺寸最相似的图片。
答案 0 :(得分:4)
您应该调用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;