/ adafruit_imageload / bmp / __init__.py
__init__.py
 1  # The MIT License (MIT)
 2  #
 3  # Copyright (c) 2018 Scott Shawcroft for Adafruit Industries LLC
 4  #
 5  # Permission is hereby granted, free of charge, to any person obtaining a copy
 6  # of this software and associated documentation files (the "Software"), to deal
 7  # in the Software without restriction, including without limitation the rights
 8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 9  # copies of the Software, and to permit persons to whom the Software is
10  # furnished to do so, subject to the following conditions:
11  #
12  # The above copyright notice and this permission notice shall be included in
13  # all copies or substantial portions of the Software.
14  #
15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21  # THE SOFTWARE.
22  """
23  `adafruit_imageload.bmp`
24  ====================================================
25  
26  Load pixel values (indices or colors) into a bitmap and colors into a palette from a BMP file.
27  
28  * Author(s): Scott Shawcroft
29  
30  """
31  # pylint: disable=import-outside-toplevel
32  
33  __version__ = "0.0.0-auto.0"
34  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
35  
36  
37  def load(file, *, bitmap=None, palette=None):
38      """Loads a bmp image from the open ``file``.
39  
40         Returns tuple of bitmap object and palette object.
41  
42         :param object bitmap: Type to store bitmap data. Must have API similar to `displayio.Bitmap`.
43           Will be skipped if None
44         :param object palette: Type to store the palette. Must have API similar to
45           `displayio.Palette`. Will be skipped if None"""
46      file.seek(10)
47      data_start = int.from_bytes(file.read(4), "little")
48      # f.seek(14)
49      # bmp_header_length = int.from_bytes(file.read(4), 'little')
50      # print(bmp_header_length)
51      file.seek(0x12)  # Width of the bitmap in pixels
52      width = int.from_bytes(file.read(4), "little")
53      try:
54          height = int.from_bytes(file.read(4), "little")
55      except OverflowError:
56          raise NotImplementedError(
57              "Negative height BMP files are not supported on builds without longint"
58          )
59      file.seek(0x1C)  # Number of bits per pixel
60      color_depth = int.from_bytes(file.read(2), "little")
61      file.seek(0x1E)  # Compression type
62      compression = int.from_bytes(file.read(2), "little")
63      file.seek(0x2E)  # Number of colors in the color palette
64      colors = int.from_bytes(file.read(4), "little")
65  
66      if colors == 0 and color_depth >= 16:
67          raise NotImplementedError("True color BMP unsupported")
68  
69      if compression > 2:
70          raise NotImplementedError("bitmask compression unsupported")
71  
72      if colors == 0:
73          colors = 2 ** color_depth
74      from . import indexed
75  
76      return indexed.load(
77          file,
78          width,
79          height,
80          data_start,
81          colors,
82          color_depth,
83          compression,
84          bitmap=bitmap,
85          palette=palette,
86      )