2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121# THE SOFTWARE.
2222"""
23- `adafruit_imageload.pnm`
23+ `adafruit_imageload.pnm.pgm `
2424====================================================
2525
2626Load pixel values (indices or colors) into a bitmap and colors into a palette.
2727
2828* Author(s): Matt Land, Brooke Storm, Sam McGahan
2929
3030"""
31- #import logging
31+
3232
3333def load (file , magic_number , header , * , bitmap = None , palette = None ):
34+ """
35+ Perform the load of Netpbm greyscale images
36+ :param file: stream resource with the pointer at the start of data
37+ :param magic_number: string P2 or P6
38+ :param header: list of width, height, max color value
39+ :param bitmap: displayio.Bitmap class object
40+ :param palette: displayio.Paletta class object
41+ :return:
42+ """
3443 if header [2 ] > 256 :
3544 raise NotImplementedError ("16 bit files are not supported" )
3645 width = header [0 ]
3746 height = header [1 ]
3847
3948 data_start = file .tell () # keep this so we can rewind
40- #bitmap = bitmap(width, height, max_colors)
4149 palette_colors = set ()
4250
43- if magic_number == b'P2' : # To handle ascii PGM files.
44- # Handle ascii
45-
51+ if magic_number == b"P2" : # To handle ascii PGM files.
4652 pixel = bytearray ()
47- byte = True
48- # scan all colors present in the file
49- while byte :
50- byte = file .read (1 ) # type: byte
53+ # build a set of all colors present in the file, so palette and bitmap can be constructed
54+ while True :
55+ byte = file .read (1 )
56+ if byte == b'' :
57+ break
5158 if not byte .isdigit ():
5259 int_pixel = int ("" .join (["%c" % char for char in pixel ]))
53- #logging.info(f'color {pixel} {int_pixel}')
54- #bitmap[x, y] = int_pixel
5560 palette_colors .add (int_pixel )
5661 pixel = bytearray ()
5762 pixel += byte
58-
59- # logging.info(f'{x}, {y}, {byte}, {int_pixel}')
6063 if palette :
61- palette = palette (len (palette_colors ))
62- for counter , color in enumerate (palette_colors ):
63- palette [counter ] = bytes ([color , color , color ])
64+ palette = build_palette (palette , palette_colors )
6465 if bitmap :
6566 bitmap = bitmap (width , height , len (palette_colors ))
6667 palette_colors = list (palette_colors )
6768 file .seek (data_start )
6869 for y in range (height ):
6970 for x in range (width ):
7071 pixel = bytearray ()
71- # Takes int and converts to an 8 bit
7272 while True :
73- byte = file .read (1 ) # type: byte
73+ byte = file .read (1 )
7474 if not byte .isdigit ():
7575 break
7676 pixel += byte
77- # convert b'255' to b'\xff'
78- color = int ("" .join (["%c" % char for char in pixel ]))
79- #logging.info(f'found color {color} in palette at {palette_colors.index(color)}')
80- bitmap [x , y ] = palette_colors .index (color )
81-
77+ int_pixel = int ("" .join (["%c" % char for char in pixel ]))
78+ bitmap [x , y ] = palette_colors .index (int_pixel )
8279 return bitmap , palette
8380
84- if magic_number == b'P5' : # To handle binary PGM files.
85- for y in range (height ):
86- for x in range (width ):
87- byte = file .read (1 )
88- if byte == b"" :
89- raise ValueError ("ran out of file unexpectedly" )
90- int_pixel = int .from_bytes (byte , "little" )
91- bitmap [x , y ] = int_pixel
92- palette_colors .add (int_pixel )
81+ if magic_number == b"P5" : # To handle binary PGM files.
82+ while True :
83+ byte = file .read (1 )
84+ if byte == b'' :
85+ break
86+ int_pixel = int .from_bytes (byte , "little" )
87+ palette_colors .add (int_pixel )
9388
9489 if palette :
95- palette = palette (len (palette_colors ))
96- for counter , color in enumerate (palette_colors ):
97- color_bytearray = bytearray ()
98- for i in range (3 ):
99- color_bytearray += bytes ([color ])
100- palette [counter ] = color_bytearray
101-
90+ palette = build_palette (palette , palette_colors )
91+ if bitmap :
92+ bitmap = bitmap (width , height , len (palette_colors ))
93+ palette_colors = list (palette_colors )
94+ file .seek (data_start )
95+ for y in range (height ):
96+ for x in range (width ):
97+ byte = file .read (1 )
98+ if byte == b"" :
99+ raise ValueError ("ran out of file unexpectedly" )
100+ int_pixel = int .from_bytes (byte , "little" )
101+ bitmap [x , y ] = palette_colors .index (int_pixel )
102102 return bitmap , palette
103103
104104 raise NotImplementedError ("Was not able to send image" )
105+
106+
107+ def build_palette (palette_class , palette_colors ):
108+ palette = palette_class (len (palette_colors ))
109+ for counter , color in enumerate (palette_colors ):
110+ palette [counter ] = bytes ([color , color , color ])
111+ return palette
0 commit comments