Skip to content

Commit

Permalink
fixed orientations and alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Sep 26, 2023
1 parent eca3f01 commit 5e694b0
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>JxlCoder.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
<integer>2</integer>
</dict>
<key>jxlcoder.xcscheme_^#shared#^_</key>
<dict>
Expand Down
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.0.7'
s.version = '1.0.9'
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
9 changes: 6 additions & 3 deletions Sources/jxlc/JxlInternalCoder.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#import "JxlWorker.hpp"
#import <Accelerate/Accelerate.h>
#import "RgbRgbaConverter.h"
#import "RgbScaler.h"

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

jxl_colorspace jColorspace;
jxl_compression_option jCompressionOption;
JxlPixelType jColorspace;
JxlCompressionOption jCompressionOption;

switch (colorSpace) {
case kRGB:
Expand Down Expand Up @@ -203,14 +204,16 @@ - (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream error:(
int depth;
std::vector<uint8_t> outputData;
int components;
JxlExposedOrientation jxlExposedOrientation = Identity;
auto decoded = DecodeJpegXlOneShot(imageData.data(), imageData.size(),
&outputData, &xSize, &ySize,
&iccProfile, &depth, &components, &useFloats);
&iccProfile, &depth, &components, &useFloats, &jxlExposedOrientation);
if (!decoded) {
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: @"Failed to decode JXL image" }];
return nil;
}


CGColorSpaceRef colorSpace;
if (iccProfile.size() > 0) {
CFDataRef iccData = CFDataCreate(kCFAllocatorDefault, iccProfile.data(), iccProfile.size());
Expand Down
13 changes: 9 additions & 4 deletions Sources/jxlc/JxlWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
std::vector<uint8_t> *icc_profile,
int* depth,
int* components,
bool* useFloats) {
bool* useFloats,
JxlExposedOrientation* exposedOrientation) {
// Multi-threaded parallel runner.
auto runner = JxlResizableParallelRunnerMake(nullptr);

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

JxlDecoderSetKeepOrientation(dec.get(), JXL_TRUE);
JxlDecoderSetUnpremultiplyAlpha(dec.get(), JXL_TRUE);

JxlBasicInfo info;
JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_NATIVE_ENDIAN, 0};
JxlPixelFormat format = {4, JXL_TYPE_UINT8, JXL_LITTLE_ENDIAN, 0};

JxlDecoderSetInput(dec.get(), jxl, size);
JxlDecoderCloseInput(dec.get());
Expand Down Expand Up @@ -90,10 +94,11 @@ bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
baseComponents = 4;
}
*components = baseComponents;
*exposedOrientation = static_cast<JxlExposedOrientation>(info.orientation);
if (bitDepth > 8) {
*useFloats = true;
hdrImage = true;
format = { static_cast<uint32_t>(baseComponents), JXL_TYPE_FLOAT16, JXL_NATIVE_ENDIAN, 0 };
format = { static_cast<uint32_t>(baseComponents), JXL_TYPE_FLOAT16, JXL_LITTLE_ENDIAN, 0 };
} else {
format.num_channels = baseComponents;
*useFloats = false;
Expand Down Expand Up @@ -212,7 +217,7 @@ bool DecodeBasicInfo(const uint8_t *jxl, size_t size, size_t *xsize, size_t *ysi
*/
bool EncodeJxlOneshot(const std::vector<uint8_t> &pixels, const uint32_t xsize,
const uint32_t ysize, std::vector<uint8_t> *compressed,
jxl_colorspace colorspace, jxl_compression_option compression_option,
JxlPixelType colorspace, JxlCompressionOption compression_option,
float compression_distance) {
auto enc = JxlEncoderMake(/*memory_manager=*/nullptr);
auto runner = JxlThreadParallelRunnerMake(
Expand Down
20 changes: 16 additions & 4 deletions Sources/jxlc/JxlWorker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,39 @@
#endif
#ifdef __cplusplus

enum jxl_colorspace {
enum JxlPixelType {
rgb = 1,
rgba = 2
};

enum jxl_compression_option {
enum JxlCompressionOption {
loseless = 1,
loosy = 2
};

enum JxlExposedOrientation {
Identity = 1,
FlipHorizontal = 2,
Rotate180 = 3,
FlipVertical = 4,
OrientTranspose = 5,
Rotate90CW = 6,
AntiTranspose = 7,
Rotate90CCW = 8
};

bool DecodeJpegXlOneShot(const uint8_t *jxl, size_t size,
std::vector<uint8_t> *pixels, size_t *xsize,
size_t *ysize,
std::vector<uint8_t> *icc_profile,
int* depth,
int* components,
bool* useFloats);
bool* useFloats,
JxlExposedOrientation* exposedOrientation);
bool DecodeBasicInfo(const uint8_t *jxl, size_t size, size_t *xsize, size_t *ysize);
bool EncodeJxlOneshot(const std::vector<uint8_t> &pixels, const uint32_t xsize,
const uint32_t ysize, std::vector<uint8_t> *compressed,
jxl_colorspace colorspace, jxl_compression_option compression_option,
JxlPixelType colorspace, JxlCompressionOption compression_option,
float compression_distance);

template <typename DataType>
Expand Down
27 changes: 27 additions & 0 deletions Sources/jxlc/RgbScaler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// RgbScaler.h
//
//
// Created by Radzivon Bartoshyk on 25/09/2023.
//

#ifndef RgbScaler_h
#define RgbScaler_h

#import <vector>

@interface RgbScaler : NSObject
/**
* Converts unsigned uint8_t RGB to RGBA in uint8_t. Depth is always considered as 8 bit
* @author Radzivon Bartoshyk
*
* @param src Source buffer
* @param dst Destination buffer
* @param width width of of the image
* @param height width of of the image
* @param components components count, supported 3 or 4
* @return true if conversion were successfull
*/
+(bool)scaleRGBU8:(uint8_t*)src dst:(uint8_t*)dst width:(int)width height:(int)height components:(int)components;
@end
#endif /* Header_h */
44 changes: 44 additions & 0 deletions Sources/jxlc/RgbScaler.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// RgbScaler.m
//
//
// Created by Radzivon Bartoshyk on 25/09/2023.
//

#import <Foundation/Foundation.h>
#import "RgbScaler.h"
#import "Accelerate/Accelerate.h"

@implementation RgbScaler

+(bool)scaleRGBU8:(uint8_t*)src dst:(uint8_t*)dst width:(int)width height:(int)height components:(int)components {
if (components != 3 || components != 4) {
return false;
}
uint16_t whiteColor = (uint16_t)(powf(2.0f, (float)8) - 1);
vImage_Buffer srcBuffer = {
.data = (void*)src,
.width = static_cast<vImagePixelCount>(width),
.height = static_cast<vImagePixelCount>(height),
.rowBytes = width * components * sizeof(uint8_t)
};

vImage_Buffer dstBuffer = {
.data = dst,
.width = static_cast<vImagePixelCount>(width),
.height = static_cast<vImagePixelCount>(height),
.rowBytes = width * components * sizeof(uint8_t)
};
// if components == 3 {
// vImageScale_ARGB8888(<#const vImage_Buffer *src#>, <#const vImage_Buffer *dest#>, <#void *tempBuffer#>, <#vImage_Flags flags#>)
// }
// vImage_Error vEerror = vImageConvert_RGB888toRGBA8888(&srcBuffer, NULL, whiteColor, &dstBuffer, false, kvImageNoFlags);
// if (vEerror != kvImageNoError) {
// return false;
// }
return true;

return true;
}

@end

0 comments on commit 5e694b0

Please sign in to comment.