Skip to content

Commit

Permalink
init repo
Browse files Browse the repository at this point in the history
  • Loading branch information
Isotr0py committed Sep 26, 2023
1 parent 8e6d1b7 commit 54abc4e
Show file tree
Hide file tree
Showing 10 changed files with 558 additions and 0 deletions.
138 changes: 138 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# This file is autogenerated by maturin v1.2.3
# To update, run
#
# maturin generate-ci github
#
name: CI

on:
push:
branches:
- main
- master
tags:
- '*'
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
linux:
runs-on: ubuntu-latest
timeout-minutes: 10
strategy:
matrix:
target: [x86_64]
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'

- name: Install dependencys
run: |
sudo apt --fix-broken install
sudo apt update
sudo apt install cmake clang doxygen g++ extra-cmake-modules libgif-dev libjpeg-dev ninja-build libgoogle-perftools-dev libunwind-dev
- name: Build and install libjxl
run: |
git clone https://github.com/libjxl/libjxl.git --recursive --shallow-submodules
cd libjxl
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTING=OFF ..
cmake --build . -- -j$(nproc)
sudo cmake --install .
- name: Build wheels
uses: PyO3/maturin-action@v1
with:
target: ${{ matrix.target }}
args: --release --out dist --find-interpreter
sccache: 'true'
manylinux: auto
docker-options: |
-e DEP_JXL_LIB=${{ github.workspace }}/libjxl/build/lib
-e RUST_BACKTRACE=1
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

windows:
runs-on: windows-latest
timeout-minutes: 60
strategy:
matrix:
target: [x64]
steps:
- name: Install dependencys
uses: msys2/setup-msys2@v2
with:
update: true
install: >-
mingw-w64-x86_64-toolchain
mingw-w64-x86_64-libjxl
- name: Put MSYS2_MinGW64 on PATH
# there is not yet an environment variable for this path from msys2/setup-msys2
run: echo "${{ runner.temp }}/msys64/mingw64/bin" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

- uses: actions/checkout@v3
- uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: ${{ matrix.target }}

- name: Build wheels
uses: PyO3/maturin-action@v1
with:
rust-toolchain: stable-x86_64-pc-windows-gnu
target: x86_64-pc-windows-gnu
args: --release --out dist --find-interpreter
sccache: 'true'

- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

sdist:
runs-on: ubuntu-latest
timeout-minutes: 60
steps:
- uses: actions/checkout@v3
- name: Build sdist
uses: PyO3/maturin-action@v1
with:
command: sdist
args: --out dist
rust-toolchain:
- name: Upload sdist
uses: actions/upload-artifact@v3
with:
name: wheels
path: dist

release:
name: Release
runs-on: ubuntu-latest
timeout-minutes: 10
if: "startsWith(github.ref, 'refs/tags/')"
needs: [linux, windows, sdist]
steps:
- uses: actions/download-artifact@v3
with:
name: wheels
- name: Publish to PyPI
uses: PyO3/maturin-action@v1
env:
MATURIN_PYPI_TOKEN: ${{ secrets.PYPI_API_TOKEN }}
with:
command: upload
args: --non-interactive --skip-existing *
75 changes: 75 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/target
Cargo.lock

# Byte-compiled / optimized / DLL files
__pycache__/
.pytest_cache/
*.py[cod]

# C extensions
*.so
*.dll

# Distribution / packaging
.Python
.venv/
env/
bin/
build/
develop-eggs/
dist/
eggs/
lib/
lib64/
parts/
sdist/
var/
include/
man/
venv/
wsl_venv/
*.egg-info/
.installed.cfg
*.egg

# Installer logs
pip-log.txt
pip-delete-this-directory.txt
pip-selfcheck.json

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.cache
nosetests.xml
coverage.xml

# Translations
*.mo

# Mr Developer
.mr.developer.cfg
.project
.pydevproject

# Rope
.ropeproject

# Django stuff:
*.log
*.pot

.DS_Store

# Sphinx documentation
docs/_build/

# PyCharm
.idea/

# VSCode
.vscode/

# Pyenv
.python-version
13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "pillow-jxl-plugin"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "pillow_jxl_plugin"
crate-type = ["cdylib"]

[dependencies]
pyo3 = "0.19.2"
jpegxl-rs = { version = "0.8.2", default-features = false, features = ["threads"] }
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Isotr0py

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
91 changes: 91 additions & 0 deletions pillow_jxl_plugin/JpegXLImagePlugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from io import BytesIO
from PIL import Image, ImageFile
from pillow_jxl_plugin import Decoder, Encoder

_VALID_JXL_MODES = {"RGB", "RGBA", "L", "LA"}


def _accept(data):
return data[:2] == b'\xff\x0a' \
or data[:12] == b'\x00\x00\x00\x0c\x4a\x58\x4c\x20\x0d\x0a\x87\x0a' \
or data[4:7] == b"JXL"


class JXLImageFile(ImageFile.ImageFile):

format = "JXL"
format_description = "Jpeg XL image"
__loaded = -1
__frame = 0

def _open(self):

self.fc = self.fp.read()
self._decoder = Decoder()

self._jxlinfo, self._data = self._decoder(self.fc)
# self._size = (self._jxlinfo['width'], self._jxlinfo['height'])
# self.mode = self.rawmode = self._jxlinfo["mode"]
self._size = (self._jxlinfo.width, self._jxlinfo.height)
self.mode = self.rawmode = self._jxlinfo.mode
self.tile = []

def seek(self, frame):

self.load()

if self.__frame+1 != frame:
# I believe JPEG XL doesn't support seeking in animations
raise NotImplementedError(
'Seeking more than one frame forward is currently not supported.'
)
self.__frame = frame

def load(self):

if self.__loaded != self.__frame:

if self._data is None:
EOFError('no more frames')

self.__loaded = self.__frame

if self.fp and self._exclusive_fp:
self.fp.close()
self.fp = BytesIO(self._data)
self.tile = [("raw", (0, 0) + self.size, 0, self.rawmode)]

return super().load()

def tell(self):
return self.__frame


def _save(im, fp, filename, save_all=False):

if im.mode not in _VALID_JXL_MODES:
raise NotImplementedError('Only RGB, RGBA, L, LA are supported.')

info = im.encoderinfo.copy()

# default quality is 1
lossless = info.get('lossless', False)
quality = 0 if lossless else 1
decoding_speed = info.get('decoding_speed', 0)
use_container = info.get('use_container', True)

enc = Encoder(
mode=im.mode,
lossless=lossless,
quality=quality,
decoding_speed=decoding_speed,
use_container=use_container
)
data = enc(im.tobytes(), im.width, im.height)
fp.write(data)


Image.register_open(JXLImageFile.format, JXLImageFile, _accept)
Image.register_save(JXLImageFile.format, _save)
Image.register_extension(JXLImageFile.format, ".jxl")
Image.register_mime(JXLImageFile.format, "image/jxl")
5 changes: 5 additions & 0 deletions pillow_jxl_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from .pillow_jxl_plugin import *

__doc__ = pillow_jxl_plugin.__doc__
if hasattr(pillow_jxl_plugin, "__all__"):
__all__ = pillow_jxl_plugin.__all__
43 changes: 43 additions & 0 deletions pillow_jxl_plugin/__init__.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class ImageInfo:
"""
A class contains metadata of ddecoded image.
"""
mode: str
width: int
height: int
num_channels: int
has_alpha_channel: bool


class Encoder:

def __init__(self,
parallel: bool = True,
has_alpha: bool = False,
lossless: bool = True,
quality: float = 0.0): ...

def __call__(self, data: bytes, width: int, height: int) -> bytes: ...


class Decoder:
'''
Initialize a jpeg-xl decoder.
Args:
parallel(`bool`): enable parallel decoding
'''

def __init__(self, parallel: bool = True): ...

def __call__(self, data: bytes) -> (ImageInfo, bytes): ...
'''
Decode a jpeg-xl image.
Args:
data(`bytes`): jpeg-xl image
Return:
`ImageInfo`: The metadata of decoded image
`bytes`: The decoded image.
'''
Empty file added pillow_jxl_plugin/py.typed
Empty file.
Loading

0 comments on commit 54abc4e

Please sign in to comment.