mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-27 07:34:46 +03:00
Additional tests -- access/pack/unpack
This commit is contained in:
parent
585b7e42ef
commit
efe8479af9
|
@ -1,7 +1,12 @@
|
|||
from helper import unittest, PillowTestCase, hopper
|
||||
|
||||
from PIL import Image
|
||||
import struct
|
||||
|
||||
HAS_PYACCESS = False
|
||||
try:
|
||||
from PIL import PyAccess
|
||||
except: pass
|
||||
|
||||
class TestModeI16(PillowTestCase):
|
||||
|
||||
|
@ -113,5 +118,40 @@ class TestModeI16(PillowTestCase):
|
|||
self.verify(im.convert("I;16S").convert("I"))
|
||||
|
||||
|
||||
def _test_i16s(self, pixels, data, rawmode):
|
||||
ct = len(pixels)
|
||||
im = Image.frombytes('I;16S',
|
||||
(ct,1),
|
||||
data,
|
||||
'raw',
|
||||
rawmode)
|
||||
|
||||
self.assertEqual(im.tobytes('raw', rawmode), data)
|
||||
|
||||
|
||||
def _test_access(im, pixels):
|
||||
access = im.load()
|
||||
if HAS_PYACCESS:
|
||||
py_access = PyAccess.new(im, im.readonly)
|
||||
for ix, val in enumerate(pixels):
|
||||
self.assertEqual(access[(ix, 0)], val)
|
||||
if HAS_PYACCESS:
|
||||
self.assertEqual(py_access[(ix, 0)], val)
|
||||
|
||||
_test_access(im, pixels)
|
||||
_test_access(im.convert('I'), pixels) # lossless
|
||||
_test_access(im.convert('F'), pixels) # lossless
|
||||
_test_access(im.convert('L'), (0,0,0,0,1,128,255,255)) #lossy
|
||||
|
||||
def test_i16s(self):
|
||||
# LE, base Rawmode:
|
||||
pixels = (-2**15, -2**7, -1, 0, 1, 128, 255, 2**15-1)
|
||||
ct = len(pixels)
|
||||
data = struct.pack("<%dh"%ct, *pixels)
|
||||
self._test_i16s(pixels, data, 'I;16S')
|
||||
data = struct.pack(">%dh"%ct, *pixels)
|
||||
self._test_i16s(pixels, data, 'I;16BS')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
|
14
_imaging.c
14
_imaging.c
|
@ -440,10 +440,11 @@ static inline PyObject*
|
|||
getpixel(Imaging im, ImagingAccess access, int x, int y)
|
||||
{
|
||||
union {
|
||||
UINT8 b[4];
|
||||
UINT16 h;
|
||||
INT32 i;
|
||||
FLOAT32 f;
|
||||
UINT8 b[4];
|
||||
UINT16 H;
|
||||
INT16 h;
|
||||
INT32 i;
|
||||
FLOAT32 f;
|
||||
} pixel;
|
||||
|
||||
if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
|
||||
|
@ -471,8 +472,11 @@ getpixel(Imaging im, ImagingAccess access, int x, int y)
|
|||
case IMAGING_TYPE_FLOAT32:
|
||||
return PyFloat_FromDouble(pixel.f);
|
||||
case IMAGING_TYPE_SPECIAL:
|
||||
if (strncmp(im->mode, "I;16", 4) == 0)
|
||||
if (strncmp(im->mode, "I;16S", 5) == 0) {
|
||||
return PyInt_FromLong(pixel.h);
|
||||
} else if (strncmp(im->mode, "I;16", 4) == 0) {
|
||||
return PyInt_FromLong(pixel.H);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
@ -402,6 +402,16 @@ packI16N_I16(UINT8* out, const UINT8* in, int pixels){
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
packI16S_I16BS(UINT8* out, const UINT8* in, int pixels){
|
||||
int i;
|
||||
UINT8* tmp = (UINT8*) in;
|
||||
for (i = 0; i < pixels; i++) {
|
||||
C16S;
|
||||
out += 2; tmp += 2;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void
|
||||
packI32S(UINT8* out, const UINT8* in, int pixels)
|
||||
|
@ -615,6 +625,7 @@ static struct {
|
|||
{"I;16B", "I;16B", 16, copy2},
|
||||
{"I;16L", "I;16L", 16, copy2},
|
||||
{"I;16S", "I;16S", 16, copy2},
|
||||
{"I;16S", "I;16BS", 16, packI16S_I16BS},
|
||||
{"I;16", "I;16N", 16, packI16N_I16}, // LibTiff native->image endian.
|
||||
{"I;16L", "I;16N", 16, packI16N_I16},
|
||||
{"I;16B", "I;16N", 16, packI16N_I16B},
|
||||
|
|
|
@ -966,6 +966,16 @@ unpackI16N_I16(UINT8* out, const UINT8* in, int pixels){
|
|||
}
|
||||
}
|
||||
|
||||
static void
|
||||
unpackI16BS_I16S(UINT8* out, const UINT8* in, int pixels){
|
||||
int i;
|
||||
UINT8* tmp = (UINT8*) out;
|
||||
for (i = 0; i < pixels; i++) {
|
||||
C16S;
|
||||
in += 2; tmp += 2;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
unpackI12_I16(UINT8* out, const UINT8* in, int pixels){
|
||||
/* Fillorder 1/MSB -> LittleEndian, for 12bit integer greyscale tiffs.
|
||||
|
@ -1406,7 +1416,8 @@ static struct {
|
|||
{"I;16", "I;16", 16, copy2},
|
||||
{"I;16B", "I;16B", 16, copy2},
|
||||
{"I;16L", "I;16L", 16, copy2},
|
||||
{"I;16S", "I;16S", 16, copy2},
|
||||
{"I;16S", "I;16S", 16, copy2},
|
||||
{"I;16S", "I;16BS", 16, unpackI16BS_I16S},
|
||||
|
||||
{"I;16", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.
|
||||
{"I;16L", "I;16N", 16, unpackI16N_I16}, // LibTiff native->image endian.
|
||||
|
|
Loading…
Reference in New Issue
Block a user