Skip to content

Commit

Permalink
use TJ3 API for encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
qbnu committed Jun 20, 2024
1 parent e75cea9 commit 1c89052
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/JPEGView/TJPEGWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,28 @@ void * TurboJpeg::Compress(const void *source,
{
outOfMemory = false;
len = 0;
tjhandle hEncoder = tjInitCompress();
tjhandle hEncoder = tj3Init(TJINIT_COMPRESS);
if (hEncoder == NULL) {
return NULL;
}

unsigned char* pJPEGCompressed = NULL;
unsigned long nCompressedLen = 0;
int nResult = tjCompress2(hEncoder, (unsigned char*)source, width, TJPAD(width * 3), height, TJPF_BGR,
&pJPEGCompressed, &nCompressedLen, TJSAMP_420, quality, 0);
if (nResult != 0) {
if (pJPEGCompressed != NULL) {
tjFree(pJPEGCompressed);
pJPEGCompressed = NULL;
} else {
size_t nCompressedLen = 0;
tj3Set(hEncoder, TJPARAM_SUBSAMP, TJSAMP_420);
tj3Set(hEncoder, TJPARAM_QUALITY, quality);
int nResult = tj3Compress8(hEncoder, (unsigned char*)source, width, TJPAD(width * 3), height, TJPF_BGR,
&pJPEGCompressed, &nCompressedLen);
if (nResult != 0 || nCompressedLen > INT_MAX) {
if (pJPEGCompressed == NULL) {
outOfMemory = true;
}
tj3Free(pJPEGCompressed);
pJPEGCompressed = NULL;
}

len = nCompressedLen;

tjDestroy(hEncoder);
tj3Destroy(hEncoder);

return pJPEGCompressed;
}
}

0 comments on commit 1c89052

Please sign in to comment.