|
| 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.pnm` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +Load pixel values (indices or colors) into a bitmap and colors into a palette. |
| 27 | +
|
| 28 | +* Author(s): Matt Land, Brooke Storm, Sam McGahan |
| 29 | +
|
| 30 | +""" |
| 31 | + |
| 32 | +__version__ = "0.0.0-auto.0" |
| 33 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git" |
| 34 | + |
| 35 | + |
| 36 | +def load(file, header, *, bitmap=None, palette=None): |
| 37 | + """ |
| 38 | + Scan for netpbm format info, skip over comments, and and delegate to a submodule |
| 39 | + to do the actual data loading. |
| 40 | + Formats P1, P4 have two space padded pieces of information: width and height. |
| 41 | + All other formats have three: width, height, and max color value. |
| 42 | + This load function will move the file stream pointer to the start of data in all cases. |
| 43 | + """ |
| 44 | + # pylint: disable=too-many-branches |
| 45 | + magic_number = header[:2] |
| 46 | + file.seek(2) |
| 47 | + pnm_header = [] |
| 48 | + next_value = bytearray() |
| 49 | + while True: |
| 50 | + # We have all we need at length 3 for formats P2, P3, P5, P6 |
| 51 | + if len(pnm_header) == 3: |
| 52 | + if magic_number in [b"P2", b"P5"]: |
| 53 | + from . import pgm |
| 54 | + |
| 55 | + return pgm.load( |
| 56 | + file, magic_number, pnm_header, bitmap=bitmap, palette=palette |
| 57 | + ) |
| 58 | + |
| 59 | + if magic_number == b"P3": |
| 60 | + from . import ppm_ascii |
| 61 | + |
| 62 | + return ppm_ascii.load( |
| 63 | + file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette |
| 64 | + ) |
| 65 | + |
| 66 | + if magic_number == b"P6": |
| 67 | + from . import ppm_binary |
| 68 | + |
| 69 | + return ppm_binary.load( |
| 70 | + file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette |
| 71 | + ) |
| 72 | + |
| 73 | + if len(pnm_header) == 2 and magic_number in [b"P1", b"P4"]: |
| 74 | + bitmap = bitmap(pnm_header[0], pnm_header[1], 1) |
| 75 | + if palette: |
| 76 | + palette = palette(1) |
| 77 | + palette[0] = b"\xFF\xFF\xFF" |
| 78 | + if magic_number.startswith(b"P1"): |
| 79 | + from . import pbm_ascii |
| 80 | + |
| 81 | + return pbm_ascii.load( |
| 82 | + file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette |
| 83 | + ) |
| 84 | + |
| 85 | + from . import pbm_binary |
| 86 | + |
| 87 | + return pbm_binary.load( |
| 88 | + file, pnm_header[0], pnm_header[1], bitmap=bitmap, palette=palette |
| 89 | + ) |
| 90 | + |
| 91 | + next_byte = file.read(1) |
| 92 | + if next_byte == b"": |
| 93 | + raise RuntimeError("Unsupported image format {}".format(magic_number)) |
| 94 | + if next_byte == b"#": # comment found, seek until a newline or EOF is found |
| 95 | + while file.read(1) not in [b"", b"\n"]: # EOF or NL |
| 96 | + pass |
| 97 | + elif not next_byte.isdigit(): # boundary found in header data |
| 98 | + if next_value: |
| 99 | + # pull values until space is found |
| 100 | + pnm_header.append(int("".join(["%c" % char for char in next_value]))) |
| 101 | + next_value = bytearray() # reset the byte array |
| 102 | + else: |
| 103 | + next_value += next_byte # push the digit into the byte array |
0 commit comments