libjpeg-turbo实现jpeg的解码

JPEG解压为RGB

int tj3_decode_image(const char* jpeg_file)
{
    tjscalingfactor scalingFactor = TJUNSCALED;
    int outSubsamp = -1, outQual = -1;
    int width, height;
    FILE* jpegFile = NULL;
    unsigned char* imgBuf = NULL, * jpegBuf = NULL;
    int retval = 0, i, pixelFormat = TJPF_UNKNOWN;
    tjhandle tjInstance = NULL;
    int fastUpsample = 1, fastDCT = 1;
    long size;
    int inSubsamp, inColorspace;
    size_t jpegSize;

    if ((tjInstance = tj3Init(TJINIT_DECOMPRESS)) == NULL)
        THROW_TJ("creating TurboJPEG instance");

    /* Read the JPEG file into memory. */
    if ((jpegFile = fopen(jpeg_file, "rb")) == NULL)
        THROW_UNIX("opening input file");
    if (fseek(jpegFile, 0, SEEK_END) < 0 || ((size = ftell(jpegFile)) < 0) ||
        fseek(jpegFile, 0, SEEK_SET) < 0)
        THROW_UNIX("determining input file size");
    if (size == 0)
        THROW("determining input file size", "Input file contains no data");
    jpegSize = size;
    if ((jpegBuf = (unsigned char*)tj3Alloc(jpegSize)) == NULL)
        THROW_UNIX("allocating JPEG buffer");
    if (fread(jpegBuf, jpegSize, 1, jpegFile) < 1)
        THROW_UNIX("reading input file");
    fclose(jpegFile);  jpegFile = NULL;
    printf("jpegSize:%d\n", jpegSize);

    if (tj3Set(tjInstance, TJPARAM_FASTUPSAMPLE, fastUpsample) < 0)
        THROW_TJ("setting TJPARAM_FASTUPSAMPLE");
    if (tj3Set(tjInstance, TJPARAM_FASTDCT, fastDCT) < 0)
        THROW_TJ("setting TJPARAM_FASTDCT");

    if (tj3DecompressHeader(tjInstance, jpegBuf, jpegSize) < 0)
        THROW_TJ("reading JPEG header");
    width = tj3Get(tjInstance, TJPARAM_JPEGWIDTH);
    height = tj3Get(tjInstance, TJPARAM_JPEGHEIGHT);
    inSubsamp = tj3Get(tjInstance, TJPARAM_SUBSAMP);
    inColorspace = tj3Get(tjInstance, TJPARAM_COLORSPACE);

    if (tj3Get(tjInstance, TJPARAM_LOSSLESS))
        scalingFactor = TJUNSCALED;

    printf("Input Image:  %d x %d pixels, %s subsampling, %s colorspace\n",
       width, height, subsampName[inSubsamp], colorspaceName[inColorspace]);

    /* Scaling and/or a non-JPEG output image format and/or compression options
       have been selected, so we need to decompress the input/transformed
       image. */
    if (tj3SetScalingFactor(tjInstance, scalingFactor) < 0)
        THROW_TJ("setting scaling factor");
    width = TJSCALED(width, scalingFactor);
    height = TJSCALED(height, scalingFactor);
    if (outSubsamp < 0)
        outSubsamp = inSubsamp;

    pixelFormat = TJPF_BGRX;
    if ((unsigned long long)width * height * tjPixelSize[pixelFormat] >
        (unsigned long long)((size_t)-1))
        THROW("allocating uncompressed image buffer", "Image is too large");
    if ((imgBuf =
        (unsigned char*)malloc(sizeof(unsigned char) * width * height *
            tjPixelSize[pixelFormat])) == NULL)
        THROW_UNIX("allocating uncompressed image buffer");

    if (tj3Decompress8(tjInstance, jpegBuf, jpegSize, imgBuf, 0,
        pixelFormat) < 0)
        THROW_TJ("decompressing JPEG image");

    /* Output image format is not JPEG.  Save the uncompressed image
    directly to disk. */
    printf("\n");
    if (tj3SaveImage8(tjInstance, "decode_rgb_image.bmp", imgBuf, width, 0, height,
        pixelFormat) < 0)
        THROW_TJ("saving output image");

bailout:
    free(imgBuf);
    tj3Free(jpegBuf); jpegBuf = NULL;
    tj3Destroy(tjInstance);
    if (jpegFile) fclose(jpegFile);
    return retval;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Attract__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值