twain2/jpeglib.cpp

67 lines
1.2 KiB
C++

#include "stdafx.h"
#include "jpeglib.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();
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);
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)
{
tjDecompress2(handle.get(), buff, buff_size, img_buffer.get(), width, 0, height, pixelFormat, 0);
}
}
int JpegLib::getWidth()
{
return width;
}
int JpegLib::getHeight()
{
return height;
}
int JpegLib::getBufferSize()
{
return sizeBuffer;
}
unsigned char* JpegLib::getData()
{
return img_buffer.get();
}