Skip to content

Commit d9d92fe

Browse files
committed
finalized scale function
1 parent a43a0c1 commit d9d92fe

File tree

5 files changed

+83
-82
lines changed

5 files changed

+83
-82
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.1.12'
3+
s.version = '1.2.0'
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,41 +32,41 @@ public class JXLCoder {
3232
}
3333

3434
/***
35-
- Parameter sampleSize: if image size larger than sampler then it will be resized to sample
35+
- Parameter rescale: image will be rescaled to provided size
3636
- Returns: Decoded JXL image if this is the valid one
3737
**/
3838
public static func decode(srcStream: InputStream,
39-
sampleSize: CGSize = .zero,
39+
rescale: CGSize = .zero,
4040
pixelFormat: JXLPreferredPixelFormat = .optimal,
4141
sampler: JxlSampler = .cubic) throws -> JXLPlatformImage {
42-
return try shared.decode(srcStream, sampleSize: sampleSize, pixelFormat: pixelFormat, sampler: sampler)
42+
return try shared.decode(srcStream, rescale: rescale, pixelFormat: pixelFormat, sampler: sampler)
4343
}
4444

4545
/***
4646
- Parameter sampleSize: if image size larger than sampler then it will be resized to sample
4747
- Returns: Decoded JXL image if this is the valid one
4848
**/
4949
public static func decode(url: URL,
50-
sampleSize: CGSize = .zero,
50+
rescale: CGSize = .zero,
5151
pixelFormat: JXLPreferredPixelFormat = .optimal,
5252
sampler: JxlSampler = .cubic) throws -> JXLPlatformImage {
5353
guard let srcStream = InputStream(url: url) else {
5454
throw NSError(domain: "JXLCoder", code: 500,
5555
userInfo: [NSLocalizedDescriptionKey: "JXLCoder cannot open provided URL"])
5656
}
57-
return try shared.decode(srcStream, sampleSize: sampleSize, pixelFormat: pixelFormat, sampler: sampler)
57+
return try shared.decode(srcStream, rescale: rescale, pixelFormat: pixelFormat, sampler: sampler)
5858
}
5959

6060
/***
61-
- Parameter sampleSize: if image size larger than sampler then it will be resized to sample
61+
- Parameter rescale: image will be rescaled to provided size
6262
- Returns: Decoded JXL image if this is the valid one
6363
**/
6464
public static func decode(data: Data,
65-
sampleSize: CGSize = .zero,
65+
rescale: CGSize = .zero,
6666
pixelFormat: JXLPreferredPixelFormat = .optimal,
6767
sampler: JxlSampler = .cubic) throws -> JXLPlatformImage {
6868
let srcStream = InputStream(data: data)
69-
return try shared.decode(srcStream, sampleSize: sampleSize, pixelFormat: pixelFormat, sampler: sampler)
69+
return try shared.decode(srcStream, rescale: rescale, pixelFormat: pixelFormat, sampler: sampler)
7070
}
7171

7272
/***

Sources/jxlc/JxlInternalCoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ typedef NS_ENUM(NSInteger, JxlSampler) {
5656

5757
@interface JxlInternalCoder: NSObject
5858
- (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
59-
sampleSize:(CGSize)sampleSize
59+
rescale:(CGSize)rescale
6060
pixelFormat:(JXLPreferredPixelFormat)preferredPixelFormat
6161
sampler:(JxlSampler)sampler
6262
error:(NSError *_Nullable * _Nullable)error;

Sources/jxlc/JxlInternalCoder.mm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ - (CGSize)getSize:(nonnull NSInputStream *)inputStream error:(NSError *_Nullable
188188
}
189189

190190
- (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
191-
sampleSize:(CGSize)sampleSize
191+
rescale:(CGSize)rescale
192192
pixelFormat:(JXLPreferredPixelFormat)preferredPixelFormat
193193
sampler:(JxlSampler)sampler
194194
error:(NSError *_Nullable * _Nullable)error {
@@ -268,7 +268,7 @@ - (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
268268
ySize = xz;
269269
}
270270

271-
if (sampleSize.width > 0 && sampleSize.height > 0) {
271+
if (rescale.width > 0 && rescale.height > 0) {
272272
XSampler xSampler = bilinear;
273273

274274
switch (sampler) {
@@ -293,14 +293,14 @@ - (nullable JXLSystemImage *)decode:(nonnull NSInputStream *)inputStream
293293
}
294294

295295
auto scaleResult = [RgbaScaler scaleData:outputData width:(int)xSize height:(int)ySize
296-
newWidth:(int)sampleSize.width newHeight:(int)sampleSize.height
296+
newWidth:(int)rescale.width newHeight:(int)rescale.height
297297
components:components pixelFormat:useFloats ? kF16 : kU8 sampler:xSampler];
298298
if (!scaleResult) {
299299
*error = [[NSError alloc] initWithDomain:@"JXLCoder" code:500 userInfo:@{ NSLocalizedDescriptionKey: @"Rescale image has failed" }];
300300
return nil;
301301
}
302-
xSize = sampleSize.width;
303-
ySize = sampleSize.height;
302+
xSize = rescale.width;
303+
ySize = rescale.height;
304304
}
305305

306306
CGColorSpaceRef colorSpace;

Sources/jxlc/RgbaScaler.mm

Lines changed: 68 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,41 @@
3333

3434
@implementation RgbaScaler
3535

36-
static bool API_AVAILABLE(macos(13.0), ios(16.0), watchos(9.0), tvos(16.0))
37-
scaleF16iOS16(std::vector<uint8_t> &src, int components, int width, int height, int newWidth, int newHeight, XSampler sampler) {
38-
if (components != 4) {
39-
std::vector<uint8_t> dst(components * sizeof(uint16_t) * newWidth * newHeight);
40-
41-
scaleImageFloat16(reinterpret_cast<uint16_t*>(src.data()),
42-
components * sizeof(uint16_t) * width, width, height, reinterpret_cast<uint16_t*>(dst.data()),
43-
components * sizeof(uint16_t) * newWidth, newWidth, newHeight, components, sampler);
44-
45-
src = dst;
46-
return true;
47-
}
36+
//static bool API_AVAILABLE(macos(13.0), ios(16.0), watchos(9.0), tvos(16.0))
37+
static bool scaleF16iOS16(std::vector<uint8_t> &src, int components, int width, int height, int newWidth, int newHeight, XSampler sampler) {
38+
// if (components != 4) {
39+
std::vector<uint8_t> dst(components * sizeof(uint16_t) * newWidth * newHeight);
4840

49-
std::vector<uint8_t> dst(4 * sizeof(uint16_t) * newWidth * newHeight);
50-
51-
vImage_Buffer srcBuffer = {
52-
.data = (void*)src.data(),
53-
.width = static_cast<vImagePixelCount>(width),
54-
.height = static_cast<vImagePixelCount>(height),
55-
.rowBytes = width * 4 * sizeof(uint16_t)
56-
};
57-
58-
vImage_Buffer dstBuffer = {
59-
.data = dst.data(),
60-
.width = static_cast<vImagePixelCount>(newWidth),
61-
.height = static_cast<vImagePixelCount>(newHeight),
62-
.rowBytes = newWidth * 4 * sizeof(uint16_t)
63-
};
41+
scaleImageFloat16(reinterpret_cast<uint16_t*>(src.data()),
42+
components * sizeof(uint16_t) * width, width, height, reinterpret_cast<uint16_t*>(dst.data()),
43+
components * sizeof(uint16_t) * newWidth, newWidth, newHeight, components, sampler);
6444

65-
auto result = vImageScale_ARGB16F(&srcBuffer, &dstBuffer, nullptr, kvImageUseFP16Accumulator);
66-
if (result != kvImageNoError) {
67-
return false;
68-
}
6945
src = dst;
7046
return true;
47+
// }
48+
//
49+
// std::vector<uint8_t> dst(4 * sizeof(uint16_t) * newWidth * newHeight);
50+
//
51+
// vImage_Buffer srcBuffer = {
52+
// .data = (void*)src.data(),
53+
// .width = static_cast<vImagePixelCount>(width),
54+
// .height = static_cast<vImagePixelCount>(height),
55+
// .rowBytes = width * 4 * sizeof(uint16_t)
56+
// };
57+
//
58+
// vImage_Buffer dstBuffer = {
59+
// .data = dst.data(),
60+
// .width = static_cast<vImagePixelCount>(newWidth),
61+
// .height = static_cast<vImagePixelCount>(newHeight),
62+
// .rowBytes = newWidth * 4 * sizeof(uint16_t)
63+
// };
64+
//
65+
// auto result = vImageScale_ARGB16F(&srcBuffer, &dstBuffer, nullptr, kvImageUseFP16Accumulator);
66+
// if (result != kvImageNoError) {
67+
// return false;
68+
// }
69+
// src = dst;
70+
// return true;
7171
}
7272

7373
static bool scaleF16iOSPre16(std::vector<uint8_t> &src, int components, int width, int height, int newWidth, int newHeight, XSampler sampler) {
@@ -146,40 +146,40 @@ static bool scaleF16iOSPre16(std::vector<uint8_t> &src, int components, int widt
146146
}
147147

148148
+ (bool)scaleRGB8:(std::vector<uint8_t> &)src components:(int)components width:(int)width height:(int)height newWidth:(int)newWidth newHeight:(int)newHeight sampler:(XSampler)sampler {
149-
if (components != 4) {
150-
std::vector<uint8_t> dst(components * sizeof(uint8_t) * newWidth * newHeight);
151-
152-
scaleImageU8(reinterpret_cast<uint8_t*>(src.data()),
153-
components * sizeof(uint8_t) * width, width, height, reinterpret_cast<uint8_t*>(dst.data()),
154-
components * sizeof(uint8_t) * newWidth, newWidth, newHeight, components, 8, sampler);
155-
src = dst;
156-
157-
return true;
158-
}
159-
160-
std::vector<uint8_t> dst(4 * sizeof(uint8_t) * newWidth * newHeight);
161-
162-
vImage_Buffer srcBuffer = {
163-
.data = (void*)src.data(),
164-
.width = static_cast<vImagePixelCount>(width),
165-
.height = static_cast<vImagePixelCount>(height),
166-
.rowBytes = width * 4 * sizeof(uint8_t)
167-
};
168-
169-
vImage_Buffer dstBuffer = {
170-
.data = dst.data(),
171-
.width = static_cast<vImagePixelCount>(newWidth),
172-
.height = static_cast<vImagePixelCount>(newHeight),
173-
.rowBytes = newWidth * 4 * sizeof(uint8_t)
174-
};
175-
176-
auto result = vImageScale_ARGB8888(&srcBuffer, &dstBuffer, nullptr, kvImageNoFlags);
177-
if (result != kvImageNoError) {
178-
return false;
179-
}
149+
// if (components != 4) {
150+
std::vector<uint8_t> dst(components * sizeof(uint8_t) * newWidth * newHeight);
180151

152+
scaleImageU8(reinterpret_cast<uint8_t*>(src.data()),
153+
components * sizeof(uint8_t) * width, width, height, reinterpret_cast<uint8_t*>(dst.data()),
154+
components * sizeof(uint8_t) * newWidth, newWidth, newHeight, components, 8, sampler);
181155
src = dst;
156+
182157
return true;
158+
// }
159+
//
160+
// std::vector<uint8_t> dst(4 * sizeof(uint8_t) * newWidth * newHeight);
161+
//
162+
// vImage_Buffer srcBuffer = {
163+
// .data = (void*)src.data(),
164+
// .width = static_cast<vImagePixelCount>(width),
165+
// .height = static_cast<vImagePixelCount>(height),
166+
// .rowBytes = width * 4 * sizeof(uint8_t)
167+
// };
168+
//
169+
// vImage_Buffer dstBuffer = {
170+
// .data = dst.data(),
171+
// .width = static_cast<vImagePixelCount>(newWidth),
172+
// .height = static_cast<vImagePixelCount>(newHeight),
173+
// .rowBytes = newWidth * 4 * sizeof(uint8_t)
174+
// };
175+
//
176+
// auto result = vImageScale_ARGB8888(&srcBuffer, &dstBuffer, nullptr, kvImageNoFlags);
177+
// if (result != kvImageNoError) {
178+
// return false;
179+
// }
180+
//
181+
// src = dst;
182+
// return true;
183183
}
184184

185185
+(bool) scaleData:(std::vector<uint8_t>&)src width:(int)width height:(int)height newWidth:(int)newWidth newHeight:(int)newHeight components:(int)components pixelFormat:(JxlIPixelFormat)pixelFormat sampler:(XSampler)sampler {
@@ -193,11 +193,12 @@ +(bool) scaleData:(std::vector<uint8_t>&)src width:(int)width height:(int)height
193193
if (pixelFormat == kU8) {
194194
return [self scaleRGB8:src components:components width:width height:height newWidth:newWidth newHeight:newHeight sampler:sampler];
195195
} else if (pixelFormat == kF16) {
196-
if (@available(iOS 16.0, macOS 13.0, *)) {
197-
return scaleF16iOS16(src, components, width, height, newWidth, newHeight, sampler);
198-
} else {
199-
return scaleF16iOSPre16(src, components, width, height, newWidth, newHeight, sampler);
200-
}
196+
return scaleF16iOS16(src, components, width, height, newWidth, newHeight, sampler);
197+
// if (@available(iOS 16.0, macOS 13.0, *)) {
198+
// return scaleF16iOS16(src, components, width, height, newWidth, newHeight, sampler);
199+
// } else {
200+
// return scaleF16iOSPre16(src, components, width, height, newWidth, newHeight, sampler);
201+
// }
201202
}
202203
} catch (const std::bad_alloc& e) {
203204
// Memory allocation has failed

0 commit comments

Comments
 (0)