Skip to content

Commit

Permalink
Support grey-scale image and update libjxl to 0.3.7+
Browse files Browse the repository at this point in the history
  • Loading branch information
yllan committed Jul 12, 2021
1 parent 27206b0 commit d036b8c
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 467 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

libjxl

# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
Expand Down
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "jpeg-xl"]
path = jpeg-xl
url = https://gitlab.com/yllan_tw/jpeg-xl.git
url = https://github.com/libjxl/libjxl.git
566 changes: 113 additions & 453 deletions JXLook.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion JXLook/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<key>CFBundleShortVersionString</key>
<string>$(APP_VERSION)</string>
<key>CFBundleVersion</key>
<string>26ad786</string>
<string>1036</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.photography</string>
<key>LSMinimumSystemVersion</key>
Expand Down
33 changes: 25 additions & 8 deletions JXLook/JXL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ struct JXL {
Swift.print("Cannot get basic info")
break parsingLoop
}
if infoPtr.pointee.num_color_channels == 1 {
format = JxlPixelFormat(num_channels: 1, data_type: infoPtr.pointee.bits_per_sample == 16 ? JXL_TYPE_UINT16 : JXL_TYPE_UINT8, endianness: JXL_NATIVE_ENDIAN, align: 0)
}
Swift.print("basic info: \(infoPtr.pointee)")
case JXL_DEC_SUCCESS:
return true
Expand All @@ -73,16 +76,30 @@ struct JXL {

case JXL_DEC_FULL_IMAGE:
let info = infoPtr.pointee
let colorSpace = icc.flatMap({ NSColorSpace(iccProfileData: Data(buffer: $0)) }) ?? .sRGB
if let imageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(info.xsize), pixelsHigh: Int(info.ysize), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .calibratedRGB, bytesPerRow: 4 * Int(info.xsize), bitsPerPixel: 32)?.retagging(with: colorSpace) {
imageRep.size = CGSize(width: Int(info.xsize) / 2, height: Int(info.ysize) / 2)
if let pixels = imageRep.bitmapData {
memmove(pixels, buffer.baseAddress, buffer.count)
if info.num_color_channels == 1 { // greyscale
let colorSpace: NSColorSpace = .genericGray
if let imageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(info.xsize), pixelsHigh: Int(info.ysize), bitsPerSample: Int(info.bits_per_sample), samplesPerPixel: 1, hasAlpha: false, isPlanar: false, colorSpaceName: .calibratedWhite, bytesPerRow: Int(info.bits_per_sample) / 8 * Int(info.xsize), bitsPerPixel: Int(info.bits_per_sample))?.retagging(with: colorSpace) {
imageRep.size = CGSize(width: Int(info.xsize), height: Int(info.ysize))
if let pixels = imageRep.bitmapData {
memmove(pixels, buffer.baseAddress, buffer.count)
}
let img = NSImage(size: imageRep.size)
img.addRepresentation(imageRep)
image = img
}
} else { // assume it's rgb
let colorSpace = icc.flatMap({ NSColorSpace(iccProfileData: Data(buffer: $0)) }) ?? .sRGB
if let imageRep = NSBitmapImageRep(bitmapDataPlanes: nil, pixelsWide: Int(info.xsize), pixelsHigh: Int(info.ysize), bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true, isPlanar: false, colorSpaceName: .calibratedRGB, bytesPerRow: 4 * Int(info.xsize), bitsPerPixel: 32)?.retagging(with: colorSpace) {
imageRep.size = CGSize(width: Int(info.xsize), height: Int(info.ysize))
if let pixels = imageRep.bitmapData {
memmove(pixels, buffer.baseAddress, buffer.count)
}
let img = NSImage(size: imageRep.size)
img.addRepresentation(imageRep)
image = img
}
let img = NSImage(size: imageRep.size)
img.addRepresentation(imageRep)
image = img
}


case JXL_DEC_NEED_IMAGE_OUT_BUFFER:
var outputBufferSize: Int = 0
Expand Down
2 changes: 1 addition & 1 deletion JXLookTests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>26ad786</string>
<string>1036</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion JXLookUITests/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>26ad786</string>
<string>1036</string>
</dict>
</plist>
2 changes: 1 addition & 1 deletion JXQuickLook/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<key>CFBundleShortVersionString</key>
<string>$(APP_VERSION)</string>
<key>CFBundleVersion</key>
<string>26ad786</string>
<string>1036</string>
<key>LSMinimumSystemVersion</key>
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
<key>NSExtension</key>
Expand Down
13 changes: 12 additions & 1 deletion JXQuickLook/PreviewViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,21 @@ class PreviewViewController: NSViewController, QLPreviewingController {
func preparePreviewOfFile(at url: URL, completionHandler handler: @escaping (Error?) -> Void) {

// Add the supported content types to the QLSupportedContentTypes array in the Info.plist of the extension.

// Perform any setup necessary in order to prepare the view.
if let img = try? JXL.parse(data: Data(contentsOf: url)) {
imageView.image = img
if let window = self.view.window {
let maxWindowFrame = window.constrainFrameRect(CGRect(origin: CGPoint.zero, size: CGSize(width: img.size.width, height: img.size.height)), to: window.screen)
Swift.print(maxWindowFrame)
if img.size.width > maxWindowFrame.width || img.size.height > maxWindowFrame.height {
let ratio = min(maxWindowFrame.width / img.size.width, maxWindowFrame.height / img.size.height)
let newSize = CGSize(width: max(300, img.size.width * ratio), height: max(300, img.size.height * ratio))
self.preferredContentSize = newSize
} else {
self.preferredContentSize = img.size
}

}
} else {
handler(JXLError.cannotDecode)
}
Expand Down
25 changes: 25 additions & 0 deletions build_libjxl.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

git clone https://github.com/libjxl/libjxl.git --recursive
pushd libjxl

mkdir -p build
pushd build


CMAKE_OSX_ARCHITECTURES='x86_64;arm64' cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF ..
CMAKE_OSX_ARCHITECTURES='x86_64;arm64' cmake --build . --target jxl-static -- -j
CMAKE_OSX_ARCHITECTURES='x86_64;arm64' cmake --build . --target jxl_threads-static -- -j

popd
popd

mkdir -p jpeg-xl/lib
mkdir -p jpeg-xl/include/jxl
cp -R libjxl/build/lib/libjxl*.a jpeg-xl/lib
cp -R libjxl/build/third_party/libskcms.a jpeg-xl/lib
cp -R libjxl/build/third_party/highway/libhwy.a jpeg-xl/lib
cp -R libjxl/build/third_party/brotli/libbrotli*-static.a jpeg-xl/lib

cp -R libjxl/build/lib/include/jxl/* jpeg-xl/include/jxl/
cp -R libjxl/lib/include/jxl/* jpeg-xl/include/jxl/

0 comments on commit d036b8c

Please sign in to comment.