Skip to content

Commit 3042292

Browse files
author
Steve Canny
committed
img: add Png._parse_IHDR()
1 parent 578f476 commit 3042292

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

docx/image/png.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import absolute_import, division, print_function
44

5+
from .constants import TAG
56
from .exceptions import InvalidImageStreamError
67
from .helpers import StreamReader
78
from .image import Image
@@ -98,7 +99,10 @@ def _parse_IHDR(cls, stream, offset):
9899
Return a dict containing values for TAG.PX_WIDTH and TAG.PX_HEIGHT
99100
extracted from the IHDR chunk in *stream* at *offset*.
100101
"""
101-
raise NotImplementedError
102+
return {
103+
TAG.PX_WIDTH: stream.read_long(offset),
104+
TAG.PX_HEIGHT: stream.read_long(offset, 4)
105+
}
102106

103107
@classmethod
104108
def _parse_pHYs(cls, stream, offset):

tests/image/test_png.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def it_raises_on_png_having_no_IHDR_chunk(self, no_IHDR_fixture):
6161
with pytest.raises(InvalidImageStreamError):
6262
Png._parse_chunks(stream_, chunk_offsets)
6363

64+
def it_can_parse_an_IHDR_chunk(self, parse_IHDR_fixture):
65+
stream, offset, expected_attrs = parse_IHDR_fixture
66+
attrs = Png._parse_IHDR(stream, offset)
67+
assert attrs == expected_attrs
68+
6469
# fixtures -------------------------------------------------------
6570

6671
@pytest.fixture
@@ -128,6 +133,15 @@ def parse_chunks_fixture(
128133
_parse_pHYs_, phys_offset, expected_attrs
129134
)
130135

136+
@pytest.fixture
137+
def parse_IHDR_fixture(self):
138+
bytes_ = b'\x00\x00\x00\x2A\x00\x00\x00\x18'
139+
stream = BytesIO(bytes_)
140+
stream_rdr = StreamReader(stream, BIG_ENDIAN)
141+
offset = 0
142+
expected_attrs = {TAG.PX_WIDTH: 42, TAG.PX_HEIGHT: 24}
143+
return stream_rdr, offset, expected_attrs
144+
131145
@pytest.fixture
132146
def parse_png_fixture(
133147
self, stream_rdr_, _parse_chunk_offsets_, _parse_chunks_,

0 commit comments

Comments
 (0)