Skip to content

Commit

Permalink
Updated native dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
giovannicalo committed Jan 18, 2024
1 parent dc0c114 commit 1b0b163
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ ExternalProject_Add(
BUILD_COMMAND cmake --build . --config ${CMAKE_BUILD_TYPE} --target turbojpeg-static
CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON
GIT_REPOSITORY "https://github.com/libjpeg-turbo/libjpeg-turbo.git"
GIT_TAG "2.1.5.1"
GIT_TAG "3.0.1"
INSTALL_COMMAND ""
)

Expand Down
42 changes: 16 additions & 26 deletions source/native/decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,56 +12,49 @@ nodeJpeg::Decoder::Decoder(
{}

void nodeJpeg::Decoder::Execute() {
tjhandle handle = tjInitDecompress();
tjhandle handle = tj3Init(TJINIT_DECOMPRESS);
if (!handle) {
SetError("[nodeJpeg::Decoder::Execute] Failed to initialize");
return;
}
int32_t colorspace = 0;
int32_t height = 0;
int32_t subsampling = 0;
int32_t width = 0;
if (tjDecompressHeader3(
if (tj3DecompressHeader(
handle,
source.Data(),
source.ByteLength(),
&width,
&height,
&subsampling,
&colorspace
source.ByteLength()
)) {
SetError("[nodeJpeg::Decoder::Execute] Failed to read header");
tjDestroy(handle);
tj3Destroy(handle);
return;
};
int32_t height = tj3Get(handle, TJPARAM_JPEGHEIGHT);
int32_t width = tj3Get(handle, TJPARAM_JPEGWIDTH);
if (format == Format::yuv) {
int32_t colorspace = tj3Get(handle, TJPARAM_COLORSPACE);
// TODO: Support other colorspaces
if (colorspace != TJCS_YCbCr) {
SetError("[nodeJpeg::Decoder::Execute] Input image colorspace is not YUV");
tjDestroy(handle);
tj3Destroy(handle);
return;
}
int32_t subsampling = tj3Get(handle, TJPARAM_SUBSAMP);
// TODO: Support other subsamplings
if (subsampling != TJSAMP_420) {
SetError("[nodeJpeg::Decoder::Execute] Input image subsampling is not 4:2:0");
tjDestroy(handle);
tj3Destroy(handle);
return;
}
// TODO: Support odd dimensions
height = (height + 1) & ~1;
width = (width + 1) & ~1;
uint64_t size = tjBufSizeYUV2(width, 1, height, TJSAMP_420);
uint64_t size = tj3YUVBufSize(width, 1, height, subsampling);
image = new Image(Format::yuv, width, height, size);
std::memset(image->data, 0, size);
if (tjDecompressToYUV2(
if (tj3DecompressToYUV8(
handle,
source.Data(),
source.ByteLength(),
image->data,
width,
1,
height,
0
1
)) {
SetError("[nodeJpeg::Decoder::Execute] Failed to decode");
delete image;
Expand All @@ -70,24 +63,21 @@ void nodeJpeg::Decoder::Execute() {
uint32_t pitch = width * 4;
uint64_t size = pitch * height;
image = new Image(Format::rgba, width, height, size);
if (tjDecompress2(
if (tj3Decompress8(
handle,
source.Data(),
source.ByteLength(),
image->data,
width,
pitch,
height,
TJPF_RGBA,
0
TJPF_RGBA
)) {
SetError("[nodeJpeg::Decoder::Execute] Failed to decode");
delete image;
}
} else {
SetError("[nodeJpeg::Decoder::Execute] Format is invalid");
}
tjDestroy(handle);
tj3Destroy(handle);
}

void nodeJpeg::Decoder::OnError(const Napi::Error& error) {
Expand Down
22 changes: 9 additions & 13 deletions source/native/encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,46 +24,42 @@ nodeJpeg::Encoder::~Encoder() {
}

void nodeJpeg::Encoder::Execute() {
tjhandle handle = tjInitCompress();
tjhandle handle = tj3Init(TJINIT_COMPRESS);
if (!handle) {
SetError("[nodeJpeg::Encoder::Execute] Failed to initialize");
return;
}
tj3Set(handle, TJPARAM_QUALITY, quality);
tj3Set(handle, TJPARAM_SUBSAMP, TJSAMP_420);
if (image->format == Format::rgba) {
if (tjCompress2(
if (tj3Compress8(
handle,
image->data,
image->width,
image->width * 4,
image->height,
TJPF_RGBA,
&buffer,
reinterpret_cast<unsigned long*>(&size),
TJSAMP_420,
quality,
0
reinterpret_cast<size_t*>(&size)
)) {
SetError("[nodeJpeg::Encoder::Execute] Failed to encode");
}
} else if (image->format == Format::yuv) {
if (tjCompressFromYUV(
if (tj3CompressFromYUV8(
handle,
image->data,
image->width,
1,
image->height,
TJSAMP_420,
&buffer,
reinterpret_cast<unsigned long*>(&size),
quality,
0
reinterpret_cast<size_t*>(&size)
)) {
SetError("[nodeJpeg::Encoder::Execute] Failed to encode");
}
} else {
SetError("[nodeJpeg::Encoder::Execute] Format is invalid");
}
tjDestroy(handle);
tj3Destroy(handle);
}

void nodeJpeg::Encoder::OnError(const Napi::Error& error) {
Expand All @@ -76,7 +72,7 @@ void nodeJpeg::Encoder::OnOK() {
buffer,
size,
[](Napi::Env, void* data) {
tjFree(static_cast<unsigned char*>(data));
tj3Free(static_cast<unsigned char*>(data));
}
));
}
Expand Down

0 comments on commit 1b0b163

Please sign in to comment.