libjpeg-turbo(1)
生活随笔
收集整理的這篇文章主要介紹了
libjpeg-turbo(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// TurboTrans.cpp : 定義控制臺應用程序的入口點。
//#include "stdafx.h"
#include <windows.h>
#include <string>
#include "turbojpeg.h"using namespace std;int tjpeg_header(unsigned char* jpeg_buffer, int jpeg_size, int* width, int* height, int* subsample, int* colorspace)
{tjhandle handle = NULL;handle = tjInitDecompress();tjDecompressHeader3(handle, jpeg_buffer, jpeg_size, width, height, subsample, colorspace);tjDestroy(handle);return 0;
}int tjpeg2rgb(unsigned char* jpeg_buffer, int jpeg_size, unsigned char* rgb_buffer, int* size)
{tjhandle handle = NULL;int width, height, subsample, colorspace;int flags = 0;int pixelfmt = TJPF_RGB;handle = tjInitDecompress();tjDecompressHeader3(handle, jpeg_buffer, jpeg_size, &width, &height, &subsample, &colorspace);flags |= 0;tjDecompress2(handle, jpeg_buffer, jpeg_size, rgb_buffer, width, 0,height, pixelfmt, flags);tjDestroy(handle);return 0;
}int trgb2jpeg(unsigned char* rgb_buffer, int width, int height, int quality, unsigned char** jpeg_buffer, unsigned long* jpeg_size)
{tjhandle handle = NULL;//unsigned long size=0;int flags = 0;int subsamp = TJSAMP_422;int pixelfmt = TJPF_RGB;handle = tjInitCompress();//size=tjBufSize(width, height, subsamp);tjCompress2(handle, rgb_buffer, width, 0, height, pixelfmt, jpeg_buffer, jpeg_size, subsamp,quality, flags);tjDestroy(handle);return 0;
}int tjpeg2yuv(unsigned char* jpeg_buffer, int jpeg_size, unsigned char** yuv_buffer, int* yuv_size, int* yuv_type, int& width, int &height)
{tjhandle handle = NULL;int subsample, colorspace;int flags = 0;int padding = 1; // 1或4均可,但不能是0int ret = 0;handle = tjInitDecompress();tjDecompressHeader3(handle, jpeg_buffer, jpeg_size, &width, &height, &subsample, &colorspace);printf("w: %d h: %d subsample: %d color: %d\n", width, height, subsample, colorspace);flags |= 0;*yuv_type = subsample;// 注:經測試,指定的yuv采樣格式只對YUV緩沖區大小有影響,實際上還是按JPEG本身的YUV格式來轉換的*yuv_size = tjBufSizeYUV2(width, padding, height, subsample);*yuv_buffer = (unsigned char *)malloc(*yuv_size);if (*yuv_buffer == NULL){printf("malloc buffer for rgb failed.\n");return -1;}ret = tjDecompressToYUV2(handle, jpeg_buffer, jpeg_size, *yuv_buffer, width,padding, height, flags);if (ret < 0){printf("compress to jpeg failed: %s\n", tjGetErrorStr());}tjDestroy(handle);return ret;
}int tyuv2jpeg(unsigned char* yuv_buffer, int yuv_size, int width, int height, int subsample, unsigned char** jpeg_buffer, unsigned long* jpeg_size, int quality)
{tjhandle handle = NULL;int flags = 0;int padding = 1; // 1或4均可,但不能是0int need_size = 0;int ret = 0;handle = tjInitCompress();flags |= 0;need_size = tjBufSizeYUV2(width, padding, height, subsample);if (need_size != yuv_size){printf("we detect yuv size: %d, but you give: %d, check again.\n", need_size, yuv_size);return 0;}ret = tjCompressFromYUV(handle, yuv_buffer, width, padding, height, subsample, jpeg_buffer, jpeg_size, quality, flags);if (ret < 0){printf("compress to jpeg failed: %s\n", tjGetErrorStr());}tjDestroy(handle);return ret;
}int _tmain(int argc, _TCHAR* argv[])
{char strExePathTmp[260] = { 0 };GetModuleFileNameA(NULL, strExePathTmp, 260);string strExePath(strExePathTmp);strExePath.resize(strExePath.find_last_of('\\'));SetCurrentDirectoryA(strExePath.c_str());FILE* fp;fp = fopen("1.jpeg", "rb");fseek(fp, 0, SEEK_END);int nlength = ftell(fp);unsigned char*szbuf = new unsigned char[nlength + 1];fseek(fp, 0, SEEK_SET);fread(szbuf, nlength, 1, fp);fclose(fp);unsigned char* szbuf1=NULL;int nsize = 0;int yuv_type = 0;int nwidth = 0;int nheight = 0;tjpeg2yuv(szbuf, nlength, &szbuf1, &nsize, &yuv_type, nwidth, nheight);delete[]szbuf;fp = fopen("2.yuv", "wb");fwrite(szbuf1, 1, nsize, fp);fclose(fp);unsigned char* szbuf2=NULL /*= new unsigned char[nsize + 1]*/;unsigned long ulsize = 0;tyuv2jpeg(szbuf1, nsize, nwidth, nheight, yuv_type, &szbuf2, &ulsize, 100);fp = fopen("3.jpeg", "wb");fwrite(szbuf2, 1, ulsize, fp);fclose(fp);delete[] szbuf2;delete[] szbuf1;return 0;
}
總結
以上是生活随笔為你收集整理的libjpeg-turbo(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Unity -----一些可能存在的错误
- 下一篇: 什么是多核电脑?什么是64位电脑?