Skip to content

Commit 16fa473

Browse files
author
Steve Canny
committed
img: add _App0Marker.from_stream()
1 parent ee8cbe5 commit 16fa473

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

docx/image/jpeg.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,37 @@ class _App0Marker(_Marker):
266266
"""
267267
Represents a JFIF APP0 marker segment.
268268
"""
269+
def __init__(
270+
self, marker_code, offset, length, density_units, x_density,
271+
y_density):
272+
super(_App0Marker, self).__init__(marker_code, offset, length)
273+
self._density_units = density_units
274+
self._x_density = x_density
275+
self._y_density = y_density
276+
269277
@classmethod
270278
def from_stream(cls, stream, marker_code, offset):
271279
"""
272280
Return an |_App0Marker| instance for the APP0 marker at *offset* in
273281
*stream*.
274282
"""
275-
raise NotImplementedError
283+
# field off type notes
284+
# ------------------ --- ----- -------------------
285+
# segment length 0 short
286+
# JFIF identifier 2 5 chr 'JFIF\x00'
287+
# major JPEG version 7 byte typically 1
288+
# minor JPEG version 8 byte typically 1 or 2
289+
# density units 9 byte 1=inches, 2=cm
290+
# horz dots per unit 10 short
291+
# vert dots per unit 12 short
292+
# ------------------ --- ----- -------------------
293+
length = stream.read_short(offset)
294+
density_units = stream.read_byte(offset, 9)
295+
x_density = stream.read_short(offset, 10)
296+
y_density = stream.read_short(offset, 12)
297+
return cls(
298+
marker_code, offset, length, density_units, x_density, y_density
299+
)
276300

277301

278302
class _SofMarker(_Marker):

tests/image/test_jpeg.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,35 @@ def _Marker__init_(self, request):
178178
return initializer_mock(request, _Marker)
179179

180180

181+
class Describe_App0Marker(object):
182+
183+
def it_can_construct_from_a_stream_and_offset(self, from_stream_fixture):
184+
(stream, marker_code, offset, _App0Marker__init_, length,
185+
density_units, x_density, y_density) = from_stream_fixture
186+
app0_marker = _App0Marker.from_stream(stream, marker_code, offset)
187+
_App0Marker__init_.assert_called_once_with(
188+
marker_code, offset, length, density_units, x_density, y_density
189+
)
190+
assert isinstance(app0_marker, _App0Marker)
191+
192+
# fixtures -------------------------------------------------------
193+
194+
@pytest.fixture
195+
def from_stream_fixture(self, request, _App0Marker__init_):
196+
bytes_ = b'\x00\x10JFIF\x00\x01\x01\x01\x00\x2A\x00\x18'
197+
stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN)
198+
marker_code, offset, length = JPEG_MARKER_CODE.APP0, 0, 16
199+
density_units, x_density, y_density = 1, 42, 24
200+
return (
201+
stream_reader, marker_code, offset, _App0Marker__init_, length,
202+
density_units, x_density, y_density
203+
)
204+
205+
@pytest.fixture
206+
def _App0Marker__init_(self, request):
207+
return initializer_mock(request, _App0Marker)
208+
209+
181210
class Describe_MarkerFactory(object):
182211

183212
def it_constructs_the_appropriate_marker_object(self, call_fixture):

0 commit comments

Comments
 (0)