Add support for reading JPEG-XR images

This commit is contained in:
Kovid Goyal 2017-03-15 22:06:16 +05:30
parent 933c7d221d
commit 814cf18c69
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C

View File

@ -2,17 +2,28 @@
# vim:fileencoding=utf-8
# License: GPLv3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
from __future__ import (unicode_literals, division, absolute_import,
print_function)
import os, subprocess, errno, shutil, tempfile, sys
from __future__ import absolute_import, division, print_function, unicode_literals
import errno
import os
import shutil
import subprocess
import sys
import tempfile
from io import BytesIO
from threading import Thread
from PyQt5.Qt import QImage, QByteArray, QBuffer, Qt, QImageReader, QColor, QImageWriter, QTransform, QPixmap
from PyQt5.Qt import (
QBuffer, QByteArray, QColor, QImage, QImageReader, QImageWriter, QPixmap, Qt,
QTransform
)
from calibre import fit_image, force_unicode
from calibre.constants import iswindows, plugins
from calibre.ptempfile import TemporaryDirectory
from calibre.utils.config_base import tweaks
from calibre.utils.filenames import atomic_rename
from calibre.utils.imghdr import what
# Utilities {{{
imageops, imageops_err = plugins['imageops']
@ -39,6 +50,22 @@ def get_exe_path(name):
if not base:
return name
return os.path.join(base, name)
def load_jxr_data(data):
with TemporaryDirectory() as tdir:
if iswindows and isinstance(tdir, type('')):
tdir = tdir.encode('mbcs')
with lopen(os.path.join(tdir, 'input.jxr'), 'wb') as f:
f.write(data)
cmd = [get_exe_path('JxrDecApp'), '-i', 'input.jxr', '-o', 'output.tif', '-c', '0']
creationflags = 0x08 if iswindows else 0
subprocess.Popen(cmd, cwd=tdir, stdout=lopen(os.devnull, 'wb'), stderr=subprocess.STDOUT, creationflags=creationflags).wait()
i = QImage()
if not i.load(os.path.join(tdir, 'output.tif')):
raise NotImage('Failed to convert JPEG-XR image')
return i
# }}}
# Loading images {{{
@ -55,6 +82,8 @@ def image_from_data(data):
return data
i = QImage()
if not i.loadFromData(data):
if what(None, data) == 'jxr':
return load_jxr_data(data)
raise NotImage('Not a valid image')
return i
@ -543,6 +572,9 @@ def test(): # {{{
despeckle_image(img)
remove_borders_from_image(img)
image_to_data(img, fmt='GIF')
raw = subprocess.Popen([get_exe_path('JxrDecApp'), '-h'], creationflags=0x08 if iswindows else 0, stdout=subprocess.PIPE).stdout.read()
if b'JPEG XR Decoder Utility' not in raw:
raise SystemExit('Failed to run JxrDecApp')
# }}}