关于在Linux下使用TurboJPEG库

关于在Linux下使用TurboJPEG库

在做毕设的过程中要用到jpeg库。先考虑的是libjpeg,但是看完官方文档后还是不太会用,就转用了libjpeg-turbo,比libjpeg要简单。但是在写cmake的时候遇到了一些困难,jpegturbo没有给.cmake文件,我就先百度如何写.cmake,改了之后还是没啥用,就在github上看了一个issue,解决(所以有关于库的相关问题一定先看github上的issues,你遇到的问题别人肯定也遇到过)。

Question: Would a CMake Package config be useful · Issue #339 · libjpeg-turbo/libjpeg-turbo

set(CMAKE_PREFIX_PATH /opt/libjpeg-turbo)
find_package(PkgConfig REQUIRED)
pkg_search_module(TURBOJPEG REQUIRED libturbojpeg)

link_directories(${TURBOJPEG_LIBDIR})
add_executable(tjexample tjexample.c)
target_include_directories(tjexample PUBLIC ${TURBOJPEG_INCLUDE_DIRS})
target_link_libraries(tjexample ${TURBOJPEG_LIBRARIES})

直接在CmakeList.txt里加入上面一段就行了,但是执行文件要改成你自己的。

如何使用turboJpeg,有下面一段代码实例

Compression

#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);

Decompression

#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);

以上代码来自stackoverflow.com/quest

编辑于 2019-04-16