mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-05 04:50:09 +03:00
Merge 98e4dfeefb
into 958397ae02
This commit is contained in:
commit
e805e9e594
|
@ -15,20 +15,21 @@
|
|||
|
||||
__version__ = "0.1"
|
||||
|
||||
from PIL import Image, ImageFile, _binary
|
||||
import struct
|
||||
import os
|
||||
from PIL import Image, ImageFile
|
||||
import io
|
||||
import os
|
||||
import struct
|
||||
|
||||
|
||||
def _parse_codestream(fp):
|
||||
"""Parse the JPEG 2000 codestream to extract the size and component
|
||||
count from the SIZ marker segment, returning a PIL (size, mode) tuple."""
|
||||
|
||||
|
||||
hdr = fp.read(2)
|
||||
lsiz = struct.unpack('>H', hdr)[0]
|
||||
siz = hdr + fp.read(lsiz - 2)
|
||||
lsiz, rsiz, xsiz, ysiz, xosiz, yosiz, xtsiz, ytsiz, \
|
||||
xtosiz, ytosiz, csiz \
|
||||
xtosiz, ytosiz, csiz \
|
||||
= struct.unpack('>HHIIIIIIIIH', siz[:38])
|
||||
ssiz = [None]*csiz
|
||||
xrsiz = [None]*csiz
|
||||
|
@ -45,16 +46,17 @@ def _parse_codestream(fp):
|
|||
elif csiz == 3:
|
||||
mode = 'RGB'
|
||||
elif csiz == 4:
|
||||
mode == 'RGBA'
|
||||
mode = 'RGBA'
|
||||
else:
|
||||
mode = None
|
||||
|
||||
|
||||
return (size, mode)
|
||||
|
||||
|
||||
def _parse_jp2_header(fp):
|
||||
"""Parse the JP2 header box to extract size, component count and
|
||||
color space information, returning a PIL (size, mode) tuple."""
|
||||
|
||||
|
||||
# Find the JP2 header box
|
||||
header = None
|
||||
while True:
|
||||
|
@ -76,7 +78,7 @@ def _parse_jp2_header(fp):
|
|||
|
||||
size = None
|
||||
mode = None
|
||||
|
||||
|
||||
hio = io.BytesIO(header)
|
||||
while True:
|
||||
lbox, tbox = struct.unpack('>I4s', hio.read(8))
|
||||
|
@ -90,7 +92,7 @@ def _parse_jp2_header(fp):
|
|||
|
||||
if tbox == b'ihdr':
|
||||
height, width, nc, bpc, c, unkc, ipr \
|
||||
= struct.unpack('>IIHBBBB', content)
|
||||
= struct.unpack('>IIHBBBB', content)
|
||||
size = (width, height)
|
||||
if unkc:
|
||||
if nc == 1:
|
||||
|
@ -112,13 +114,13 @@ def _parse_jp2_header(fp):
|
|||
elif nc == 4:
|
||||
mode = 'RGBA'
|
||||
break
|
||||
elif cs == 17: # grayscale
|
||||
elif cs == 17: # grayscale
|
||||
if nc == 1:
|
||||
mode = 'L'
|
||||
elif nc == 2:
|
||||
mode = 'LA'
|
||||
break
|
||||
elif cs == 18: # sYCC
|
||||
elif cs == 18: # sYCC
|
||||
if nc == 3:
|
||||
mode = 'RGB'
|
||||
elif nc == 4:
|
||||
|
@ -127,6 +129,7 @@ def _parse_jp2_header(fp):
|
|||
|
||||
return (size, mode)
|
||||
|
||||
|
||||
##
|
||||
# Image plugin for JPEG2000 images.
|
||||
|
||||
|
@ -141,16 +144,16 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
|
|||
self.size, self.mode = _parse_codestream(self.fp)
|
||||
else:
|
||||
sig = sig + self.fp.read(8)
|
||||
|
||||
|
||||
if sig == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a':
|
||||
self.codec = "jp2"
|
||||
self.size, self.mode = _parse_jp2_header(self.fp)
|
||||
else:
|
||||
raise SyntaxError('not a JPEG 2000 file')
|
||||
|
||||
|
||||
if self.size is None or self.mode is None:
|
||||
raise SyntaxError('unable to determine size/mode')
|
||||
|
||||
|
||||
self.reduce = 0
|
||||
self.layers = 0
|
||||
|
||||
|
@ -177,13 +180,15 @@ class Jpeg2KImageFile(ImageFile.ImageFile):
|
|||
t = self.tile[0]
|
||||
t3 = (t[3][0], self.reduce, self.layers, t[3][3])
|
||||
self.tile = [(t[0], (0, 0) + self.size, t[2], t3)]
|
||||
|
||||
|
||||
ImageFile.ImageFile.load(self)
|
||||
|
||||
|
||||
|
||||
def _accept(prefix):
|
||||
return (prefix[:4] == b'\xff\x4f\xff\x51'
|
||||
or prefix[:12] == b'\x00\x00\x00\x0cjP \x0d\x0a\x87\x0a')
|
||||
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Save support
|
||||
|
||||
|
@ -214,7 +219,7 @@ def _save(im, fp, filename):
|
|||
fd = fp.fileno()
|
||||
except:
|
||||
fd = -1
|
||||
|
||||
|
||||
im.encoderconfig = (
|
||||
offset,
|
||||
tile_offset,
|
||||
|
@ -229,9 +234,9 @@ def _save(im, fp, filename):
|
|||
cinema_mode,
|
||||
fd
|
||||
)
|
||||
|
||||
|
||||
ImageFile._save(im, fp, [('jpeg2k', (0, 0)+im.size, 0, kind)])
|
||||
|
||||
|
||||
# ------------------------------------------------------------
|
||||
# Registry stuff
|
||||
|
||||
|
|
BIN
Tests/images/rgb_trns_ycbc.j2k
Normal file
BIN
Tests/images/rgb_trns_ycbc.j2k
Normal file
Binary file not shown.
BIN
Tests/images/rgb_trns_ycbc.jp2
Normal file
BIN
Tests/images/rgb_trns_ycbc.jp2
Normal file
Binary file not shown.
|
@ -1,7 +1,6 @@
|
|||
from tester import *
|
||||
|
||||
from PIL import Image
|
||||
from PIL import ImageFile
|
||||
|
||||
codecs = dir(Image.core)
|
||||
|
||||
|
@ -15,18 +14,20 @@ ignore('Not enough memory to handle tile data')
|
|||
test_card = Image.open('Tests/images/test-card.png')
|
||||
test_card.load()
|
||||
|
||||
|
||||
def roundtrip(im, **options):
|
||||
out = BytesIO()
|
||||
im.save(out, "JPEG2000", **options)
|
||||
bytes = out.tell()
|
||||
out.seek(0)
|
||||
im = Image.open(out)
|
||||
im.bytes = bytes # for testing only
|
||||
im.bytes = bytes # for testing only
|
||||
im.load()
|
||||
return im
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_sanity():
|
||||
# Internal version number
|
||||
assert_match(Image.core.jp2klib_version, '\d+\.\d+\.\d+$')
|
||||
|
@ -36,9 +37,10 @@ def test_sanity():
|
|||
assert_equal(im.mode, 'RGB')
|
||||
assert_equal(im.size, (640, 480))
|
||||
assert_equal(im.format, 'JPEG2000')
|
||||
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
|
||||
|
||||
# These two test pre-written JPEG 2000 files that were not written with
|
||||
# PIL (they were made using Adobe Photoshop)
|
||||
|
||||
|
@ -48,6 +50,7 @@ def test_lossless():
|
|||
im.save('/tmp/test-card.png')
|
||||
assert_image_similar(im, test_card, 1.0e-3)
|
||||
|
||||
|
||||
def test_lossy_tiled():
|
||||
im = Image.open('Tests/images/test-card-lossy-tiled.jp2')
|
||||
im.load()
|
||||
|
@ -55,49 +58,58 @@ def test_lossy_tiled():
|
|||
|
||||
# ----------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_lossless_rt():
|
||||
im = roundtrip(test_card)
|
||||
assert_image_equal(im, test_card)
|
||||
|
||||
|
||||
def test_lossy_rt():
|
||||
im = roundtrip(test_card, quality_layers=[20])
|
||||
assert_image_similar(im, test_card, 2.0)
|
||||
|
||||
|
||||
def test_tiled_rt():
|
||||
im = roundtrip(test_card, tile_size=(128, 128))
|
||||
assert_image_equal(im, test_card)
|
||||
|
||||
|
||||
def test_tiled_offset_rt():
|
||||
im = roundtrip(test_card, tile_size=(128, 128), tile_offset=(0, 0),
|
||||
offset=(32, 32))
|
||||
assert_image_equal(im, test_card)
|
||||
|
||||
|
||||
|
||||
def test_irreversible_rt():
|
||||
im = roundtrip(test_card, irreversible=True, quality_layers=[20])
|
||||
assert_image_similar(im, test_card, 2.0)
|
||||
|
||||
|
||||
def test_prog_qual_rt():
|
||||
im = roundtrip(test_card, quality_layers=[60, 40, 20], progression='LRCP')
|
||||
assert_image_similar(im, test_card, 2.0)
|
||||
|
||||
|
||||
def test_prog_res_rt():
|
||||
im = roundtrip(test_card, num_resolutions=8, progression='RLCP')
|
||||
assert_image_equal(im, test_card)
|
||||
|
||||
# ----------------------------------------------------------------------
|
||||
|
||||
|
||||
def test_reduce():
|
||||
im = Image.open('Tests/images/test-card-lossless.jp2')
|
||||
im.reduce = 2
|
||||
im.load()
|
||||
assert_equal(im.size, (160, 120))
|
||||
|
||||
|
||||
def test_layers():
|
||||
out = BytesIO()
|
||||
test_card.save(out, 'JPEG2000', quality_layers=[100, 50, 10],
|
||||
progression='LRCP')
|
||||
out.seek(0)
|
||||
|
||||
|
||||
im = Image.open(out)
|
||||
im.layers = 1
|
||||
im.load()
|
||||
|
@ -108,3 +120,17 @@ def test_layers():
|
|||
im.layers = 3
|
||||
im.load()
|
||||
assert_image_similar(im, test_card, 0.4)
|
||||
|
||||
|
||||
def test_rgba():
|
||||
# Arrange
|
||||
j2k = Image.open('Tests/images/rgb_trns_ycbc.j2k')
|
||||
jp2 = Image.open('Tests/images/rgb_trns_ycbc.jp2')
|
||||
|
||||
# Act
|
||||
j2k.load()
|
||||
jp2.load()
|
||||
|
||||
# Assert
|
||||
assert_equal(j2k.mode, 'RGBA')
|
||||
assert_equal(jp2.mode, 'RGBA')
|
||||
|
|
Loading…
Reference in New Issue
Block a user