首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用libjpeg-turbo的TurboJPEG的示例或教程

使用libjpeg-turbo的TurboJPEG的示例或教程
EN

Stack Overflow用户
提问于 2012-02-01 19:01:47
回答 4查看 43K关注 0票数 33

libjpeg-turbo的使用说明是这样描述TurboJPEG接口的:“该接口封装了libjpeg-turbo,并提供了一个易于使用的接口,用于在内存中压缩和解压缩JPEG图像。”很好,但是有没有一些使用这个API的可靠例子呢?只是想在内存中解压一个相当普通的jpeg。

我已经找到了一些,比如https://github.com/erlyvideo/jpeg/blob/master/c_src/jpeg.c,它似乎正在使用TurboJPEG应用程序接口,但是还有没有更可靠/更多样的例子呢?

libjpeg-turbo的源代码有很好的文档记录,所以这确实有帮助。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2012-02-18 11:09:58

最后,我使用了在互联网上找到的随机代码(例如https://github.com/erlyvideo/jpeg/blob/master/c_src/jpeg.c)和libjeg-turbo的.c和头文件的组合,这些文件都有很好的文档记录。This官方API也是一个很好的信息源。

票数 4
EN

Stack Overflow用户

发布于 2013-07-16 15:55:13

好吧,我知道你已经解决了你的问题,但是有些人,就像我一样,可能正在寻找一些简单的例子,我会分享我创造的东西。这是一个例子,压缩和解压缩一个RGB图像。另外,我认为TurboJPEG的应用编程接口文档非常容易理解!

压缩:

代码语言:javascript
复制
#include <turbojpeg.h>

const int JPEG_QUALITY = 75;
const int COLOR_COMPONENTS = 3;
int _width = 1920;
int _height = 1080;
long unsigned int _jpegSize = 0;
unsigned char* _compressedImage = NULL; //!< Memory is allocated by tjCompress2 if _jpegSize == 0
unsigned char buffer[_width*_height*COLOR_COMPONENTS]; //!< Contains the uncompressed image

tjhandle _jpegCompressor = tjInitCompress();

tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
          &_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY,
          TJFLAG_FASTDCT);

tjDestroy(_jpegCompressor);

//to free the memory allocated by TurboJPEG (either by tjAlloc(), 
//or by the Compress/Decompress) after you are done working on it:
tjFree(&_compressedImage);

之后,您将在_compressedImage中获得压缩后的图像。要解压缩,您必须执行以下操作:

Decompression:

代码语言:javascript
复制
#include <turbojpeg.h>

long unsigned int _jpegSize; //!< _jpegSize from above
unsigned char* _compressedImage; //!< _compressedImage from above

int jpegSubsamp, width, height;
unsigned char buffer[width*height*COLOR_COMPONENTS]; //!< will contain the decompressed image

tjhandle _jpegDecompressor = tjInitDecompress();

tjDecompressHeader2(_jpegDecompressor, _compressedImage, _jpegSize, &width, &height, &jpegSubsamp);

tjDecompress2(_jpegDecompressor, _compressedImage, _jpegSize, buffer, width, 0/*pitch*/, height, TJPF_RGB, TJFLAG_FASTDCT);

tjDestroy(_jpegDecompressor);

一些随机想法:

在我写学士论文的时候,我刚刚回到这个问题上,我注意到如果你在一个循环中运行压缩,最好存储最大大小的JPEG缓冲区,这样就不必每次都分配一个新的缓冲区。基本上,不是做以下事情:

代码语言:javascript
复制
long unsigned int _jpegSize = 0;

tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
          &_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY,
          TJFLAG_FASTDCT);

我们会添加一个对象变量,保存分配的内存long unsigned int _jpegBufferSize = 0;的大小,在每次压缩循环之前,我们会将jpegSize设置回该值:

代码语言:javascript
复制
long unsigned int jpegSize = _jpegBufferSize;

tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
          &_compressedImage, &jpegSize, TJSAMP_444, JPEG_QUALITY,
          TJFLAG_FASTDCT);

_jpegBufferSize = _jpegBufferSize >= jpegSize? _jpegBufferSize : jpegSize;

在压缩之后,可以将内存大小与实际的jpegSize进行比较,如果它高于先前的内存大小,则将其设置为jpegSize。

票数 64
EN

Stack Overflow用户

发布于 2018-03-07 08:40:38

最后,我使用以下代码作为JPEG编码和解码的工作示例。我能找到的最好的例子,它是自包含的,它初始化一个虚拟图像并将编码后的图像输出到本地文件。

下面的代码是而不是我自己的,功劳归https://sourceforge.net/p/libjpeg-turbo/discussion/1086868/thread/e402d36f/#8722。再次在这里发布它,以帮助任何人发现很难让libjpeg turbo工作。

代码语言:javascript
复制
#include "turbojpeg.h"
#include <iostream>
#include <string.h>
#include <errno.h>

using namespace std;

int main(void)
{
    unsigned char *srcBuf; //passed in as a param containing pixel data in RGB pixel interleaved format
    tjhandle handle = tjInitCompress();

    if(handle == NULL)
    {
        const char *err = (const char *) tjGetErrorStr();
        cerr << "TJ Error: " << err << " UNABLE TO INIT TJ Compressor Object\n";
        return -1;
    }
    int jpegQual =92;
    int width = 128;
    int height = 128;
    int nbands = 3;
    int flags = 0;
    unsigned char* jpegBuf = NULL;
    int pitch = width * nbands;
    int pixelFormat = TJPF_GRAY;
    int jpegSubsamp = TJSAMP_GRAY;
    if(nbands == 3)
    {
        pixelFormat = TJPF_RGB;
        jpegSubsamp = TJSAMP_411;
    }
    unsigned long jpegSize = 0;

    srcBuf = new unsigned char[width * height * nbands];
    for(int j = 0; j < height; j++)
    {
        for(int i = 0; i < width; i++)
        {
            srcBuf[(j * width + i) * nbands + 0] = (i) % 256;
            srcBuf[(j * width + i) * nbands + 1] = (j) % 256;
            srcBuf[(j * width + i) * nbands + 2] = (j + i) % 256;
        }
    }

    int tj_stat = tjCompress2( handle, srcBuf, width, pitch, height,
        pixelFormat, &(jpegBuf), &jpegSize, jpegSubsamp, jpegQual, flags);
    if(tj_stat != 0)
    {
        const char *err = (const char *) tjGetErrorStr();
        cerr << "TurboJPEG Error: " << err << " UNABLE TO COMPRESS JPEG IMAGE\n";
        tjDestroy(handle);
        handle = NULL;
        return -1;
    }

    FILE *file = fopen("out.jpg", "wb");
    if (!file) {
        cerr << "Could not open JPEG file: " << strerror(errno);
        return -1;
    }
    if (fwrite(jpegBuf, jpegSize, 1, file) < 1) {
        cerr << "Could not write JPEG file: " << strerror(errno);
        return -1;
    }
    fclose(file);

    //write out the compress date to the image file
    //cleanup
    int tjstat = tjDestroy(handle); //should deallocate data buffer
    handle = 0;
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9094691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档