Skip to content

Commit 197949b

Browse files
committed
feat: added png support
1 parent 6bd6dc8 commit 197949b

3 files changed

Lines changed: 412 additions & 7 deletions

File tree

src/python/app/io/bmp.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class Signature(IntEnum):
3838

3939
@dataclass(slots=True)
4040
class BitmapFileHeader:
41-
"""Implements the struct for the """
41+
"""Implements the struct for the Bitmap file header"""
4242

4343
HEADER_LENGTH: ClassVar[int] = 14
4444

@@ -53,7 +53,7 @@ def from_bytes(cls, data: bytes) -> 'BitmapFileHeader':
5353
if len(data) < cls.HEADER_LENGTH:
5454
raise InvalidFormatException(f"Header too short, received: {len(data)} instead of {cls.HEADER_LENGTH}")
5555

56-
return cls(*struct.unpack('HIHHI', data))
56+
return cls(*struct.unpack('>HIHHI', data))
5757

5858
def __post_init__(self) -> None:
5959
if self.signature not in set(e.value for e in Signature):
@@ -68,11 +68,11 @@ def __post_init__(self) -> None:
6868

6969
@dataclass(slots=True)
7070
class BMP:
71-
pass
71+
header: BitmapFileHeader
7272

7373

7474
@final
75-
class BMPReader(IFormatReader): # pylint: disable=too-few-public-methods
75+
class BMPReader(IFormatReader):
7676
"""Class that deserializes BMP format to Image"""
7777

7878
@override
@@ -108,12 +108,12 @@ def read_format(self, file: BinaryIO) -> Image:
108108

109109

110110
@final
111-
class BMPWriter(IFormatWriter): # pylint: disable=too-few-public-methods
111+
class BMPWriter(IFormatWriter):
112112
"""Class that serializes Image to BMP format"""
113113

114+
@override
114115
def write_format(self, file: BinaryIO, input_image: Image) -> None:
115116
input_arr = input_image.data
116-
input_arr = np.flip(np.flip(input_arr[:, :, ::-1], axis=1), axis=0)
117117

118118
image_height, image_width, num_colors = input_arr.shape
119119
bits_per_pixel = num_colors * 8

src/python/app/io/format_factory.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from app.io.bmp import BMPReader, BMPWriter
77
from app.io.format_reader import IFormatReader
88
from app.io.format_writer import IFormatWriter
9+
from app.io.png import PNGWriter, PNGReader
910

1011

1112
class KnownFormat(enum.Enum):
@@ -25,6 +26,9 @@ def from_string(cls, data_format: str) -> 'KnownFormat':
2526
case 'bmp':
2627
return cls.BMP
2728

29+
case 'png':
30+
return cls.PNG
31+
2832
case _:
2933
raise UnknownFormatException(data_format)
3034

@@ -38,7 +42,7 @@ def get_available_formats(cls) -> list[str]:
3842
def default(cls) -> 'KnownFormat':
3943
"""Return default format for commandline to use"""
4044

41-
return KnownFormat.BMP
45+
return KnownFormat.PNG
4246

4347

4448
def get_reader_from_format(data_format: KnownFormat) -> IFormatReader:
@@ -48,6 +52,9 @@ def get_reader_from_format(data_format: KnownFormat) -> IFormatReader:
4852
case KnownFormat.BMP:
4953
return BMPReader()
5054

55+
case KnownFormat.PNG:
56+
return PNGReader()
57+
5158
case _:
5259
assert False, "unreachable"
5360

@@ -59,5 +66,8 @@ def get_writer_from_format(data_format: KnownFormat) -> IFormatWriter:
5966
case KnownFormat.BMP:
6067
return BMPWriter()
6168

69+
case KnownFormat.PNG:
70+
return PNGWriter()
71+
6272
case _:
6373
assert False, "unreachable"

0 commit comments

Comments
 (0)