Skip to content

Commit 318c15f

Browse files
committed
fix: speed increase
1 parent 2e672f5 commit 318c15f

4 files changed

Lines changed: 29 additions & 21 deletions

File tree

src/python/app/command/io.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Module for input/output operations related to parsing commandline"""
2-
2+
from io import BytesIO
33
from sys import stdin, stdout
44
from typing import BinaryIO
55

@@ -10,7 +10,8 @@ def map_input(input_source: str) -> BinaryIO:
1010
if input_source is None:
1111
return stdin.buffer
1212

13-
return open(input_source, mode='rb')
13+
with open(input_source, mode='rb', buffering=16384) as file:
14+
return BytesIO(file.read())
1415

1516

1617
def map_output(output_source: str) -> BinaryIO:
@@ -19,4 +20,4 @@ def map_output(output_source: str) -> BinaryIO:
1920
if output_source is None:
2021
return stdout.buffer
2122

22-
return open(output_source, mode='wb')
23+
return open(output_source, mode='wb', buffering=16384)

src/python/app/command/parser.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ def prepare_command(command: IOperation) -> Callable[[Namespace], int]:
4848
"""Function that decorates the operation in order to provide input and output to it"""
4949

5050
def wrapper(args: Namespace) -> int:
51+
nonlocal command
52+
5153
with map_input(args.input) as input_source:
5254
data_format = determine_format(input_source)
5355

@@ -57,10 +59,10 @@ def wrapper(args: Namespace) -> int:
5759
else KnownFormat.from_string(args.output_format))
5860

5961
input_arr = reader.read_format(input_source)
60-
result = command(args, input_arr)
62+
result = command(args, input_arr)
6163

62-
with map_output(args.output) as output_source:
63-
writer.write_format(output_source, result)
64+
with map_output(args.output) as output_source:
65+
writer.write_format(output_source, result)
6466

6567
return 0
6668

src/python/app/io/bmp.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,15 @@ def write_format(self, file: BinaryIO, input_image: Image) -> None:
142142

143143
file_size = 54 + raw_bitmap_data_size
144144

145-
file.write(struct.pack('BB', 0x42, 0x4D)) # signature
146-
file.write(struct.pack('I', file_size)) # file_size
147-
file.write(struct.pack('HH', 0, 0)) # reserved1 & reserved2
148-
file.write(struct.pack('I', 54)) # file_offset_to_pixel_array
149-
150-
file.write(struct.pack('I', 40)) # dbi header size
151-
file.write(struct.pack('iiHH', image_width, image_height, 1, bits_per_pixel))
152-
file.write(struct.pack('IIiiII', 0, raw_bitmap_data_size, 2834, 2834, 0, 0))
145+
file.write(b''.join((
146+
struct.pack('BB', 0x42, 0x4D), # signature
147+
struct.pack('I', file_size), # file_size
148+
struct.pack('HH', 0, 0), # reserved1 & reserved2
149+
struct.pack('I', 54), # file_offset_to_pixel_array
150+
struct.pack('I', 40), # dbi header size
151+
struct.pack('iiHH', image_width, image_height, 1, bits_per_pixel),
152+
struct.pack('IIiiII', 0, raw_bitmap_data_size, 2834, 2834, 0, 0),
153+
)))
153154

154155
for i in range(image_height):
155-
file.write(input_arr[i].tobytes())
156-
file.write(bytes([0] * padding))
156+
file.write(input_arr[i].tobytes() + bytes([0] * padding))

src/python/app/io/png.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,16 @@ def from_numpy(cls, data: np.ndarray) -> 'IDATData':
257257
"""Serialize for IDAT binary content"""
258258

259259
new_data = np.concatenate((data, np.full((data.shape[0], data.shape[1], 1), 255, dtype=np.uint8)), axis=2)
260+
260261
stream = bytearray()
262+
compressor = zlib.compressobj(level=zlib.Z_BEST_SPEED)
263+
261264
for i in range(new_data.shape[0]):
262-
stream += b'\0' + new_data[i].tobytes()
265+
stream += compressor.compress(b'\0' + new_data[i].tobytes())
266+
267+
stream += compressor.flush()
263268

264-
return cls(compressed_data=zlib.compress(stream))
269+
return cls(compressed_data=bytes(stream))
265270

266271
@override
267272
def type(self) -> ChunkType:
@@ -401,17 +406,17 @@ def from_numpy(cls, data: np.ndarray) -> 'PNG':
401406
PNGChunk.from_chunk(end_chunk)])
402407

403408
def __post_init__(self) -> None:
404-
if self.chunks[-1].type() != ChunkType.IEND:
409+
if self.chunks[-1].chunk_type != ChunkType.IEND:
405410
raise InvalidFormatException("Last chunk is not end")
406411

407412
def to_numpy(self) -> np.ndarray:
408413
"""Serializer of the data to a numpy array"""
409414
all_data_compressed = b''.join(x.chunk_data.compressed_data
410415
for x in self.chunks
411-
if x.type() == ChunkType.IDAT)
416+
if x.chunk_type == ChunkType.IDAT)
412417
result = zlib.decompress(all_data_compressed)
413418

414-
palette = [x.chunk_data.palette_entries for x in self.chunks if x.type() == ChunkType.PLTE]
419+
palette = [x.chunk_data.palette_entries for x in self.chunks if x.chunk_type == ChunkType.PLTE]
415420

416421
if len(palette) == 1:
417422
return palette[np.frombuffer(result)].reshape(self.i_header.chunk_data.height,

0 commit comments

Comments
 (0)