| 1 |
import io |
| 2 |
import struct |
| 3 |
import unittest |
| 4 |
from unittest import mock |
| 5 |
|
| 6 |
import imagesize |
| 7 |
import imagesize.imagesize as implementation |
| 8 |
|
| 9 |
|
| 10 |
def _box(box_type, payload): |
| 11 |
return struct.pack('>L4s', len(payload) + 8, box_type) + payload |
| 12 |
|
| 13 |
|
| 14 |
def _full_box(box_type, version, payload, flags=0): |
| 15 |
return _box(box_type, bytes([version]) + flags.to_bytes(3, 'big') + payload) |
| 16 |
|
| 17 |
|
| 18 |
def _exif_orientation(orientation=6): |
| 19 |
entry = struct.pack('<HHI', 0x0112, 3, 1) + struct.pack('<H', orientation) + b'\x00\x00' |
| 20 |
return b'\x00\x00\x00\x00' + b'II*\x00' + struct.pack('<L', 8) + struct.pack('<H', 1) + entry |
| 21 |
|
| 22 |
|
| 23 |
def _jpeg_segment(marker, payload): |
| 24 |
return b'\xff' + bytes([marker]) + struct.pack('>H', len(payload) + 2) + payload |
| 25 |
|
| 26 |
|
| 27 |
def _jpeg_with_order(exif_first): |
| 28 |
app1 = _jpeg_segment(0xe1, b'Exif\x00\x00' + _exif_orientation()[4:]) |
| 29 |
sof = _jpeg_segment(0xc0, struct.pack('>BHHB', 8, 20, 40, 3)) |
| 30 |
segments = app1 + sof if exif_first else sof + app1 |
| 31 |
return b'\xff\xd8' + segments + b'\xff\xda' |
| 32 |
|
| 33 |
|
| 34 |
def _tiff_header_metadata(): |
| 35 |
entries = b''.join(( |
| 36 |
struct.pack('<HHI4s', 256, 4, 1, struct.pack('<L', 40)), |
| 37 |
struct.pack('<HHI4s', 257, 4, 1, struct.pack('<L', 20)), |
| 38 |
struct.pack('<HHI4s', 274, 3, 1, struct.pack('<H', 6) + b'\x00\x00'), |
| 39 |
)) |
| 40 |
return b'II*\x00' + struct.pack('<L', 8) + struct.pack('<H', 3) + entries + b'\x00' * 64 |
| 41 |
|
| 42 |
|
| 43 |
def _heif_with_property_order(rotation_first): |
| 44 |
ftyp = _box(b'ftyp', b'avif\x00\x00\x00\x00avif') |
| 45 |
ispe = _full_box(b'ispe', 0, struct.pack('>LL', 630, 420)) |
| 46 |
irot = _full_box(b'irot', 0, b'\x01') |
| 47 |
properties = irot + ispe if rotation_first else ispe + irot |
| 48 |
ipco = _box(b'ipco', properties) |
| 49 |
ipma = _full_box(b'ipma', 0, struct.pack('>LHBBB', 1, 1, 2, 1, 2)) |
| 50 |
iprp = _box(b'iprp', ipco + ipma) |
| 51 |
pitm = _full_box(b'pitm', 0, struct.pack('>H', 1)) |
| 52 |
return ftyp + _full_box(b'meta', 0, pitm + iprp) |
| 53 |
|
| 54 |
|
| 55 |
def _heif_with_exif_order(metadata_first, split_extents=False): |
| 56 |
ftyp = _box(b'ftyp', b'avif\x00\x00\x00\x00avif') |
| 57 |
pitm = _full_box(b'pitm', 0, struct.pack('>H', 1)) |
| 58 |
ispe = _full_box(b'ispe', 0, struct.pack('>LL', 630, 420)) |
| 59 |
ipco = _box(b'ipco', ispe) |
| 60 |
ipma = _full_box(b'ipma', 0, struct.pack('>LHBB', 1, 1, 1, 1)) |
| 61 |
iprp = _box(b'iprp', ipco + ipma) |
| 62 |
|
| 63 |
infe_payload = struct.pack('>HH4s', 2, 0, b'Exif') |
| 64 |
iinf = _full_box(b'iinf', 0, struct.pack('>H', 1) + _full_box(b'infe', 2, infe_payload)) |
| 65 |
exif = _exif_orientation() |
| 66 |
extent_data = ( |
| 67 |
struct.pack('>HHHH', 0, len(exif) // 2, len(exif) // 2, len(exif) - len(exif) // 2) |
| 68 |
if split_extents else struct.pack('>HH', 0, len(exif)) |
| 69 |
) |
| 70 |
iloc_payload = ( |
| 71 |
b'\x22\x00' + struct.pack('>H', 1) + struct.pack('>H', 2) + |
| 72 |
struct.pack('>H', 1) + struct.pack('>H', 0) + |
| 73 |
struct.pack('>H', 2 if split_extents else 1) + extent_data |
| 74 |
) |
| 75 |
iloc = _full_box(b'iloc', 1, iloc_payload) |
| 76 |
idat = _box(b'idat', exif) |
| 77 |
metadata = iinf + iloc + idat |
| 78 |
children = pitm + (metadata + iprp if metadata_first else iprp + metadata) |
| 79 |
return ftyp + _full_box(b'meta', 0, children) |
| 80 |
|
| 81 |
|
| 82 |
def _jp2_with_header_order(resolution_first): |
| 83 |
signature = _box(b'jP ', b'\r\n\x87\n') |
| 84 |
ihdr = _box(b'ihdr', struct.pack('>LL', 670, 802)) |
| 85 |
resd = _box(b'resd', struct.pack('>HHHHbb', 72, 1, 72, 1, 0, 0)) |
| 86 |
resolution = _box(b'res ', resd) |
| 87 |
children = resolution + ihdr if resolution_first else ihdr + resolution |
| 88 |
return signature + _box(b'jp2h', children) |
| 89 |
|
| 90 |
|
| 91 |
class _CountingBytesIO(io.BytesIO): |
| 92 |
def __init__(self, data): |
| 93 |
super().__init__(data) |
| 94 |
self.bytes_read = 0 |
| 95 |
self.seek_history = [] |
| 96 |
|
| 97 |
def read(self, size=-1): |
| 98 |
data = super().read(size) |
| 99 |
self.bytes_read += len(data) |
| 100 |
return data |
| 101 |
|
| 102 |
def seek(self, offset, whence=0): |
| 103 |
self.seek_history.append((offset, whence)) |
| 104 |
return super().seek(offset, whence) |
| 105 |
|
| 106 |
|
| 107 |
class PerformanceRegressionTest(unittest.TestCase): |
| 108 |
def test_large_png_only_reads_header(self): |
| 109 |
png = bytes.fromhex('89504e470d0a1a0a0000000d494844520000000100000001') |
| 110 |
stream = _CountingBytesIO(png + bytes(8 * 1024 * 1024 - len(png))) |
| 111 |
self.assertEqual(imagesize.get(stream), (1, 1)) |
| 112 |
self.assertLessEqual(stream.bytes_read, 64) |
| 113 |
|
| 114 |
def test_jpeg_exif_and_sof_orders_use_one_forward_scan(self): |
| 115 |
for exif_first in (True, False): |
| 116 |
with self.subTest(exif_first=exif_first): |
| 117 |
stream = _CountingBytesIO(_jpeg_with_order(exif_first)) |
| 118 |
self.assertEqual(imagesize.get(stream), (20, 40)) |
| 119 |
self.assertEqual(stream.seek_history.count((0, 0)), 1) |
| 120 |
|
| 121 |
def test_already_read_header_is_reused_by_format_parsers(self): |
| 122 |
fixtures = ( |
| 123 |
(_jpeg_with_order(True) + b'\x00' * 64, (20, 40)), |
| 124 |
(_tiff_header_metadata(), (20, 40)), |
| 125 |
) |
| 126 |
for source, expected in fixtures: |
| 127 |
with self.subTest(signature=source[:4]): |
| 128 |
stream = _CountingBytesIO(source) |
| 129 |
self.assertEqual(imagesize.get(stream), expected) |
| 130 |
self.assertEqual(stream.bytes_read, 64) |
| 131 |
|
| 132 |
jp2 = _jp2_with_header_order(False) |
| 133 |
stream = _CountingBytesIO(jp2) |
| 134 |
self.assertEqual(imagesize.get(stream), (802, 670)) |
| 135 |
self.assertEqual(stream.bytes_read, len(jp2)) |
| 136 |
|
| 137 |
def test_heif_property_order_is_independent(self): |
| 138 |
for rotation_first in (True, False): |
| 139 |
with self.subTest(rotation_first=rotation_first): |
| 140 |
self.assertEqual(imagesize.get(io.BytesIO(_heif_with_property_order(rotation_first))), (420, 630)) |
| 141 |
|
| 142 |
def test_heif_exif_declaration_order_is_independent(self): |
| 143 |
for metadata_first in (True, False): |
| 144 |
for split_extents in (True, False): |
| 145 |
with self.subTest(metadata_first=metadata_first, split_extents=split_extents): |
| 146 |
source = _heif_with_exif_order(metadata_first, split_extents) |
| 147 |
self.assertEqual(imagesize.get(io.BytesIO(source)), (420, 630)) |
| 148 |
|
| 149 |
def test_get_without_rotation_skips_heif_exif_item(self): |
| 150 |
source = _heif_with_exif_order(True, True) |
| 151 |
with mock.patch.object( |
| 152 |
implementation, |
| 153 |
'_read_tiff_orientation_stream', |
| 154 |
wraps=implementation._read_tiff_orientation_stream, |
| 155 |
) as parser: |
| 156 |
self.assertEqual( |
| 157 |
imagesize.get(io.BytesIO(source), exif_rotation=False), |
| 158 |
(630, 420), |
| 159 |
) |
| 160 |
parser.assert_not_called() |
| 161 |
|
| 162 |
def test_heif_extended_and_zero_sized_boxes(self): |
| 163 |
source = _heif_with_property_order(True) |
| 164 |
ftyp_size = struct.unpack('>L', source[:4])[0] |
| 165 |
ftyp_payload = source[8:ftyp_size] |
| 166 |
extended_ftyp = struct.pack('>L4sQ', 1, b'ftyp', len(ftyp_payload) + 16) + ftyp_payload |
| 167 |
zero_sized_meta = b'\x00\x00\x00\x00' + source[ftyp_size + 4:] |
| 168 |
self.assertEqual(imagesize.get(io.BytesIO(extended_ftyp + zero_sized_meta)), (420, 630)) |
| 169 |
|
| 170 |
def test_jp2_header_and_resolution_orders_share_one_scan(self): |
| 171 |
for resolution_first in (True, False): |
| 172 |
with self.subTest(resolution_first=resolution_first): |
| 173 |
info = imagesize.get_info(io.BytesIO(_jp2_with_header_order(resolution_first))) |
| 174 |
self.assertEqual((info.width, info.height), (802, 670)) |
| 175 |
self.assertEqual((info.xdpi, info.ydpi), (2, 2)) |
| 176 |
|
| 177 |
def test_invalid_iso_box_boundary_is_rejected(self): |
| 178 |
source = _heif_with_property_order(True) |
| 179 |
malformed = source[:4] + b'ftyp' + source[8:12] |
| 180 |
self.assertEqual(imagesize.get(io.BytesIO(malformed)), (-1, -1)) |
| 181 |
|
| 182 |
def test_large_heif_mdat_is_skipped_and_metadata_is_parsed_once(self): |
| 183 |
source = _heif_with_property_order(True) |
| 184 |
ftyp_size = struct.unpack('>L', source[:4])[0] |
| 185 |
ftyp, meta = source[:ftyp_size], source[ftyp_size:] |
| 186 |
mdat = _box(b'mdat', bytes(8 * 1024 * 1024)) |
| 187 |
stream = _CountingBytesIO(ftyp + mdat + meta) |
| 188 |
with mock.patch.object( |
| 189 |
implementation, |
| 190 |
'_read_heif_metadata_stream', |
| 191 |
wraps=implementation._read_heif_metadata_stream, |
| 192 |
) as parser: |
| 193 |
self.assertEqual(imagesize.get(stream), (420, 630)) |
| 194 |
self.assertEqual(parser.call_count, 1) |
| 195 |
self.assertLess(stream.bytes_read, 4096) |
| 196 |
|
| 197 |
def test_heif_top_level_boxes_are_traversed_once(self): |
| 198 |
source = _heif_with_property_order(True) |
| 199 |
with mock.patch.object( |
| 200 |
implementation, |
| 201 |
'_iter_iso_file_boxes', |
| 202 |
wraps=implementation._iter_iso_file_boxes, |
| 203 |
) as iterator: |
| 204 |
self.assertEqual(imagesize.get(io.BytesIO(source)), (420, 630)) |
| 205 |
top_level_calls = [ |
| 206 |
call for call in iterator.call_args_list |
| 207 |
if len(call.args) >= 2 and call.args[1] == 0 |
| 208 |
] |
| 209 |
self.assertEqual(len(top_level_calls), 1) |
| 210 |
|
| 211 |
|
| 212 |
if __name__ == '__main__': |
| 213 |
unittest.main() |