1+ import io
2+
3+ import numpy as np
4+ import pytest
5+
6+ from app .image .image import Image
7+ from app .io .bmp import BMPWriter , BMPReader
8+
9+
10+ @pytest .mark .parametrize ('data,expected' , [
11+ (
12+ np .array ([[[255 , 0 , 0 ], [0 , 255 , 0 ]], [[0 , 0 , 255 ], [255 , 255 , 255 ]]], dtype = np .uint8 ),
13+ b'BMF\x00 \x00 \x00 \x00 \x00 \x00 \x00 6\x00 \x00 \x00 (\x00 \x00 \x00 \x02 \x00 \x00 \x00 \x02 \x00 \x00 \x00 \x01 \x00 \x18 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \xc8 \x00 \x00 \x00 \xc8 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xff \x00 \x00 \x00 \xff \x00 \x00 \x00 \x00 \x00 \xff \xff \xff \xff \x00 \x00 ' ,
14+ ),
15+ (
16+ np .array ([[[237 , 28 , 36 ], [0 , 162 , 232 ]], [[255 , 242 , 0 ], [163 , 73 , 164 ]]], dtype = np .uint8 ),
17+ b'BMF\x00 \x00 \x00 \x00 \x00 \x00 \x00 6\x00 \x00 \x00 (\x00 \x00 \x00 \x02 \x00 \x00 \x00 \x02 \x00 \x00 \x00 \x01 \x00 \x18 \x00 \x00 \x00 \x00 \x00 \x01 \x00 \x00 \x00 \xc8 \x00 \x00 \x00 \xc8 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \xed \x1c $\x00 \xa2 \xe8 \x00 \x00 \xff \xf2 \x00 \xa3 I\xa4 \x00 \x00 ' ,
18+ ),
19+ ])
20+ def test_writer (data : np .ndarray , expected : bytes ) -> None :
21+ buffer = io .BytesIO ()
22+ writer = BMPWriter ()
23+ writer .write_format (buffer , Image (data = data ))
24+
25+ assert buffer .getvalue () == expected
26+
27+
28+ @pytest .mark .parametrize ('data' , [
29+ np .array ([[[255 , 0 , 0 ], [0 , 255 , 0 ]], [[0 , 0 , 255 ], [255 , 255 , 255 ]]], dtype = np .uint8 ),
30+ np .array ([[[237 , 28 , 36 ], [0 , 162 , 232 ]], [[255 , 242 , 0 ], [163 , 73 , 164 ]]], dtype = np .uint8 ),
31+ ])
32+ def test_transcoding_from_writer (data : np .ndarray ) -> None :
33+ out_buffer = io .BytesIO ()
34+
35+ reader = BMPReader ()
36+ writer = BMPWriter ()
37+ writer .write_format (out_buffer , Image (data = data ))
38+
39+ assert np .all (reader .read_format (io .BytesIO (out_buffer .getvalue ())).data == data )
0 commit comments