Skip to content

Commit ca43cef

Browse files
committed
fix scaler
1 parent c839a06 commit ca43cef

File tree

7 files changed

+70
-20
lines changed

7 files changed

+70
-20
lines changed

.swiftpm/xcode/xcuserdata/radzivonbartoshyk.xcuserdatad/xcschemes/xcschememanagement.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>JxlCoder.xcscheme_^#shared#^_</key>
1818
<dict>
1919
<key>orderHint</key>
20-
<integer>1</integer>
20+
<integer>0</integer>
2121
</dict>
2222
<key>jxlcoder.xcscheme_^#shared#^_</key>
2323
<dict>

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.1.5'
3+
s.version = '1.1.6'
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/JxlCoder/JXLCoder.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,33 @@ public class JXLCoder {
3434
/***
3535
- Returns: Decoded JXL image if this is the valid one
3636
**/
37-
public static func decode(srcStream: InputStream, sampleSize: CGSize = .zero) throws -> JXLPlatformImage {
38-
return try shared.decode(srcStream, sampleSize: sampleSize)
37+
public static func decode(srcStream: InputStream,
38+
sampleSize: CGSize = .zero,
39+
pixelFormat: JXLPreferredPixelFormat = .optimal) throws -> JXLPlatformImage {
40+
return try shared.decode(srcStream, sampleSize: sampleSize, pixelFormat: pixelFormat)
3941
}
4042

4143
/***
4244
- Returns: Decoded JXL image if this is the valid one
4345
**/
44-
public static func decode(url: URL, sampleSize: CGSize = .zero) throws -> JXLPlatformImage {
46+
public static func decode(url: URL,
47+
sampleSize: CGSize = .zero,
48+
pixelFormat: JXLPreferredPixelFormat = .optimal) throws -> JXLPlatformImage {
4549
guard let srcStream = InputStream(url: url) else {
4650
throw NSError(domain: "JXLCoder", code: 500,
4751
userInfo: [NSLocalizedDescriptionKey: "JXLCoder cannot open provided URL"])
4852
}
49-
return try shared.decode(srcStream, sampleSize: sampleSize)
53+
return try shared.decode(srcStream, sampleSize: sampleSize, pixelFormat: pixelFormat)
5054
}
5155

5256
/***
5357
- Returns: Decoded JXL image if this is the valid one
5458
**/
55-
public static func decode(data: Data, sampleSize: CGSize = .zero) throws -> JXLPlatformImage {
59+
public static func decode(data: Data,
60+
sampleSize: CGSize = .zero,
61+
pixelFormat: JXLPreferredPixelFormat = .optimal) throws -> JXLPlatformImage {
5662
let srcStream = InputStream(data: data)
57-
return try shared.decode(srcStream, sampleSize: sampleSize)
63+
return try shared.decode(srcStream, sampleSize: sampleSize, pixelFormat: pixelFormat)
5864
}
5965

6066
/***

Sources/jxlc/JxlInternalCoder.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,18 @@ typedef NS_ENUM(NSInteger, JXLCompressionOption) {
3939
kLossy NS_SWIFT_NAME(lossy)
4040
};
4141

42+
typedef NS_ENUM(NSInteger, JXLPreferredPixelFormat) {
43+
kOptimal NS_SWIFT_NAME(optimal),
44+
kUniform8 NS_SWIFT_NAME(r8),
45+
kFloat16 NS_SWIFT_NAME(float16),
46+
};
47+
4248
@interface JxlInternalCoder: NSObject
43-
- (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream sampleSize:(CGSize)sampleSize error:(NSError *_Nullable * _Nullable)error;
44-
- (CGSize)getSize:(nonnull NSInputStream *)inputStream error:(NSError *_Nullable * _Nullable)error;
49+
- (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
50+
sampleSize:(CGSize)sampleSize
51+
pixelFormat:(JXLPreferredPixelFormat)pixelFormat
52+
error:(NSError *_Nullable * _Nullable)error;
53+
- (CGSize)getSize:(nonnull NSInputStream *)inputStream error:(NSError *_Nullable * _Nullable)error;
4554
- (nullable NSData *)encode:(nonnull JXLSystemImage *)platformImage
4655
colorSpace:(JXLColorSpace)colorSpace
4756
compressionOption:(JXLCompressionOption)compressionOption

Sources/jxlc/JxlInternalCoder.mm

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ - (nullable NSData *)encode:(nonnull JXLSystemImage *)platformImage
138138
}
139139
}
140140

141-
- (CGSize)getSize:(nonnull NSInputStream *)inputStream error:(NSError *_Nullable * _Nullable)error {
141+
- (CGSize)getSize:(nonnull NSInputStream *)inputStream error:(NSError *_Nullable * _Nullable)error {
142142
try {
143143
int buffer_length = 30196;
144144
std::vector<uint8_t> buffer;
@@ -188,8 +188,9 @@ - (CGSize)getSize:(nonnull NSInputStream *)inputStream error:(NSError *_Nullabl
188188
}
189189

190190
- (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
191-
sampleSize:(CGSize)sampleSize
192-
error:(NSError *_Nullable * _Nullable)error {
191+
sampleSize:(CGSize)sampleSize
192+
pixelFormat:(JXLPreferredPixelFormat)pixelFormat
193+
error:(NSError *_Nullable * _Nullable)error {
193194
try {
194195
int buffer_length = 30196;
195196
std::vector<uint8_t> buffer;
@@ -234,9 +235,23 @@ - (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
234235
std::vector<uint8_t> outputData;
235236
int components;
236237
JxlExposedOrientation jxlExposedOrientation = Identity;
238+
JxlDecodingPixelFormat pixelFormat;
239+
switch (pixelFormat) {
240+
case optimal:
241+
pixelFormat = optimal;
242+
break;
243+
case r8:
244+
pixelFormat = r8;
245+
break;
246+
case float16:
247+
pixelFormat = float16;
248+
break;
249+
}
237250
auto decoded = DecodeJpegXlOneShot(imageData.data(), imageData.size(),
238251
&outputData, &xSize, &ySize,
239-
&iccProfile, &depth, &components, &useFloats, &jxlExposedOrientation);
252+
&iccProfile, &depth, &components,
253+
&useFloats, &jxlExposedOrientation,
254+
pixelFormat);
240255
if (!decoded) {
241256
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: @"Failed to decode JXL image" }];
242257
return nil;
@@ -252,8 +267,8 @@ - (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
252267

253268
if (sampleSize.width > 0 && sampleSize.height > 0) {
254269
auto scaleResult = [RgbaScaler scaleData:outputData width:(int)xSize height:(int)ySize
255-
newWidth:(int)sampleSize.width newHeight:(int)sampleSize.height
256-
components:components pixelFormat:useFloats ? kF16 : kU8];
270+
newWidth:(int)sampleSize.width newHeight:(int)sampleSize.height
271+
components:components pixelFormat:useFloats ? kF16 : kU8];
257272
if (!scaleResult) {
258273
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: @"Rescale image has failed" }];
259274
return nil;

Sources/jxlc/JxlWorker.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
4040
int* depth,
4141
int* components,
4242
bool* useFloats,
43-
JxlExposedOrientation* exposedOrientation) {
43+
JxlExposedOrientation* exposedOrientation,
44+
JxlDecodingPixelFormat pixelFormat) {
4445
// Multi-threaded parallel runner.
4546
auto runner = JxlResizableParallelRunnerMake(nullptr);
4647

@@ -61,7 +62,16 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
6162
JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE);
6263

6364
JxlBasicInfo info;
64-
JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
65+
JxlPixelFormat format;
66+
if (pixelFormat == optimal) {
67+
format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
68+
} else {
69+
if (pixelFormat == float16) {
70+
format = {4, JXL_TYPE_FLOAT16, JXL_NATIVE_ENDIAN, 0};
71+
} else if (pixelFormat == r8) {
72+
format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
73+
}
74+
}
6575

6676
JxlDecoderSetInput(dec.get(), jxl, size);
6777
JxlDecoderCloseInput(dec.get());
@@ -94,11 +104,14 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
94104
}
95105
*components = baseComponents;
96106
*exposedOrientation = static_cast<JxlExposedOrientation>(info.orientation);
97-
if (bitDepth > 8) {
107+
if (bitDepth > 8 && pixelFormat == optimal) {
98108
*useFloats = true;
99109
hdrImage = true;
100110
format = { static_cast<uint32_t>(baseComponents), JXL_TYPE_FLOAT16, JXL_NATIVE_ENDIAN, 0 };
101111
} else {
112+
if (pixelFormat == r8) {
113+
*depth = 8;
114+
}
102115
format.num_channels = baseComponents;
103116
*useFloats = false;
104117
}

Sources/jxlc/JxlWorker.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ enum JxlCompressionOption {
4242
loosy = 2
4343
};
4444

45+
enum JxlDecodingPixelFormat {
46+
optimal = 1,
47+
r8 = 2,
48+
float16 = 3
49+
};
50+
4551
enum JxlExposedOrientation {
4652
Identity = 1,
4753
FlipHorizontal = 2,
@@ -60,7 +66,8 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
6066
int* depth,
6167
int* components,
6268
bool* useFloats,
63-
JxlExposedOrientation* exposedOrientation);
69+
JxlExposedOrientation* exposedOrientation,
70+
JxlDecodingPixelFormat pixelFormat);
6471
bool DecodeBasicInfo(const uint8_t *jxl, size_t size, size_t *xsize, size_t *ysize);
6572
bool EncodeJxlOneshot(const std::vector<uint8_t> &pixels, const uint32_t xsize,
6673
const uint32_t ysize, std::vector<uint8_t> *compressed,

0 commit comments

Comments
 (0)