├── qtbuild_6.5.3 ├── .qmake.conf └── qt-jpegxl-image-plugin_alternative_appveyor.pro ├── qtbuild_6.8.3 ├── .qmake.conf └── qt-jpegxl-image-plugin_alternative_appveyor.pro ├── qtbuild_6.9.2 ├── .qmake.conf └── qt-jpegxl-image-plugin_alternative_appveyor.pro ├── .qmake.conf ├── src ├── jpegxl.json ├── jxl.desktop ├── util_p.h ├── main.cpp ├── CMakeLists.txt ├── qjpegxlhandler_p.h ├── .clang-format └── qjpegxlhandler.cpp ├── .gitignore ├── imgs ├── qView.png ├── qimgv.png ├── PhotoQt.png ├── YACReader.png ├── digiKam.png ├── digiKam2.png ├── KPhotoAlbum.png ├── KolourPaint.png └── LXImage-Qt.png ├── testfiles ├── gwenview.png └── jpegxl-logo.jxl ├── metainfo.yaml ├── qt-jpegxl-image-plugin6.pro ├── qt-jpegxl-image-plugin.pro ├── CMakeLists.txt ├── appveyor.yml ├── README.md └── LICENSE /qtbuild_6.5.3/.qmake.conf: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /qtbuild_6.8.3/.qmake.conf: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /qtbuild_6.9.2/.qmake.conf: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.qmake.conf: -------------------------------------------------------------------------------- 1 | load(qt_build_config) 2 | 3 | -------------------------------------------------------------------------------- /src/jpegxl.json: -------------------------------------------------------------------------------- 1 | { 2 | "Keys": [ "jxl" ], 3 | "MimeTypes": [ "image/jxl" ] 4 | } 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .moc/ 2 | .obj/ 3 | .qmake.stash 4 | Makefile 5 | lib/ 6 | mkspecs/ 7 | plugins/ 8 | -------------------------------------------------------------------------------- /imgs/qView.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/qView.png -------------------------------------------------------------------------------- /imgs/qimgv.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/qimgv.png -------------------------------------------------------------------------------- /imgs/PhotoQt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/PhotoQt.png -------------------------------------------------------------------------------- /imgs/YACReader.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/YACReader.png -------------------------------------------------------------------------------- /imgs/digiKam.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/digiKam.png -------------------------------------------------------------------------------- /imgs/digiKam2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/digiKam2.png -------------------------------------------------------------------------------- /imgs/KPhotoAlbum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/KPhotoAlbum.png -------------------------------------------------------------------------------- /imgs/KolourPaint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/KolourPaint.png -------------------------------------------------------------------------------- /imgs/LXImage-Qt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/imgs/LXImage-Qt.png -------------------------------------------------------------------------------- /testfiles/gwenview.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/testfiles/gwenview.png -------------------------------------------------------------------------------- /testfiles/jpegxl-logo.jxl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/novomesk/qt-jpegxl-image-plugin/HEAD/testfiles/jpegxl-logo.jxl -------------------------------------------------------------------------------- /src/jxl.desktop: -------------------------------------------------------------------------------- 1 | [Desktop Entry] 2 | Type=Service 3 | X-KDE-ServiceTypes=QImageIOPlugins 4 | X-KDE-ImageFormat=jxl 5 | X-KDE-MimeType=image/jxl 6 | X-KDE-Read=true 7 | X-KDE-Write=true 8 | -------------------------------------------------------------------------------- /metainfo.yaml: -------------------------------------------------------------------------------- 1 | description: Qt plug-in to allow Qt and KDE based applications to read/write JXL images. 2 | platforms: 3 | - name: Linux 4 | - name: FreeBSD 5 | - name: macOS 6 | - name: Windows 7 | - name: Android 8 | -------------------------------------------------------------------------------- /qt-jpegxl-image-plugin6.pro: -------------------------------------------------------------------------------- 1 | TARGET = qjpegxl6 2 | 3 | HEADERS = src/qjpegxlhandler_p.h src/util_p.h 4 | SOURCES = src/qjpegxlhandler.cpp 5 | OTHER_FILES = src/jpegxl.json 6 | 7 | SOURCES += src/main.cpp 8 | 9 | LIBS += -ljxl -ljxl_threads -ljxl_cms -lbrotlicommon -lbrotlienc -lbrotlidec 10 | 11 | TEMPLATE = lib 12 | 13 | CONFIG += release skip_target_version_ext c++14 warn_on plugin 14 | CONFIG -= separate_debug_info debug debug_and_release force_debug_info 15 | 16 | QMAKE_TARGET_COMPANY = "Daniel Novomesky" 17 | QMAKE_TARGET_PRODUCT = "qt-jpegxl-image-plugin" 18 | QMAKE_TARGET_DESCRIPTION = "Qt plug-in to allow Qt and KDE based applications to read/write JPEG XL images." 19 | QMAKE_TARGET_COPYRIGHT = "Copyright (C) 2020-2025 Daniel Novomesky" 20 | -------------------------------------------------------------------------------- /qt-jpegxl-image-plugin.pro: -------------------------------------------------------------------------------- 1 | TARGET = qjpegxl 2 | 3 | HEADERS = src/qjpegxlhandler_p.h src/util_p.h 4 | SOURCES = src/qjpegxlhandler.cpp 5 | OTHER_FILES = src/jpegxl.json 6 | 7 | SOURCES += src/main.cpp 8 | 9 | LIBS += -ljxl -ljxl_threads -ljxl_cms -lbrotlicommon -lbrotlienc -lbrotlidec 10 | 11 | PLUGIN_TYPE = imageformats 12 | PLUGIN_CLASS_NAME = QJpegXLPlugin 13 | load(qt_plugin) 14 | 15 | CONFIG += release skip_target_version_ext c++14 warn_on 16 | CONFIG -= separate_debug_info debug debug_and_release force_debug_info 17 | 18 | QMAKE_TARGET_COMPANY = "Daniel Novomesky" 19 | QMAKE_TARGET_PRODUCT = "qt-jpegxl-image-plugin" 20 | QMAKE_TARGET_DESCRIPTION = "Qt plug-in to allow Qt and KDE based applications to read/write JPEG XL images." 21 | QMAKE_TARGET_COPYRIGHT = "Copyright (C) 2020-2025 Daniel Novomesky" 22 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | 3 | project(qt-jpegxl-image-plugin) 4 | 5 | include(FeatureSummary) 6 | find_package(ECM 5.89.0 NO_MODULE) 7 | set_package_properties(ECM PROPERTIES TYPE REQUIRED DESCRIPTION "Extra CMake Modules." URL "https://commits.kde.org/extra-cmake-modules") 8 | feature_summary(WHAT REQUIRED_PACKAGES_NOT_FOUND FATAL_ON_MISSING_REQUIRED_PACKAGES) 9 | 10 | 11 | set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH}) 12 | 13 | include(KDEInstallDirs) 14 | include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE) 15 | include(KDECMakeSettings) 16 | 17 | 18 | include(CheckIncludeFiles) 19 | 20 | set(REQUIRED_QT_VERSION 5.14.0) 21 | find_package(Qt${QT_MAJOR_VERSION}Gui ${REQUIRED_QT_VERSION} REQUIRED NO_MODULE) 22 | 23 | include(FindPkgConfig) 24 | pkg_check_modules(LibJXL REQUIRED IMPORTED_TARGET libjxl>=0.7.0) 25 | pkg_check_modules(LibJXLThreads REQUIRED IMPORTED_TARGET libjxl_threads>=0.7.0) 26 | pkg_check_modules(LibJXLCMS IMPORTED_TARGET libjxl_cms>=0.9.0) 27 | 28 | add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x050f02) 29 | add_definitions(-DKF_DISABLE_DEPRECATED_BEFORE_AND_AT=0x055900) 30 | add_subdirectory(src) 31 | 32 | feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) 33 | -------------------------------------------------------------------------------- /qtbuild_6.5.3/qt-jpegxl-image-plugin_alternative_appveyor.pro: -------------------------------------------------------------------------------- 1 | TARGET = qjpegxl6 2 | 3 | DEFINES += JXL_STATIC_DEFINE JXL_THREADS_STATIC_DEFINE 4 | 5 | INCLUDEPATH += ../libjxl/lib/include ../libjxl/build/lib/include 6 | 7 | HEADERS = ../src/qjpegxlhandler_p.h ../src/util_p.h 8 | SOURCES = ../src/qjpegxlhandler.cpp 9 | OTHER_FILES = ../src/jpegxl.json 10 | 11 | SOURCES += ../src/main.cpp 12 | 13 | LIBS_PRIVATE += ../libjxl/build/lib/jxl.lib ../libjxl/build/lib/jxl_cms.lib ../libjxl/build/lib/jxl_threads.lib ../libjxl/build/third_party/highway/hwy.lib ../libjxl/build/third_party/brotli/brotlidec.lib ../libjxl/build/third_party/brotli/brotlienc.lib ../libjxl/build/third_party/brotli/brotlicommon.lib 14 | 15 | TEMPLATE = lib 16 | 17 | CONFIG += release skip_target_version_ext c++14 warn_on plugin 18 | CONFIG -= separate_debug_info debug debug_and_release force_debug_info 19 | 20 | win32:VERSION = 0.8.1 21 | QMAKE_TARGET_COMPANY = "Daniel Novomesky" 22 | QMAKE_TARGET_PRODUCT = "qt-jpegxl-image-plugin" 23 | QMAKE_TARGET_DESCRIPTION = "Qt plug-in to allow Qt and KDE based applications to read/write JPEG XL images." 24 | QMAKE_TARGET_COPYRIGHT = "Copyright (C) 2020-2025 Daniel Novomesky" 25 | QMAKE_TARGET_COMMENTS = "Build using Qt 6.5.3" 26 | -------------------------------------------------------------------------------- /qtbuild_6.8.3/qt-jpegxl-image-plugin_alternative_appveyor.pro: -------------------------------------------------------------------------------- 1 | TARGET = qjpegxl6 2 | 3 | DEFINES += JXL_STATIC_DEFINE JXL_THREADS_STATIC_DEFINE 4 | 5 | INCLUDEPATH += ../libjxl/lib/include ../libjxl/build/lib/include 6 | 7 | HEADERS = ../src/qjpegxlhandler_p.h ../src/util_p.h 8 | SOURCES = ../src/qjpegxlhandler.cpp 9 | OTHER_FILES = ../src/jpegxl.json 10 | 11 | SOURCES += ../src/main.cpp 12 | 13 | LIBS_PRIVATE += ../libjxl/build/lib/jxl.lib ../libjxl/build/lib/jxl_cms.lib ../libjxl/build/lib/jxl_threads.lib ../libjxl/build/third_party/highway/hwy.lib ../libjxl/build/third_party/brotli/brotlidec.lib ../libjxl/build/third_party/brotli/brotlienc.lib ../libjxl/build/third_party/brotli/brotlicommon.lib 14 | 15 | TEMPLATE = lib 16 | 17 | CONFIG += release skip_target_version_ext c++17 warn_on plugin 18 | CONFIG -= separate_debug_info debug debug_and_release force_debug_info 19 | 20 | win32:VERSION = 0.8.1 21 | QMAKE_TARGET_COMPANY = "Daniel Novomesky" 22 | QMAKE_TARGET_PRODUCT = "qt-jpegxl-image-plugin" 23 | QMAKE_TARGET_DESCRIPTION = "Qt plug-in to allow Qt and KDE based applications to read/write JPEG XL images." 24 | QMAKE_TARGET_COPYRIGHT = "Copyright (C) 2020-2025 Daniel Novomesky" 25 | QMAKE_TARGET_COMMENTS = "Build using Qt 6.8.3" 26 | -------------------------------------------------------------------------------- /qtbuild_6.9.2/qt-jpegxl-image-plugin_alternative_appveyor.pro: -------------------------------------------------------------------------------- 1 | TARGET = qjpegxl6 2 | 3 | DEFINES += JXL_STATIC_DEFINE JXL_THREADS_STATIC_DEFINE 4 | 5 | INCLUDEPATH += ../libjxl/lib/include ../libjxl/build/lib/include 6 | 7 | HEADERS = ../src/qjpegxlhandler_p.h ../src/util_p.h 8 | SOURCES = ../src/qjpegxlhandler.cpp 9 | OTHER_FILES = ../src/jpegxl.json 10 | 11 | SOURCES += ../src/main.cpp 12 | 13 | LIBS_PRIVATE += ../libjxl/build/lib/jxl.lib ../libjxl/build/lib/jxl_cms.lib ../libjxl/build/lib/jxl_threads.lib ../libjxl/build/third_party/highway/hwy.lib ../libjxl/build/third_party/brotli/brotlidec.lib ../libjxl/build/third_party/brotli/brotlienc.lib ../libjxl/build/third_party/brotli/brotlicommon.lib 14 | 15 | TEMPLATE = lib 16 | 17 | CONFIG += release skip_target_version_ext c++17 warn_on plugin 18 | CONFIG -= separate_debug_info debug debug_and_release force_debug_info 19 | 20 | win32:VERSION = 0.8.1 21 | QMAKE_TARGET_COMPANY = "Daniel Novomesky" 22 | QMAKE_TARGET_PRODUCT = "qt-jpegxl-image-plugin" 23 | QMAKE_TARGET_DESCRIPTION = "Qt plug-in to allow Qt and KDE based applications to read/write JPEG XL images." 24 | QMAKE_TARGET_COPYRIGHT = "Copyright (C) 2020-2025 Daniel Novomesky" 25 | QMAKE_TARGET_COMMENTS = "Build using Qt 6.9.2" 26 | -------------------------------------------------------------------------------- /src/util_p.h: -------------------------------------------------------------------------------- 1 | /* 2 | SPDX-FileCopyrightText: 2022 Albert Astals Cid 3 | SPDX-FileCopyrightText: 2022 Mirco Miranda 4 | 5 | SPDX-License-Identifier: LGPL-2.0-or-later 6 | */ 7 | 8 | #ifndef UTIL_P_H 9 | #define UTIL_P_H 10 | 11 | #include 12 | 13 | #include 14 | 15 | #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 16 | #include 17 | #endif 18 | 19 | // QVector uses some extra space for stuff, hence the 32 here suggested by Thiago Macieira 20 | static constexpr int kMaxQVectorSize = std::numeric_limits::max() - 32; 21 | 22 | // On Qt 6 to make the plugins fail to allocate if the image size is greater than QImageReader::allocationLimit() 23 | // it is necessary to allocate the image with QImageIOHandler::allocateImage(). 24 | inline QImage imageAlloc(const QSize &size, const QImage::Format &format) 25 | { 26 | QImage img; 27 | #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 28 | img = QImage(size, format); 29 | #else 30 | if (!QImageIOHandler::allocateImage(size, format, &img)) { 31 | img = QImage(); // paranoia 32 | } 33 | #endif 34 | return img; 35 | } 36 | 37 | inline QImage imageAlloc(qint32 width, qint32 height, const QImage::Format &format) 38 | { 39 | return imageAlloc(QSize(width, height), format); 40 | } 41 | 42 | #endif // UTIL_P_H 43 | -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * QT plug-in to allow import/export in JPEG XL image format. 3 | * Author: Daniel Novomesky 4 | */ 5 | 6 | #include "qjpegxlhandler_p.h" 7 | #include 8 | 9 | class QJpegXLPlugin : public QImageIOPlugin 10 | { 11 | Q_OBJECT 12 | Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QImageIOHandlerFactoryInterface" FILE "jpegxl.json") 13 | 14 | public: 15 | Capabilities capabilities(QIODevice *device, const QByteArray &format) const override; 16 | QImageIOHandler *create(QIODevice *device, const QByteArray &format = QByteArray()) const override; 17 | }; 18 | 19 | QImageIOPlugin::Capabilities QJpegXLPlugin::capabilities(QIODevice *device, const QByteArray &format) const 20 | { 21 | if (format == "jxl") { 22 | return Capabilities(CanRead | CanWrite); 23 | } 24 | 25 | if (!format.isEmpty()) { 26 | return {}; 27 | } 28 | if (!device->isOpen()) { 29 | return {}; 30 | } 31 | 32 | Capabilities cap; 33 | if (device->isReadable() && QJpegXLHandler::canRead(device)) { 34 | cap |= CanRead; 35 | } 36 | 37 | if (device->isWritable()) { 38 | cap |= CanWrite; 39 | } 40 | 41 | return cap; 42 | } 43 | 44 | QImageIOHandler *QJpegXLPlugin::create(QIODevice *device, const QByteArray &format) const 45 | { 46 | QImageIOHandler *handler = new QJpegXLHandler; 47 | handler->setDevice(device); 48 | handler->setFormat(format); 49 | return handler; 50 | } 51 | 52 | #include "main.moc" 53 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # NB: the desktop files are installed for the benefit of KImageIO in KDELibs4Support. 2 | 3 | ################################## 4 | 5 | function(kimageformats_add_plugin plugin) 6 | set(options) 7 | set(multiValueArgs SOURCES) 8 | cmake_parse_arguments(KIF_ADD_PLUGIN "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN}) 9 | if(NOT KIF_ADD_PLUGIN_SOURCES) 10 | message(FATAL_ERROR "kimageformats_add_plugin called without SOURCES parameter") 11 | endif() 12 | 13 | add_library(${plugin} MODULE ${KIF_ADD_PLUGIN_SOURCES}) 14 | set_target_properties(${plugin} PROPERTIES LIBRARY_OUTPUT_DIRECTORY "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/imageformats") 15 | target_link_libraries(${plugin} Qt${QT_MAJOR_VERSION}::Gui) 16 | install(TARGETS ${plugin} DESTINATION ${KDE_INSTALL_QTPLUGINDIR}/imageformats) 17 | endfunction() 18 | 19 | ################################## 20 | 21 | if (LibJXL_FOUND AND LibJXLThreads_FOUND) 22 | kimageformats_add_plugin("libqjpegxl${QT_MAJOR_VERSION}" SOURCES "main.cpp" "qjpegxlhandler.cpp") 23 | target_link_libraries("libqjpegxl${QT_MAJOR_VERSION}" PkgConfig::LibJXL PkgConfig::LibJXLThreads) 24 | if(LibJXL_VERSION VERSION_GREATER_EQUAL "0.9.0") 25 | if(LibJXLCMS_FOUND) 26 | target_link_libraries("libqjpegxl${QT_MAJOR_VERSION}" PkgConfig::LibJXLCMS) 27 | else() 28 | message(SEND_ERROR "libjxl_cms was not found!") 29 | endif() 30 | endif() 31 | #install(FILES jxl.desktop DESTINATION ${KDE_INSTALL_KSERVICESDIR}/qimageioplugins/) 32 | endif() 33 | 34 | ################################## 35 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | version: 0.8.1.{build} 2 | image: Visual Studio 2022 3 | environment: 4 | CC: cl.exe 5 | CXX: cl.exe 6 | install: 7 | - cmd: >- 8 | call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat" 9 | 10 | git clone --depth 1 -b v0.11.x https://github.com/libjxl/libjxl.git --recursive --shallow-submodules 11 | 12 | cd libjxl 13 | 14 | mkdir build 15 | 16 | cd build 17 | 18 | cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF -DBUILD_TESTING=OFF -DJPEGXL_ENABLE_TOOLS=OFF -DJPEGXL_ENABLE_DOXYGEN=OFF -DJPEGXL_ENABLE_MANPAGES=OFF -DJPEGXL_ENABLE_BENCHMARK=OFF -DJPEGXL_ENABLE_EXAMPLES=OFF -DJPEGXL_ENABLE_SJPEG=OFF -DJPEGXL_ENABLE_SKCMS=ON -DJPEGXL_WARNINGS_AS_ERRORS=OFF -DJPEGXL_ENABLE_JPEGLI=OFF -DJPEGXL_ENABLE_JPEGLI_LIBJPEG=OFF -DCMAKE_C_FLAGS="-DJXL_STATIC_DEFINE -DJXL_THREADS_STATIC_DEFINE -DJXL_CMS_STATIC_DEFINE" -DCMAKE_CXX_FLAGS="-DJXL_STATIC_DEFINE -DJXL_THREADS_STATIC_DEFINE -DJXL_CMS_STATIC_DEFINE" .. 19 | 20 | ninja jxl jxl_cms jxl_threads hwy brotlicommon brotlidec brotlienc 21 | 22 | cd ..\.. 23 | build_script: 24 | - cmd: >- 25 | cd qtbuild_6.5.3 26 | 27 | C:\Qt\6.5.3\msvc2019_64\bin\qmake.exe qt-jpegxl-image-plugin_alternative_appveyor.pro 28 | 29 | nmake 30 | 31 | cd ..\qtbuild_6.8.3 32 | 33 | C:\Qt\6.8.3\msvc2022_64\bin\qmake.exe qt-jpegxl-image-plugin_alternative_appveyor.pro 34 | 35 | nmake 36 | 37 | cd ..\qtbuild_6.9.2 38 | 39 | C:\Qt\6.9.2\msvc2022_64\bin\qmake.exe qt-jpegxl-image-plugin_alternative_appveyor.pro 40 | 41 | nmake 42 | 43 | artifacts: 44 | - path: qtbuild_6.5.3\*.dll 45 | - path: qtbuild_6.8.3\*.dll 46 | - path: qtbuild_6.9.2\*.dll 47 | -------------------------------------------------------------------------------- /src/qjpegxlhandler_p.h: -------------------------------------------------------------------------------- 1 | #ifndef QJPEGXLHANDLER_P_H 2 | #define QJPEGXLHANDLER_P_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | class QJpegXLHandler : public QImageIOHandler 14 | { 15 | public: 16 | QJpegXLHandler(); 17 | ~QJpegXLHandler(); 18 | 19 | bool canRead() const override; 20 | bool read(QImage *image) override; 21 | bool write(const QImage &image) override; 22 | 23 | static bool canRead(QIODevice *device); 24 | 25 | QVariant option(ImageOption option) const override; 26 | void setOption(ImageOption option, const QVariant &value) override; 27 | bool supportsOption(ImageOption option) const override; 28 | 29 | int imageCount() const override; 30 | int currentImageNumber() const override; 31 | bool jumpToNextImage() override; 32 | bool jumpToImage(int imageNumber) override; 33 | 34 | int nextImageDelay() const override; 35 | 36 | int loopCount() const override; 37 | 38 | private: 39 | bool ensureParsed() const; 40 | bool ensureALLCounted() const; 41 | bool ensureDecoder(); 42 | bool countALLFrames(); 43 | bool decode_one_frame(); 44 | bool rewind(); 45 | 46 | enum ParseJpegXLState { 47 | ParseJpegXLError = -1, 48 | ParseJpegXLNotParsed = 0, 49 | ParseJpegXLSuccess = 1, 50 | ParseJpegXLBasicInfoParsed = 2, 51 | ParseJpegXLFinished = 3, 52 | }; 53 | 54 | ParseJpegXLState m_parseState; 55 | int m_quality; 56 | int m_currentimage_index; 57 | int m_previousimage_index; 58 | 59 | QByteArray m_rawData; 60 | 61 | JxlDecoder *m_decoder; 62 | void *m_runner; 63 | JxlBasicInfo m_basicinfo; 64 | 65 | QVector m_framedelays; 66 | int m_next_image_delay; 67 | 68 | QImage m_current_image; 69 | QColorSpace m_colorspace; 70 | bool m_isCMYK; 71 | uint32_t m_cmyk_channel_id; 72 | uint32_t m_alpha_channel_id; 73 | 74 | QImage::Format m_input_image_format; 75 | QImage::Format m_target_image_format; 76 | 77 | JxlPixelFormat m_input_pixel_format; 78 | }; 79 | 80 | #endif // QJPEGXLHANDLER_P_H 81 | -------------------------------------------------------------------------------- /src/.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | # SPDX-FileCopyrightText: 2019 Christoph Cullmann 3 | # SPDX-FileCopyrightText: 2019 Gernot Gebhard 4 | # 5 | # SPDX-License-Identifier: MIT 6 | 7 | # This file got automatically created by ECM, do not edit 8 | # See https://clang.llvm.org/docs/ClangFormatStyleOptions.html for the config options 9 | # and https://community.kde.org/Policies/Frameworks_Coding_Style#Clang-format_automatic_code_formatting 10 | # for clang-format tips & tricks 11 | --- 12 | Language: JavaScript 13 | DisableFormat: true 14 | --- 15 | 16 | # Style for C++ 17 | Language: Cpp 18 | 19 | # base is WebKit coding style: https://webkit.org/code-style-guidelines/ 20 | # below are only things set that diverge from this style! 21 | BasedOnStyle: WebKit 22 | 23 | # enforce C++11 (e.g. for std::vector> 24 | Standard: Cpp11 25 | 26 | # 4 spaces indent 27 | TabWidth: 4 28 | 29 | # 2 * 80 wide lines 30 | ColumnLimit: 160 31 | 32 | # sort includes inside line separated groups 33 | SortIncludes: true 34 | 35 | # break before braces on function, namespace and class definitions. 36 | BreakBeforeBraces: Linux 37 | 38 | # CrlInstruction *a; 39 | PointerAlignment: Right 40 | 41 | # horizontally aligns arguments after an open bracket. 42 | AlignAfterOpenBracket: Align 43 | 44 | # don't move all parameters to new line 45 | AllowAllParametersOfDeclarationOnNextLine: false 46 | 47 | # no single line functions 48 | AllowShortFunctionsOnASingleLine: None 49 | 50 | # always break before you encounter multi line strings 51 | AlwaysBreakBeforeMultilineStrings: true 52 | 53 | # don't move arguments to own lines if they are not all on the same 54 | BinPackArguments: false 55 | 56 | # don't move parameters to own lines if they are not all on the same 57 | BinPackParameters: false 58 | 59 | # In case we have an if statement with multiple lines the operator should be at the beginning of the line 60 | # but we do not want to break assignments 61 | BreakBeforeBinaryOperators: NonAssignment 62 | 63 | # format C++11 braced lists like function calls 64 | Cpp11BracedListStyle: true 65 | 66 | # do not put a space before C++11 braced lists 67 | SpaceBeforeCpp11BracedList: false 68 | 69 | # remove empty lines 70 | KeepEmptyLinesAtTheStartOfBlocks: false 71 | 72 | # no namespace indentation to keep indent level low 73 | NamespaceIndentation: None 74 | 75 | # we use template< without space. 76 | SpaceAfterTemplateKeyword: false 77 | 78 | # Always break after template declaration 79 | AlwaysBreakTemplateDeclarations: true 80 | 81 | # macros for which the opening brace stays attached. 82 | ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH, forever, Q_FOREVER, QBENCHMARK, QBENCHMARK_ONCE , wl_resource_for_each, wl_resource_for_each_safe ] 83 | 84 | # keep lambda formatting multi-line if not empty 85 | AllowShortLambdasOnASingleLine: Empty 86 | 87 | # We do not want clang-format to put all arguments on a new line 88 | AllowAllArgumentsOnNextLine: false 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # qt-jpegxl-image-plugin 2 | 3 | ## Table of Contents 4 | 5 | 1. [Description](#Description) 6 | 2. [Installation](#Installation) 7 | 3. [Test](#Test) 8 | 9 | # Description 10 | 11 | Qt plug-in to allow Qt and KDE based applications to read/write JXL images. 12 | 13 | Work in progress experimental implementation using [libjxl](https://github.com/libjxl/libjxl) 14 | 15 | **kimageformats 5.89** contains almost identical plug-in like this one already. If you have `kimg_jxl.so` installed, don’t install qt-jpegxl-image-plugin. If your distribution maintainer built kimageformats without JPEG XL support, you may install qt-jpegxl-image-plugin. It is not recommended to have `libqjpegxl.so` and `kimg_jxl.so` installed at the same time. 16 | 17 | # Installation 18 | 19 | ### 1. Clone, build and install JPEG-XL 20 | 21 | **!Important!** Clone must be `--recursive` to include third party packages 22 | 23 | Code for download and compilation: 24 | ``` 25 | git clone --depth 1 https://github.com/libjxl/libjxl.git --recursive 26 | cd libjxl 27 | mkdir build 28 | cd build 29 | cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr -DJPEGXL_ENABLE_PLUGINS=ON -DBUILD_TESTING=OFF -DJPEGXL_WARNINGS_AS_ERRORS=OFF -DJPEGXL_ENABLE_SJPEG=OFF .. 30 | make 31 | ``` 32 | Code for installation (run as root): 33 | 34 | `make install` 35 | 36 | ### 2. Update mime database file 37 | 38 | Check if the `image-jxl.xml` file was installed to `/usr/share/mime/packages/` folder and run (as root): 39 | 40 | `update-mime-database /usr/share/mime/` 41 | 42 | ### 3. Build qt-jpegxl-image-plugin 43 | 44 | Make sure that the Qt 5 base development packages (`qtbase5-dev` on Debian/Ubuntu) are installed. At least Qt 5.14 is required. 45 | 46 | Download and build: 47 | ``` 48 | git clone --depth 1 https://github.com/novomesk/qt-jpegxl-image-plugin 49 | cd qt-jpegxl-image-plugin 50 | ./build_libqjpegxl_dynamic.sh 51 | ``` 52 | Install (run as root): 53 | 54 | `make install` 55 | 56 | ### 4. Add `image/jxl` to `/usr/share/kservices5/imagethumbnail.desktop` 57 | 58 | 1. Open `/usr/share/kservices5/imagethumbnail.desktop` with editor of your choise 59 | 2. Find line that starts with `MimeType=` and add `image/jxl;` to it's end, so line looks like `MimeType=image/cgm; ... ;image/rle;image/avif;image/jxl;`, save file. 60 | 3. Run `update-desktop-database` 61 | 62 | # Test 63 | ``` 64 | cd testfiles 65 | gwenview jpegxl-logo.jxl 66 | ``` 67 | 68 | Expected result: 69 | 70 | ![jpegxl-logo.jxl in gwenview](testfiles/gwenview.png) 71 | 72 | # Enjoy using JXL in applications 73 | 74 | ### digiKam 75 | ![JPEG XL in digiKam](imgs/digiKam.png) 76 | 77 | JPEG XL support has to be explicitly enabled in digiKam. Go to Settings -> Configure digikam -> Views -> Mime Types and add `jxl` to the to the Additional image file extensions field: 78 | ![How to enable JXL extension in digiKam](imgs/digiKam2.png) 79 | 80 | ### KolourPaint 81 | ![JPEG XL in KolourPaint](imgs/KolourPaint.png) 82 | 83 | ### KPhotoAlbum 84 | ![JPEG XL in KPhotoAlbum](imgs/KPhotoAlbum.png) 85 | 86 | ### LXImage-Qt 87 | ![JPEG XL in LXImage-Qt](imgs/LXImage-Qt.png) 88 | 89 | ### qimgv 90 | ![JPEG XL in qimgv](imgs/qimgv.png) 91 | 92 | ### qView 93 | ![JPEG XL in qView](imgs/qView.png) 94 | 95 | ### PhotoQt 96 | ![JPEG XL in PhotoQt](imgs/PhotoQt.png) 97 | 98 | ### YACReader 99 | ![JPEG XL in YACReader - Yet Another Comic Reader](imgs/YACReader.png) 100 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /src/qjpegxlhandler.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * QT plug-in to allow import/export in JPEG XL image format. 3 | * Author: Daniel Novomesky 4 | */ 5 | 6 | #include 7 | #include 8 | 9 | #include "qjpegxlhandler_p.h" 10 | #include "util_p.h" 11 | 12 | #include 13 | #include 14 | 15 | #if JPEGXL_NUMERIC_VERSION > JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) 16 | #include 17 | #endif 18 | 19 | #include 20 | 21 | QJpegXLHandler::QJpegXLHandler() 22 | : m_parseState(ParseJpegXLNotParsed) 23 | , m_quality(90) 24 | , m_currentimage_index(0) 25 | , m_previousimage_index(-1) 26 | , m_decoder(nullptr) 27 | , m_runner(nullptr) 28 | , m_next_image_delay(0) 29 | , m_isCMYK(false) 30 | , m_cmyk_channel_id(0) 31 | , m_alpha_channel_id(0) 32 | , m_input_image_format(QImage::Format_Invalid) 33 | , m_target_image_format(QImage::Format_Invalid) 34 | { 35 | } 36 | 37 | QJpegXLHandler::~QJpegXLHandler() 38 | { 39 | if (m_runner) { 40 | JxlThreadParallelRunnerDestroy(m_runner); 41 | } 42 | if (m_decoder) { 43 | JxlDecoderDestroy(m_decoder); 44 | } 45 | } 46 | 47 | bool QJpegXLHandler::canRead() const 48 | { 49 | if (m_parseState == ParseJpegXLNotParsed && !canRead(device())) { 50 | return false; 51 | } 52 | 53 | if (m_parseState != ParseJpegXLError) { 54 | setFormat("jxl"); 55 | 56 | if (m_parseState == ParseJpegXLFinished) { 57 | return false; 58 | } 59 | 60 | return true; 61 | } 62 | return false; 63 | } 64 | 65 | bool QJpegXLHandler::canRead(QIODevice *device) 66 | { 67 | if (!device) { 68 | return false; 69 | } 70 | QByteArray header = device->peek(32); 71 | if (header.size() < 12) { 72 | return false; 73 | } 74 | 75 | JxlSignature signature = JxlSignatureCheck(reinterpret_cast(header.constData()), header.size()); 76 | if (signature == JXL_SIG_CODESTREAM || signature == JXL_SIG_CONTAINER) { 77 | return true; 78 | } 79 | return false; 80 | } 81 | 82 | bool QJpegXLHandler::ensureParsed() const 83 | { 84 | if (m_parseState == ParseJpegXLSuccess || m_parseState == ParseJpegXLBasicInfoParsed || m_parseState == ParseJpegXLFinished) { 85 | return true; 86 | } 87 | if (m_parseState == ParseJpegXLError) { 88 | return false; 89 | } 90 | 91 | QJpegXLHandler *that = const_cast(this); 92 | 93 | return that->ensureDecoder(); 94 | } 95 | 96 | bool QJpegXLHandler::ensureALLCounted() const 97 | { 98 | if (!ensureParsed()) { 99 | return false; 100 | } 101 | 102 | if (m_parseState == ParseJpegXLSuccess || m_parseState == ParseJpegXLFinished) { 103 | return true; 104 | } 105 | 106 | QJpegXLHandler *that = const_cast(this); 107 | 108 | return that->countALLFrames(); 109 | } 110 | 111 | bool QJpegXLHandler::ensureDecoder() 112 | { 113 | if (m_decoder) { 114 | return true; 115 | } 116 | 117 | m_rawData = device()->readAll(); 118 | 119 | if (m_rawData.isEmpty()) { 120 | return false; 121 | } 122 | 123 | JxlSignature signature = JxlSignatureCheck(reinterpret_cast(m_rawData.constData()), m_rawData.size()); 124 | if (signature != JXL_SIG_CODESTREAM && signature != JXL_SIG_CONTAINER) { 125 | m_parseState = ParseJpegXLError; 126 | return false; 127 | } 128 | 129 | m_decoder = JxlDecoderCreate(nullptr); 130 | if (!m_decoder) { 131 | qWarning("ERROR: JxlDecoderCreate failed"); 132 | m_parseState = ParseJpegXLError; 133 | return false; 134 | } 135 | 136 | int num_worker_threads = QThread::idealThreadCount(); 137 | if (!m_runner && num_worker_threads >= 4) { 138 | /* use half of the threads because plug-in is usually used in environment 139 | * where application performs another tasks in backround (pre-load other images) */ 140 | num_worker_threads = num_worker_threads / 2; 141 | num_worker_threads = qBound(2, num_worker_threads, 64); 142 | m_runner = JxlThreadParallelRunnerCreate(nullptr, num_worker_threads); 143 | 144 | if (JxlDecoderSetParallelRunner(m_decoder, JxlThreadParallelRunner, m_runner) != JXL_DEC_SUCCESS) { 145 | qWarning("ERROR: JxlDecoderSetParallelRunner failed"); 146 | m_parseState = ParseJpegXLError; 147 | return false; 148 | } 149 | } 150 | 151 | if (JxlDecoderSetInput(m_decoder, reinterpret_cast(m_rawData.constData()), m_rawData.size()) != JXL_DEC_SUCCESS) { 152 | qWarning("ERROR: JxlDecoderSetInput failed"); 153 | m_parseState = ParseJpegXLError; 154 | return false; 155 | } 156 | 157 | JxlDecoderCloseInput(m_decoder); 158 | 159 | JxlDecoderStatus status = JxlDecoderSubscribeEvents(m_decoder, JXL_DEC_BASIC_INFO | JXL_DEC_COLOR_ENCODING | JXL_DEC_FRAME); 160 | if (status == JXL_DEC_ERROR) { 161 | qWarning("ERROR: JxlDecoderSubscribeEvents failed"); 162 | m_parseState = ParseJpegXLError; 163 | return false; 164 | } 165 | 166 | status = JxlDecoderProcessInput(m_decoder); 167 | if (status == JXL_DEC_ERROR) { 168 | qWarning("ERROR: JXL decoding failed"); 169 | m_parseState = ParseJpegXLError; 170 | return false; 171 | } 172 | if (status == JXL_DEC_NEED_MORE_INPUT) { 173 | qWarning("ERROR: JXL data incomplete"); 174 | m_parseState = ParseJpegXLError; 175 | return false; 176 | } 177 | 178 | status = JxlDecoderGetBasicInfo(m_decoder, &m_basicinfo); 179 | if (status != JXL_DEC_SUCCESS) { 180 | qWarning("ERROR: JXL basic info not available"); 181 | m_parseState = ParseJpegXLError; 182 | return false; 183 | } 184 | 185 | if (m_basicinfo.xsize == 0 || m_basicinfo.ysize == 0) { 186 | qWarning("ERROR: JXL image has zero dimensions"); 187 | m_parseState = ParseJpegXLError; 188 | return false; 189 | } 190 | 191 | if (m_basicinfo.xsize > 262144 || m_basicinfo.ysize > 262144) { 192 | qWarning("JXL image (%dx%d) is too large", m_basicinfo.xsize, m_basicinfo.ysize); 193 | m_parseState = ParseJpegXLError; 194 | return false; 195 | } 196 | 197 | if (sizeof(void *) <= 4) { 198 | /* On 32bit systems, there is limited address space. 199 | * We skip imagess bigger than 8192 x 8192 pixels. 200 | * If we don't do it, abort() in libjxl may close whole application */ 201 | if (m_basicinfo.xsize > ((8192 * 8192) / m_basicinfo.ysize)) { 202 | qWarning("JXL image (%dx%d) is too large for 32bit build of the plug-in", m_basicinfo.xsize, m_basicinfo.ysize); 203 | m_parseState = ParseJpegXLError; 204 | return false; 205 | } 206 | } else { 207 | /* On 64bit systems 208 | * We skip images bigger than 16384 x 16384 pixels. 209 | * It is an artificial limit not to use extreme amount of memory */ 210 | if (m_basicinfo.xsize > ((16384 * 16384) / m_basicinfo.ysize)) { 211 | qWarning("JXL image (%dx%d) is bigger than security limit 256 megapixels", m_basicinfo.xsize, m_basicinfo.ysize); 212 | m_parseState = ParseJpegXLError; 213 | return false; 214 | } 215 | } 216 | 217 | m_parseState = ParseJpegXLBasicInfoParsed; 218 | return true; 219 | } 220 | 221 | bool QJpegXLHandler::countALLFrames() 222 | { 223 | if (m_parseState != ParseJpegXLBasicInfoParsed) { 224 | return false; 225 | } 226 | 227 | JxlDecoderStatus status = JxlDecoderProcessInput(m_decoder); 228 | if (status != JXL_DEC_COLOR_ENCODING) { 229 | qWarning("Unexpected event %d instead of JXL_DEC_COLOR_ENCODING", status); 230 | m_parseState = ParseJpegXLError; 231 | return false; 232 | } 233 | 234 | bool is_gray = m_basicinfo.num_color_channels == 1 && m_basicinfo.alpha_bits == 0; 235 | JxlColorEncoding color_encoding; 236 | if (m_basicinfo.uses_original_profile == JXL_FALSE && m_basicinfo.have_animation == JXL_FALSE) { 237 | #if JPEGXL_NUMERIC_VERSION > JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) 238 | const JxlCmsInterface *jxlcms = JxlGetDefaultCms(); 239 | if (jxlcms) { 240 | status = JxlDecoderSetCms(m_decoder, *jxlcms); 241 | if (status != JXL_DEC_SUCCESS) { 242 | qWarning("JxlDecoderSetCms ERROR"); 243 | } 244 | } else { 245 | qWarning("No JPEG XL CMS Interface"); 246 | } 247 | #endif 248 | JxlColorEncodingSetToSRGB(&color_encoding, is_gray ? JXL_TRUE : JXL_FALSE); 249 | JxlDecoderSetPreferredColorProfile(m_decoder, &color_encoding); 250 | } 251 | 252 | bool loadalpha = false; 253 | if (m_basicinfo.alpha_bits > 0) { 254 | loadalpha = true; 255 | } 256 | 257 | m_input_pixel_format.endianness = JXL_NATIVE_ENDIAN; 258 | m_input_pixel_format.align = 4; 259 | 260 | if (m_basicinfo.bits_per_sample > 8) { // high bit depth 261 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 262 | bool is_fp = m_basicinfo.exponent_bits_per_sample > 0 && m_basicinfo.num_color_channels == 3; 263 | #endif 264 | 265 | m_input_pixel_format.num_channels = 4; 266 | 267 | if (is_gray) { 268 | m_input_pixel_format.num_channels = 1; 269 | m_input_pixel_format.data_type = JXL_TYPE_UINT16; 270 | m_input_image_format = m_target_image_format = QImage::Format_Grayscale16; 271 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 272 | } else if (m_basicinfo.bits_per_sample > 16 && is_fp) { 273 | m_input_pixel_format.data_type = JXL_TYPE_FLOAT; 274 | m_input_image_format = QImage::Format_RGBA32FPx4; 275 | if (loadalpha) 276 | m_target_image_format = QImage::Format_RGBA32FPx4; 277 | else 278 | m_target_image_format = QImage::Format_RGBX32FPx4; 279 | #endif 280 | } else { 281 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 282 | m_input_pixel_format.data_type = is_fp ? JXL_TYPE_FLOAT16 : JXL_TYPE_UINT16; 283 | m_input_image_format = is_fp ? QImage::Format_RGBA16FPx4 : QImage::Format_RGBA64; 284 | if (loadalpha) 285 | m_target_image_format = is_fp ? QImage::Format_RGBA16FPx4 : QImage::Format_RGBA64; 286 | else 287 | m_target_image_format = is_fp ? QImage::Format_RGBX16FPx4 : QImage::Format_RGBX64; 288 | #else 289 | m_input_pixel_format.data_type = JXL_TYPE_UINT16; 290 | m_input_image_format = QImage::Format_RGBA64; 291 | if (loadalpha) 292 | m_target_image_format = QImage::Format_RGBA64; 293 | else 294 | m_target_image_format = QImage::Format_RGBX64; 295 | #endif 296 | } 297 | } else { // 8bit depth 298 | m_input_pixel_format.data_type = JXL_TYPE_UINT8; 299 | 300 | if (is_gray) { 301 | m_input_pixel_format.num_channels = 1; 302 | m_input_image_format = m_target_image_format = QImage::Format_Grayscale8; 303 | } else { 304 | if (loadalpha) { 305 | m_input_pixel_format.num_channels = 4; 306 | m_input_image_format = QImage::Format_RGBA8888; 307 | m_target_image_format = QImage::Format_ARGB32; 308 | } else { 309 | m_input_pixel_format.num_channels = 3; 310 | m_input_image_format = QImage::Format_RGB888; 311 | m_target_image_format = QImage::Format_RGB32; 312 | } 313 | } 314 | } 315 | 316 | status = JxlDecoderGetColorAsEncodedProfile(m_decoder, 317 | #if JPEGXL_NUMERIC_VERSION < JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) 318 | &m_input_pixel_format, 319 | #endif 320 | JXL_COLOR_PROFILE_TARGET_DATA, 321 | &color_encoding); 322 | 323 | if (status == JXL_DEC_SUCCESS && color_encoding.color_space == JXL_COLOR_SPACE_RGB && color_encoding.white_point == JXL_WHITE_POINT_D65 324 | && color_encoding.primaries == JXL_PRIMARIES_SRGB && color_encoding.transfer_function == JXL_TRANSFER_FUNCTION_SRGB) { 325 | m_colorspace = QColorSpace(QColorSpace::SRgb); 326 | } else { 327 | size_t icc_size = 0; 328 | if (JxlDecoderGetICCProfileSize(m_decoder, 329 | #if JPEGXL_NUMERIC_VERSION < JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) 330 | &m_input_pixel_format, 331 | #endif 332 | JXL_COLOR_PROFILE_TARGET_DATA, 333 | &icc_size) 334 | == JXL_DEC_SUCCESS) { 335 | if (icc_size > 0) { 336 | QByteArray icc_data(icc_size, 0); 337 | if (JxlDecoderGetColorAsICCProfile(m_decoder, 338 | #if JPEGXL_NUMERIC_VERSION < JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) 339 | &m_input_pixel_format, 340 | #endif 341 | JXL_COLOR_PROFILE_TARGET_DATA, 342 | reinterpret_cast(icc_data.data()), 343 | icc_data.size()) 344 | == JXL_DEC_SUCCESS) { 345 | m_colorspace = QColorSpace::fromIccProfile(icc_data); 346 | 347 | if (!m_colorspace.isValid()) { 348 | qWarning("JXL image has Qt-unsupported or invalid ICC profile!"); 349 | } 350 | } else { 351 | qWarning("Failed to obtain data from JPEG XL decoder"); 352 | } 353 | } else { 354 | qWarning("Empty ICC data"); 355 | } 356 | } else { 357 | qWarning("no ICC, other color profile"); 358 | } 359 | } 360 | 361 | if (m_basicinfo.have_animation) { // count all frames 362 | JxlFrameHeader frame_header; 363 | int delay; 364 | 365 | for (status = JxlDecoderProcessInput(m_decoder); status != JXL_DEC_SUCCESS; status = JxlDecoderProcessInput(m_decoder)) { 366 | if (status != JXL_DEC_FRAME) { 367 | switch (status) { 368 | case JXL_DEC_ERROR: 369 | qWarning("ERROR: JXL decoding failed"); 370 | break; 371 | case JXL_DEC_NEED_MORE_INPUT: 372 | qWarning("ERROR: JXL data incomplete"); 373 | break; 374 | default: 375 | qWarning("Unexpected event %d instead of JXL_DEC_FRAME", status); 376 | break; 377 | } 378 | m_parseState = ParseJpegXLError; 379 | return false; 380 | } 381 | 382 | if (JxlDecoderGetFrameHeader(m_decoder, &frame_header) != JXL_DEC_SUCCESS) { 383 | qWarning("ERROR: JxlDecoderGetFrameHeader failed"); 384 | m_parseState = ParseJpegXLError; 385 | return false; 386 | } 387 | 388 | if (m_basicinfo.animation.tps_denominator > 0 && m_basicinfo.animation.tps_numerator > 0) { 389 | delay = (int)(0.5 + 1000.0 * frame_header.duration * m_basicinfo.animation.tps_denominator / m_basicinfo.animation.tps_numerator); 390 | } else { 391 | delay = 0; 392 | } 393 | 394 | m_framedelays.append(delay); 395 | 396 | if (frame_header.is_last == JXL_TRUE) { 397 | break; 398 | } 399 | } 400 | 401 | if (m_framedelays.isEmpty()) { 402 | qWarning("no frames loaded by the JXL plug-in"); 403 | m_parseState = ParseJpegXLError; 404 | return false; 405 | } 406 | 407 | if (m_framedelays.count() == 1) { 408 | qWarning("JXL file was marked as animation but it has only one frame."); 409 | m_basicinfo.have_animation = JXL_FALSE; 410 | } 411 | } else { // static picture 412 | m_framedelays.resize(1); 413 | m_framedelays[0] = 0; 414 | } 415 | 416 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) 417 | // CMYK detection 418 | if ((m_basicinfo.uses_original_profile == JXL_TRUE) && (m_basicinfo.num_color_channels == 3) && (m_colorspace.isValid())) { 419 | bool alpha_found = false; 420 | JxlExtraChannelInfo channel_info; 421 | for (uint32_t index = 0; index < m_basicinfo.num_extra_channels; index++) { 422 | status = JxlDecoderGetExtraChannelInfo(m_decoder, index, &channel_info); 423 | if (status != JXL_DEC_SUCCESS) { 424 | qWarning("JxlDecoderGetExtraChannelInfo for channel %d returned %d", index, status); 425 | m_parseState = ParseJpegXLError; 426 | return false; 427 | } 428 | 429 | if (channel_info.type == JXL_CHANNEL_BLACK) { 430 | if (m_colorspace.colorModel() == QColorSpace::ColorModel::Cmyk) { 431 | m_isCMYK = true; 432 | m_cmyk_channel_id = index; 433 | 434 | if (m_basicinfo.alpha_bits > 0) { 435 | if (!alpha_found) { 436 | // continue searching for alpha channel 437 | for (uint32_t alpha_index = index + 1; alpha_index < m_basicinfo.num_extra_channels; alpha_index++) { 438 | status = JxlDecoderGetExtraChannelInfo(m_decoder, alpha_index, &channel_info); 439 | if (status != JXL_DEC_SUCCESS) { 440 | qWarning("JxlDecoderGetExtraChannelInfo for channel %d returned %d", alpha_index, status); 441 | m_parseState = ParseJpegXLError; 442 | return false; 443 | } 444 | 445 | if (channel_info.type == JXL_CHANNEL_ALPHA) { 446 | alpha_found = true; 447 | m_alpha_channel_id = alpha_index; 448 | break; 449 | } 450 | } 451 | 452 | if (!alpha_found) { 453 | qWarning("JXL BasicInfo indicate Alpha channel but it was not found"); 454 | m_parseState = ParseJpegXLError; 455 | return false; 456 | } 457 | } 458 | } 459 | } else { 460 | qWarning("JXL has BLACK channel but colorspace is not CMYK!"); 461 | } 462 | break; 463 | } else if ((channel_info.type == JXL_CHANNEL_ALPHA) && !alpha_found) { 464 | alpha_found = true; 465 | m_alpha_channel_id = index; 466 | } 467 | } 468 | 469 | if (!m_isCMYK && (m_colorspace.colorModel() == QColorSpace::ColorModel::Cmyk)) { 470 | qWarning("JXL has CMYK colorspace but BLACK channel was not found!"); 471 | } 472 | } 473 | #endif 474 | 475 | if (!rewind()) { 476 | return false; 477 | } 478 | 479 | m_next_image_delay = m_framedelays[0]; 480 | m_parseState = ParseJpegXLSuccess; 481 | return true; 482 | } 483 | 484 | bool QJpegXLHandler::decode_one_frame() 485 | { 486 | JxlDecoderStatus status = JxlDecoderProcessInput(m_decoder); 487 | if (status != JXL_DEC_NEED_IMAGE_OUT_BUFFER) { 488 | qWarning("Unexpected event %d instead of JXL_DEC_NEED_IMAGE_OUT_BUFFER", status); 489 | m_parseState = ParseJpegXLError; 490 | return false; 491 | } 492 | 493 | if (m_isCMYK) { // CMYK decoding 494 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) 495 | uchar *pixels_cmy = nullptr; 496 | uchar *pixels_black = nullptr; 497 | 498 | JxlPixelFormat format_extra; 499 | 500 | m_input_pixel_format.num_channels = 3; 501 | m_input_pixel_format.data_type = JXL_TYPE_UINT8; 502 | m_input_pixel_format.endianness = JXL_NATIVE_ENDIAN; 503 | m_input_pixel_format.align = 0; 504 | 505 | format_extra.num_channels = 1; 506 | format_extra.data_type = JXL_TYPE_UINT8; 507 | format_extra.endianness = JXL_NATIVE_ENDIAN; 508 | format_extra.align = 0; 509 | 510 | const size_t extra_buffer_size = size_t(m_basicinfo.xsize) * size_t(m_basicinfo.ysize); 511 | const size_t cmy_buffer_size = extra_buffer_size * 3; 512 | 513 | if (m_basicinfo.alpha_bits > 0) { // CMYK + alpha 514 | QImage tmp_cmyk_image = imageAlloc(m_basicinfo.xsize, m_basicinfo.ysize, QImage::Format_CMYK8888); 515 | if (tmp_cmyk_image.isNull()) { 516 | qWarning("Memory cannot be allocated"); 517 | m_parseState = ParseJpegXLError; 518 | return false; 519 | } 520 | 521 | tmp_cmyk_image.setColorSpace(m_colorspace); 522 | 523 | uchar *pixels_alpha = reinterpret_cast(malloc(extra_buffer_size)); 524 | if (!pixels_alpha) { 525 | qWarning("Memory cannot be allocated for ALPHA channel"); 526 | m_parseState = ParseJpegXLError; 527 | return false; 528 | } 529 | 530 | pixels_cmy = reinterpret_cast(malloc(cmy_buffer_size)); 531 | if (!pixels_cmy) { 532 | free(pixels_alpha); 533 | pixels_alpha = nullptr; 534 | qWarning("Memory cannot be allocated for CMY buffer"); 535 | m_parseState = ParseJpegXLError; 536 | return false; 537 | } 538 | 539 | pixels_black = reinterpret_cast(malloc(extra_buffer_size)); 540 | if (!pixels_black) { 541 | free(pixels_cmy); 542 | pixels_cmy = nullptr; 543 | free(pixels_alpha); 544 | pixels_alpha = nullptr; 545 | qWarning("Memory cannot be allocated for BLACK buffer"); 546 | m_parseState = ParseJpegXLError; 547 | return false; 548 | } 549 | 550 | if (JxlDecoderSetImageOutBuffer(m_decoder, &m_input_pixel_format, pixels_cmy, cmy_buffer_size) != JXL_DEC_SUCCESS) { 551 | free(pixels_black); 552 | pixels_black = nullptr; 553 | free(pixels_cmy); 554 | pixels_cmy = nullptr; 555 | free(pixels_alpha); 556 | pixels_alpha = nullptr; 557 | qWarning("ERROR: JxlDecoderSetImageOutBuffer failed"); 558 | m_parseState = ParseJpegXLError; 559 | return false; 560 | } 561 | 562 | if (JxlDecoderSetExtraChannelBuffer(m_decoder, &format_extra, pixels_black, extra_buffer_size, m_cmyk_channel_id) != JXL_DEC_SUCCESS) { 563 | free(pixels_black); 564 | pixels_black = nullptr; 565 | free(pixels_cmy); 566 | pixels_cmy = nullptr; 567 | free(pixels_alpha); 568 | pixels_alpha = nullptr; 569 | qWarning("ERROR: JxlDecoderSetExtraChannelBuffer failed"); 570 | m_parseState = ParseJpegXLError; 571 | return false; 572 | } 573 | 574 | if (JxlDecoderSetExtraChannelBuffer(m_decoder, &format_extra, pixels_alpha, extra_buffer_size, m_alpha_channel_id) != JXL_DEC_SUCCESS) { 575 | free(pixels_black); 576 | pixels_black = nullptr; 577 | free(pixels_cmy); 578 | pixels_cmy = nullptr; 579 | free(pixels_alpha); 580 | pixels_alpha = nullptr; 581 | qWarning("ERROR: JxlDecoderSetExtraChannelBuffer failed"); 582 | m_parseState = ParseJpegXLError; 583 | return false; 584 | } 585 | 586 | status = JxlDecoderProcessInput(m_decoder); 587 | if (status != JXL_DEC_FULL_IMAGE) { 588 | free(pixels_black); 589 | pixels_black = nullptr; 590 | free(pixels_cmy); 591 | pixels_cmy = nullptr; 592 | free(pixels_alpha); 593 | pixels_alpha = nullptr; 594 | qWarning("Unexpected event %d instead of JXL_DEC_FULL_IMAGE", status); 595 | m_parseState = ParseJpegXLError; 596 | return false; 597 | } 598 | 599 | const uchar *src_CMY = pixels_cmy; 600 | const uchar *src_K = pixels_black; 601 | for (int y = 0; y < tmp_cmyk_image.height(); y++) { 602 | uchar *write_pointer = tmp_cmyk_image.scanLine(y); 603 | for (int x = 0; x < tmp_cmyk_image.width(); x++) { 604 | *write_pointer = 255 - *src_CMY; // C 605 | write_pointer++; 606 | src_CMY++; 607 | *write_pointer = 255 - *src_CMY; // M 608 | write_pointer++; 609 | src_CMY++; 610 | *write_pointer = 255 - *src_CMY; // Y 611 | write_pointer++; 612 | src_CMY++; 613 | *write_pointer = 255 - *src_K; // K 614 | write_pointer++; 615 | src_K++; 616 | } 617 | } 618 | 619 | free(pixels_black); 620 | pixels_black = nullptr; 621 | free(pixels_cmy); 622 | pixels_cmy = nullptr; 623 | 624 | m_current_image = tmp_cmyk_image.convertedToColorSpace(QColorSpace(QColorSpace::SRgb), QImage::Format_ARGB32); 625 | if (m_current_image.isNull()) { 626 | free(pixels_alpha); 627 | pixels_alpha = nullptr; 628 | qWarning("ERROR: convertedToColorSpace returned empty image"); 629 | m_parseState = ParseJpegXLError; 630 | return false; 631 | } 632 | 633 | // set alpha channel into ARGB image 634 | const uchar *src_alpha = pixels_alpha; 635 | for (int y = 0; y < m_current_image.height(); y++) { 636 | uchar *write_pointer = m_current_image.scanLine(y); 637 | for (int x = 0; x < m_current_image.width(); x++) { 638 | #if Q_BYTE_ORDER == Q_LITTLE_ENDIAN 639 | write_pointer += 3; // skip BGR 640 | *write_pointer = *src_alpha; // A 641 | write_pointer++; 642 | src_alpha++; 643 | #else 644 | *write_pointer = *src_alpha; 645 | write_pointer += 4; // move 4 bytes (skip RGB) 646 | src_alpha++; 647 | #endif 648 | } 649 | } 650 | 651 | free(pixels_alpha); 652 | pixels_alpha = nullptr; 653 | } else { // CMYK (no alpha) 654 | m_current_image = imageAlloc(m_basicinfo.xsize, m_basicinfo.ysize, QImage::Format_CMYK8888); 655 | if (m_current_image.isNull()) { 656 | qWarning("Memory cannot be allocated"); 657 | m_parseState = ParseJpegXLError; 658 | return false; 659 | } 660 | 661 | m_current_image.setColorSpace(m_colorspace); 662 | 663 | pixels_cmy = reinterpret_cast(malloc(cmy_buffer_size)); 664 | if (!pixels_cmy) { 665 | qWarning("Memory cannot be allocated for CMY buffer"); 666 | m_parseState = ParseJpegXLError; 667 | return false; 668 | } 669 | 670 | pixels_black = reinterpret_cast(malloc(extra_buffer_size)); 671 | if (!pixels_black) { 672 | free(pixels_cmy); 673 | pixels_cmy = nullptr; 674 | qWarning("Memory cannot be allocated for BLACK buffer"); 675 | m_parseState = ParseJpegXLError; 676 | return false; 677 | } 678 | 679 | if (JxlDecoderSetImageOutBuffer(m_decoder, &m_input_pixel_format, pixels_cmy, cmy_buffer_size) != JXL_DEC_SUCCESS) { 680 | free(pixels_black); 681 | pixels_black = nullptr; 682 | free(pixels_cmy); 683 | pixels_cmy = nullptr; 684 | qWarning("ERROR: JxlDecoderSetImageOutBuffer failed"); 685 | m_parseState = ParseJpegXLError; 686 | return false; 687 | } 688 | 689 | if (JxlDecoderSetExtraChannelBuffer(m_decoder, &format_extra, pixels_black, extra_buffer_size, m_cmyk_channel_id) != JXL_DEC_SUCCESS) { 690 | free(pixels_black); 691 | pixels_black = nullptr; 692 | free(pixels_cmy); 693 | pixels_cmy = nullptr; 694 | qWarning("ERROR: JxlDecoderSetExtraChannelBuffer failed"); 695 | m_parseState = ParseJpegXLError; 696 | return false; 697 | } 698 | 699 | status = JxlDecoderProcessInput(m_decoder); 700 | if (status != JXL_DEC_FULL_IMAGE) { 701 | free(pixels_black); 702 | pixels_black = nullptr; 703 | free(pixels_cmy); 704 | pixels_cmy = nullptr; 705 | qWarning("Unexpected event %d instead of JXL_DEC_FULL_IMAGE", status); 706 | m_parseState = ParseJpegXLError; 707 | return false; 708 | } 709 | 710 | const uchar *src_CMY = pixels_cmy; 711 | const uchar *src_K = pixels_black; 712 | for (int y = 0; y < m_current_image.height(); y++) { 713 | uchar *write_pointer = m_current_image.scanLine(y); 714 | for (int x = 0; x < m_current_image.width(); x++) { 715 | *write_pointer = 255 - *src_CMY; // C 716 | write_pointer++; 717 | src_CMY++; 718 | *write_pointer = 255 - *src_CMY; // M 719 | write_pointer++; 720 | src_CMY++; 721 | *write_pointer = 255 - *src_CMY; // Y 722 | write_pointer++; 723 | src_CMY++; 724 | *write_pointer = 255 - *src_K; // K 725 | write_pointer++; 726 | src_K++; 727 | } 728 | } 729 | 730 | free(pixels_black); 731 | pixels_black = nullptr; 732 | free(pixels_cmy); 733 | pixels_cmy = nullptr; 734 | } 735 | #else 736 | // CMYK not supported in older Qt 737 | m_parseState = ParseJpegXLError; 738 | return false; 739 | #endif 740 | } else { // RGB or GRAY 741 | m_current_image = imageAlloc(m_basicinfo.xsize, m_basicinfo.ysize, m_input_image_format); 742 | if (m_current_image.isNull()) { 743 | qWarning("Memory cannot be allocated"); 744 | m_parseState = ParseJpegXLError; 745 | return false; 746 | } 747 | 748 | m_current_image.setColorSpace(m_colorspace); 749 | 750 | m_input_pixel_format.align = m_current_image.bytesPerLine(); 751 | 752 | size_t rgb_buffer_size = size_t(m_current_image.height() - 1) * size_t(m_current_image.bytesPerLine()); 753 | switch (m_input_pixel_format.data_type) { 754 | case JXL_TYPE_FLOAT: 755 | rgb_buffer_size += 4 * size_t(m_input_pixel_format.num_channels) * size_t(m_current_image.width()); 756 | break; 757 | case JXL_TYPE_UINT8: 758 | rgb_buffer_size += size_t(m_input_pixel_format.num_channels) * size_t(m_current_image.width()); 759 | break; 760 | case JXL_TYPE_UINT16: 761 | case JXL_TYPE_FLOAT16: 762 | rgb_buffer_size += 2 * size_t(m_input_pixel_format.num_channels) * size_t(m_current_image.width()); 763 | break; 764 | default: 765 | qWarning("ERROR: unsupported data type"); 766 | m_parseState = ParseJpegXLError; 767 | return false; 768 | break; 769 | } 770 | 771 | if (JxlDecoderSetImageOutBuffer(m_decoder, &m_input_pixel_format, m_current_image.bits(), rgb_buffer_size) != JXL_DEC_SUCCESS) { 772 | qWarning("ERROR: JxlDecoderSetImageOutBuffer failed"); 773 | m_parseState = ParseJpegXLError; 774 | return false; 775 | } 776 | 777 | status = JxlDecoderProcessInput(m_decoder); 778 | if (status != JXL_DEC_FULL_IMAGE) { 779 | qWarning("Unexpected event %d instead of JXL_DEC_FULL_IMAGE", status); 780 | m_parseState = ParseJpegXLError; 781 | return false; 782 | } 783 | 784 | if (m_target_image_format != m_input_image_format) { 785 | m_current_image.convertTo(m_target_image_format); 786 | } 787 | } 788 | 789 | m_next_image_delay = m_framedelays[m_currentimage_index]; 790 | m_previousimage_index = m_currentimage_index; 791 | 792 | if (m_framedelays.count() > 1) { 793 | m_currentimage_index++; 794 | 795 | if (m_currentimage_index >= m_framedelays.count()) { 796 | if (!rewind()) { 797 | return false; 798 | } 799 | 800 | // all frames in animation have been read 801 | m_parseState = ParseJpegXLFinished; 802 | } else { 803 | m_parseState = ParseJpegXLSuccess; 804 | } 805 | } else { 806 | // the static image has been read 807 | m_parseState = ParseJpegXLFinished; 808 | } 809 | 810 | return true; 811 | } 812 | 813 | bool QJpegXLHandler::read(QImage *image) 814 | { 815 | if (!ensureALLCounted()) { 816 | return false; 817 | } 818 | 819 | if (m_currentimage_index == m_previousimage_index) { 820 | *image = m_current_image; 821 | return jumpToNextImage(); 822 | } 823 | 824 | if (decode_one_frame()) { 825 | *image = m_current_image; 826 | return true; 827 | } else { 828 | return false; 829 | } 830 | } 831 | 832 | bool QJpegXLHandler::write(const QImage &image) 833 | { 834 | if (image.format() == QImage::Format_Invalid) { 835 | qWarning("No image data to save"); 836 | return false; 837 | } 838 | 839 | if ((image.width() > 0) && (image.height() > 0)) { 840 | if ((image.width() > 262144) || (image.height() > 262144)) { 841 | qWarning("Image (%dx%d) is too large to save!", image.width(), image.height()); 842 | return false; 843 | } 844 | 845 | if (sizeof(void *) <= 4) { 846 | if (image.width() > ((8192 * 8192) / image.height())) { 847 | qWarning("Image (%dx%d) is too large save via 32bit build of JXL plug-in", image.width(), image.height()); 848 | return false; 849 | } 850 | } else { 851 | if (image.width() > ((16384 * 16384) / image.height())) { 852 | qWarning("Image (%dx%d) will not be saved because it has more than 256 megapixels", image.width(), image.height()); 853 | return false; 854 | } 855 | } 856 | } else { 857 | qWarning("Image has zero dimension!"); 858 | return false; 859 | } 860 | 861 | JxlEncoder *encoder = JxlEncoderCreate(nullptr); 862 | if (!encoder) { 863 | qWarning("Failed to create Jxl encoder"); 864 | return false; 865 | } 866 | 867 | void *runner = nullptr; 868 | int num_worker_threads = qBound(1, QThread::idealThreadCount(), 64); 869 | 870 | if (num_worker_threads > 1) { 871 | runner = JxlThreadParallelRunnerCreate(nullptr, num_worker_threads); 872 | if (JxlEncoderSetParallelRunner(encoder, JxlThreadParallelRunner, runner) != JXL_ENC_SUCCESS) { 873 | qWarning("JxlEncoderSetParallelRunner failed"); 874 | JxlThreadParallelRunnerDestroy(runner); 875 | JxlEncoderDestroy(encoder); 876 | return false; 877 | } 878 | } 879 | 880 | if (m_quality > 100) { 881 | m_quality = 100; 882 | } else if (m_quality < 0) { 883 | m_quality = 90; 884 | } 885 | 886 | JxlBasicInfo output_info; 887 | JxlEncoderInitBasicInfo(&output_info); 888 | 889 | output_info.animation.tps_numerator = 10; 890 | output_info.animation.tps_denominator = 1; 891 | output_info.orientation = JXL_ORIENT_IDENTITY; 892 | 893 | bool save_cmyk = false; 894 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) 895 | if (image.format() == QImage::Format_CMYK8888 && image.colorSpace().isValid() && image.colorSpace().colorModel() == QColorSpace::ColorModel::Cmyk) { 896 | save_cmyk = true; 897 | } 898 | #endif 899 | 900 | JxlEncoderStatus status; 901 | JxlPixelFormat pixel_format; 902 | pixel_format.endianness = JXL_NATIVE_ENDIAN; 903 | pixel_format.align = 0; 904 | 905 | if (save_cmyk) { // CMYK is always lossless 906 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) 907 | JxlEncoderUseContainer(encoder, JXL_TRUE); 908 | JxlEncoderSetCodestreamLevel(encoder, 10); 909 | 910 | output_info.have_container = JXL_TRUE; 911 | output_info.uses_original_profile = JXL_TRUE; 912 | output_info.xsize = image.width(); 913 | output_info.ysize = image.height(); 914 | output_info.num_color_channels = 3; 915 | output_info.bits_per_sample = 8; 916 | output_info.alpha_bits = 0; 917 | output_info.num_extra_channels = 1; 918 | 919 | pixel_format.num_channels = 3; 920 | pixel_format.data_type = JXL_TYPE_UINT8; 921 | 922 | JxlPixelFormat format_extra; 923 | format_extra.num_channels = 1; 924 | format_extra.data_type = JXL_TYPE_UINT8; 925 | format_extra.endianness = JXL_NATIVE_ENDIAN; 926 | format_extra.align = 0; 927 | 928 | JxlExtraChannelInfo extra_black_channel; 929 | JxlEncoderInitExtraChannelInfo(JXL_CHANNEL_BLACK, &extra_black_channel); 930 | extra_black_channel.bits_per_sample = output_info.bits_per_sample; 931 | extra_black_channel.exponent_bits_per_sample = output_info.exponent_bits_per_sample; 932 | 933 | const QByteArray cmyk_profile = image.colorSpace().iccProfile(); 934 | if (cmyk_profile.isEmpty()) { 935 | qWarning("ERROR saving CMYK JXL: empty ICC profile"); 936 | if (runner) { 937 | JxlThreadParallelRunnerDestroy(runner); 938 | } 939 | JxlEncoderDestroy(encoder); 940 | return false; 941 | } 942 | 943 | status = JxlEncoderSetBasicInfo(encoder, &output_info); 944 | if (status != JXL_ENC_SUCCESS) { 945 | qWarning("JxlEncoderSetBasicInfo for CMYK image failed!"); 946 | if (runner) { 947 | JxlThreadParallelRunnerDestroy(runner); 948 | } 949 | JxlEncoderDestroy(encoder); 950 | return false; 951 | } 952 | 953 | status = JxlEncoderSetExtraChannelInfo(encoder, 0, &extra_black_channel); 954 | if (status != JXL_ENC_SUCCESS) { 955 | qWarning("JxlEncoderSetExtraChannelInfo for CMYK image failed!"); 956 | if (runner) { 957 | JxlThreadParallelRunnerDestroy(runner); 958 | } 959 | JxlEncoderDestroy(encoder); 960 | return false; 961 | } 962 | 963 | status = JxlEncoderSetICCProfile(encoder, reinterpret_cast(cmyk_profile.constData()), cmyk_profile.size()); 964 | if (status != JXL_ENC_SUCCESS) { 965 | qWarning("JxlEncoderSetICCProfile for CMYK image failed!"); 966 | if (runner) { 967 | JxlThreadParallelRunnerDestroy(runner); 968 | } 969 | JxlEncoderDestroy(encoder); 970 | return false; 971 | } 972 | 973 | const size_t extra_buffer_size = size_t(image.width()) * size_t(image.height()); 974 | const size_t cmy_buffer_size = extra_buffer_size * 3; 975 | 976 | uchar *pixels_cmy = nullptr; 977 | uchar *pixels_black = nullptr; 978 | 979 | pixels_cmy = reinterpret_cast(malloc(cmy_buffer_size)); 980 | if (!pixels_cmy) { 981 | qWarning("Memory cannot be allocated for CMY buffer"); 982 | if (runner) { 983 | JxlThreadParallelRunnerDestroy(runner); 984 | } 985 | JxlEncoderDestroy(encoder); 986 | return false; 987 | } 988 | 989 | pixels_black = reinterpret_cast(malloc(extra_buffer_size)); 990 | if (!pixels_black) { 991 | qWarning("Memory cannot be allocated for BLACK buffer"); 992 | free(pixels_cmy); 993 | pixels_cmy = nullptr; 994 | 995 | if (runner) { 996 | JxlThreadParallelRunnerDestroy(runner); 997 | } 998 | JxlEncoderDestroy(encoder); 999 | return false; 1000 | } 1001 | 1002 | uchar *dest_CMY = pixels_cmy; 1003 | uchar *dest_K = pixels_black; 1004 | for (int y = 0; y < image.height(); y++) { 1005 | const uchar *src_CMYK = image.constScanLine(y); 1006 | for (int x = 0; x < image.width(); x++) { 1007 | *dest_CMY = 255 - *src_CMYK; // C 1008 | dest_CMY++; 1009 | src_CMYK++; 1010 | *dest_CMY = 255 - *src_CMYK; // M 1011 | dest_CMY++; 1012 | src_CMYK++; 1013 | *dest_CMY = 255 - *src_CMYK; // Y 1014 | dest_CMY++; 1015 | src_CMYK++; 1016 | *dest_K = 255 - *src_CMYK; // K 1017 | dest_K++; 1018 | src_CMYK++; 1019 | } 1020 | } 1021 | 1022 | JxlEncoderFrameSettings *frame_settings_lossless = JxlEncoderFrameSettingsCreate(encoder, nullptr); 1023 | JxlEncoderSetFrameDistance(frame_settings_lossless, 0); 1024 | JxlEncoderSetFrameLossless(frame_settings_lossless, JXL_TRUE); 1025 | 1026 | status = JxlEncoderAddImageFrame(frame_settings_lossless, &pixel_format, pixels_cmy, cmy_buffer_size); 1027 | if (status == JXL_ENC_ERROR) { 1028 | qWarning("JxlEncoderAddImageFrame failed!"); 1029 | free(pixels_black); 1030 | pixels_black = nullptr; 1031 | free(pixels_cmy); 1032 | pixels_cmy = nullptr; 1033 | if (runner) { 1034 | JxlThreadParallelRunnerDestroy(runner); 1035 | } 1036 | JxlEncoderDestroy(encoder); 1037 | return false; 1038 | } 1039 | 1040 | status = JxlEncoderSetExtraChannelBuffer(frame_settings_lossless, &format_extra, pixels_black, extra_buffer_size, 0); 1041 | 1042 | free(pixels_black); 1043 | pixels_black = nullptr; 1044 | free(pixels_cmy); 1045 | pixels_cmy = nullptr; 1046 | 1047 | if (status == JXL_ENC_ERROR) { 1048 | qWarning("JxlEncoderSetExtraChannelBuffer failed!"); 1049 | if (runner) { 1050 | JxlThreadParallelRunnerDestroy(runner); 1051 | } 1052 | JxlEncoderDestroy(encoder); 1053 | return false; 1054 | } 1055 | #else 1056 | if (runner) { 1057 | JxlThreadParallelRunnerDestroy(runner); 1058 | } 1059 | JxlEncoderDestroy(encoder); 1060 | return false; 1061 | #endif 1062 | } else { // RGB or GRAY saving 1063 | int save_depth = 8; // 8 / 16 / 32 1064 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 1065 | bool save_fp = false; 1066 | #endif 1067 | bool is_gray = false; 1068 | // depth detection 1069 | switch (image.format()) { 1070 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 1071 | case QImage::Format_RGBX32FPx4: 1072 | case QImage::Format_RGBA32FPx4: 1073 | case QImage::Format_RGBA32FPx4_Premultiplied: 1074 | save_depth = 32; 1075 | save_fp = true; 1076 | break; 1077 | case QImage::Format_RGBX16FPx4: 1078 | case QImage::Format_RGBA16FPx4: 1079 | case QImage::Format_RGBA16FPx4_Premultiplied: 1080 | save_depth = 16; 1081 | save_fp = true; 1082 | break; 1083 | #endif 1084 | case QImage::Format_BGR30: 1085 | case QImage::Format_A2BGR30_Premultiplied: 1086 | case QImage::Format_RGB30: 1087 | case QImage::Format_A2RGB30_Premultiplied: 1088 | case QImage::Format_RGBX64: 1089 | case QImage::Format_RGBA64: 1090 | case QImage::Format_RGBA64_Premultiplied: 1091 | save_depth = 16; 1092 | break; 1093 | case QImage::Format_RGB32: 1094 | case QImage::Format_ARGB32: 1095 | case QImage::Format_ARGB32_Premultiplied: 1096 | case QImage::Format_RGB888: 1097 | case QImage::Format_RGBX8888: 1098 | case QImage::Format_RGBA8888: 1099 | case QImage::Format_RGBA8888_Premultiplied: 1100 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) 1101 | case QImage::Format_CMYK8888: 1102 | #endif 1103 | save_depth = 8; 1104 | break; 1105 | case QImage::Format_Grayscale16: 1106 | save_depth = 16; 1107 | is_gray = true; 1108 | break; 1109 | case QImage::Format_Grayscale8: 1110 | case QImage::Format_Alpha8: 1111 | case QImage::Format_Mono: 1112 | case QImage::Format_MonoLSB: 1113 | save_depth = 8; 1114 | is_gray = true; 1115 | break; 1116 | case QImage::Format_Indexed8: 1117 | save_depth = 8; 1118 | is_gray = image.isGrayscale(); 1119 | break; 1120 | default: 1121 | if (image.depth() > 32) { 1122 | save_depth = 16; 1123 | } else { 1124 | save_depth = 8; 1125 | } 1126 | break; 1127 | } 1128 | 1129 | QImage::Format tmpformat; 1130 | 1131 | if (save_depth > 8 && is_gray) { // 16bit depth gray 1132 | pixel_format.data_type = JXL_TYPE_UINT16; 1133 | output_info.num_color_channels = 1; 1134 | output_info.bits_per_sample = 16; 1135 | tmpformat = QImage::Format_Grayscale16; 1136 | pixel_format.num_channels = 1; 1137 | } else if (is_gray) { // 8bit depth gray 1138 | pixel_format.data_type = JXL_TYPE_UINT8; 1139 | output_info.num_color_channels = 1; 1140 | output_info.bits_per_sample = 8; 1141 | tmpformat = QImage::Format_Grayscale8; 1142 | pixel_format.num_channels = 1; 1143 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 1144 | } else if (save_depth > 16) { // 32bit depth rgb 1145 | pixel_format.data_type = JXL_TYPE_FLOAT; 1146 | output_info.exponent_bits_per_sample = 8; 1147 | output_info.num_color_channels = 3; 1148 | output_info.bits_per_sample = 32; 1149 | 1150 | if (image.hasAlphaChannel()) { 1151 | tmpformat = QImage::Format_RGBA32FPx4; 1152 | pixel_format.num_channels = 4; 1153 | output_info.alpha_bits = 32; 1154 | output_info.alpha_exponent_bits = 8; 1155 | output_info.num_extra_channels = 1; 1156 | } else { 1157 | tmpformat = QImage::Format_RGBX32FPx4; 1158 | pixel_format.num_channels = 3; 1159 | output_info.alpha_bits = 0; 1160 | output_info.num_extra_channels = 0; 1161 | } 1162 | #endif 1163 | } else if (save_depth > 8) { // 16bit depth rgb 1164 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 1165 | pixel_format.data_type = save_fp ? JXL_TYPE_FLOAT16 : JXL_TYPE_UINT16; 1166 | output_info.exponent_bits_per_sample = save_fp ? 5 : 0; 1167 | #else 1168 | pixel_format.data_type = JXL_TYPE_UINT16; 1169 | output_info.exponent_bits_per_sample = 0; 1170 | #endif 1171 | output_info.num_color_channels = 3; 1172 | output_info.bits_per_sample = 16; 1173 | 1174 | if (image.hasAlphaChannel()) { 1175 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 1176 | tmpformat = save_fp ? QImage::Format_RGBA16FPx4 : QImage::Format_RGBA64; 1177 | output_info.alpha_exponent_bits = save_fp ? 5 : 0; 1178 | #else 1179 | tmpformat = QImage::Format_RGBA64; 1180 | output_info.alpha_exponent_bits = 0; 1181 | #endif 1182 | pixel_format.num_channels = 4; 1183 | output_info.alpha_bits = 16; 1184 | output_info.num_extra_channels = 1; 1185 | } else { 1186 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 1187 | tmpformat = save_fp ? QImage::Format_RGBX16FPx4 : QImage::Format_RGBX64; 1188 | #else 1189 | tmpformat = QImage::Format_RGBX64; 1190 | #endif 1191 | pixel_format.num_channels = 3; 1192 | output_info.alpha_bits = 0; 1193 | output_info.num_extra_channels = 0; 1194 | } 1195 | } else { // 8bit depth rgb 1196 | pixel_format.data_type = JXL_TYPE_UINT8; 1197 | output_info.num_color_channels = 3; 1198 | output_info.bits_per_sample = 8; 1199 | 1200 | if (image.hasAlphaChannel()) { 1201 | tmpformat = QImage::Format_RGBA8888; 1202 | pixel_format.num_channels = 4; 1203 | output_info.alpha_bits = 8; 1204 | output_info.num_extra_channels = 1; 1205 | } else { 1206 | tmpformat = QImage::Format_RGB888; 1207 | pixel_format.num_channels = 3; 1208 | output_info.alpha_bits = 0; 1209 | output_info.num_extra_channels = 0; 1210 | } 1211 | } 1212 | 1213 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) 1214 | QImage tmpimage; 1215 | if (image.colorSpace().isValid()) { 1216 | if (is_gray && image.colorSpace().colorModel() != QColorSpace::ColorModel::Gray) { 1217 | // convert to Gray profile 1218 | QPointF gray_whitePoint = image.colorSpace().whitePoint(); 1219 | if (gray_whitePoint.isNull()) { 1220 | gray_whitePoint = QPointF(0.3127f, 0.329f); 1221 | } 1222 | 1223 | QColorSpace::TransferFunction gray_trc = image.colorSpace().transferFunction(); 1224 | float gamma_gray = image.colorSpace().gamma(); 1225 | if (gray_trc == QColorSpace::TransferFunction::Custom) { 1226 | gray_trc = QColorSpace::TransferFunction::SRgb; 1227 | } 1228 | 1229 | const QColorSpace gray_profile(gray_whitePoint, gray_trc, gamma_gray); 1230 | if (gray_profile.isValid()) { 1231 | tmpimage = image.convertedToColorSpace(gray_profile, tmpformat); 1232 | } else { 1233 | qWarning("JXL plugin created invalid grayscale QColorSpace!"); 1234 | tmpimage = image.convertToFormat(tmpformat); 1235 | } 1236 | } else if (!is_gray && image.colorSpace().colorModel() != QColorSpace::ColorModel::Rgb) { 1237 | // convert to RGB profile 1238 | QPointF whitePoint = image.colorSpace().whitePoint(); 1239 | if (whitePoint.isNull()) { 1240 | whitePoint = QPointF(0.3127f, 0.329f); 1241 | } 1242 | 1243 | const QPointF redP(0.64f, 0.33f); 1244 | const QPointF greenP(0.3f, 0.6f); 1245 | const QPointF blueP(0.15f, 0.06f); 1246 | 1247 | QColorSpace::TransferFunction trc_rgb = image.colorSpace().transferFunction(); 1248 | float gamma_rgb = image.colorSpace().gamma(); 1249 | if (trc_rgb == QColorSpace::TransferFunction::Custom) { 1250 | trc_rgb = QColorSpace::TransferFunction::SRgb; 1251 | } 1252 | 1253 | const QColorSpace rgb_profile(whitePoint, redP, greenP, blueP, trc_rgb, gamma_rgb); 1254 | if (rgb_profile.isValid()) { 1255 | tmpimage = image.convertedToColorSpace(rgb_profile, tmpformat); 1256 | } else { 1257 | qWarning("JXL plugin created invalid RGB QColorSpace!"); 1258 | tmpimage = image.convertToFormat(tmpformat); 1259 | } 1260 | } else { // ColorSpace matches the format 1261 | tmpimage = image.convertToFormat(tmpformat); 1262 | } 1263 | } else { // no ColorSpace or invalid 1264 | tmpimage = image.convertToFormat(tmpformat); 1265 | } 1266 | #else 1267 | QImage tmpimage = image.convertToFormat(tmpformat); 1268 | #endif 1269 | 1270 | output_info.xsize = tmpimage.width(); 1271 | output_info.ysize = tmpimage.height(); 1272 | 1273 | if (output_info.xsize == 0 || output_info.ysize == 0 || tmpimage.isNull()) { 1274 | qWarning("Unable to allocate memory for output image"); 1275 | if (runner) { 1276 | JxlThreadParallelRunnerDestroy(runner); 1277 | } 1278 | JxlEncoderDestroy(encoder); 1279 | return false; 1280 | } 1281 | 1282 | JxlColorEncoding color_profile; 1283 | JxlColorEncodingSetToSRGB(&color_profile, is_gray ? JXL_TRUE : JXL_FALSE); 1284 | 1285 | QByteArray iccprofile; 1286 | 1287 | if (m_quality == 100) { // try to use ICC for lossless 1288 | output_info.uses_original_profile = JXL_TRUE; 1289 | iccprofile = tmpimage.colorSpace().iccProfile(); 1290 | } else { // try to detect encoded profile (smaller than ICC) 1291 | output_info.uses_original_profile = JXL_FALSE; 1292 | 1293 | if (tmpimage.colorSpace().isValid()) { 1294 | QPointF whiteP(0.3127f, 0.329f); 1295 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) 1296 | whiteP = image.colorSpace().whitePoint(); 1297 | #endif 1298 | 1299 | switch (tmpimage.colorSpace().primaries()) { 1300 | case QColorSpace::Primaries::SRgb: 1301 | color_profile.white_point = JXL_WHITE_POINT_D65; 1302 | color_profile.primaries = JXL_PRIMARIES_SRGB; 1303 | break; 1304 | case QColorSpace::Primaries::AdobeRgb: 1305 | color_profile.white_point = JXL_WHITE_POINT_D65; 1306 | color_profile.primaries = JXL_PRIMARIES_CUSTOM; 1307 | color_profile.primaries_red_xy[0] = 0.640; 1308 | color_profile.primaries_red_xy[1] = 0.330; 1309 | color_profile.primaries_green_xy[0] = 0.210; 1310 | color_profile.primaries_green_xy[1] = 0.710; 1311 | color_profile.primaries_blue_xy[0] = 0.150; 1312 | color_profile.primaries_blue_xy[1] = 0.060; 1313 | break; 1314 | case QColorSpace::Primaries::DciP3D65: 1315 | color_profile.white_point = JXL_WHITE_POINT_D65; 1316 | color_profile.primaries = JXL_PRIMARIES_P3; 1317 | color_profile.primaries_red_xy[0] = 0.680; 1318 | color_profile.primaries_red_xy[1] = 0.320; 1319 | color_profile.primaries_green_xy[0] = 0.265; 1320 | color_profile.primaries_green_xy[1] = 0.690; 1321 | color_profile.primaries_blue_xy[0] = 0.150; 1322 | color_profile.primaries_blue_xy[1] = 0.060; 1323 | break; 1324 | case QColorSpace::Primaries::ProPhotoRgb: 1325 | color_profile.white_point = JXL_WHITE_POINT_CUSTOM; 1326 | #if QT_VERSION < QT_VERSION_CHECK(6, 8, 0) 1327 | whiteP = QPointF(0.3457f, 0.3585f); 1328 | #endif 1329 | color_profile.white_point_xy[0] = whiteP.x(); 1330 | color_profile.white_point_xy[1] = whiteP.y(); 1331 | color_profile.primaries = JXL_PRIMARIES_CUSTOM; 1332 | color_profile.primaries_red_xy[0] = 0.7347; 1333 | color_profile.primaries_red_xy[1] = 0.2653; 1334 | color_profile.primaries_green_xy[0] = 0.1596; 1335 | color_profile.primaries_green_xy[1] = 0.8404; 1336 | color_profile.primaries_blue_xy[0] = 0.0366; 1337 | color_profile.primaries_blue_xy[1] = 0.0001; 1338 | break; 1339 | #if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0) 1340 | case QColorSpace::Primaries::Bt2020: 1341 | color_profile.white_point = JXL_WHITE_POINT_D65; 1342 | color_profile.primaries = JXL_PRIMARIES_2100; 1343 | color_profile.primaries_red_xy[0] = 0.708; 1344 | color_profile.primaries_red_xy[1] = 0.292; 1345 | color_profile.primaries_green_xy[0] = 0.170; 1346 | color_profile.primaries_green_xy[1] = 0.797; 1347 | color_profile.primaries_blue_xy[0] = 0.131; 1348 | color_profile.primaries_blue_xy[1] = 0.046; 1349 | break; 1350 | #endif 1351 | default: 1352 | if (is_gray && !whiteP.isNull()) { 1353 | color_profile.white_point = JXL_WHITE_POINT_CUSTOM; 1354 | color_profile.white_point_xy[0] = whiteP.x(); 1355 | color_profile.white_point_xy[1] = whiteP.y(); 1356 | } else { 1357 | iccprofile = tmpimage.colorSpace().iccProfile(); 1358 | } 1359 | break; 1360 | } 1361 | 1362 | if (iccprofile.isEmpty()) { 1363 | const double gamma_profile = tmpimage.colorSpace().gamma(); 1364 | 1365 | switch (tmpimage.colorSpace().transferFunction()) { 1366 | case QColorSpace::TransferFunction::Linear: 1367 | color_profile.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR; 1368 | break; 1369 | case QColorSpace::TransferFunction::Gamma: 1370 | if (gamma_profile > 0) { 1371 | color_profile.transfer_function = JXL_TRANSFER_FUNCTION_GAMMA; 1372 | color_profile.gamma = 1.0 / gamma_profile; 1373 | } else { 1374 | iccprofile = tmpimage.colorSpace().iccProfile(); 1375 | } 1376 | break; 1377 | case QColorSpace::TransferFunction::SRgb: 1378 | color_profile.transfer_function = JXL_TRANSFER_FUNCTION_SRGB; 1379 | break; 1380 | default: 1381 | iccprofile = tmpimage.colorSpace().iccProfile(); 1382 | break; 1383 | } 1384 | } 1385 | } 1386 | } 1387 | 1388 | if ((save_depth > 8) && (image.hasAlphaChannel() || output_info.uses_original_profile)) { 1389 | output_info.have_container = JXL_TRUE; 1390 | JxlEncoderUseContainer(encoder, JXL_TRUE); 1391 | } 1392 | 1393 | status = JxlEncoderSetBasicInfo(encoder, &output_info); 1394 | if (status != JXL_ENC_SUCCESS) { 1395 | qWarning("JxlEncoderSetBasicInfo failed!"); 1396 | if (runner) { 1397 | JxlThreadParallelRunnerDestroy(runner); 1398 | } 1399 | JxlEncoderDestroy(encoder); 1400 | return false; 1401 | } 1402 | 1403 | if (iccprofile.size() > 0) { 1404 | status = JxlEncoderSetICCProfile(encoder, reinterpret_cast(iccprofile.constData()), iccprofile.size()); 1405 | if (status != JXL_ENC_SUCCESS) { 1406 | qWarning("JxlEncoderSetICCProfile failed!"); 1407 | if (runner) { 1408 | JxlThreadParallelRunnerDestroy(runner); 1409 | } 1410 | JxlEncoderDestroy(encoder); 1411 | return false; 1412 | } 1413 | } else { 1414 | status = JxlEncoderSetColorEncoding(encoder, &color_profile); 1415 | if (status != JXL_ENC_SUCCESS) { 1416 | qWarning("JxlEncoderSetColorEncoding failed!"); 1417 | if (runner) { 1418 | JxlThreadParallelRunnerDestroy(runner); 1419 | } 1420 | JxlEncoderDestroy(encoder); 1421 | return false; 1422 | } 1423 | } 1424 | 1425 | JxlEncoderFrameSettings *encoder_options = JxlEncoderFrameSettingsCreate(encoder, nullptr); 1426 | 1427 | if (m_quality == 100) { // lossless 1428 | JxlEncoderSetFrameDistance(encoder_options, 0.0f); 1429 | JxlEncoderSetFrameLossless(encoder_options, JXL_TRUE); 1430 | } else { 1431 | JxlEncoderSetFrameDistance(encoder_options, JxlEncoderDistanceFromQuality(m_quality)); 1432 | JxlEncoderSetFrameLossless(encoder_options, JXL_FALSE); 1433 | } 1434 | 1435 | size_t buffer_size; 1436 | #if QT_VERSION >= QT_VERSION_CHECK(6, 2, 0) 1437 | if (tmpimage.format() == QImage::Format_RGBX32FPx4) { // pack 32-bit depth RGBX -> RGB 1438 | buffer_size = 12 * size_t(tmpimage.width()) * size_t(tmpimage.height()); 1439 | 1440 | float *packed_pixels32 = reinterpret_cast(malloc(buffer_size)); 1441 | if (!packed_pixels32) { 1442 | qWarning("ERROR: JXL plug-in failed to allocate memory"); 1443 | return false; 1444 | } 1445 | 1446 | float *dest_pixels32 = packed_pixels32; 1447 | for (int y = 0; y < tmpimage.height(); y++) { 1448 | const float *src_pixels32 = reinterpret_cast(tmpimage.constScanLine(y)); 1449 | for (int x = 0; x < tmpimage.width(); x++) { 1450 | *dest_pixels32 = *src_pixels32; // R 1451 | dest_pixels32++; 1452 | src_pixels32++; 1453 | *dest_pixels32 = *src_pixels32; // G 1454 | dest_pixels32++; 1455 | src_pixels32++; 1456 | *dest_pixels32 = *src_pixels32; // B 1457 | dest_pixels32++; 1458 | src_pixels32 += 2; // skip X 1459 | } 1460 | } 1461 | 1462 | status = JxlEncoderAddImageFrame(encoder_options, &pixel_format, packed_pixels32, buffer_size); 1463 | free(packed_pixels32); 1464 | } else if (tmpimage.format() == QImage::Format_RGBX16FPx4 || tmpimage.format() == QImage::Format_RGBX64) { 1465 | #else 1466 | if (tmpimage.format() == QImage::Format_RGBX64) { 1467 | #endif 1468 | // pack 16-bit depth RGBX -> RGB 1469 | buffer_size = 6 * size_t(tmpimage.width()) * size_t(tmpimage.height()); 1470 | 1471 | quint16 *packed_pixels16 = reinterpret_cast(malloc(buffer_size)); 1472 | if (!packed_pixels16) { 1473 | qWarning("ERROR: JXL plug-in failed to allocate memory"); 1474 | return false; 1475 | } 1476 | 1477 | quint16 *dest_pixels16 = packed_pixels16; 1478 | for (int y = 0; y < tmpimage.height(); y++) { 1479 | const quint16 *src_pixels16 = reinterpret_cast(tmpimage.constScanLine(y)); 1480 | for (int x = 0; x < tmpimage.width(); x++) { 1481 | *dest_pixels16 = *src_pixels16; // R 1482 | dest_pixels16++; 1483 | src_pixels16++; 1484 | *dest_pixels16 = *src_pixels16; // G 1485 | dest_pixels16++; 1486 | src_pixels16++; 1487 | *dest_pixels16 = *src_pixels16; // B 1488 | dest_pixels16++; 1489 | src_pixels16 += 2; // skip X 1490 | } 1491 | } 1492 | 1493 | status = JxlEncoderAddImageFrame(encoder_options, &pixel_format, packed_pixels16, buffer_size); 1494 | free(packed_pixels16); 1495 | } else { // use QImage's data directly 1496 | pixel_format.align = tmpimage.bytesPerLine(); 1497 | 1498 | buffer_size = size_t(tmpimage.height() - 1) * size_t(tmpimage.bytesPerLine()); 1499 | switch (pixel_format.data_type) { 1500 | case JXL_TYPE_FLOAT: 1501 | buffer_size += 4 * size_t(pixel_format.num_channels) * size_t(tmpimage.width()); 1502 | break; 1503 | case JXL_TYPE_UINT8: 1504 | buffer_size += size_t(pixel_format.num_channels) * size_t(tmpimage.width()); 1505 | break; 1506 | case JXL_TYPE_UINT16: 1507 | case JXL_TYPE_FLOAT16: 1508 | buffer_size += 2 * size_t(pixel_format.num_channels) * size_t(tmpimage.width()); 1509 | break; 1510 | default: 1511 | qWarning("ERROR: unsupported data type"); 1512 | return false; 1513 | break; 1514 | } 1515 | 1516 | status = JxlEncoderAddImageFrame(encoder_options, &pixel_format, tmpimage.constBits(), buffer_size); 1517 | } 1518 | 1519 | if (status == JXL_ENC_ERROR) { 1520 | qWarning("JxlEncoderAddImageFrame failed!"); 1521 | if (runner) { 1522 | JxlThreadParallelRunnerDestroy(runner); 1523 | } 1524 | JxlEncoderDestroy(encoder); 1525 | return false; 1526 | } 1527 | } 1528 | 1529 | JxlEncoderCloseInput(encoder); 1530 | 1531 | std::vector compressed; 1532 | compressed.resize(4096); 1533 | size_t offset = 0; 1534 | uint8_t *next_out; 1535 | size_t avail_out; 1536 | do { 1537 | next_out = compressed.data() + offset; 1538 | avail_out = compressed.size() - offset; 1539 | status = JxlEncoderProcessOutput(encoder, &next_out, &avail_out); 1540 | 1541 | if (status == JXL_ENC_NEED_MORE_OUTPUT) { 1542 | offset = next_out - compressed.data(); 1543 | compressed.resize(compressed.size() * 2); 1544 | } else if (status == JXL_ENC_ERROR) { 1545 | qWarning("JxlEncoderProcessOutput failed!"); 1546 | if (runner) { 1547 | JxlThreadParallelRunnerDestroy(runner); 1548 | } 1549 | JxlEncoderDestroy(encoder); 1550 | return false; 1551 | } 1552 | } while (status != JXL_ENC_SUCCESS); 1553 | 1554 | if (runner) { 1555 | JxlThreadParallelRunnerDestroy(runner); 1556 | } 1557 | JxlEncoderDestroy(encoder); 1558 | 1559 | compressed.resize(next_out - compressed.data()); 1560 | 1561 | if (compressed.size() > 0) { 1562 | qint64 write_status = device()->write(reinterpret_cast(compressed.data()), compressed.size()); 1563 | 1564 | if (write_status > 0) { 1565 | return true; 1566 | } else if (write_status == -1) { 1567 | qWarning("Write error: %s\n", qUtf8Printable(device()->errorString())); 1568 | } 1569 | } 1570 | 1571 | return false; 1572 | } 1573 | 1574 | QVariant QJpegXLHandler::option(ImageOption option) const 1575 | { 1576 | if (option == Quality) { 1577 | return m_quality; 1578 | } 1579 | 1580 | if (!supportsOption(option) || !ensureParsed()) { 1581 | return QVariant(); 1582 | } 1583 | 1584 | switch (option) { 1585 | case Size: 1586 | return QSize(m_basicinfo.xsize, m_basicinfo.ysize); 1587 | case Animation: 1588 | if (m_basicinfo.have_animation) { 1589 | return true; 1590 | } else { 1591 | return false; 1592 | } 1593 | default: 1594 | return QVariant(); 1595 | } 1596 | } 1597 | 1598 | void QJpegXLHandler::setOption(ImageOption option, const QVariant &value) 1599 | { 1600 | switch (option) { 1601 | case Quality: 1602 | m_quality = value.toInt(); 1603 | if (m_quality > 100) { 1604 | m_quality = 100; 1605 | } else if (m_quality < 0) { 1606 | m_quality = 90; 1607 | } 1608 | return; 1609 | default: 1610 | break; 1611 | } 1612 | QImageIOHandler::setOption(option, value); 1613 | } 1614 | 1615 | bool QJpegXLHandler::supportsOption(ImageOption option) const 1616 | { 1617 | return option == Quality || option == Size || option == Animation; 1618 | } 1619 | 1620 | int QJpegXLHandler::imageCount() const 1621 | { 1622 | if (!ensureParsed()) { 1623 | return 0; 1624 | } 1625 | 1626 | if (m_parseState == ParseJpegXLBasicInfoParsed) { 1627 | if (!m_basicinfo.have_animation) { 1628 | return 1; 1629 | } 1630 | 1631 | if (!ensureALLCounted()) { 1632 | return 0; 1633 | } 1634 | } 1635 | 1636 | if (!m_framedelays.isEmpty()) { 1637 | return m_framedelays.count(); 1638 | } 1639 | return 0; 1640 | } 1641 | 1642 | int QJpegXLHandler::currentImageNumber() const 1643 | { 1644 | if (m_parseState == ParseJpegXLNotParsed) { 1645 | return -1; 1646 | } 1647 | 1648 | if (m_parseState == ParseJpegXLError || m_parseState == ParseJpegXLBasicInfoParsed || !m_decoder) { 1649 | return 0; 1650 | } 1651 | 1652 | return m_currentimage_index; 1653 | } 1654 | 1655 | bool QJpegXLHandler::jumpToNextImage() 1656 | { 1657 | if (!ensureALLCounted()) { 1658 | return false; 1659 | } 1660 | 1661 | if (m_framedelays.count() > 1) { 1662 | m_currentimage_index++; 1663 | 1664 | if (m_currentimage_index >= m_framedelays.count()) { 1665 | if (!rewind()) { 1666 | return false; 1667 | } 1668 | } else { 1669 | JxlDecoderSkipFrames(m_decoder, 1); 1670 | } 1671 | } 1672 | 1673 | m_parseState = ParseJpegXLSuccess; 1674 | return true; 1675 | } 1676 | 1677 | bool QJpegXLHandler::jumpToImage(int imageNumber) 1678 | { 1679 | if (!ensureALLCounted()) { 1680 | return false; 1681 | } 1682 | 1683 | if (imageNumber < 0 || imageNumber >= m_framedelays.count()) { 1684 | return false; 1685 | } 1686 | 1687 | if (imageNumber == m_currentimage_index) { 1688 | m_parseState = ParseJpegXLSuccess; 1689 | return true; 1690 | } 1691 | 1692 | if (imageNumber > m_currentimage_index) { 1693 | JxlDecoderSkipFrames(m_decoder, imageNumber - m_currentimage_index); 1694 | m_currentimage_index = imageNumber; 1695 | m_parseState = ParseJpegXLSuccess; 1696 | return true; 1697 | } 1698 | 1699 | if (!rewind()) { 1700 | return false; 1701 | } 1702 | 1703 | if (imageNumber > 0) { 1704 | JxlDecoderSkipFrames(m_decoder, imageNumber); 1705 | } 1706 | m_currentimage_index = imageNumber; 1707 | m_parseState = ParseJpegXLSuccess; 1708 | return true; 1709 | } 1710 | 1711 | int QJpegXLHandler::nextImageDelay() const 1712 | { 1713 | if (!ensureALLCounted()) { 1714 | return 0; 1715 | } 1716 | 1717 | if (m_framedelays.count() < 2) { 1718 | return 0; 1719 | } 1720 | 1721 | return m_next_image_delay; 1722 | } 1723 | 1724 | int QJpegXLHandler::loopCount() const 1725 | { 1726 | if (!ensureParsed()) { 1727 | return 0; 1728 | } 1729 | 1730 | if (m_basicinfo.have_animation) { 1731 | return (m_basicinfo.animation.num_loops > 0) ? m_basicinfo.animation.num_loops - 1 : -1; 1732 | } else { 1733 | return 0; 1734 | } 1735 | } 1736 | 1737 | bool QJpegXLHandler::rewind() 1738 | { 1739 | m_currentimage_index = 0; 1740 | 1741 | JxlDecoderReleaseInput(m_decoder); 1742 | JxlDecoderRewind(m_decoder); 1743 | if (m_runner) { 1744 | if (JxlDecoderSetParallelRunner(m_decoder, JxlThreadParallelRunner, m_runner) != JXL_DEC_SUCCESS) { 1745 | qWarning("ERROR: JxlDecoderSetParallelRunner failed"); 1746 | m_parseState = ParseJpegXLError; 1747 | return false; 1748 | } 1749 | } 1750 | 1751 | if (JxlDecoderSetInput(m_decoder, reinterpret_cast(m_rawData.constData()), m_rawData.size()) != JXL_DEC_SUCCESS) { 1752 | qWarning("ERROR: JxlDecoderSetInput failed"); 1753 | m_parseState = ParseJpegXLError; 1754 | return false; 1755 | } 1756 | 1757 | JxlDecoderCloseInput(m_decoder); 1758 | 1759 | if (m_basicinfo.uses_original_profile == JXL_FALSE && m_basicinfo.have_animation == JXL_FALSE) { 1760 | if (JxlDecoderSubscribeEvents(m_decoder, JXL_DEC_COLOR_ENCODING | JXL_DEC_FULL_IMAGE) != JXL_DEC_SUCCESS) { 1761 | qWarning("ERROR: JxlDecoderSubscribeEvents failed"); 1762 | m_parseState = ParseJpegXLError; 1763 | return false; 1764 | } 1765 | 1766 | JxlDecoderStatus status = JxlDecoderProcessInput(m_decoder); 1767 | if (status != JXL_DEC_COLOR_ENCODING) { 1768 | qWarning("Unexpected event %d instead of JXL_DEC_COLOR_ENCODING", status); 1769 | m_parseState = ParseJpegXLError; 1770 | return false; 1771 | } 1772 | 1773 | #if JPEGXL_NUMERIC_VERSION > JPEGXL_COMPUTE_NUMERIC_VERSION(0, 9, 0) 1774 | const JxlCmsInterface *jxlcms = JxlGetDefaultCms(); 1775 | if (jxlcms) { 1776 | status = JxlDecoderSetCms(m_decoder, *jxlcms); 1777 | if (status != JXL_DEC_SUCCESS) { 1778 | qWarning("JxlDecoderSetCms ERROR"); 1779 | } 1780 | } else { 1781 | qWarning("No JPEG XL CMS Interface"); 1782 | } 1783 | #endif 1784 | 1785 | bool is_gray = m_basicinfo.num_color_channels == 1 && m_basicinfo.alpha_bits == 0; 1786 | JxlColorEncoding color_encoding; 1787 | JxlColorEncodingSetToSRGB(&color_encoding, is_gray ? JXL_TRUE : JXL_FALSE); 1788 | JxlDecoderSetPreferredColorProfile(m_decoder, &color_encoding); 1789 | } else { 1790 | if (JxlDecoderSubscribeEvents(m_decoder, JXL_DEC_FULL_IMAGE) != JXL_DEC_SUCCESS) { 1791 | qWarning("ERROR: JxlDecoderSubscribeEvents failed"); 1792 | m_parseState = ParseJpegXLError; 1793 | return false; 1794 | } 1795 | } 1796 | 1797 | return true; 1798 | } 1799 | --------------------------------------------------------------------------------