Skip to content

High performance self-hosted photo and video management solution.

feat(mobile): jxl decoder (#32592)

Mertd474ec966bc4
diff --git a/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeImage.kt b/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeImage.kt
index 30453985cd..9d91f79a69 100644
--- a/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeImage.kt
+++ b/mobile/android/app/src/main/kotlin/app/alextran/immich/NativeImage.kt
@@ -1,6 +1,7 @@
package app.alextran.immich
import android.graphics.Bitmap
+import java.nio.ByteBuffer
object NativeImage {
init {
@@ -25,4 +26,24 @@ object NativeImage {
*/
@JvmStatic
external fun convert1010102(bitmap: Bitmap, outInfo: IntArray): Long
+
+ /**
+ * Like [convert1010102], but decodes the JPEG XL image of [length] bytes at [address], stopped by
+ * [cancel], downscaled to cover [targetWidth] x [targetHeight] unless either is 0 and to fit
+ * within [maxPixelSize].
+ */
+ @JvmStatic
+ external fun decodeJxl(
+ address: Long,
+ length: Int,
+ targetWidth: Int,
+ targetHeight: Int,
+ maxPixelSize: Int,
+ cancelled: ByteBuffer,
+ outInfo: IntArray
+ ): Long
+
+ /** Stops the [decodeJxl] given this one-byte direct buffer. */
+ @JvmStatic
+ external fun cancel(cancelled: ByteBuffer)
}
diff --git a/mobile/android/app/src/main/kotlin/app/alextran/immich/images/RemoteImagesImpl.kt b/mobile/android/app/src/main/kotlin/app/alextran/immich/images/RemoteImagesImpl.kt
index a5481d5b7a..91dd4f9853 100644
--- a/mobile/android/app/src/main/kotlin/app/alextran/immich/images/RemoteImagesImpl.kt
+++ b/mobile/android/app/src/main/kotlin/app/alextran/immich/images/RemoteImagesImpl.kt
@@ -10,6 +10,7 @@ import androidx.exifinterface.media.ExifInterface
import app.alextran.immich.INITIAL_BUFFER_SIZE
import app.alextran.immich.NativeBuffer
import app.alextran.immich.NativeByteBuffer
+import app.alextran.immich.NativeImage
import app.alextran.immich.core.HttpClientManager
import kotlinx.coroutines.*
import okhttp3.Cache
@@ -56,10 +57,9 @@ private val RAW_MIME_TYPES = setOf(
"image/x-sony-sr2", "image/x-sony-srf", "image/x3f",
)
-private fun isRawMime(contentType: String?): Boolean {
- val mime = contentType?.substringBefore(';')?.trim()?.lowercase() ?: return false
- return mime in RAW_MIME_TYPES
-}
+private fun mimeType(contentType: String?) = contentType?.substringBefore(';')?.trim()?.lowercase()
+
+private fun isRawMime(contentType: String?) = mimeType(contentType) in RAW_MIME_TYPES
class RemoteImagesImpl(context: Context) : RemoteImageApi {
private val requestMap = ConcurrentHashMap<Long, RemoteRequest>()
@@ -97,6 +97,42 @@ class RemoteImagesImpl(context: Context) : RemoteImageApi {
return@fetch callback(CANCELLED)
}
+ // Neither Android nor Flutter can decode JPEG XL, so this applies to encoded requests too.
+ if (mimeType(contentType) == "image/jxl") {
+ decodeExecutor.execute {
+ val info = IntArray(3)
+ // The fetch is done, so cancelling now stops the decode instead.
+ val cancelled = ByteBuffer.allocateDirect(1)
+ signal.setOnCancelListener { NativeImage.cancel(cancelled) }
+ val pointer = if (signal.isCanceled) 0L else NativeImage.decodeJxl(
+ buffer.pointer,
+ buffer.offset,
+ width?.toInt() ?: 0,
+ height?.toInt() ?: 0,
+ MAX_PIXEL_SIZE,
+ cancelled,
+ info
+ )
+ buffer.free()
+ requestMap.remove(requestId)
+ callback(
+ when {
+ pointer != 0L -> Result.success(
+ mapOf(
+ "pointer" to pointer,
+ "width" to info[0].toLong(),
+ "height" to info[1].toLong(),
+ "rowBytes" to info[2].toLong()
+ )
+ )
+ signal.isCanceled -> CANCELLED
+ else -> Result.failure(IOException("Failed to decode JPEG XL image"))
+ }
+ )
+ }
+ return@fetch
+ }
+
// Decode natively when the caller wants pixels: Flutter's fallback decoder copies
// 10-bit bitmaps (RGBA_1010102) as if they were rgba8888, garbling colors. Decode on a
// dedicated pool - the fetch callback threads are shared with video streaming. On any
diff --git a/mobile/ios/Runner/Core/NativeCore.swift b/mobile/ios/Runner/Core/NativeCore.swift
index 1d72557f76..03eba1f95e 100644
--- a/mobile/ios/Runner/Core/NativeCore.swift
+++ b/mobile/ios/Runner/Core/NativeCore.swift
@@ -9,6 +9,8 @@ enum NativeCore {
}
private static let coreThumbhash = symbol("native_core_thumbhash", as: NativeCoreThumbhashFn.self)
+ private static let coreJxlDecode = symbol("native_core_jxl_decode", as: NativeCoreJxlDecodeFn.self)
+ private static let coreCancel = symbol("native_core_cancel", as: NativeCoreCancelFn.self)
static func thumbhash(_ hash: Data) -> (width: Int, height: Int, pointer: UnsafeMutableRawPointer)? {
guard let coreThumbhash else { return nil }
@@ -20,4 +22,18 @@ enum NativeCore {
return (Int(width), Int(height), UnsafeMutableRawPointer(pointer))
}
}
+
+ static func decodeJxl(_ data: Data, targetWidth: Int64?, targetHeight: Int64?, maxPixelSize: Int, cancelled: UnsafeMutablePointer<Bool>) -> (width: Int, height: Int, pointer: UnsafeMutableRawPointer)? {
+ guard let coreJxlDecode else { return nil }
+ var info = NativeCoreImageInfo()
+ return data.withUnsafeBytes { bytes in
+ guard let pointer = coreJxlDecode(bytes.bindMemory(to: UInt8.self).baseAddress, bytes.count, false, UInt32(clamping: targetWidth ?? 0), UInt32(clamping: targetHeight ?? 0), UInt32(clamping: maxPixelSize), cancelled, &info)
+ else { return nil }
+ return (Int(info.width), Int(info.height), UnsafeMutableRawPointer(pointer))
+ }
+ }
+
+ static func cancel(_ flag: UnsafeMutablePointer<Bool>) {
+ coreCancel?(flag)
+ }
}
diff --git a/mobile/ios/Runner/Images/ImageProcessing.swift b/mobile/ios/Runner/Images/ImageProcessing.swift
index 686a3464a7..f7ec72101c 100644
--- a/mobile/ios/Runner/Images/ImageProcessing.swift
+++ b/mobile/ios/Runner/Images/ImageProcessing.swift
@@ -9,4 +9,6 @@ enum ImageProcessing {
return q
}()
static let cancelledResult = Result<[String: Int64]?, any Error>.success(nil)
+ // Impeller shrinks each side to the GPU texture limit on its own, which squishes long images.
+ static let maxPixelSize = 16384
}
diff --git a/mobile/ios/Runner/Images/LocalImagesImpl.swift b/mobile/ios/Runner/Images/LocalImagesImpl.swift
index 709be5958a..c892ddf8dd 100644
--- a/mobile/ios/Runner/Images/LocalImagesImpl.swift
+++ b/mobile/ios/Runner/Images/LocalImagesImpl.swift
@@ -20,7 +20,6 @@ class LocalImageApiImpl: LocalImageApi {
requestOptions.version = .current
return requestOptions
}()
- private static let maxPixelSize: CGFloat = 16384
private static let registry = RequestRegistry<ImageRequest>()
@@ -112,7 +111,7 @@ class LocalImageApiImpl: LocalImageApi {
let isOriginal = !(width > 0 && height > 0)
let targetSize = isOriginal
- ? CGSize(width: Self.maxPixelSize, height: Self.maxPixelSize)
+ ? CGSize(width: ImageProcessing.maxPixelSize, height: ImageProcessing.maxPixelSize)
: CGSize(width: Double(width), height: Double(height))
let contentMode: PHImageContentMode = isOriginal ? .aspectFit : .aspectFill
var image: UIImage?
@@ -137,7 +136,7 @@ class LocalImageApiImpl: LocalImageApi {
}
// .fast can return larger than the target, so retry with .exact to guarantee the bound.
- if max(cgImage.width, cgImage.height) > Int(Self.maxPixelSize) {
+ if max(cgImage.width, cgImage.height) > ImageProcessing.maxPixelSize {
let exactOptions = Self.requestOptions.copy() as! PHImageRequestOptions
exactOptions.resizeMode = .exact
image = nil
diff --git a/mobile/ios/Runner/Images/RemoteImagesImpl.swift b/mobile/ios/Runner/Images/RemoteImagesImpl.swift
index b4b70f0b30..ac405bf7ec 100644
--- a/mobile/ios/Runner/Images/RemoteImagesImpl.swift
+++ b/mobile/ios/Runner/Images/RemoteImagesImpl.swift
@@ -7,15 +7,23 @@ import Photos
final class RemoteImageRequest: ImageRequest {
var task: URLSessionDataTask?
let id: Int64
+ /// Lets cancellation stop a native decode partway through.
+ let decodeCancelled = UnsafeMutablePointer<Bool>.allocate(capacity: 1)
init(id: Int64, completion: @escaping @Sendable (Result<[String: Int64]?, any Error>) -> Void) {
self.id = id
super.init(completion: completion)
+ decodeCancelled.initialize(to: false)
+ }
+
+ deinit {
+ decodeCancelled.deallocate()
}
override func cancel() {
super.cancel()
task?.cancel()
+ NativeCore.cancel(decodeCancelled)
}
}
@@ -65,6 +73,38 @@ class RemoteImageApiImpl: NSObject, RemoteImageApi {
return request.completion(.failure(PigeonError(code: "", message: "No data received", details: nil)))
}
+ // Flutter can't decode JPEG XL and older iOS versions can't either, so this applies to encoded requests too.
+ if response?.mimeType == "image/jxl" {
+ ImageProcessing.queue.addOperation {
+ if request.isCancelled {
+ return request.completion(ImageProcessing.cancelledResult)
+ }
+
+ guard let (width, height, pointer) = NativeCore.decodeJxl(data, targetWidth: width, targetHeight: height, maxPixelSize: ImageProcessing.maxPixelSize, cancelled: request.decodeCancelled) else {
+ registry.remove(requestId: request.id)
+ if request.isCancelled {
+ return request.completion(ImageProcessing.cancelledResult)
+ }
+ return request.completion(.failure(PigeonError(code: "", message: "Failed to decode JPEG XL image", details: nil)))
+ }
+
+ if request.isCancelled {
+ free(pointer)
+ return request.completion(ImageProcessing.cancelledResult)
+ }
+
+ registry.remove(requestId: request.id)
+ request.completion(
+ .success([
+ "pointer": Int64(Int(bitPattern: pointer)),
+ "width": Int64(width),
+ "height": Int64(height),
+ "rowBytes": Int64(width * 4),
+ ]))
+ }
+ return
+ }
+
if encoded {
let length = data.count
let pointer = malloc(length)!
diff --git a/mobile/ios/Runner/Runner-Bridging-Header.h b/mobile/ios/Runner/Runner-Bridging-Header.h
index f7fb444153..d5b7f601c0 100644
--- a/mobile/ios/Runner/Runner-Bridging-Header.h
+++ b/mobile/ios/Runner/Runner-Bridging-Header.h
@@ -3,3 +3,5 @@
// The function pointer type comes from the declaration, so nothing is retyped here.
typedef __typeof__(&native_core_thumbhash) NativeCoreThumbhashFn;
+typedef __typeof__(&native_core_jxl_decode) NativeCoreJxlDecodeFn;
+typedef __typeof__(&native_core_cancel) NativeCoreCancelFn;
diff --git a/mobile/lib/infrastructure/loaders/image_request.dart b/mobile/lib/infrastructure/loaders/image_request.dart
index f15fd01b43..1a410119c5 100644
--- a/mobile/lib/infrastructure/loaders/image_request.dart
+++ b/mobile/lib/infrastructure/loaders/image_request.dart
@@ -79,8 +79,7 @@ abstract class ImageRequest {
return (codec, descriptor);
}
- Future<ui.FrameInfo?> _fromEncodedPlatformImage(int address, int length, {ui.Size? decodeSize}) async {
- final result = await _codecFromEncodedPlatformImage(address, length, decodeSize: decodeSize);
+ Future<ui.FrameInfo?> _firstFrame((ui.Codec, ui.ImageDescriptor)? result) async {
if (result == null) {
return null;
}
@@ -116,7 +115,12 @@ abstract class ImageRequest {
return ((width * scale).ceil(), (height * scale).ceil());
}
- Future<ui.FrameInfo?> _fromDecodedPlatformImage(int address, int width, int height, int rowBytes) async {
+ Future<(ui.Codec, ui.ImageDescriptor)?> _codecFromDecodedPlatformImage(
+ int address,
+ int width,
+ int height,
+ int rowBytes,
+ ) async {
final pointer = Pointer<Uint8>.fromAddress(address);
if (_isCancelled) {
malloc.free(pointer);
@@ -152,14 +156,6 @@ abstract class ImageRequest {
return null;
}
- final frame = await codec.getNextFrame();
- descriptor.dispose();
- codec.dispose();
- if (_isCancelled) {
- frame.image.dispose();
- return null;
- }
-
- return frame;
+ return (codec, descriptor);
}
}
diff --git a/mobile/lib/infrastructure/loaders/local_image_request.dart b/mobile/lib/infrastructure/loaders/local_image_request.dart
index 403e5d34cb..e570a24d66 100644
--- a/mobile/lib/infrastructure/loaders/local_image_request.dart
+++ b/mobile/lib/infrastructure/loaders/local_image_request.dart
@@ -28,7 +28,9 @@ class LocalImageRequest extends ImageRequest {
return null;
}
- final frame = await _fromDecodedPlatformImage(info["pointer"]!, info["width"]!, info["height"]!, info["rowBytes"]!);
+ final frame = await _firstFrame(
+ await _codecFromDecodedPlatformImage(info["pointer"]!, info["width"]!, info["height"]!, info["rowBytes"]!),
+ );
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
}
diff --git a/mobile/lib/infrastructure/loaders/remote_image_request.dart b/mobile/lib/infrastructure/loaders/remote_image_request.dart
index 7b504697ac..10127bec8b 100644
--- a/mobile/lib/infrastructure/loaders/remote_image_request.dart
+++ b/mobile/lib/infrastructure/loaders/remote_image_request.dart
@@ -21,22 +21,7 @@ class RemoteImageRequest extends ImageRequest {
width: decodeSize?.width.ceil(),
height: decodeSize?.height.ceil(),
);
- // Android falls back to encoded data if native decoding fails, so check for both shapes of the response.
- final frame = switch (info) {
- {'pointer': final int pointer, 'length': final int length} => await _fromEncodedPlatformImage(
- pointer,
- length,
- decodeSize: decodeSize,
- ),
- {
- 'pointer': final int pointer,
- 'width': final int width,
- 'height': final int height,
- 'rowBytes': final int rowBytes,
- } =>
- await _fromDecodedPlatformImage(pointer, width, height, rowBytes),
- _ => null,
- };
+ final frame = await _firstFrame(await _codecFromResponse(info, decodeSize: decodeSize));
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
}
@@ -53,14 +38,29 @@ class RemoteImageRequest extends ImageRequest {
width: null,
height: null,
);
- if (info == null) {
- return null;
- }
-
- final (codec, _) = await _codecFromEncodedPlatformImage(info['pointer']!, info['length']!) ?? (null, null);
- return codec;
+ final result = await _codecFromResponse(info);
+ return result?.$1;
}
+ // Android falls back to encoded data if native decoding fails, and JPEG XL arrives decoded even when the encoded image
+ // is requested, so check for both shapes of the response.
+ Future<(ui.Codec, ui.ImageDescriptor)?> _codecFromResponse(Map<String, int>? info, {ui.Size? decodeSize}) =>
+ switch (info) {
+ {'pointer': final int pointer, 'length': final int length} => _codecFromEncodedPlatformImage(
+ pointer,
+ length,
+ decodeSize: decodeSize,
+ ),
+ {
+ 'pointer': final int pointer,
+ 'width': final int width,
+ 'height': final int height,
+ 'rowBytes': final int rowBytes,
+ } =>
+ _codecFromDecodedPlatformImage(pointer, width, height, rowBytes),
+ _ => Future.value(),
+ };
+
@override
Future<void> _onCancelled() {
return remoteImageApi.cancelRequest(requestId);
diff --git a/mobile/lib/infrastructure/loaders/thumbhash_image_request.dart b/mobile/lib/infrastructure/loaders/thumbhash_image_request.dart
index 61e6a1b3ad..b79b6ec5fb 100644
--- a/mobile/lib/infrastructure/loaders/thumbhash_image_request.dart
+++ b/mobile/lib/infrastructure/loaders/thumbhash_image_request.dart
@@ -12,7 +12,9 @@ class ThumbhashImageRequest extends ImageRequest {
}
final Map<String, int> info = await localImageApi.getThumbhash(thumbhash);
- final frame = await _fromDecodedPlatformImage(info["pointer"]!, info["width"]!, info["height"]!, info["rowBytes"]!);
+ final frame = await _firstFrame(
+ await _codecFromDecodedPlatformImage(info["pointer"]!, info["width"]!, info["height"]!, info["rowBytes"]!),
+ );
return frame == null ? null : ImageInfo(image: frame.image, scale: scale);
}
diff --git a/mobile/test/infrastructure/loaders/remote_image_request_test.dart b/mobile/test/infrastructure/loaders/remote_image_request_test.dart
index a267985ab1..71dd74ba29 100644
--- a/mobile/test/infrastructure/loaders/remote_image_request_test.dart
+++ b/mobile/test/infrastructure/loaders/remote_image_request_test.dart
@@ -76,6 +76,24 @@ void main() {
expect(args[4], isNull);
});
+ test('accepts a decoded image for an animation request', () async {
+ final pointer = malloc<Uint8>(8)..asTypedList(8).setAll(0, [255, 0, 0, 255, 0, 0, 255, 255]);
+ TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger.setMockDecodedMessageHandler(channel, (
+ message,
+ ) async {
+ return <Object?>[
+ <Object?, Object?>{'pointer': pointer.address, 'width': 2, 'height': 1, 'rowBytes': 8},
+ ];
+ });
+
+ final codec = (await RemoteImageRequest(uri: 'https://example.test/animation').loadCodec())!;
+ final image = (await codec.getNextFrame()).image;
+
+ expect((image.width, image.height), (2, 1));
+ image.dispose();
+ codec.dispose();
+ });
+
test('keeps portrait cover quality in a wide tile', () async {
final image = await loadEncoded('assets/feature_message/ocr.webp', const ui.Size(963, 642));
diff --git a/native/Cargo.lock b/native/Cargo.lock
index 61ea918cd1..ed327891ac 100644
--- a/native/Cargo.lock
+++ b/native/Cargo.lock
@@ -2,12 +2,30 @@
# It is not intended for manual editing.
version = 4
+[[package]]
+name = "array-init"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3d62b7694a562cdf5a74227903507c56ab2cc8bdd1f781ed5cb4cf9c9f810bfc"
+
+[[package]]
+name = "autocfg"
+version = "1.5.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
+
[[package]]
name = "bitflags"
version = "2.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b588b76d00fde79687d7646a9b5bdf3cc0f655e0bbd080335a95d7e96f3587da"
+[[package]]
+name = "byteorder"
+version = "1.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
+
[[package]]
name = "bytes"
version = "1.12.1"
@@ -48,6 +66,37 @@ dependencies = [
"memchr",
]
+[[package]]
+name = "crossbeam-deque"
+version = "0.8.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a"
+dependencies = [
+ "crossbeam-epoch",
+ "crossbeam-utils",
+]
+
+[[package]]
+name = "crossbeam-epoch"
+version = "0.9.21"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d"
+dependencies = [
+ "crossbeam-utils",
+]
+
+[[package]]
+name = "crossbeam-utils"
+version = "0.8.23"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6"
+
+[[package]]
+name = "either"
+version = "1.19.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
+
[[package]]
name = "equivalent"
version = "1.0.2"
@@ -158,6 +207,48 @@ dependencies = [
"syn 2.0.119",
]
+[[package]]
+name = "jxl"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b8379fc83d79477900c9aed78cb8370a556d834081bebcb771c6010381c4e230"
+dependencies = [
+ "array-init",
+ "byteorder",
+ "jxl_macros",
+ "jxl_simd",
+ "jxl_transforms",
+ "num-derive",
+ "num-traits",
+ "thiserror",
+]
+
+[[package]]
+name = "jxl_macros"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "eeb27c06a9957bd9c8e65d0aeea0db6a555a9e1c85597e284f8304015c410c87"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.119",
+]
+
+[[package]]
+name = "jxl_simd"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a645451b4d34b4017caf24110a14eb737e275b5d3d121bc754e67727ee65af0e"
+
+[[package]]
+name = "jxl_transforms"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "21c4b7f8957f5ba578b2e28cd5ebbc97dad58a3ad5ff26765da61e8dab8f1cad"
+dependencies = [
+ "jxl_simd",
+]
+
[[package]]
name = "libc"
version = "0.2.190"
@@ -182,9 +273,27 @@ version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
+[[package]]
+name = "moxcms"
+version = "0.9.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "396077b717181ce3f9a6b66739b269b82d756be5ec0f76be0bc40a7c51775830"
+dependencies = [
+ "num-traits",
+ "pxfm",
+]
+
[[package]]
name = "native_core"
version = "0.1.0"
+dependencies = [
+ "jxl",
+ "libc",
+ "moxcms",
+ "pic-scale",
+ "quick_cache",
+ "rayon",
+]
[[package]]
name = "native_core_ffi"
@@ -196,12 +305,42 @@ dependencies = [
"native_core",
]
+[[package]]
+name = "num-derive"
+version = "0.4.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ed3955f1a9c7c0c15e092f9c887db08b1fc683305fdf6eb6684f22555355e202"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.119",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
+dependencies = [
+ "autocfg",
+]
+
[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
+[[package]]
+name = "pic-scale"
+version = "0.7.12"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6edb0ff645d35accaec252de081d55984a0d6c952cb1e5dc2b9afa7c78f07288"
+dependencies = [
+ "num-traits",
+ "pxfm",
+]
+
[[package]]
name = "proc-macro2"
version = "1.0.107"
@@ -211,6 +350,22 @@ dependencies = [
"unicode-ident",
]
+[[package]]
+name = "pxfm"
+version = "0.1.30"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d55d956fa96f5ec02be2e13af0e20391a5aa83d6a074e3ad368959d0fab299ea"
+
+[[package]]
+name = "quick_cache"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "403c1a912fec895cafb223201e368234842acb9220aaf08ab042ae89ba5f135c"
+dependencies = [
+ "equivalent",
+ "hashbrown",
+]
+
[[package]]
name = "quote"
version = "1.0.47"
@@ -226,6 +381,26 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
+[[package]]
+name = "rayon"
+version = "1.12.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d"
+dependencies = [
+ "either",
+ "rayon-core",
+]
+
+[[package]]
+name = "rayon-core"
+version = "1.13.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91"
+dependencies = [
+ "crossbeam-deque",
+ "crossbeam-utils",
+]
+
[[package]]
name = "rustc_version"
version = "0.4.1"
diff --git a/native/Cargo.toml b/native/Cargo.toml
index 2a9a7da070..377057f54e 100644
--- a/native/Cargo.toml
+++ b/native/Cargo.toml
@@ -10,7 +10,12 @@ license = "AGPL-3.0-only"
[workspace.dependencies]
cbindgen = { version = "0.29", default-features = false }
jni = { version = "0.22", default-features = false }
+jxl = { version = "0.8", default-features = false, features = ["neon"] }
libc = { version = "0.2", default-features = false }
+moxcms = { version = "0.9", default-features = false, features = ["neon_luts", "neon_shaper_paths"] }
+pic-scale = { version = "0.7", default-features = false, features = ["neon", "rdm"] }
+quick_cache = { version = "0.7", default-features = false }
+rayon = { version = "1.11", default-features = false }
[workspace.lints.clippy]
undocumented_unsafe_blocks = "deny"
diff --git a/native/crates/native_core/Cargo.toml b/native/crates/native_core/Cargo.toml
index eff59c46f5..8326eaa366 100644
--- a/native/crates/native_core/Cargo.toml
+++ b/native/crates/native_core/Cargo.toml
@@ -4,5 +4,13 @@ version.workspace = true
edition.workspace = true
license.workspace = true
+[dependencies]
+jxl = { workspace = true }
+libc = { workspace = true }
+moxcms = { workspace = true }
+pic-scale = { workspace = true }
+quick_cache = { workspace = true }
+rayon = { workspace = true }
+
[lints]
workspace = true
diff --git a/native/crates/native_core/src/color.rs b/native/crates/native_core/src/color.rs
new file mode 100644
index 0000000000..2929a0f706
--- /dev/null
+++ b/native/crates/native_core/src/color.rs
@@ -0,0 +1,92 @@
+use std::sync::Arc;
+use std::sync::atomic::{AtomicBool, Ordering};
+
+pub use moxcms::{CicpColorPrimaries, CicpProfile, TransferCharacteristics};
+use moxcms::{
+ ColorProfile, DataColorSpace, Layout, MatrixCoefficients, RenderingIntent,
+ Transform16BitExecutor, TransformOptions,
+};
+
+use crate::image::Pixels;
+
+/// The colour space decoded pixels should be in.
+#[derive(Clone, Copy, PartialEq)]
+pub enum Target {
+ /// sRGB, converting and tone-mapping as needed.
+ Srgb,
+ /// The image's own space whenever CICP can describe it, otherwise sRGB.
+ Source,
+}
+
+pub(crate) const SRGB: CicpProfile = CicpProfile {
+ color_primaries: CicpColorPrimaries::Bt709,
+ transfer_characteristics: TransferCharacteristics::Srgb,
+ matrix_coefficients: MatrixCoefficients::Identity,
+ full_range: true,
+};
+
+pub(crate) fn is_srgb(cicp: CicpProfile) -> bool {
+ cicp.color_primaries == SRGB.color_primaries
+ && cicp.transfer_characteristics == SRGB.transfer_characteristics
+}
+
+pub(crate) fn is_hdr(cicp: CicpProfile) -> bool {
+ matches!(
+ cicp.transfer_characteristics,
+ TransferCharacteristics::Smpte2084 | TransferCharacteristics::Hlg
+ )
+}
+
+/// Builds the transform from an ICC profile to sRGB, and the layout of the 12-bit samples it reads.
+pub(crate) fn to_srgb(icc: &[u8]) -> Option<(Arc<Transform16BitExecutor>, Layout)> {
+ let source = ColorProfile::new_from_slice(icc).ok()?;
+ let layout = match source.color_space {
+ DataColorSpace::Gray => Layout::GrayAlpha,
+ _ => Layout::Rgba,
+ };
+ let options = TransformOptions {
+ // Profiles that tone-map HDR, like those jxl-rs makes, may only have a perceptual table.
+ rendering_intent: RenderingIntent::Perceptual,
+ // The fixed-point path loses precision with the narrower samples it reads.
+ prefer_fixed_point: false,
+ ..Default::default()
+ };
+ // Going past 12-bit makes the transform's tables much slower to build for negligible benefit.
+ let transform = source
+ .create_transform_12bit(layout, &ColorProfile::new_srgb(), Layout::Rgba, options)
+ .ok()?;
+ Some((transform, layout))
+}
+
+/// Converts the 12-bit samples in `rgba`, `stride` bytes per row, to RGBA8 with `transform`.
+pub(crate) fn convert_bands(
+ transform: &Transform16BitExecutor,
+ rgba: &mut Pixels<u8>,
+ stride: usize,
+ cancelled: &AtomicBool,
+) -> Option<()> {
+ let (w, h) = (rgba.width(), rgba.height());
+ let buf = rgba.bytes_mut();
+ // Narrowing in place keeps peak memory at the wide decode plus one band.
+ let rows = 64;
+ let mut converted = vec![0u16; w * 4 * rows.min(h)];
+ for y in (0..h).step_by(rows) {
+ if cancelled.load(Ordering::Relaxed) {
+ return None;
+ }
+ let n = rows.min(h - y);
+ let band = &buf[y * stride..][..n * stride];
+ // SAFETY: any two bytes are a valid u16.
+ let ([], src, []) = (unsafe { band.align_to::<u16>() }) else {
+ return None;
+ };
+ transform.transform(src, &mut converted[..n * w * 4]).ok()?;
+ // Pixels decode at least as wide as RGBA8, so this only overwrites converted rows.
+ let narrowed = &mut buf[y * w * 4..][..n * w * 4];
+ for (dst, &src) in narrowed.iter_mut().zip(&converted) {
+ const NARROW: u32 = ((255 << 16) + 4095 / 2) / 4095;
+ *dst = ((u32::from(src) * NARROW + (1 << 15)) >> 16) as u8;
+ }
+ }
+ Some(())
+}
diff --git a/native/crates/native_core/src/image.rs b/native/crates/native_core/src/image.rs
new file mode 100644
index 0000000000..3e3e252934
--- /dev/null
+++ b/native/crates/native_core/src/image.rs
@@ -0,0 +1,202 @@
+use std::marker::PhantomData;
+use std::mem::ManuallyDrop;
+use std::ptr::NonNull;
+use std::slice;
+
+use crate::color::CicpProfile;
+use crate::resize::{self, Lanczos};
+
+/// Decoded pixels and the colour space they are in.
+pub struct Image {
+ pub cicp: CicpProfile,
+ pub samples: Samples,
+}
+
+pub enum Samples {
+ U8(Pixels<u8>),
+ /// For HDR transfers, which need more than 8 bits.
+ U16(Pixels<u16>),
+}
+
+/// Premultiplied RGBA pixels in a malloc'd buffer.
+pub struct Pixels<T> {
+ /// Initialized bytes from calloc or realloc that only self references.
+ buf: NonNull<[u8]>,
+ width: usize,
+ height: usize,
+ sample: PhantomData<T>,
+}
+
+pub(crate) trait Sample: Lanczos + Ord {
+ const MAX: Self;
+
+ /// Rounds like dividing the product by MAX would.
+ fn scale(self, alpha: Self) -> Self;
+}
+
+impl Sample for u8 {
+ const MAX: Self = u8::MAX;
+
+ fn scale(self, alpha: Self) -> Self {
+ let x = u16::from(self) * u16::from(alpha) + 128;
+ ((x + (x >> 8)) >> 8) as u8
+ }
+}
+
+impl Sample for u16 {
+ const MAX: Self = u16::MAX;
+
+ fn scale(self, alpha: Self) -> Self {
+ let x = u32::from(self) * u32::from(alpha) + 32768;
+ ((x + (x >> 16)) >> 16) as u16
+ }
+}
+
+impl<T> Pixels<T> {
+ /// Allocates pixels of u8 or u16 samples, the only kinds this module makes, with `pixel_bytes`
+ /// each if a decoder first writes them wider.
+ pub(crate) fn zeroed(width: usize, height: usize, pixel_bytes: usize) -> Option<Self>
+ where
+ T: Sample,
+ {
+ let len = width
+ .checked_mul(height)?
+ .checked_mul(pixel_bytes.max(size_of::<[T; 4]>()))?;
+ // SAFETY: calloc has no preconditions.
+ let ptr = NonNull::new(unsafe { libc::calloc(len, 1) }.cast())?;
+ Some(Self {
+ buf: NonNull::slice_from_raw_parts(ptr, len),
+ width,
+ height,
+ sample: PhantomData,
+ })
+ }
+
+ pub(crate) fn bytes_mut(&mut self) -> &mut [u8] {
+ // SAFETY: self owns these initialized bytes.
+ unsafe { self.buf.as_mut() }
+ }
+
+ pub(crate) fn pixels_mut(&mut self) -> &mut [[T; 4]] {
+ // SAFETY: as for pixels, and self is borrowed mutably.
+ unsafe { slice::from_raw_parts_mut(self.buf.as_ptr().cast(), self.width * self.height) }
+ }
+
+ /// Frees everything after the pixels.
+ fn shrink(mut self) -> Option<Self> {
+ let len = self.width * self.height * 4 * size_of::<T>();
+ // SAFETY: the buffer is live and the pixels fill its first len bytes.
+ let shrunk = unsafe { libc::realloc(self.buf.as_ptr().cast(), len) };
+ // A failed realloc leaves the original buffer, which self still frees.
+ self.buf = NonNull::slice_from_raw_parts(NonNull::new(shrunk.cast())?, len);
+ Some(self)
+ }
+
+ /// Premultiplies if `alpha`, then resizes to `size`, or else frees everything after the pixels.
+ pub(crate) fn premultiply_resize(
+ mut self,
+ alpha: bool,
+ size: Option<(usize, usize)>,
+ ) -> Option<Self>
+ where
+ T: Sample,
+ {
+ if alpha {
+ self.premultiply();
+ }
+ let Some(size) = size else {
+ return self.shrink();
+ };
+ let mut out = self.resize(size)?;
+ if alpha {
+ // Lanczos3 overshoots, while premultiplied colour must stay within alpha.
+ out.clamp();
+ }
+ Some(out)
+ }
+
+ fn premultiply(&mut self)
+ where
+ T: Sample,
+ {
+ // Alpha scaling itself at full opacity lets this vectorize as every lane does the same work.
+ for px in self.pixels_mut() {
+ let [r, g, b, a] = *px;
+ *px = [r.scale(a), g.scale(a), b.scale(a), a.scale(T::MAX)];
+ }
+ }
+
+ /// Clamps colour to alpha.
+ fn clamp(&mut self)
+ where
+ T: Sample,
+ {
+ for px in self.pixels_mut() {
+ let a = px[3];
+ *px = px.map(|c| c.min(a));
+ }
+ }
+
+ /// The pixels resampled to `size` with Lanczos3, in a new buffer.
+ fn resize(&self, (tw, th): (usize, usize)) -> Option<Self>
+ where
+ T: Sample,
+ {
+ let mut out = Self::zeroed(tw, th, size_of::<[T; 4]>())?;
+ resize::lanczos(self, &mut out)?;
+ Some(out)
+ }
+
+ pub fn width(&self) -> usize {
+ self.width
+ }
+
+ pub fn height(&self) -> usize {
+ self.height
+ }
+
+ pub fn pixels(&self) -> &[[T; 4]] {
+ // SAFETY: zeroed made room for at least these initialized pixels, and their u8 or u16 samples are
+ // valid for any bytes and aligned by malloc.
+ unsafe { slice::from_raw_parts(self.buf.as_ptr().cast(), self.width * self.height) }
+ }
+
+ /// Hands the buffer to a C caller, who frees it with free().
+ pub fn into_raw(self) -> *mut u8 {
+ ManuallyDrop::new(self).buf.as_ptr().cast()
+ }
+}
+
+impl<T> Drop for Pixels<T> {
+ fn drop(&mut self) {
+ // SAFETY: self owns the buffer, which came from calloc or realloc.
+ unsafe { libc::free(self.buf.as_ptr().cast()) };
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn keeps_colour_within_alpha() {
+ fn check<T: Sample>(half: T) {
+ let (zero, max) = (T::default(), T::MAX);
+ let mut px = Pixels::<T>::zeroed(16, 16, size_of::<[T; 4]>()).unwrap();
+ // Opaque black against translucent white rings both ways across the edge.
+ for (i, p) in px.pixels_mut().iter_mut().enumerate() {
+ *p = if i % 16 < 8 {
+ [zero, zero, zero, max]
+ } else {
+ [max, max, max, half]
+ };
+ }
+ let px = px.premultiply_resize(true, Some((5, 5))).unwrap();
+ for p in px.pixels() {
+ assert!(p[..3].iter().all(|&c| c <= p[3]), "{p:?}");
+ }
+ }
+ check(128u8);
+ check(32768u16);
+ }
+}
diff --git a/native/crates/native_core/src/jxl.rs b/native/crates/native_core/src/jxl.rs
new file mode 100644
index 0000000000..afdd1957f1
--- /dev/null
+++ b/native/crates/native_core/src/jxl.rs
@@ -0,0 +1,235 @@
+use std::sync::Arc;
+use std::sync::atomic::{AtomicBool, Ordering};
+
+use jxl::api::{
+ AlphaMode, Endianness, Event, ExtraChannel, JxlColorEncoding, JxlColorProfile, JxlColorType,
+ JxlDataFormat, JxlDecoder, JxlDecoderOptions, JxlOutputBuffer, JxlParallelRunner,
+ JxlParallelRunnerFun, JxlPixelFormat, JxlPrimaries, JxlTransferFunction, JxlWhitePoint,
+};
+use moxcms::{Layout, MatrixCoefficients, Transform16BitExecutor};
+use rayon::prelude::*;
+
+use crate::color::{self, CicpColorPrimaries, CicpProfile, Target, TransferCharacteristics};
+use crate::image::{Image, Pixels, Samples};
+use crate::resize;
+
+/// A JPEG XL image whose header has been read.
+pub struct Jxl<'a> {
+ decoder: JxlDecoder,
+ input: &'a [u8],
+ /// The space of the output.
+ cicp: CicpProfile,
+ /// Converts wide samples to sRGB when the output is not in the source space.
+ transform: Option<Arc<Transform16BitExecutor>>,
+}
+
+/// Runs on the global pool with up to this many threads, or inline with one.
+struct Rayon(usize);
+
+impl JxlParallelRunner for Rayon {
+ fn run(&mut self, num: usize, fun: &JxlParallelRunnerFun<'_>) -> jxl::error::Result<()> {
+ (0..num).into_par_iter().try_for_each(fun)
+ }
+
+ fn num_threads(&self) -> usize {
+ self.0
+ }
+}
+
+/// Stops handing out work once the flag is set.
+struct Cancellable<'a> {
+ pool: Rayon,
+ cancelled: &'a AtomicBool,
+}
+
+impl JxlParallelRunner for Cancellable<'_> {
+ fn run(&mut self, num: usize, fun: &JxlParallelRunnerFun<'_>) -> jxl::error::Result<()> {
+ self.pool.run(num, fun)
+ }
+
+ fn num_threads(&self) -> usize {
+ self.pool.num_threads()
+ }
+
+ // jxl-rs only calls this, and each of its tasks loops many items, so check cancellation here.
+ fn run_ordered(
+ &mut self,
+ num: usize,
+ max_threads: Option<usize>,
+ fun: &JxlParallelRunnerFun<'_>,
+ ) -> jxl::error::Result<()> {
+ self.pool.run_ordered(num, max_threads, &|i| {
+ if self.cancelled.load(Ordering::Relaxed) {
+ // Any error stops the decode.
+ return Err(jxl::error::Error::ApiUsageError("cancelled"));
+ }
+ fun(i)
+ })
+ }
+}
+
+impl<'a> Jxl<'a> {
+ pub fn open(mut input: &'a [u8], target: Target) -> Option<Self> {
+ let mut options = JxlDecoderOptions::default();
+ // Colour transforms need straight alpha; premultiplying happens last.
+ options.alpha_mode = AlphaMode::Unpremultiplied;
+ let mut decoder = JxlDecoder::new(options);
+ let Event::BasicInfo = decoder.process(&mut input, None, None).ok()? else {
+ return None;
+ };
+ let extra = decoder.basic_info()?.extra_channels.len();
+ // An integer pixel format ensures the output profile below is sRGB rather than float scRGB.
+ decoder
+ .set_pixel_format(JxlPixelFormat::rgba8(extra))
+ .ok()?;
+ let profile = decoder.output_color_profile()?;
+ if let Some(cicp) = cicp(profile)
+ && (target == Target::Source || color::is_srgb(cicp))
+ {
+ if color::is_hdr(cicp) {
+ decoder
+ .set_pixel_format(JxlPixelFormat::rgba16(extra))
+ .ok()?;
+ }
+ return Some(Self {
+ decoder,
+ input,
+ cicp,
+ transform: None,
+ });
+ }
+ let (transform, layout) = color::to_srgb(&profile.try_as_icc()?)?;
+ decoder
+ .set_pixel_format(JxlPixelFormat {
+ color_type: match layout {
+ Layout::GrayAlpha => JxlColorType::GrayscaleAlpha,
+ _ => JxlColorType::Rgba,
+ },
+ color_data_format: Some(JxlDataFormat::U16 {
+ endianness: Endianness::native(),
+ bit_depth: 12,
+ }),
+ extra_channel_format: vec![None; extra],
+ })
+ .ok()?;
+ Some(Self {
+ decoder,
+ input,
+ cicp: color::SRGB,
+ transform: Some(transform),
+ })
+ }
+
+ /// Decodes the first frame unless cancelled, downscaled to cover `target` pixels and to fit
+ /// within `max_pixel_size`.
+ pub fn decode(
+ mut self,
+ target: Option<(usize, usize)>,
+ max_pixel_size: usize,
+ cancelled: &AtomicBool,
+ ) -> Option<Image> {
+ let info = self.decoder.basic_info()?;
+ let (w, h) = info.size;
+ let alpha = info
+ .extra_channels
+ .iter()
+ .any(|ec| ec.ec_type == ExtraChannel::Alpha);
+ let format = self.decoder.current_pixel_format()?;
+ let pixel_bytes =
+ format.color_type.samples_per_pixel() * format.color_data_format?.bytes_per_sample();
+ let stride = w.checked_mul(pixel_bytes)?;
+ let size = resize::cover((w, h), target, max_pixel_size);
+ let samples = if color::is_hdr(self.cicp) {
+ let mut rgba = Pixels::zeroed(w, h, pixel_bytes)?;
+ self.decode_frame(&mut rgba, stride, cancelled)?;
+ Samples::U16(rgba.premultiply_resize(alpha, size)?)
+ } else {
+ // The buffer is sized for the wide decode and ends up holding the RGBA8 result at its
+ // front.
+ let mut rgba = Pixels::zeroed(w, h, pixel_bytes)?;
+ self.decode_frame(&mut rgba, stride, cancelled)?;
+ if let Some(transform) = &self.transform {
+ color::convert_bands(transform.as_ref(), &mut rgba, stride, cancelled)?;
+ }
+ Samples::U8(rgba.premultiply_resize(alpha, size)?)
+ };
+ Some(Image {
+ cicp: self.cicp,
+ samples,
+ })
+ }
+
+ fn decode_frame<T>(
+ &mut self,
+ rgba: &mut Pixels<T>,
+ stride: usize,
+ cancelled: &AtomicBool,
+ ) -> Option<()> {
+ let (w, h) = (rgba.width(), rgba.height());
+ let buf = rgba.bytes_mut();
+ // The pool only pays off for large images, since thumbs are already decoded in parallel.
+ let threads = if w * h > 512 * 512 {
+ rayon::current_num_threads()
+ } else {
+ 1
+ };
+ let mut runner = Cancellable {
+ pool: Rayon(threads),
+ cancelled,
+ };
+ loop {
+ let output = &mut [JxlOutputBuffer::new(buf, h, stride)];
+ match self
+ .decoder
+ .process(&mut self.input, Some(output), Some(&mut runner))
+ .ok()?
+ {
+ Event::FrameHeader => {}
+ // Resizing costs ~10% of a thumbnail decode, wasted on tiles that scrolled away
+ // meanwhile.
+ Event::FrameComplete { .. } => {
+ return (!cancelled.load(Ordering::Relaxed)).then_some(());
+ }
+ _ => return None,
+ }
+ }
+ }
+}
+
+/// The space of the decoded samples, if CICP can describe it.
+fn cicp(profile: &JxlColorProfile) -> Option<CicpProfile> {
+ let JxlColorProfile::Simple(encoding) = profile else {
+ return None;
+ };
+ let (primaries, transfer_function) = match encoding {
+ JxlColorEncoding::RgbColorSpace {
+ white_point: JxlWhitePoint::D65,
+ primaries,
+ transfer_function,
+ ..
+ } => (primaries, transfer_function),
+ // Gray samples decode to neutral RGB, which any primaries reproduce.
+ JxlColorEncoding::GrayscaleColorSpace {
+ white_point: JxlWhitePoint::D65,
+ transfer_function,
+ ..
+ } => (&JxlPrimaries::SRGB, transfer_function),
+ _ => return None,
+ };
+ Some(CicpProfile {
+ color_primaries: match primaries {
+ JxlPrimaries::SRGB => CicpColorPrimaries::Bt709,
+ JxlPrimaries::BT2100 => CicpColorPrimaries::Bt2020,
+ JxlPrimaries::P3 => CicpColorPrimaries::Smpte432,
+ JxlPrimaries::Chromaticities { .. } => return None,
+ },
+ transfer_characteristics: match transfer_function {
+ JxlTransferFunction::SRGB => TransferCharacteristics::Srgb,
+ JxlTransferFunction::PQ => TransferCharacteristics::Smpte2084,
+ JxlTransferFunction::HLG => TransferCharacteristics::Hlg,
+ _ => return None,
+ },
+ matrix_coefficients: MatrixCoefficients::Identity,
+ full_range: true,
+ })
+}
diff --git a/native/crates/native_core/src/lib.rs b/native/crates/native_core/src/lib.rs
index 258913d03b..f5c08829f0 100644
--- a/native/crates/native_core/src/lib.rs
+++ b/native/crates/native_core/src/lib.rs
@@ -1,4 +1,8 @@
+pub mod color;
pub mod convert;
+pub mod image;
+pub mod jxl;
+mod resize;
pub mod rotate;
pub mod thumbhash;
diff --git a/native/crates/native_core/src/resize.rs b/native/crates/native_core/src/resize.rs
new file mode 100644
index 0000000000..7ad83925ce
--- /dev/null
+++ b/native/crates/native_core/src/resize.rs
@@ -0,0 +1,103 @@
+use std::fmt::Debug;
+use std::sync::{Arc, LazyLock};
+
+use pic_scale::{
+ ImageSize, ImageStore, ImageStoreMut, Resampling, ResamplingFunction, Scaler, WorkloadStrategy,
+};
+use quick_cache::sync::Cache;
+
+use crate::image::Pixels;
+
+/// Reuse the resampling plan for a given src-dst image size pair.
+type Plans<T> = Cache<[usize; 4], Arc<Resampling<T, 4>>>;
+
+const PLANS: usize = 16;
+
+pub(crate) trait Lanczos: Copy + Default + Debug + 'static {
+ fn plan(from: ImageSize, to: ImageSize) -> Option<Arc<Resampling<Self, 4>>>;
+
+ fn cache() -> &'static Plans<Self>;
+}
+
+impl Lanczos for u8 {
+ fn plan(from: ImageSize, to: ImageSize) -> Option<Arc<Resampling<Self, 4>>> {
+ // The 8-bit speed path brightens by up to two levels and is barely faster.
+ Scaler::new(ResamplingFunction::Lanczos3)
+ .set_workload_strategy(WorkloadStrategy::PreferQuality)
+ .plan_rgba_resampling(from, to, false)
+ .ok()
+ }
+
+ fn cache() -> &'static Plans<Self> {
+ static CACHE: LazyLock<Plans<u8>> = LazyLock::new(|| Cache::new(PLANS));
+ &CACHE
+ }
+}
+
+impl Lanczos for u16 {
+ fn plan(from: ImageSize, to: ImageSize) -> Option<Arc<Resampling<Self, 4>>> {
+ Scaler::new(ResamplingFunction::Lanczos3)
+ .plan_rgba_resampling16(from, to, false, 16)
+ .ok()
+ }
+
+ fn cache() -> &'static Plans<Self> {
+ static CACHE: LazyLock<Plans<u16>> = LazyLock::new(|| Cache::new(PLANS));
+ &CACHE
+ }
+}
+
+/// The size of `size` scaled down to cover `target` and to fit within `max_pixel_size`, if that
+/// is smaller.
+pub(crate) fn cover(
+ (w, h): (usize, usize),
+ target: Option<(usize, usize)>,
+ max_pixel_size: usize,
+) -> Option<(usize, usize)> {
+ // Scales as side ratios, so the side that sets the scale lands exactly on its limit.
+ let fit = (max_pixel_size, w.max(h));
+ let (num, den) = match target {
+ Some((tw, th)) => {
+ let cover = if tw * h >= th * w { (tw, w) } else { (th, h) };
+ if cover.0 * fit.1 < fit.0 * cover.1 {
+ cover
+ } else {
+ fit
+ }
+ }
+ None => fit,
+ };
+ (num < den).then(|| ((w * num).div_ceil(den), (h * num).div_ceil(den)))
+}
+
+/// Resamples `src` into `dst` with Lanczos3.
+pub(crate) fn lanczos<T: Lanczos>(src: &Pixels<T>, dst: &mut Pixels<T>) -> Option<()> {
+ let [w, h, tw, th] = [src.width(), src.height(), dst.width(), dst.height()];
+ let from = ImageStore::borrow(src.pixels().as_flattened(), w, h).ok()?;
+ let mut to = ImageStoreMut::borrow(dst.pixels_mut().as_flattened_mut(), tw, th).ok()?;
+ to.bit_depth = 8 * size_of::<T>();
+ T::cache()
+ .get_or_insert_with(&[w, h, tw, th], || {
+ T::plan(from.size(), to.size()).ok_or(())
+ })
+ .ok()?
+ .resample(&from, &mut to)
+ .ok()
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn covers_within_max_pixel_size() {
+ // A 720p thumbnail for a 266 px tile and for a larger target, and panoramas past the limit.
+ assert_eq!(cover((960, 720), Some((266, 266)), 16384), Some((355, 266)));
+ assert_eq!(cover((960, 720), Some((1000, 1000)), 16384), None);
+ assert_eq!(cover((40000, 1000), None, 16384), Some((16384, 410)));
+ assert_eq!(
+ cover((1000, 40000), Some((20000, 20000)), 16384),
+ Some((410, 16384))
+ );
+ }
+}
diff --git a/native/crates/native_core/tests/fixtures/animated.jxl b/native/crates/native_core/tests/fixtures/animated.jxl
new file mode 100644
index 0000000000..f4cccc2814
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/animated.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/gray.jxl b/native/crates/native_core/tests/fixtures/gray.jxl
new file mode 100644
index 0000000000..eb7a009717
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/gray.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/gray_icc.jxl b/native/crates/native_core/tests/fixtures/gray_icc.jxl
new file mode 100644
index 0000000000..4c75718d74
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/gray_icc.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/jpeg_icc.jxl b/native/crates/native_core/tests/fixtures/jpeg_icc.jxl
new file mode 100644
index 0000000000..855a98b97a
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/jpeg_icc.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/large.jxl b/native/crates/native_core/tests/fixtures/large.jxl
new file mode 100644
index 0000000000..950640fd4e
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/large.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/p3.jxl b/native/crates/native_core/tests/fixtures/p3.jxl
new file mode 100644
index 0000000000..9465685b88
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/p3.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/pq_alpha.jxl b/native/crates/native_core/tests/fixtures/pq_alpha.jxl
new file mode 100644
index 0000000000..11cdd5b426
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/pq_alpha.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/pq_grays.jxl b/native/crates/native_core/tests/fixtures/pq_grays.jxl
new file mode 100644
index 0000000000..b04b6009e5
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/pq_grays.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/srgb_alpha.jxl b/native/crates/native_core/tests/fixtures/srgb_alpha.jxl
new file mode 100644
index 0000000000..3d8942e933
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/srgb_alpha.jxl differ
diff --git a/native/crates/native_core/tests/fixtures/xyb_icc.jxl b/native/crates/native_core/tests/fixtures/xyb_icc.jxl
new file mode 100644
index 0000000000..e7e16f3df2
Binary files /dev/null and b/native/crates/native_core/tests/fixtures/xyb_icc.jxl differ
diff --git a/native/crates/native_core/tests/jxl.rs b/native/crates/native_core/tests/jxl.rs
new file mode 100644
index 0000000000..17b39825da
--- /dev/null
+++ b/native/crates/native_core/tests/jxl.rs
@@ -0,0 +1,200 @@
+use std::sync::atomic::AtomicBool;
+
+use native_core::color::{CicpColorPrimaries, CicpProfile, Target, TransferCharacteristics};
+use native_core::image::{Image, Pixels, Samples};
+use native_core::jxl::Jxl;
+
+fn decode(data: &[u8], target: Target) -> Option<Image> {
+ Jxl::open(data, target)?.decode(None, 16384, &AtomicBool::new(false))
+}
+
+fn decode_srgb(data: &[u8]) -> Option<Pixels<u8>> {
+ let Image {
+ cicp:
+ CicpProfile {
+ color_primaries: CicpColorPrimaries::Bt709,
+ transfer_characteristics: TransferCharacteristics::Srgb,
+ ..
+ },
+ samples: Samples::U8(rgba),
+ } = decode(data, Target::Srgb)?
+ else {
+ panic!("not decoded to sRGB");
+ };
+ Some(rgba)
+}
+
+#[test]
+fn decodes_to_premultiplied_srgb() {
+ let cases: [(&str, &[u8], [u8; 8]); 5] = [
+ (
+ "sRGB with alpha",
+ include_bytes!("fixtures/srgb_alpha.jxl"),
+ [128, 0, 0, 128, 0, 0, 255, 255],
+ ),
+ (
+ "Display P3",
+ include_bytes!("fixtures/p3.jxl"),
+ [255, 0, 0, 255, 255, 255, 255, 255],
+ ),
+ (
+ "grayscale",
+ include_bytes!("fixtures/gray.jxl"),
+ [0, 0, 0, 255, 128, 128, 128, 255],
+ ),
+ (
+ "grayscale with ICC profile",
+ include_bytes!("fixtures/gray_icc.jxl"),
+ [81, 81, 81, 255, 203, 203, 203, 255],
+ ),
+ (
+ "animation",
+ include_bytes!("fixtures/animated.jxl"),
+ [255, 0, 0, 255, 255, 0, 0, 255],
+ ),
+ ];
+ for (name, data, expected) in cases {
+ let rgba = decode_srgb(data).expect(name);
+ assert_eq!(
+ (rgba.width(), rgba.height(), rgba.pixels().as_flattened()),
+ (2, 1, &expected[..]),
+ "{name}"
+ );
+ }
+}
+
+#[test]
+fn converts_lossy_icc_to_srgb() {
+ // jxl-rs renders XYB straight to sRGB, while a JPEG transcode decodes in its ICC space and
+ // takes our transform.
+ for data in [
+ &include_bytes!("fixtures/xyb_icc.jxl")[..],
+ include_bytes!("fixtures/jpeg_icc.jxl"),
+ ] {
+ let rgba = decode_srgb(data).unwrap();
+ for px in rgba.pixels() {
+ assert!(
+ px.iter()
+ .zip([210, 119, 65, 255])
+ .all(|(&a, b)| a.abs_diff(b) <= 3),
+ "{px:?}"
+ );
+ }
+ }
+}
+
+#[test]
+fn tone_maps_hdr() {
+ let rgba = decode_srgb(include_bytes!("fixtures/pq_grays.jxl")).unwrap();
+ let levels: Vec<u8> = rgba
+ .pixels()
+ .iter()
+ .map(|px| {
+ assert!(
+ px[..3].iter().max().unwrap() - px[..3].iter().min().unwrap() <= 4,
+ "{px:?} is not neutral"
+ );
+ px[1]
+ })
+ .collect();
+ assert!(
+ levels.is_sorted_by(|a, b| a < b) && levels[2] < 255,
+ "{levels:?}"
+ );
+}
+
+#[test]
+fn keeps_spaces_that_cicp_describes() {
+ let pq = decode(include_bytes!("fixtures/pq_alpha.jxl"), Target::Source);
+ let Some(Image {
+ cicp:
+ CicpProfile {
+ color_primaries: CicpColorPrimaries::Bt2020,
+ transfer_characteristics: TransferCharacteristics::Smpte2084,
+ ..
+ },
+ samples: Samples::U16(rgba),
+ }) = pq
+ else {
+ panic!("not decoded to BT.2100 PQ");
+ };
+ assert_eq!(
+ rgba.pixels(),
+ [[20000, 15000, 10000, 32768], [49271, 49271, 49271, 65535]]
+ );
+ let p3 = decode(include_bytes!("fixtures/p3.jxl"), Target::Source);
+ let Some(Image {
+ cicp:
+ CicpProfile {
+ color_primaries: CicpColorPrimaries::Smpte432,
+ transfer_characteristics: TransferCharacteristics::Srgb,
+ ..
+ },
+ samples: Samples::U8(rgba),
+ }) = p3
+ else {
+ panic!("not decoded to Display P3");
+ };
+ assert_eq!(rgba.pixels(), [[255, 0, 0, 255], [255, 255, 255, 255]]);
+}
+
+#[test]
+fn converts_other_spaces_to_srgb() {
+ let data = include_bytes!("fixtures/gray_icc.jxl");
+ let Some(Image {
+ cicp:
+ CicpProfile {
+ color_primaries: CicpColorPrimaries::Bt709,
+ transfer_characteristics: TransferCharacteristics::Srgb,
+ ..
+ },
+ samples: Samples::U8(rgba),
+ }) = decode(data, Target::Source)
+ else {
+ panic!("not decoded to sRGB");
+ };
+ assert_eq!(rgba.pixels(), decode_srgb(data).unwrap().pixels());
+}
+
+#[test]
+fn downscales_to_cover() {
+ let data = include_bytes!("fixtures/jpeg_icc.jxl");
+ let Some(Image {
+ samples: Samples::U8(rgba),
+ ..
+ }) =
+ Jxl::open(data, Target::Srgb)
+ .unwrap()
+ .decode(Some((8, 4)), 16384, &AtomicBool::new(false))
+ else {
+ panic!("not decoded to 8 bits");
+ };
+ // 16x16 covers 8x4 at 8x8, read through the wide decode buffer.
+ assert_eq!((rgba.width(), rgba.height()), (8, 8));
+ for px in rgba.pixels() {
+ assert!(
+ px.iter()
+ .zip([210, 119, 65, 255])
+ .all(|(&a, b)| a.abs_diff(b) <= 3),
+ "{px:?}"
+ );
+ }
+}
+
+#[test]
+fn rejects_truncated_input() {
+ let data = include_bytes!("fixtures/srgb_alpha.jxl");
+ assert!(decode_srgb(&data[..data.len() / 2]).is_none());
+}
+
+#[test]
+fn stops_when_cancelled() {
+ let data = include_bytes!("fixtures/large.jxl");
+ let decode = |cancelled| {
+ Jxl::open(data, Target::Srgb)
+ .unwrap()
+ .decode(None, 16384, &AtomicBool::new(cancelled))
+ .is_some()
+ };
+ assert_eq!((decode(false), decode(true)), (true, false));
+}
diff --git a/native/crates/native_core_ffi/build.rs b/native/crates/native_core_ffi/build.rs
index e829519f9d..219b607a92 100644
--- a/native/crates/native_core_ffi/build.rs
+++ b/native/crates/native_core_ffi/build.rs
@@ -3,6 +3,7 @@ use std::path::Path;
fn main() {
// Files that define exported items. A directory here would make Flutter rerun the hook every build.
println!("cargo:rerun-if-changed=src/lib.rs");
+ println!("cargo:rerun-if-changed=src/jxl.rs");
println!("cargo:rerun-if-changed=src/thumbhash.rs");
println!("cargo:rerun-if-changed=cbindgen.toml");
let crate_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap();
diff --git a/native/crates/native_core_ffi/src/jxl.rs b/native/crates/native_core_ffi/src/jxl.rs
new file mode 100644
index 0000000000..f970a45d69
--- /dev/null
+++ b/native/crates/native_core_ffi/src/jxl.rs
@@ -0,0 +1,162 @@
+use std::sync::atomic::{AtomicBool, Ordering};
+use std::{ptr, slice};
+
+use libc::size_t;
+
+#[cfg(target_os = "android")]
+use jni::{
+ EnvUnowned,
+ errors::ThrowRuntimeExAndDefault,
+ objects::{JByteBuffer, JClass, JIntArray},
+ sys::{jint, jlong},
+};
+use native_core::color::Target;
+use native_core::image::{Image, Samples};
+use native_core::jxl::Jxl;
+
+use super::guard;
+
+/// The size and colour space of decoded pixels.
+#[repr(C)]
+#[derive(Default)]
+pub struct NativeCoreImageInfo {
+ pub width: i32,
+ pub height: i32,
+ /// 8 or 16, for samples in native byte order.
+ pub bit_depth: u8,
+ /// ITU-T H.273 colour primaries.
+ pub primaries: u8,
+ /// ITU-T H.273 transfer characteristics.
+ pub transfer: u8,
+}
+
+/// Decodes a JPEG XL image to premultiplied RGBA in sRGB, or with `keep_space`, in the image's own
+/// space when it has BT.709, BT.2020 or Display P3 primaries with the sRGB, PQ or HLG transfer.
+/// Unless either is 0, it is downscaled to cover `target_width` x `target_height`, and it never
+/// exceeds `max_pixel_size` pixels per side. `info` gets the size and space. The caller frees the
+/// returned buffer with free(). Returns null on failure, or once `native_core_cancel` was called on
+/// `cancelled`.
+///
+/// # Safety
+/// `data` must be null or readable for `len` bytes, at most `isize::MAX`.
+/// `cancelled` must be null or valid for the call and only changed through `native_core_cancel`.
+/// `info` must be null or a valid writable pointer, outside `data`.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn native_core_jxl_decode(
+ data: *const u8,
+ len: size_t,
+ keep_space: bool,
+ target_width: u32,
+ target_height: u32,
+ max_pixel_size: u32,
+ cancelled: *mut bool,
+ info: *mut NativeCoreImageInfo,
+) -> *mut u8 {
+ guard(ptr::null_mut(), || {
+ if data.is_null() || cancelled.is_null() || info.is_null() {
+ return ptr::null_mut();
+ }
+ // SAFETY: the caller keeps the flag alive for the call and only changes it atomically.
+ let cancelled = unsafe { AtomicBool::from_ptr(cancelled) };
+ // SAFETY: the caller provides a buffer readable for len bytes.
+ let input = unsafe { slice::from_raw_parts(data, len) };
+ let target = if keep_space {
+ Target::Source
+ } else {
+ Target::Srgb
+ };
+ let cover = (target_width > 0 && target_height > 0)
+ .then_some((target_width as usize, target_height as usize));
+ let Some(Image { cicp, samples }) = Jxl::open(input, target)
+ .and_then(|image| image.decode(cover, max_pixel_size as usize, cancelled))
+ else {
+ return ptr::null_mut();
+ };
+ let (width, height, bit_depth, pixels) = match samples {
+ Samples::U8(rgba) => (rgba.width(), rgba.height(), 8, rgba.into_raw()),
+ Samples::U16(rgba) => (rgba.width(), rgba.height(), 16, rgba.into_raw()),
+ };
+ // SAFETY: the output is writable.
+ unsafe {
+ *info = NativeCoreImageInfo {
+ width: width as i32,
+ height: height as i32,
+ bit_depth,
+ primaries: cicp.color_primaries as u8,
+ transfer: cicp.transfer_characteristics as u8,
+ };
+ }
+ pixels
+ })
+}
+
+/// Asks the decode given `flag` to stop early.
+///
+/// # Safety
+/// `flag` must be null or valid and otherwise only read by `native_core_jxl_decode`.
+#[unsafe(no_mangle)]
+pub unsafe extern "C" fn native_core_cancel(flag: *mut bool) {
+ if !flag.is_null() {
+ // SAFETY: the caller guarantees the flag is valid and only accessed atomically.
+ unsafe { AtomicBool::from_ptr(flag) }.store(true, Ordering::Relaxed);
+ }
+}
+
+#[cfg(target_os = "android")]
+#[unsafe(no_mangle)]
+pub extern "system" fn Java_app_alextran_immich_NativeImage_decodeJxl<'caller>(
+ mut env: EnvUnowned<'caller>,
+ _class: JClass<'caller>,
+ data: jlong,
+ length: jint,
+ target_width: jint,
+ target_height: jint,
+ max_pixel_size: jint,
+ cancelled: JByteBuffer<'caller>,
+ info: JIntArray<'caller>,
+) -> jlong {
+ env.with_env(|env| -> jni::errors::Result<jlong> {
+ let flag = env.get_direct_buffer_address(&cancelled)?.cast::<bool>();
+ let mut decoded = NativeCoreImageInfo::default();
+ // SAFETY: Kotlin passes a live native buffer of `length` bytes and a direct buffer it only
+ // changes through `cancel`, and the output is valid.
+ let rgba = unsafe {
+ native_core_jxl_decode(
+ data as *const u8,
+ length as size_t,
+ false,
+ target_width as u32,
+ target_height as u32,
+ max_pixel_size as u32,
+ flag,
+ &mut decoded,
+ )
+ };
+ if !rgba.is_null()
+ && let Err(err) =
+ info.set_region(env, 0, &[decoded.width, decoded.height, decoded.width * 4])
+ {
+ // SAFETY: this malloc buffer has not been transferred to Kotlin.
+ unsafe { libc::free(rgba.cast()) };
+ return Err(err);
+ }
+ Ok(rgba as jlong)
+ })
+ .resolve::<ThrowRuntimeExAndDefault>()
+}
+
+#[cfg(target_os = "android")]
+#[unsafe(no_mangle)]
+pub extern "system" fn Java_app_alextran_immich_NativeImage_cancel<'caller>(
+ mut env: EnvUnowned<'caller>,
+ _class: JClass<'caller>,
+ flag: JByteBuffer<'caller>,
+) {
+ env.with_env(|env| -> jni::errors::Result<()> {
+ let flag = env.get_direct_buffer_address(&flag)?.cast::<bool>();
+ // SAFETY: the buffer is the live one-byte flag Kotlin passed to `decodeJxl`.
+ unsafe { native_core_cancel(flag) };
+ Ok(())
+ })
+ .resolve::<ThrowRuntimeExAndDefault>();
+}
diff --git a/native/crates/native_core_ffi/src/lib.rs b/native/crates/native_core_ffi/src/lib.rs
index ccb80985aa..864736d963 100644
--- a/native/crates/native_core_ffi/src/lib.rs
+++ b/native/crates/native_core_ffi/src/lib.rs
@@ -9,6 +9,7 @@ mod android;
mod bitmap;
#[cfg(target_os = "android")]
mod convert;
+mod jxl;
#[cfg(target_os = "android")]
mod rotate;
mod thumbhash;
diff --git a/native/native_core/NOTICES b/native/native_core/NOTICES
index d26bf7933a..7fd3d7d121 100644
--- a/native/native_core/NOTICES
+++ b/native/native_core/NOTICES
@@ -1,11 +1,23 @@
+array-init
+byteorder
bytes
cfg-if
combine
+crossbeam-deque
+crossbeam-epoch
+crossbeam-utils
+either
+equivalent
+hashbrown
jni
jni-sys
libc
log
memchr
+num-traits
+quick_cache
+rayon
+rayon-core
simd_cesu8
simdutf8
thiserror
@@ -18,6 +30,13 @@ Copyright (c) 2015 The rust-jni-sys Developers
Copyright (c) The Rust Project Developers
Copyright (c) 2014 The Rust Project Developers
Copyright (c) 2015 Andrew Gallant
+Copyright (c) 2017-2020 The array-init developers
+Copyright (c) 2019 The Crossbeam Project Developers
+Copyright (c) 2015
+Copyright (c) 2010 The Rust Project Developers
+Copyright (c) 2016--2023
+Copyright (c) 2016 Amanieu d'Antras
+Copyright (c) 2022 Arthur Silva
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
@@ -36,3 +55,40 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
+
+--------------------------------------------------------------------------------
+jxl
+jxl_simd
+jxl_transforms
+moxcms
+pic-scale
+pxfm
+
+Copyright (c) the JPEG XL Project Authors.
+Copyright (c) Radzivon Bartoshyk.
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice, this
+ list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+ this list of conditions and the following disclaimer in the documentation
+ and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.