|
shun-iwasawa |
31accf |
#include "jpgconverter.h"
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
#include <qfile></qfile>
|
|
shun-iwasawa |
31accf |
#include <qdatastream></qdatastream>
|
|
shun-iwasawa |
31accf |
//=============================================================================
|
|
shun-iwasawa |
31accf |
//=============================================================================
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
JpgConverter::JpgConverter() {}
|
|
shun-iwasawa |
31accf |
JpgConverter::~JpgConverter() {}
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
#ifdef WITH_CANON
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
void JpgConverter::setStream(EdsStreamRef stream) { m_stream = stream; }
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
void JpgConverter::convertFromJpg() {
|
|
shun-iwasawa |
31accf |
#ifdef MACOSX
|
|
shun-iwasawa |
31accf |
UInt64 mySize = 0;
|
|
shun-iwasawa |
31accf |
#else
|
|
shun-iwasawa |
31accf |
unsigned __int64 mySize = 0;
|
|
shun-iwasawa |
31accf |
#endif
|
|
shun-iwasawa |
31accf |
unsigned char* data = NULL;
|
|
shun-iwasawa |
31accf |
EdsError err = EdsGetPointer(m_stream, (EdsVoid**)&data);
|
|
shun-iwasawa |
31accf |
err = EdsGetLength(m_stream, &mySize);
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
int width, height, pixelFormat;
|
|
shun-iwasawa |
31accf |
int inSubsamp, inColorspace;
|
|
shun-iwasawa |
31accf |
tjhandle tjInstance = NULL;
|
|
shun-iwasawa |
31accf |
unsigned char* imgBuf = NULL;
|
|
shun-iwasawa |
31accf |
tjInstance = tjInitDecompress();
|
|
shun-iwasawa |
31accf |
tjDecompressHeader3(tjInstance, data, mySize, &width, &height, &inSubsamp,
|
|
shun-iwasawa |
31accf |
&inColorspace);
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
if (width < 0 || height < 0) {
|
|
shun-iwasawa |
31accf |
emit(imageReady(false));
|
|
shun-iwasawa |
31accf |
return;
|
|
shun-iwasawa |
31accf |
}
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
pixelFormat = TJPF_BGRX;
|
|
shun-iwasawa |
31accf |
imgBuf = (unsigned char*)tjAlloc(width * height * tjPixelSize[pixelFormat]);
|
|
shun-iwasawa |
31accf |
int flags = 0;
|
|
shun-iwasawa |
31accf |
flags |= TJFLAG_BOTTOMUP;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
int factorsNum;
|
|
shun-iwasawa |
31accf |
tjscalingfactor scalingFactor = {1, 1};
|
|
shun-iwasawa |
31accf |
tjscalingfactor* factor = tjGetScalingFactors(&factorsNum);
|
|
shun-iwasawa |
31accf |
int i = 0;
|
|
shun-iwasawa |
31accf |
int tempWidth, tempHeight;
|
|
shun-iwasawa |
31accf |
while (i < factorsNum) {
|
|
shun-iwasawa |
31accf |
scalingFactor = factor[i];
|
|
shun-iwasawa |
31accf |
i++;
|
|
shun-iwasawa |
31accf |
tempWidth = TJSCALED(width, scalingFactor);
|
|
shun-iwasawa |
31accf |
tempHeight = TJSCALED(height, scalingFactor);
|
|
shun-iwasawa |
31accf |
}
|
|
shun-iwasawa |
31accf |
tjDecompress2(tjInstance, data, mySize, imgBuf, width,
|
|
shun-iwasawa |
31accf |
width * tjPixelSize[pixelFormat], height, pixelFormat, flags);
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
m_finalImage = TRaster32P(width, height);
|
|
shun-iwasawa |
31accf |
m_finalImage->lock();
|
|
shun-iwasawa |
31accf |
uchar* rawData = m_finalImage->getRawData();
|
|
shun-iwasawa |
31accf |
memcpy(rawData, imgBuf, width * height * tjPixelSize[pixelFormat]);
|
|
shun-iwasawa |
31accf |
m_finalImage->unlock();
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
tjFree(imgBuf);
|
|
shun-iwasawa |
31accf |
imgBuf = NULL;
|
|
shun-iwasawa |
31accf |
tjDestroy(tjInstance);
|
|
shun-iwasawa |
31accf |
tjInstance = NULL;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
if (m_stream != NULL) {
|
|
shun-iwasawa |
31accf |
EdsRelease(m_stream);
|
|
shun-iwasawa |
31accf |
m_stream = NULL;
|
|
shun-iwasawa |
31accf |
}
|
|
shun-iwasawa |
31accf |
data = NULL;
|
|
shun-iwasawa |
31accf |
emit(imageReady(true));
|
|
shun-iwasawa |
31accf |
}
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
void JpgConverter::run() { convertFromJpg(); }
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
#endif
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
//-----------------------------------------------------------------------------
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
void JpgConverter::saveJpg(TRaster32P image, TFilePath path) {
|
|
shun-iwasawa |
31accf |
unsigned char* jpegBuf = NULL; /* Dynamically allocate the JPEG buffer */
|
|
shun-iwasawa |
31accf |
unsigned long jpegSize = 0;
|
|
shun-iwasawa |
31accf |
int pixelFormat = TJPF_BGRX;
|
|
shun-iwasawa |
31accf |
int outQual = 95;
|
|
shun-iwasawa |
31accf |
int subSamp = TJSAMP_411;
|
|
shun-iwasawa |
31accf |
bool success = false;
|
|
shun-iwasawa |
31accf |
tjhandle tjInstance;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
int width = image->getLx();
|
|
shun-iwasawa |
31accf |
int height = image->getLy();
|
|
shun-iwasawa |
31accf |
int flags = 0;
|
|
shun-iwasawa |
31accf |
flags |= TJFLAG_BOTTOMUP;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
image->lock();
|
|
shun-iwasawa |
31accf |
uchar* rawData = image->getRawData();
|
|
shun-iwasawa |
31accf |
if ((tjInstance = tjInitCompress()) != NULL) {
|
|
shun-iwasawa |
31accf |
if (tjCompress2(tjInstance, rawData, width, 0, height, pixelFormat,
|
|
shun-iwasawa |
31accf |
&jpegBuf, &jpegSize, subSamp, outQual, flags) >= 0) {
|
|
shun-iwasawa |
31accf |
success = true;
|
|
shun-iwasawa |
31accf |
}
|
|
shun-iwasawa |
31accf |
}
|
|
shun-iwasawa |
31accf |
image->unlock();
|
|
shun-iwasawa |
31accf |
tjDestroy(tjInstance);
|
|
shun-iwasawa |
31accf |
tjInstance = NULL;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
if (success) {
|
|
shun-iwasawa |
31accf |
/* Write the JPEG image to disk. */
|
|
shun-iwasawa |
31accf |
QFile fullImage(path.getQString());
|
|
shun-iwasawa |
31accf |
fullImage.open(QIODevice::WriteOnly);
|
|
shun-iwasawa |
31accf |
QDataStream dataStream(&fullImage);
|
|
shun-iwasawa |
31accf |
dataStream.writeRawData((const char*)jpegBuf, jpegSize);
|
|
shun-iwasawa |
31accf |
fullImage.close();
|
|
shun-iwasawa |
31accf |
}
|
|
shun-iwasawa |
31accf |
tjFree(jpegBuf);
|
|
shun-iwasawa |
31accf |
jpegBuf = NULL;
|
|
shun-iwasawa |
31accf |
}
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
//-----------------------------------------------------------------------------
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
bool JpgConverter::loadJpg(TFilePath path, TRaster32P& image) {
|
|
shun-iwasawa |
31accf |
long size;
|
|
shun-iwasawa |
31accf |
int inSubsamp, inColorspace, width, height;
|
|
shun-iwasawa |
31accf |
unsigned long jpegSize;
|
|
shun-iwasawa |
31accf |
unsigned char* jpegBuf;
|
|
shun-iwasawa |
31accf |
FILE* jpegFile;
|
|
shun-iwasawa |
31accf |
QString qPath = path.getQString();
|
|
shun-iwasawa |
31accf |
QByteArray ba = qPath.toLocal8Bit();
|
|
shun-iwasawa |
31accf |
const char* c_path = ba.data();
|
|
shun-iwasawa |
31accf |
bool success = true;
|
|
shun-iwasawa |
31accf |
tjhandle tjInstance;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
/* Read the JPEG file into memory. */
|
|
shun-iwasawa |
31accf |
if ((jpegFile = fopen(c_path, "rb")) == NULL) success = false;
|
|
shun-iwasawa |
31accf |
if (success && fseek(jpegFile, 0, SEEK_END) < 0 ||
|
|
shun-iwasawa |
31accf |
((size = ftell(jpegFile)) < 0) || fseek(jpegFile, 0, SEEK_SET) < 0)
|
|
shun-iwasawa |
6bac0f |
success = false;
|
|
shun-iwasawa |
31accf |
if (success && size == 0) success = false;
|
|
shun-iwasawa |
6bac0f |
jpegSize = (unsigned long)size;
|
|
shun-iwasawa |
31accf |
if (success && (jpegBuf = (unsigned char*)tjAlloc(jpegSize)) == NULL)
|
|
shun-iwasawa |
31accf |
success = false;
|
|
shun-iwasawa |
31accf |
if (success && fread(jpegBuf, jpegSize, 1, jpegFile) < 1) success = false;
|
|
shun-iwasawa |
31accf |
fclose(jpegFile);
|
|
shun-iwasawa |
31accf |
jpegFile = NULL;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
if (success && (tjInstance = tjInitDecompress()) == NULL) success = false;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
6bac0f |
if (success && tjDecompressHeader3(tjInstance, jpegBuf, jpegSize, &width,
|
|
shun-iwasawa |
6bac0f |
&height, &inSubsamp, &inColorspace) < 0)
|
|
shun-iwasawa |
31accf |
success = false;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
int pixelFormat = TJPF_BGRX;
|
|
shun-iwasawa |
31accf |
unsigned char* imgBuf = NULL;
|
|
shun-iwasawa |
31accf |
if (success &&
|
|
shun-iwasawa |
31accf |
(imgBuf = tjAlloc(width * height * tjPixelSize[pixelFormat])) == NULL)
|
|
shun-iwasawa |
31accf |
success = false;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
int flags = 0;
|
|
shun-iwasawa |
31accf |
flags |= TJFLAG_BOTTOMUP;
|
|
shun-iwasawa |
6bac0f |
if (success && tjDecompress2(tjInstance, jpegBuf, jpegSize, imgBuf, width, 0,
|
|
shun-iwasawa |
6bac0f |
height, pixelFormat, flags) < 0)
|
|
shun-iwasawa |
31accf |
success = false;
|
|
shun-iwasawa |
31accf |
tjFree(jpegBuf);
|
|
shun-iwasawa |
31accf |
jpegBuf = NULL;
|
|
shun-iwasawa |
31accf |
tjDestroy(tjInstance);
|
|
shun-iwasawa |
31accf |
tjInstance = NULL;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
image = TRaster32P(width, height);
|
|
shun-iwasawa |
31accf |
image->lock();
|
|
shun-iwasawa |
31accf |
uchar* rawData = image->getRawData();
|
|
shun-iwasawa |
31accf |
memcpy(rawData, imgBuf, width * height * tjPixelSize[pixelFormat]);
|
|
shun-iwasawa |
31accf |
image->unlock();
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
tjFree(imgBuf);
|
|
shun-iwasawa |
31accf |
imgBuf = NULL;
|
|
shun-iwasawa |
31accf |
|
|
shun-iwasawa |
31accf |
return success;
|
|
shun-iwasawa |
31accf |
}
|