mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
Fix and cleanup
This commit is contained in:
parent
88dbfd5257
commit
0674485e76
@ -2,25 +2,27 @@ import typing
|
||||
|
||||
from kaitaistruct import KaitaiStream
|
||||
|
||||
from . import png
|
||||
from mitmproxy.contrib.kaitaistruct import png
|
||||
|
||||
Metadata = typing.List[typing.Tuple[str, str]]
|
||||
|
||||
def parse_png(data: bytes) -> Metadata:
|
||||
img = png.Png(KaitaiStream(data))
|
||||
parts = [tuple(['Format', 'Portable network graphics'])]
|
||||
parts.append(tuple(['Size', str(img.ihdr.width) + " x " + str(img.ihdr.height) + " px"]))
|
||||
parts = [
|
||||
('Format', 'Portable network graphics')
|
||||
]
|
||||
parts.append(('Size', "{0} x {1} px".format(img.ihdr.width, img.ihdr.height)))
|
||||
for chunk in img.chunks:
|
||||
if chunk.type == 'gAMA':
|
||||
parts.append(tuple(['gamma', str(chunk.body.gamma_int / 100000)]))
|
||||
parts.append(('gamma', str(chunk.body.gamma_int / 100000)))
|
||||
elif chunk.type == 'pHYs':
|
||||
aspectx = chunk.body.pixels_per_unit_x
|
||||
aspecty = chunk.body.pixels_per_unit_y
|
||||
parts.append(tuple(['aspect', str(aspectx) + " x " + str(aspecty)]))
|
||||
parts.append(('aspect', "{0} x {1}".format(aspectx, aspecty)))
|
||||
elif chunk.type == 'tEXt':
|
||||
parts.append(tuple([chunk.body.keyword, chunk.body.text]))
|
||||
parts.append((chunk.body.keyword, chunk.body.text))
|
||||
elif chunk.type == 'iTXt':
|
||||
parts.append(tuple([chunk.body.keyword, chunk.body.text]))
|
||||
parts.append((chunk.body.keyword, chunk.body.text))
|
||||
elif chunk.type == 'zTXt':
|
||||
parts.append(tuple([chunk.body.keyword, chunk.body.text_datastream.decode('iso8859-1')]))
|
||||
parts.append((chunk.body.keyword, chunk.body.text_datastream.decode('iso8859-1')))
|
||||
return parts
|
||||
|
@ -1,5 +1,5 @@
|
||||
# This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
|
||||
# The source was png.ksy from here - https://github.com/kaitai-io/kaitai_struct_formats/tree/master/image
|
||||
# The source was png.ksy from here - https://github.com/kaitai-io/kaitai_struct_formats/blob/9370c720b7d2ad329102d89bdc880ba6a706ef26/image/png.ksy
|
||||
|
||||
import array
|
||||
import struct
|
||||
@ -262,7 +262,7 @@ class Png(KaitaiStruct):
|
||||
self.keyword = self._io.read_strz("UTF-8", 0, False, True, True)
|
||||
self.compression_flag = self._io.read_u1()
|
||||
self.compression_method = self._io.read_u1()
|
||||
self.language_tag = self._io.read_strz("iso8859-1", 0, False, True, True)
|
||||
self.language_tag = self._io.read_strz("ASCII", 0, False, True, True)
|
||||
self.translated_keyword = self._io.read_strz("UTF-8", 0, False, True, True)
|
||||
self.text = self._io.read_str_eos("UTF-8")
|
||||
|
@ -1,88 +1,62 @@
|
||||
import io
|
||||
|
||||
import pytest
|
||||
|
||||
from mitmproxy.contentviews.image import image_parser
|
||||
from mitmproxy.test import tutils
|
||||
|
||||
class TestPngParser:
|
||||
def test_png_size(self):
|
||||
img = "mitmproxy/data/image.png"
|
||||
with open(tutils.test_data.path(img), "rb") as f:
|
||||
parts = image_parser.parse_png(io.BytesIO(f.read()))
|
||||
assert parts
|
||||
assert tuple(['Size', '174 x 174 px']) in parts
|
||||
assert tuple(["Format", "Portable network graphics"]) in parts
|
||||
|
||||
def test_no_textual_data(self):
|
||||
img = "mitmproxy/data/png_parser/ct0n0g04.png"
|
||||
with open(tutils.test_data.path(img), "rb") as f:
|
||||
parts = image_parser.parse_png(io.BytesIO(f.read()))
|
||||
assert parts
|
||||
metadata = [
|
||||
('Format', 'Portable network graphics'),
|
||||
('Size', '32 x 32 px'),
|
||||
('gamma', '1.0')
|
||||
]
|
||||
parts = [data for data in parts if data not in metadata]
|
||||
assert not parts
|
||||
|
||||
def test_textual_data(self):
|
||||
img = "mitmproxy/data/png_parser/ct1n0g04.png"
|
||||
expected = [
|
||||
('Title', 'PngSuite'),
|
||||
('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'),
|
||||
('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'),
|
||||
('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'),
|
||||
('Software', 'Created on a NeXTstation color using "pnmtopng".'),
|
||||
('Disclaimer', 'Freeware.')
|
||||
]
|
||||
with open(tutils.test_data.path(img), "rb") as f:
|
||||
parts = image_parser.parse_png(io.BytesIO(f.read()))
|
||||
assert parts
|
||||
for data in expected:
|
||||
assert data in parts
|
||||
|
||||
def test_compressed_textual_data(self):
|
||||
img = "mitmproxy/data/png_parser/ctzn0g04.png"
|
||||
expected = [
|
||||
('Title', 'PngSuite'),
|
||||
('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'),
|
||||
('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'),
|
||||
('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'),
|
||||
('Software', 'Created on a NeXTstation color using "pnmtopng".'),
|
||||
('Disclaimer', 'Freeware.')
|
||||
]
|
||||
with open(tutils.test_data.path(img), "rb") as f:
|
||||
parts = image_parser.parse_png(io.BytesIO(f.read()))
|
||||
assert parts
|
||||
for data in expected:
|
||||
assert data in parts
|
||||
|
||||
def test_international_textual_data(self):
|
||||
img = "mitmproxy/data/png_parser/cten0g04.png"
|
||||
expected = [
|
||||
('Author', 'Willem van Schaik (willem@schaik.com)'),
|
||||
('Copyright', 'Copyright Willem van Schaik, Canada 2011'),
|
||||
('Description', 'A compilation of a set of images created to test the various color-types of the PNG format. Included are black&white, color, paletted, with alpha channel, with transparency formats. All bit-depths allowed according to the spec are present.'),
|
||||
('Software', 'Created on a NeXTstation color using "pnmtopng".'),
|
||||
('Disclaimer', 'Freeware.')
|
||||
]
|
||||
with open(tutils.test_data.path(img), "rb") as f:
|
||||
parts = image_parser.parse_png(io.BytesIO(f.read()))
|
||||
print(parts)
|
||||
assert parts
|
||||
for data in expected:
|
||||
assert data in parts
|
||||
|
||||
def test_gamma(self):
|
||||
img = "mitmproxy/data/png_parser/g07n0g16.png"
|
||||
with open(tutils.test_data.path(img), "rb") as f:
|
||||
parts = image_parser.parse_png(io.BytesIO(f.read()))
|
||||
assert parts
|
||||
assert ('gamma', '0.7') in parts
|
||||
|
||||
def test_aspect(self):
|
||||
img = "mitmproxy/data/png_parser/aspect.png"
|
||||
with open(tutils.test_data.path(img), "rb") as f:
|
||||
parts = image_parser.parse_png(io.BytesIO(f.read()))
|
||||
assert parts
|
||||
assert ('aspect', '72 x 72') in parts
|
||||
@pytest.mark.parametrize("filename, metadata", {
|
||||
# no textual data
|
||||
"mitmproxy/data/png_parser/ct0n0g04.png": [
|
||||
('Format', 'Portable network graphics'),
|
||||
('Size', '32 x 32 px'),
|
||||
('gamma', '1.0')
|
||||
],
|
||||
# with textual data
|
||||
"mitmproxy/data/png_parser/ct1n0g04.png": [
|
||||
('Format', 'Portable network graphics'),
|
||||
('Size', '32 x 32 px'),
|
||||
('gamma', '1.0'),
|
||||
('Title', 'PngSuite'),
|
||||
('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'),
|
||||
('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'),
|
||||
('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'), ('Software', 'Created on a NeXTstation color using "pnmtopng".'), ('Disclaimer', 'Freeware.')
|
||||
],
|
||||
# with compressed textual data
|
||||
"mitmproxy/data/png_parser/ctzn0g04.png": [
|
||||
('Format', 'Portable network graphics'),
|
||||
('Size', '32 x 32 px'),
|
||||
('gamma', '1.0'),
|
||||
('Title', 'PngSuite'),
|
||||
('Author', 'Willem A.J. van Schaik\n(willem@schaik.com)'),
|
||||
('Copyright', 'Copyright Willem van Schaik, Singapore 1995-96'),
|
||||
('Description', 'A compilation of a set of images created to test the\nvarious color-types of the PNG format. Included are\nblack&white, color, paletted, with alpha channel, with\ntransparency formats. All bit-depths allowed according\nto the spec are present.'), ('Software', 'Created on a NeXTstation color using "pnmtopng".'), ('Disclaimer', 'Freeware.')
|
||||
],
|
||||
# UTF-8 international text - english
|
||||
"mitmproxy/data/png_parser/cten0g04.png": [
|
||||
('Format', 'Portable network graphics'),
|
||||
('Size', '32 x 32 px'),
|
||||
('gamma', '1.0'),
|
||||
('Title', 'PngSuite'),
|
||||
('Author', 'Willem van Schaik (willem@schaik.com)'),
|
||||
('Copyright', 'Copyright Willem van Schaik, Canada 2011'),
|
||||
('Description', 'A compilation of a set of images created to test the various color-types of the PNG format. Included are black&white, color, paletted, with alpha channel, with transparency formats. All bit-depths allowed according to the spec are present.'), ('Software', 'Created on a NeXTstation color using "pnmtopng".'), ('Disclaimer', 'Freeware.')
|
||||
],
|
||||
# check gamma value
|
||||
"mitmproxy/data/png_parser/g07n0g16.png": [
|
||||
('Format', 'Portable network graphics'),
|
||||
('Size', '32 x 32 px'),
|
||||
('gamma', '0.7')
|
||||
],
|
||||
# check aspect value
|
||||
"mitmproxy/data/png_parser/aspect.png": [
|
||||
('Format', 'Portable network graphics'),
|
||||
('Size', '1280 x 798 px'),
|
||||
('aspect', '72 x 72'),
|
||||
('date:create', '2012-07-11T14:04:52-07:00'),
|
||||
('date:modify', '2012-07-11T14:04:52-07:00')
|
||||
],
|
||||
}.items())
|
||||
def test_parse_png(filename, metadata):
|
||||
with open(tutils.test_data.path(filename), "rb") as f:
|
||||
assert metadata == image_parser.parse_png(io.BytesIO(f.read()))
|
||||
|
Loading…
Reference in New Issue
Block a user