Skip to content

Commit 1389d27

Browse files
committed
added gray decoding
1 parent e5406e5 commit 1389d27

File tree

7 files changed

+46
-33
lines changed

7 files changed

+46
-33
lines changed

JxlCoder.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Pod::Spec.new do |s|
22
s.name = 'JxlCoder'
3-
s.version = '1.7.1'
3+
s.version = '1.7.2'
44
s.summary = 'JXL coder for iOS and MacOS'
55
s.description = 'Provides support for JXL files in iOS and MacOS'
66
s.homepage = 'https://github.com/awxkee/jxl-coder-swift'

Sources/jxlc/CJpegXLAnimatedDecoder.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ @implementation CJpegXLAnimatedDecoder {
4646
}
4747

4848
-(nullable id)initWith:(nonnull NSData*)data error:(NSError * _Nullable *_Nullable)error {
49+
dec = nullptr;
4950
try {
5051
const uint8_t* ptr = reinterpret_cast<const uint8_t*>([data bytes]);
5152
mSrc.resize([data length]);

Sources/jxlc/CJpegXLAnimatedEncoder.mm

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ -(nullable id)initWith:(int)width height:(int)height
5353
quality:(int)quality
5454
decodingSpeed:(JXLEncoderDecodingSpeed)decodingSpeed
5555
error:(NSError * _Nullable *_Nullable)error {
56+
enc = nullptr;
5657
JxlPixelType jColorspace;
5758
JxlCompressionOption jCompressionOption;
5859

@@ -106,7 +107,8 @@ -(nullable void*)addFrame:(nonnull JXLSystemImage *)platformImage duration:(int)
106107
if (enc->getJxlPixelType() == rgb) {
107108
auto resizedVector = [RgbRgbaConverter convertRGBAtoRGB:buf width:width height:height];
108109
if (resizedVector.size() == 1) {
109-
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: @"Cannot convert RGBA pixels to RGB" }];
110+
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500
111+
userInfo:@{ NSLocalizedDescriptionKey: @"Cannot convert RGBA pixels to RGB" }];
110112
return nil;
111113
}
112114
buf = resizedVector;

Sources/jxlc/JxlAnimatedDecoder.hpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ class JxlAnimatedDecoder {
7676
std::string str = "Cannot create decoder";
7777
throw AnimatedDecoderError(str);
7878
}
79+
80+
if (JXL_DEC_SUCCESS != JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE)) {
81+
return false;
82+
}
83+
7984
if (JXL_DEC_SUCCESS !=
8085
JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_BASIC_INFO |
8186
JXL_DEC_COLOR_ENCODING |
@@ -146,18 +151,18 @@ class JxlAnimatedDecoder {
146151
}
147152
} else if (status == JXL_DEC_COLOR_ENCODING) {
148153
size_t iccSize;
149-
if (JXL_DEC_SUCCESS !=
150-
JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
151-
&iccSize)) {
152-
std::string str = "Cannot retreive color info";
153-
throw AnimatedDecoderError(str);
154+
if (JXL_DEC_SUCCESS ==
155+
JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA, &iccSize)) {
156+
iccProfile.resize(iccSize);
157+
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
158+
iccProfile.data(), iccProfile.size())) {
159+
std::string str = "Cannot retreive color icc profile";
160+
throw AnimatedDecoderError(str);
161+
}
162+
} else {
163+
iccProfile.resize(0);
154164
}
155165
iccProfile.resize(iccSize);
156-
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
157-
iccProfile.data(), iccProfile.size())) {
158-
std::string str = "Cannot retreive color icc profile";
159-
throw AnimatedDecoderError(str);
160-
}
161166
} else if (status == JXL_DEC_SUCCESS) {
162167
JxlDecoderRewind(dec.get());
163168
JxlDecoderSubscribeEvents(dec.get(), JXL_DEC_FRAME | JXL_DEC_FULL_IMAGE);

Sources/jxlc/JxlInternalCoder.mm

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,11 +327,19 @@ - (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
327327
colorSpace = CGColorSpaceCreateWithICCData(iccData);
328328
CFRelease(iccData);
329329
} else {
330-
colorSpace = CGColorSpaceCreateDeviceRGB();
330+
if (components > 1) {
331+
colorSpace = CGColorSpaceCreateDeviceRGB();
332+
} else {
333+
colorSpace = CGColorSpaceCreateDeviceGray();
334+
}
331335
}
332336

333337
if (!colorSpace) {
334-
colorSpace = CGColorSpaceCreateDeviceRGB();
338+
if (components > 1) {
339+
colorSpace = CGColorSpaceCreateDeviceRGB();
340+
} else {
341+
colorSpace = CGColorSpaceCreateDeviceGray();
342+
}
335343
}
336344

337345
int stride = components*(int)xSize * (int)(useFloats ? sizeof(uint16_t) : sizeof(uint8_t));

Sources/jxlc/JxlWorker.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
5858
runner.get())) {
5959
return false;
6060
}
61-
62-
JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE);
61+
62+
if (JXL_DEC_SUCCESS != JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE)) {
63+
return false;
64+
}
6365

6466
JxlBasicInfo info;
6567
JxlPixelFormat format;
@@ -95,10 +97,6 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
9597
bitDepth = info.bits_per_sample;
9698
*depth = info.bits_per_sample;
9799
int baseComponents = info.num_color_channels;
98-
// Will not support mono
99-
if (baseComponents < 3) {
100-
baseComponents = 3;
101-
}
102100
if (info.num_extra_channels > 0) {
103101
baseComponents = 4;
104102
}
@@ -129,19 +127,18 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
129127
// if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsEncodedProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA, &colorEncoding)) {
130128
// return false;
131129
// }
132-
133-
size_t icc_size;
134-
if (JXL_DEC_SUCCESS !=
135-
JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
136-
&icc_size)) {
137-
return false;
130+
131+
size_t iccSize;
132+
if (JXL_DEC_SUCCESS ==
133+
JxlDecoderGetICCProfileSize(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA, &iccSize)) {
134+
iccProfile->resize(iccSize);
135+
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
136+
iccProfile->data(), iccProfile->size())) {
137+
return false;
138+
}
139+
} else {
140+
iccProfile->resize(0);
138141
}
139-
iccProfile->resize(icc_size);
140-
if (JXL_DEC_SUCCESS != JxlDecoderGetColorAsICCProfile(
141-
dec.get(), JXL_COLOR_PROFILE_TARGET_DATA,
142-
iccProfile->data(), iccProfile->size())) {
143-
return false;
144-
}
145142
} else if (status == JXL_DEC_NEED_IMAGE_OUT_BUFFER) {
146143
size_t buffer_size;
147144
if (JXL_DEC_SUCCESS !=

Sources/jxlc/include/module.modulemap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module jxlc {
2-
umbrella header "../JxlInternalCoder.h"
2+
header "../JxlInternalCoder.h"
33
header "../JxlConstruction.h"
44
header "../JxlJpegLiEncoder.h"
55
export *

0 commit comments

Comments
 (0)