Skip to content

Commit 5e694b0

Browse files
committed
fixed orientations and alpha
1 parent eca3f01 commit 5e694b0

File tree

7 files changed

+104
-13
lines changed

7 files changed

+104
-13
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>2</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.0.7'
3+
s.version = '1.0.9'
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/JxlInternalCoder.mm

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#import "JxlWorker.hpp"
3030
#import <Accelerate/Accelerate.h>
3131
#import "RgbRgbaConverter.h"
32+
#import "RgbScaler.h"
3233

3334
static void JXLCGData16ProviderReleaseDataCallback(void *info, const void *data, size_t size) {
3435
auto dataWrapper = static_cast<JXLDataWrapper<uint16_t>*>(info);
@@ -64,8 +65,8 @@ - (nullable NSData *)encode:(nonnull JXLSystemImage *)platformImage
6465
return nil;
6566
}
6667

67-
jxl_colorspace jColorspace;
68-
jxl_compression_option jCompressionOption;
68+
JxlPixelType jColorspace;
69+
JxlCompressionOption jCompressionOption;
6970

7071
switch (colorSpace) {
7172
case kRGB:
@@ -203,14 +204,16 @@ - (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream error:(
203204
int depth;
204205
std::vector<uint8_t> outputData;
205206
int components;
207+
JxlExposedOrientation jxlExposedOrientation = Identity;
206208
auto decoded = DecodeJpegXlOneShot(imageData.data(), imageData.size(),
207209
&outputData, &xSize, &ySize,
208-
&iccProfile, &depth, &components, &useFloats);
210+
&iccProfile, &depth, &components, &useFloats, &jxlExposedOrientation);
209211
if (!decoded) {
210212
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: @"Failed to decode JXL image" }];
211213
return nil;
212214
}
213215

216+
214217
CGColorSpaceRef colorSpace;
215218
if (iccProfile.size() > 0) {
216219
CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, iccProfile.data(), iccProfile.size());

Sources/jxlc/JxlWorker.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
3939
std::vector<uint8_t> *icc_profile,
4040
int* depth,
4141
int* components,
42-
bool* useFloats) {
42+
bool* useFloats,
43+
JxlExposedOrientation* exposedOrientation) {
4344
// Multi-threaded parallel runner.
4445
auto runner = JxlResizableParallelRunnerMake(nullptr);
4546

@@ -57,8 +58,11 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
5758
return false;
5859
}
5960

61+
JxlDecoderSetKeepOrientation(dec.get(), JXL_TRUE);
62+
JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE);
63+
6064
JxlBasicInfo info;
61-
JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
65+
JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_LITTLE_ENDIAN, 0};
6266

6367
JxlDecoderSetInput(dec.get(), jxl, size);
6468
JxlDecoderCloseInput(dec.get());
@@ -90,10 +94,11 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
9094
baseComponents = 4;
9195
}
9296
*components = baseComponents;
97+
*exposedOrientation = static_cast<JxlExposedOrientation>(info.orientation);
9398
if (bitDepth > 8) {
9499
*useFloats = true;
95100
hdrImage = true;
96-
format = { static_cast<uint32_t>(baseComponents), JXL_TYPE_FLOAT16, JXL_NATIVE_ENDIAN, 0 };
101+
format = { static_cast<uint32_t>(baseComponents), JXL_TYPE_FLOAT16, JXL_LITTLE_ENDIAN, 0 };
97102
} else {
98103
format.num_channels = baseComponents;
99104
*useFloats = false;
@@ -212,7 +217,7 @@ bool DecodeBasicInfo(const uint8_t *jxl, size_t size, size_t *xsize, size_t *ysi
212217
*/
213218
bool EncodeJxlOneshot(const std::vector<uint8_t> &pixels, const uint32_t xsize,
214219
const uint32_t ysize, std::vector<uint8_t> *compressed,
215-
jxl_colorspace colorspace, jxl_compression_option compression_option,
220+
JxlPixelType colorspace, JxlCompressionOption compression_option,
216221
float compression_distance) {
217222
auto enc = JxlEncoderMake(/*memory_manager=*/nullptr);
218223
auto runner = JxlThreadParallelRunnerMake(

Sources/jxlc/JxlWorker.hpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,39 @@
3232
#endif
3333
#ifdef __cplusplus
3434

35-
enum jxl_colorspace {
35+
enum JxlPixelType {
3636
rgb = 1,
3737
rgba = 2
3838
};
3939

40-
enum jxl_compression_option {
40+
enum JxlCompressionOption {
4141
loseless = 1,
4242
loosy = 2
4343
};
4444

45+
enum JxlExposedOrientation {
46+
Identity = 1,
47+
FlipHorizontal = 2,
48+
Rotate180 = 3,
49+
FlipVertical = 4,
50+
OrientTranspose = 5,
51+
Rotate90CW = 6,
52+
AntiTranspose = 7,
53+
Rotate90CCW = 8
54+
};
55+
4556
bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
4657
std::vector<uint8_t> *pixels, size_t *xsize,
4758
size_t *ysize,
4859
std::vector<uint8_t> *icc_profile,
4960
int* depth,
5061
int* components,
51-
bool* useFloats);
62+
bool* useFloats,
63+
JxlExposedOrientation* exposedOrientation);
5264
bool DecodeBasicInfo(const uint8_t *jxl, size_t size, size_t *xsize, size_t *ysize);
5365
bool EncodeJxlOneshot(const std::vector<uint8_t> &pixels, const uint32_t xsize,
5466
const uint32_t ysize, std::vector<uint8_t> *compressed,
55-
jxl_colorspace colorspace, jxl_compression_option compression_option,
67+
JxlPixelType colorspace, JxlCompressionOption compression_option,
5668
float compression_distance);
5769

5870
template <typename DataType>

Sources/jxlc/RgbScaler.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//
2+
// RgbScaler.h
3+
//
4+
//
5+
// Created by Radzivon Bartoshyk on 25/09/2023.
6+
//
7+
8+
#ifndef RgbScaler_h
9+
#define RgbScaler_h
10+
11+
#import <vector>
12+
13+
@interface RgbScaler : NSObject
14+
/**
15+
* Converts unsigned uint8_t RGB to RGBA in uint8_t. Depth is always considered as 8 bit
16+
* @author Radzivon Bartoshyk
17+
*
18+
* @param src Source buffer
19+
* @param dst Destination buffer
20+
* @param width width of of the image
21+
* @param height width of of the image
22+
* @param components components count, supported 3 or 4
23+
* @return true if conversion were successfull
24+
*/
25+
+(bool)scaleRGBU8:(uint8_t*)src dst:(uint8_t*)dst width:(int)width height:(int)height components:(int)components;
26+
@end
27+
#endif /* Header_h */

Sources/jxlc/RgbScaler.mm

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// RgbScaler.m
3+
//
4+
//
5+
// Created by Radzivon Bartoshyk on 25/09/2023.
6+
//
7+
8+
#import <Foundation/Foundation.h>
9+
#import "RgbScaler.h"
10+
#import "Accelerate/Accelerate.h"
11+
12+
@implementation RgbScaler
13+
14+
+(bool)scaleRGBU8:(uint8_t*)src dst:(uint8_t*)dst width:(int)width height:(int)height components:(int)components {
15+
if (components != 3 || components != 4) {
16+
return false;
17+
}
18+
uint16_t whiteColor = (uint16_t)(powf(2.0f, (float)8) - 1);
19+
vImage_Buffer srcBuffer = {
20+
.data = (void*)src,
21+
.width = static_cast<vImagePixelCount>(width),
22+
.height = static_cast<vImagePixelCount>(height),
23+
.rowBytes = width * components * sizeof(uint8_t)
24+
};
25+
26+
vImage_Buffer dstBuffer = {
27+
.data = dst,
28+
.width = static_cast<vImagePixelCount>(width),
29+
.height = static_cast<vImagePixelCount>(height),
30+
.rowBytes = width * components * sizeof(uint8_t)
31+
};
32+
// if components == 3 {
33+
// vImageScale_ARGB8888(<#const vImage_Buffer *src#>, <#const vImage_Buffer *dest#>, <#void *tempBuffer#>, <#vImage_Flags flags#>)
34+
// }
35+
// vImage_Error vEerror = vImageConvert_RGB888toRGBA8888(&srcBuffer, NULL, whiteColor, &dstBuffer, false, kvImageNoFlags);
36+
// if (vEerror != kvImageNoError) {
37+
// return false;
38+
// }
39+
return true;
40+
41+
return true;
42+
}
43+
44+
@end

0 commit comments

Comments
 (0)