Skip to content

Commit

Permalink
added gray decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Mar 26, 2024
1 parent e5406e5 commit 1389d27
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 33 deletions.
2 changes: 1 addition & 1 deletion JxlCoder.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'JxlCoder'
s.version = '1.7.1'
s.version = '1.7.2'
s.summary = 'JXL coder for iOS and MacOS'
s.description = 'Provides support for JXL files in iOS and MacOS'
s.homepage = 'https://github.com/awxkee/jxl-coder-swift'
Expand Down
1 change: 1 addition & 0 deletions Sources/jxlc/CJpegXLAnimatedDecoder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ @implementation CJpegXLAnimatedDecoder {
}

-(nullable id)initWith:(nonnull NSData*)data error:(NSError * _Nullable *_Nullable)error {
dec = nullptr;
try {
const uint8_t* ptr = reinterpret_cast<const uint8_t*>([data bytes]);
mSrc.resize([data length]);
Expand Down
4 changes: 3 additions & 1 deletion Sources/jxlc/CJpegXLAnimatedEncoder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ -(nullable id)initWith:(int)width height:(int)height
quality:(int)quality
decodingSpeed:(JXLEncoderDecodingSpeed)decodingSpeed
error:(NSError * _Nullable *_Nullable)error {
enc = nullptr;
JxlPixelType jColorspace;
JxlCompressionOption jCompressionOption;

Expand Down Expand Up @@ -106,7 +107,8 @@ -(nullable void*)addFrame:(nonnull JXLSystemImage *)platformImage duration:(int)
if (enc->getJxlPixelType() == rgb) {
auto resizedVector = [RgbRgbaConverter convertRGBAtoRGB:buf width:width height:height];
if (resizedVector.size() == 1) {
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: @"Cannot convert RGBA pixels to RGB" }];
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500
userInfo:@{ NSLocalizedDescriptionKey: @"Cannot convert RGBA pixels to RGB" }];
return nil;
}
buf = resizedVector;
Expand Down
25 changes: 15 additions & 10 deletions Sources/jxlc/JxlAnimatedDecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ class JxlAnimatedDecoder {
std::string str = "Cannot create decoder";
throw AnimatedDecoderError(str);
}

if (JXL_DEC_SUCCESS != JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE)) {
return false;
}

if (JXL_DEC_SUCCESS !=
JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_BASIC_INFO |
JXL_DEC_COLOR_ENCODING |
Expand Down Expand Up @@ -146,18 +151,18 @@ class JxlAnimatedDecoder {
}
} else if (status == JXL_DEC_COLOR_ENCODING) {
size_t iccSize;
if (JXL_DEC_SUCCESS !=
JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
&iccSize)) {
std::string str = "Cannot retreive color info";
throw AnimatedDecoderError(str);
if (JXL_DEC_SUCCESS ==
JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA, &iccSize)) {
iccProfile.resize(iccSize);
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
iccProfile.data(), iccProfile.size())) {
std::string str = "Cannot retreive color icc profile";
throw AnimatedDecoderError(str);
}
} else {
iccProfile.resize(0);
}
iccProfile.resize(iccSize);
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
iccProfile.data(), iccProfile.size())) {
std::string str = "Cannot retreive color icc profile";
throw AnimatedDecoderError(str);
}
} else if (status == JXL_DEC_SUCCESS) {
JxlDecoderRewind(dec.get());
JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_FRAME | JXL_DEC_FULL_IMAGE);
Expand Down
12 changes: 10 additions & 2 deletions Sources/jxlc/JxlInternalCoder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,19 @@ - (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
colorSpace = CGColorSpaceCreateWithICCData(iccData);
CFRelease(iccData);
} else {
colorSpace = CGColorSpaceCreateDeviceRGB();
if (components > 1) {
colorSpace = CGColorSpaceCreateDeviceRGB();
} else {
colorSpace = CGColorSpaceCreateDeviceGray();
}
}

if (!colorSpace) {
colorSpace = CGColorSpaceCreateDeviceRGB();
if (components > 1) {
colorSpace = CGColorSpaceCreateDeviceRGB();
} else {
colorSpace = CGColorSpaceCreateDeviceGray();
}
}

int stride = components*(int)xSize * (int)(useFloats ? sizeof(uint16_t) : sizeof(uint8_t));
Expand Down
33 changes: 15 additions & 18 deletions Sources/jxlc/JxlWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
runner.get())) {
return false;
}

JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE);

if (JXL_DEC_SUCCESS != JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE)) {
return false;
}

JxlBasicInfo info;
JxlPixelFormat format;
Expand Down Expand Up @@ -95,10 +97,6 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
bitDepth = info.bits_per_sample;
*depth = info.bits_per_sample;
int baseComponents = info.num_color_channels;
// Will not support mono
if (baseComponents < 3) {
baseComponents = 3;
}
if (info.num_extra_channels > 0) {
baseComponents = 4;
}
Expand Down Expand Up @@ -129,19 +127,18 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
// if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsEncodedProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA, &colorEncoding)) {
// return false;
// }

size_t icc_size;
if (JXL_DEC_SUCCESS !=
JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
&icc_size)) {
return false;

size_t iccSize;
if (JXL_DEC_SUCCESS ==
JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA, &iccSize)) {
iccProfile->resize(iccSize);
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
iccProfile->data(), iccProfile->size())) {
return false;
}
} else {
iccProfile->resize(0);
}
iccProfile->resize(icc_size);
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(
dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
iccProfile->data(), iccProfile->size())) {
return false;
}
} else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
size_t buffer_size;
if (JXL_DEC_SUCCESS !=
Expand Down
2 changes: 1 addition & 1 deletion Sources/jxlc/include/module.modulemap
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module jxlc {
umbrella header "../JxlInternalCoder.h"
header "../JxlInternalCoder.h"
header "../JxlConstruction.h"
header "../JxlJpegLiEncoder.h"
export *
Expand Down

0 comments on commit 1389d27

Please sign in to comment.