huago-corrcet_tools/HuaGoCorrect/jpeglib.cpp

72 lines
1.5 KiB
C++

#include "stdafx.h"
#include "jpeglib.h"
#include "PublicFunc.h"
JpegLib::JpegLib()
:img_buffer(NULL), sizeBuffer(0)
{
handle = std::shared_ptr<void> (tjInitDecompress(),tjDestroy);
}
JpegLib::~JpegLib()
{
clear();
}
void JpegLib::clear()
{
img_buffer.reset();
sizeBuffer = 0;
}
cv::Mat JpegLib::decode(cv::Mat& buf, int pixelFormat)
{
if(buf.rows != 1)
return cv::Mat();
//XdPrint("==============decode cols is :%d ============\n",buf.cols);
decode(buf.data, buf.cols, pixelFormat);
return cv::Mat(getHeight(), getWidth() , pixelFormat == TJPF_BGR ? CV_8UC3 : CV_8UC1, getData());
}
void JpegLib::decode(unsigned char* buff, unsigned long buff_size, int pixelFormat)
{
tjDecompressHeader(handle.get(), buff, buff_size, &width, &height);
//XdPrint("==============tjDecompressHeader width is :%d height is : %d ============\n",width , height);
int size = width * height *tjPixelSize[pixelFormat];
if (sizeBuffer != size)
{
clear();
img_buffer = std::shared_ptr<unsigned char>(tjAlloc(size), tjFree);
sizeBuffer = size;
}
if (img_buffer)
{
//XdPrint("==============img_buffer buff_size is :%d ============\n",buff_size);
tjDecompress2(handle.get(), buff, buff_size, img_buffer.get(), width, 0, height, pixelFormat, 0);
//XdPrint("============== tjDecompress2 finish ============\n",buff_size);
}
}
int JpegLib::getWidth()
{
return width;
}
int JpegLib::getHeight()
{
return height;
}
int JpegLib::getBufferSize()
{
return sizeBuffer;
}
unsigned char* JpegLib::getData()
{
return img_buffer.get();
}