libjpeg-turbo实现jpeg的解码

本文介绍了如何使用libjpeg-turbo库将JPEG图像解码转换为RGB格式,详细解析了解码过程的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
### 关于ArcGIS License Server无法启动的解决方案 当遇到ArcGIS License Server无法启动的情况时,可以从以下几个方面排查并解决问题: #### 1. **检查网络配置** 确保License Server所在的计算机能够被其他客户端正常访问。如果是在局域网环境中部署了ArcGIS Server Local,则需要确认该环境下的网络设置是否允许远程连接AO组件[^1]。 #### 2. **验证服务状态** 检查ArcGIS Server Object Manager (SOM) 的运行情况。通常情况下,在Host SOM机器上需将此服务更改为由本地系统账户登录,并重启相关服务来恢复其正常工作流程[^2]。 #### 3. **审查日志文件** 查看ArcGIS License Manager的日志记录,寻找任何可能指示错误原因的信息。这些日志可以帮助识别具体是什么阻止了许可证服务器的成功初始化。 #### 4. **权限问题** 确认用于启动ArcGIS License Server的服务账号具有足够的权限执行所需操作。这包括但不限于读取/写入特定目录的权利以及与其他必要进程通信的能力。 #### 5. **软件版本兼容性** 保证所使用的ArcGIS产品及其依赖项之间存在良好的版本匹配度。不一致可能会导致意外行为或完全失败激活license server的功能。 #### 示例代码片段:修改服务登录身份 以下是更改Windows服务登录凭据的一个简单PowerShell脚本例子: ```powershell $serviceName = "ArcGISServerObjectManager" $newUsername = ".\LocalSystemUser" # 替换为实际用户名 $newPassword = ConvertTo-SecureString "" -AsPlainText -Force Set-Service -Name $serviceName -StartupType Automatic New-ServiceCredential -ServiceName $serviceName -Account $newUsername -Password $newPassword Restart-Service -Name $serviceName ``` 上述脚本仅作为示范用途,请依据实际情况调整参数值后再实施。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Attract__

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

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

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

打赏作者

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

抵扣说明:

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

余额充值