Skip to content

Commit e31d95f

Browse files
committed
fix byte ordering
1 parent e8fd6b5 commit e31d95f

File tree

5 files changed

+11
-46
lines changed

5 files changed

+11
-46
lines changed

JxlCoder.podspec

+1-1
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.3.1'
3+
s.version = '1.3.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'

JxlNukePlugin/JxlNukePlugin.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import JxlCoder
1313

1414
public final class JxlNukePlugin: Nuke.ImageDecoding {
1515
public func decode(_ data: Data) throws -> Nuke.ImageContainer {
16-
guard try JXLCoder.isJXL(data: data) else { throw JXLNukePluginDecodeError() }
16+
guard JXLCoder.isJXL(data: data) else { throw JXLNukePluginDecodeError() }
1717
let image = try JXLCoder.decode(data: data)
1818
return ImageContainer(image: image)
1919
}
@@ -46,7 +46,7 @@ extension JxlNukePlugin {
4646
}
4747

4848
public static func enable(context: Nuke.ImageDecodingContext) -> Nuke.ImageDecoding? {
49-
return try? JXLCoder.isJXL(data: context.data) ? JxlNukePlugin() : nil
49+
return JXLCoder.isJXL(data: context.data) ? JxlNukePlugin() : nil
5050
}
5151

5252
}

Sources/JxlCoder/JXLCoder.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public class JXLCoder {
6767
public static func decode(url: URL,
6868
rescale: CGSize = .zero,
6969
pixelFormat: JXLPreferredPixelFormat = .optimal,
70-
sampler: JxlSampler = .hann) throws -> JXLPlatformImage {
70+
sampler: JxlSampler = .lanczos) throws -> JXLPlatformImage {
7171
guard let srcStream = InputStream(url: url) else {
7272
throw NSError(domain: "JXLCoder", code: 500,
7373
userInfo: [NSLocalizedDescriptionKey: "JXLCoder cannot open provided URL"])
@@ -82,7 +82,7 @@ public class JXLCoder {
8282
public static func decode(data: Data,
8383
rescale: CGSize = .zero,
8484
pixelFormat: JXLPreferredPixelFormat = .optimal,
85-
sampler: JxlSampler = .hann) throws -> JXLPlatformImage {
85+
sampler: JxlSampler = .lanczos) throws -> JXLPlatformImage {
8686
let srcStream = InputStream(data: data)
8787
return try shared.decode(srcStream, rescale: rescale, pixelFormat: pixelFormat, sampler: sampler)
8888
}

Sources/jxlc/JXLSystemImage.mm

+3-38
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ -(bool)unpremultiply:(nonnull unsigned char*)data width:(NSInteger)width height:
5454
#if TARGET_OS_OSX
5555

5656
-(nullable CGImageRef)makeCGImage {
57-
NSRect rect = NSMakeRect(0, 0, self.size.width, self.size.height);
58-
CGImageRef imageRef = [self CGImageForProposedRect: &rect context:nil hints:nil];
57+
CGImageRef imageRef = [self CGImageForProposedRect:nil context:nil hints:nil];
5958
return imageRef;
6059
}
6160

@@ -70,13 +69,12 @@ - (nullable uint8_t*)jxlRGBAPixels:(nonnull size_t*)bufferSize width:(nonnull in
7069
*ySize = (int)height;
7170

7271
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
73-
CGBitmapInfo bitmapInfo = (int)kCGImageAlphaPremultipliedLast | (int)kCGBitmapByteOrder32Big;
72+
CGBitmapInfo bitmapInfo = (int)kCGImageAlphaPremultipliedLast | (int)kCGImageByteOrderDefault;
7473

7574
CGContextRef targetContext = CGBitmapContextCreate(targetMemory, width, height, 8, stride, colorSpace, bitmapInfo);
7675

7776
[NSGraphicsContext saveGraphicsState];
7877
[NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithCGContext:targetContext flipped:FALSE]];
79-
CGColorSpaceRelease(colorSpace);
8078

8179
[self drawInRect: NSMakeRect(0, 0, width, height)
8280
fromRect: NSZeroRect
@@ -86,6 +84,7 @@ - (nullable uint8_t*)jxlRGBAPixels:(nonnull size_t*)bufferSize width:(nonnull in
8684
[NSGraphicsContext restoreGraphicsState];
8785

8886
CGContextRelease(targetContext);
87+
CGColorSpaceRelease(colorSpace);
8988

9089
if (![self unpremultiply:targetMemory width:width height:height]) {
9190
free(targetMemory);
@@ -94,40 +93,6 @@ - (nullable uint8_t*)jxlRGBAPixels:(nonnull size_t*)bufferSize width:(nonnull in
9493

9594
return targetMemory;
9695
}
97-
98-
- (bool)jxlRGBAVPixels:(std::vector<uint8_t>&)buf width:(nonnull int*)xSize height:(nonnull int*)ySize {
99-
CGImageRef imageRef = [self makeCGImage];
100-
NSUInteger width = CGImageGetWidth(imageRef);
101-
NSUInteger height = CGImageGetHeight(imageRef);
102-
int stride = (int)4 * (int)width * sizeof(uint8_t);
103-
buf.resize(stride * height);
104-
*xSize = (int)width;
105-
*ySize = (int)height;
106-
107-
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
108-
CGBitmapInfo bitmapInfo = (int)kCGImageAlphaPremultipliedLast | (int)kCGBitmapByteOrder32Big;
109-
110-
CGContextRef targetContext = CGBitmapContextCreate(buf.data(), width, height, 8, stride, colorSpace, bitmapInfo);
111-
112-
[NSGraphicsContext saveGraphicsState];
113-
[NSGraphicsContext setCurrentContext: [NSGraphicsContext graphicsContextWithCGContext:targetContext flipped:FALSE]];
114-
CGColorSpaceRelease(colorSpace);
115-
116-
[self drawInRect: NSMakeRect(0, 0, width, height)
117-
fromRect: NSZeroRect
118-
operation: NSCompositingOperationCopy
119-
fraction: 1.0];
120-
121-
[NSGraphicsContext restoreGraphicsState];
122-
123-
CGContextRelease(targetContext);
124-
125-
if (![self unpremultiply:buf.data() width:width height:height]) {
126-
return false;
127-
}
128-
129-
return true;
130-
}
13196
#else
13297
- (nullable uint8_t*)jxlRGBAPixels:(nonnull size_t*)bufferSize width:(nonnull int*)xSize height:(nonnull int*)ySize {
13398
CGImageRef imageRef = [self CGImage];

Sources/jxlc/JxlWorker.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,13 @@ bool EncodeJxlOneshot(const std::vector<uint8_t> &pixels, const uint32_t xsize,
243243
return false;
244244
}
245245

246-
JxlPixelFormat pixel_format = {3, JXL_TYPE_UINT8, JXL_BIG_ENDIAN, 0};
246+
JxlPixelFormat pixel_format = {3, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
247247
switch (colorspace) {
248248
case rgb:
249-
pixel_format = {3, JXL_TYPE_UINT8, JXL_BIG_ENDIAN, 0};
249+
pixel_format = {3, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
250250
break;
251251
case rgba:
252-
pixel_format = {4, JXL_TYPE_UINT8, JXL_BIG_ENDIAN, 0};
252+
pixel_format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
253253
break;
254254
}
255255

0 commit comments

Comments
 (0)