Skip to content

Commit

Permalink
Merge pull request #456 from gdt050579/type-jpg
Browse files Browse the repository at this point in the history
[JPEG] # handle pixel format based on colorspace
  • Loading branch information
gheorghitamutu authored Jul 26, 2024
2 parents 7bb41cd + 3ab4995 commit a043123
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions AppCUI/src/Graphics/JPGLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,45 @@ bool LoadJPGToImage(Image& img, const uint8* imageBuffer, uint32 size)
reinterpret_cast<void*>(tjInitDecompress()), [](void* p) { tjDestroy(reinterpret_cast<tjhandle>(p)); });
CHECK(handle != nullptr, false, "Failed to initialize TurboJPEG decompressor!");

int width, height, jpegSubsamp;
CHECK(tjDecompressHeader2(
int width, height, jpegSubsamp, colorspace;
CHECK(tjDecompressHeader3(
handle.get(),
const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(imageBuffer)),
size,
&width,
&height,
&jpegSubsamp) == 0,
&jpegSubsamp,
&colorspace) == 0,
false,
"Failed to read JPEG header: %s",
tjGetErrorStr());

TJPF pixelFormat = TJPF_RGB;

if (colorspace == TJCS_GRAY) {
pixelFormat = TJPF_RGBX;
}
else if (colorspace == TJCS_RGB) {
pixelFormat = TJPF_RGB;
}
else if (colorspace == TJCS_YCbCr || colorspace == TJCS_CMYK || colorspace == TJCS_YCCK) {
pixelFormat = TJPF_BGRA;
}

CHECK(img.Create(width, height), false, "Fail to create a %ux%u size image", width, height);

uint8* pixelBuffer = reinterpret_cast<uint8*>(img.GetPixelsBuffer());
CHECK(pixelBuffer != nullptr, false, "Failed to get the image pixel buffer!");

CHECK(tjDecompress2(
handle.get(),
const_cast<unsigned char*>(reinterpret_cast<const unsigned char*>(imageBuffer)),
size,
reinterpret_cast<unsigned char*>(img.GetPixelsBuffer()),
pixelBuffer,
width,
0 /* pitch */,
0,
height,
TJPF_RGB,
pixelFormat,
TJFLAG_FASTDCT) == 0,
false,
"Failed to decompress JPEG image: %s",
Expand Down

0 comments on commit a043123

Please sign in to comment.