Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 921afe8

Browse files
author
Steve Canny
committed
img: add _IHDRChunk
1 parent 8f3e5d4 commit 921afe8

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

docx/image/png.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,10 @@ class _Chunk(object):
283283
Base class for specific chunk types. Also serves as the default chunk
284284
type.
285285
"""
286+
def __init__(self, chunk_type):
287+
super(_Chunk, self).__init__()
288+
self._chunk_type = chunk_type
289+
286290
@classmethod
287291
def from_offset(cls, chunk_type, stream_rdr, offset):
288292
"""
@@ -295,13 +299,28 @@ class _IHDRChunk(_Chunk):
295299
"""
296300
IHDR chunk, contains the image dimensions
297301
"""
302+
def __init__(self, chunk_type, px_width, px_height):
303+
super(_IHDRChunk, self).__init__(chunk_type)
304+
self._px_width = px_width
305+
self._px_height = px_height
306+
298307
@classmethod
299308
def from_offset(cls, chunk_type, stream_rdr, offset):
300309
"""
301310
Return an _IHDRChunk instance containing the image dimensions
302311
extracted from the IHDR chunk in *stream* at *offset*.
303312
"""
304-
raise NotImplementedError
313+
px_width = stream_rdr.read_long(offset)
314+
px_height = stream_rdr.read_long(offset, 4)
315+
return cls(chunk_type, px_width, px_height)
316+
317+
@property
318+
def px_width(self):
319+
return self._px_width
320+
321+
@property
322+
def px_height(self):
323+
return self._px_height
305324

306325

307326
class _pHYsChunk(_Chunk):

tests/image/test_png.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,22 @@ def phys_chunk_(self, request):
504504
@pytest.fixture
505505
def stream_rdr_(self, request):
506506
return instance_mock(request, StreamReader)
507+
508+
509+
class Describe_IHDRChunk(object):
510+
511+
def it_can_construct_from_a_stream_and_offset(self, from_offset_fixture):
512+
stream_rdr, offset, px_width, px_height = from_offset_fixture
513+
ihdr_chunk = _IHDRChunk.from_offset(None, stream_rdr, offset)
514+
assert isinstance(ihdr_chunk, _IHDRChunk)
515+
assert ihdr_chunk.px_width == px_width
516+
assert ihdr_chunk.px_height == px_height
517+
518+
# fixtures -------------------------------------------------------
519+
520+
@pytest.fixture
521+
def from_offset_fixture(self):
522+
bytes_ = b'\x00\x00\x00\x2A\x00\x00\x00\x18'
523+
stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN)
524+
offset, px_width, px_height = 0, 42, 24
525+
return stream_rdr, offset, px_width, px_height

0 commit comments

Comments
 (0)