【问题标题】:libjpeg: how to get proper image decompressed size?libjpeg:如何获得正确的图像解压缩大小?
【发布时间】:2012-10-18 14:53:27
【问题描述】:

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

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 = 1scale_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 以获得与所需最大相似尺寸的图像。

【问题讨论】:

    标签: c++ jpeg compression libjpeg


    【解决方案1】:

    您应该调用 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;
    

    【讨论】:

    • 你输入的宽高和needed_width、needed_height是多少?
    • 3264 x 2448 到 1024 x 768,结果是 1632 x 1224。但 libjpeg 可以为此图像加载例如 1224 x 918。
    • 您应该读取 1632 x 1224 (scale_den = 2) 的图像,然后执行 resize 到所需大小
    猜你喜欢
    • 1970-01-01
    • 2016-01-29
    • 2013-09-03
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 2016-02-08
    • 2012-07-30
    相关资源
    最近更新 更多