Skip to content

Commit 72145fe

Browse files
author
Steve Canny
committed
img: add _SofMarker.from_stream()
1 parent 16fa473 commit 72145fe

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

docx/image/jpeg.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,23 +290,40 @@ def from_stream(cls, stream, marker_code, offset):
290290
# horz dots per unit 10 short
291291
# vert dots per unit 12 short
292292
# ------------------ --- ----- -------------------
293-
length = stream.read_short(offset)
293+
segment_length = stream.read_short(offset)
294294
density_units = stream.read_byte(offset, 9)
295295
x_density = stream.read_short(offset, 10)
296296
y_density = stream.read_short(offset, 12)
297297
return cls(
298-
marker_code, offset, length, density_units, x_density, y_density
298+
marker_code, offset, segment_length, density_units, x_density,
299+
y_density
299300
)
300301

301302

302303
class _SofMarker(_Marker):
303304
"""
304305
Represents a JFIF start of frame (SOFx) marker segment.
305306
"""
307+
def __init__(
308+
self, marker_code, offset, segment_length, px_width, px_height):
309+
super(_SofMarker, self).__init__(marker_code, offset, segment_length)
310+
self._px_width = px_width
311+
self._px_height = px_height
312+
306313
@classmethod
307314
def from_stream(cls, stream, marker_code, offset):
308315
"""
309316
Return an |_SofMarker| instance for the SOFn marker at *offset* in
310317
stream.
311318
"""
312-
raise NotImplementedError
319+
# field off type notes
320+
# ------------------ --- ----- ----------------------------
321+
# segment length 0 short
322+
# Data precision 2 byte
323+
# Vertical lines 3 short px_height
324+
# Horizontal lines 5 short px_width
325+
# ------------------ --- ----- ----------------------------
326+
segment_length = stream.read_short(offset)
327+
px_height = stream.read_short(offset, 3)
328+
px_width = stream.read_short(offset, 5)
329+
return cls(marker_code, offset, segment_length, px_width, px_height)

tests/image/test_jpeg.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,35 @@ def _App0Marker__init_(self, request):
207207
return initializer_mock(request, _App0Marker)
208208

209209

210+
class Describe_SofMarker(object):
211+
212+
def it_can_construct_from_a_stream_and_offset(self, from_stream_fixture):
213+
(stream, marker_code, offset, _SofMarker__init_, length,
214+
px_width, px_height) = from_stream_fixture
215+
sof_marker = _SofMarker.from_stream(stream, marker_code, offset)
216+
_SofMarker__init_.assert_called_once_with(
217+
marker_code, offset, length, px_width, px_height
218+
)
219+
assert isinstance(sof_marker, _SofMarker)
220+
221+
# fixtures -------------------------------------------------------
222+
223+
@pytest.fixture
224+
def from_stream_fixture(self, request, _SofMarker__init_):
225+
bytes_ = b'\x00\x11\x00\x00\x2A\x00\x18'
226+
stream_reader = StreamReader(BytesIO(bytes_), BIG_ENDIAN)
227+
marker_code, offset, length = JPEG_MARKER_CODE.SOF0, 0, 17
228+
px_width, px_height = 24, 42
229+
return (
230+
stream_reader, marker_code, offset, _SofMarker__init_, length,
231+
px_width, px_height
232+
)
233+
234+
@pytest.fixture
235+
def _SofMarker__init_(self, request):
236+
return initializer_mock(request, _SofMarker)
237+
238+
210239
class Describe_MarkerFactory(object):
211240

212241
def it_constructs_the_appropriate_marker_object(self, call_fixture):

0 commit comments

Comments
 (0)