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

Skip to content

Commit 8ab2f34

Browse files
author
Steve Canny
committed
img: add _pHYsChunk
1 parent 921afe8 commit 8ab2f34

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

docx/image/png.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,34 @@ class _pHYsChunk(_Chunk):
327327
"""
328328
pYHs chunk, contains the image dpi information
329329
"""
330+
def __init__(self, chunk_type, horz_px_per_unit, vert_px_per_unit,
331+
units_specifier):
332+
super(_pHYsChunk, self).__init__(chunk_type)
333+
self._horz_px_per_unit = horz_px_per_unit
334+
self._vert_px_per_unit = vert_px_per_unit
335+
self._units_specifier = units_specifier
336+
330337
@classmethod
331338
def from_offset(cls, chunk_type, stream_rdr, offset):
332339
"""
333340
Return a _pHYsChunk instance containing the image resolution
334341
extracted from the pHYs chunk in *stream* at *offset*.
335342
"""
336-
raise NotImplementedError
343+
horz_px_per_unit = stream_rdr.read_long(offset)
344+
vert_px_per_unit = stream_rdr.read_long(offset, 4)
345+
units_specifier = stream_rdr.read_byte(offset, 8)
346+
return cls(
347+
chunk_type, horz_px_per_unit, vert_px_per_unit, units_specifier
348+
)
349+
350+
@property
351+
def horz_px_per_unit(self):
352+
return self._horz_px_per_unit
353+
354+
@property
355+
def vert_px_per_unit(self):
356+
return self._vert_px_per_unit
357+
358+
@property
359+
def units_specifier(self):
360+
return self._units_specifier

tests/image/test_png.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,30 @@ def from_offset_fixture(self):
523523
stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN)
524524
offset, px_width, px_height = 0, 42, 24
525525
return stream_rdr, offset, px_width, px_height
526+
527+
528+
class Describe_pHYsChunk(object):
529+
530+
def it_can_construct_from_a_stream_and_offset(self, from_offset_fixture):
531+
stream_rdr, offset = from_offset_fixture[:2]
532+
horz_px_per_unit, vert_px_per_unit = from_offset_fixture[2:4]
533+
units_specifier = from_offset_fixture[4]
534+
pHYs_chunk = _pHYsChunk.from_offset(None, stream_rdr, offset)
535+
assert isinstance(pHYs_chunk, _pHYsChunk)
536+
assert pHYs_chunk.horz_px_per_unit == horz_px_per_unit
537+
assert pHYs_chunk.vert_px_per_unit == vert_px_per_unit
538+
assert pHYs_chunk.units_specifier == units_specifier
539+
540+
# fixtures -------------------------------------------------------
541+
542+
@pytest.fixture
543+
def from_offset_fixture(self):
544+
bytes_ = b'\x00\x00\x00\x2A\x00\x00\x00\x18\x01'
545+
stream_rdr = StreamReader(BytesIO(bytes_), BIG_ENDIAN)
546+
offset, horz_px_per_unit, vert_px_per_unit, units_specifier = (
547+
0, 42, 24, 1
548+
)
549+
return (
550+
stream_rdr, offset, horz_px_per_unit, vert_px_per_unit,
551+
units_specifier
552+
)

0 commit comments

Comments
 (0)