Skip to content

Commit 0ce27af

Browse files
authored
Merge pull request #1 from crookedstorm/ppm
Ppm -- adding module
2 parents 1663263 + e1b2aaa commit 0ce27af

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.ppm`
24+
====================================================
25+
26+
Load pixel values (indices or colors) into a bitmap and for a binary ppm,
27+
return None for pallet.
28+
29+
* Author(s): Matt Land, Brooke Storm, Sam McGahan
30+
31+
"""
32+
33+
__version__ = "0.0.0-auto.0"
34+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
35+
36+
37+
def load(file, magic_number, header, bitmap=None, palette=None):
38+
"""Load pixel values (indices or colors) into a bitmap and for a binary
39+
ppm, return None for pallet."""
40+
width = header[0]
41+
height = header[1]
42+
max_colors = (header[2] + 1) ** 3
43+
bitmap = bitmap(width, height, max_colors)
44+
palette = None
45+
46+
if bitmap:
47+
minimum_color_depth = 1
48+
while max_colors > 2 ** minimum_color_depth:
49+
minimum_color_depth *= 2
50+
51+
line_size = width // (8 // max_colors)
52+
53+
if line_size % 4 != 0:
54+
line_size += 4 - line_size % 4
55+
56+
if magic_number == b"P3":
57+
# This is ascii
58+
from . import ppm_ascii
59+
60+
return ppm_ascii.load(
61+
file,
62+
width,
63+
height,
64+
max_colors,
65+
bitmap=bitmap,
66+
palette=None,
67+
)
68+
chunk = bytearray(line_size)
69+
70+
for y in range(height - 1):
71+
file.readinto(chunk)
72+
pixels_per_byte = 8 // max_colors
73+
offset = y * width
74+
75+
for x in range(width):
76+
i = x // pixels_per_byte
77+
pixel = (
78+
chunk[i] >> (8 - max_colors * (x % pixels_per_byte + 1))
79+
) & ((1 << minimum_color_depth) - 1)
80+
bitmap[offset + x] = pixel
81+
82+
return bitmap, palette
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.ppm.ppm_ascii`
24+
====================================================
25+
26+
Load pixel values (indices or colors) into a bitmap and for an ascii ppm,
27+
return None for pallet.
28+
29+
* Author(s): Matt Land, Brooke Storm, Sam McGahan
30+
31+
"""
32+
33+
__version__ = "0.0.0-auto.0"
34+
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_ImageLoad.git"
35+
36+
37+
def load(file, width, height, max_colors, bitmap=None, palette=None):
38+
"""Load an ascii ppm into the Bitmap object"""
39+
if bitmap:
40+
for y in range(height - 1):
41+
offset = y * width
42+
for x in range(width):
43+
triplet = bytearray()
44+
color = bytearray()
45+
while True:
46+
this_byte = file.read()
47+
if this_byte.isdigit():
48+
color += this_byte
49+
else:
50+
triplet += color
51+
color = bytearray()
52+
break
53+
54+
if len(triplet) == max_colors:
55+
bitmap[offset + x] = triplet
56+
57+
return bitmap, palette

0 commit comments

Comments
 (0)