问题描述
libjpeg-turbo的说明
我找到了一些部分,例如 .com/erlyvideo/jpeg/blob/master/c_src/jpeg.c 似乎正在使用turbojpeg API,但是还有更多固体/变化的示例吗? libjpeg-turbo的来源有充分的记录,因此确实有帮助. 最后,我使用了在Internet上找到的随机代码的组合(例如 https://github.com/erlyvideo/jpeg/blob/master/c_src/jpeg.c )和.c and header Files for Libjeg-turbo,for Libjeg-turbo,已有文献记载.
其他推荐答案 好吧,我知道您确实已经解决了您的问题,但是作为某些人,就像我一样,可以搜索一些简单的例子,我将分享我的创建.
这是一个示例,正在压缩和解压缩RGB图像.否则,我认为 turbojpeg 的API文档非常容易理解! 压缩: 之后,您在_compressedimage中具有压缩图像.
要解压缩您必须执行以下操作: 减压: 一些随机的想法: 我刚刚撰写学士学位论文时,我又回来了,我注意到,如果您在循环中运行压缩,则最好是存储JPEG缓冲区最大的尺寸,而不必分配新的尺寸转动.基本上,而不是这样做: 我们将添加一个对象变量,保留分配的内存long unsigned int _jpegBufferSize = 0;的大小,在每次压缩回合之前,我们将jpegsize设置回该值: 压缩后,如果将内存大小与实际的jpegsize进行比较,并将其设置为jpegsize,如果它高于先前的内存大小. 我最终使用以下代码作为JPEG编码和解码的工作示例.我能找到的最好的例子是,它是独立的,可以初始化虚拟图像并将编码的图像输出到本地文件. 下面的代码是不是我自己的,荣誉转到 https://sourceforge.net/p/libjpeg-turbo/discussion/1086868/thread/e402d36f/#8722 .再次在这里发布它,以帮助任何人发现很难让Libjpeg Turbo工作.推荐答案
#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);
#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);
long unsigned int _jpegSize = 0;
tjCompress2(_jpegCompressor, buffer, _width, 0, _height, TJPF_RGB,
&_compressedImage, &_jpegSize, TJSAMP_444, JPEG_QUALITY,
TJFLAG_FASTDCT);
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;
其他推荐答案
#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;
}