Debian Codegraph @

CorsixTH/Bitmap/lib_bmp.lua

raw
1 --[[ Copyright (c) 2010 Peter "Corsix" Cawley
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 -- This is a custom written BMP library for CorsixTH. It currently only supports
22 -- BMP files 8bpp without compression
23
24
25 local io_open, table_concat, setmetatable, ipairs, string_reverse, string_char
26 = io.open, table.concat, setmetatable, ipairs, string.reverse, string.char
27 local math_floor
28 = math.floor
29
30 local B = {}
31 local mt = {__index = _M}
32
33 -- Convert a little endian byte string into an integer
34 local function LE(s)
35 local value = 0
36 for n, i in ipairs{s:byte(1, #s)} do
37 value = value + i * 256 ^ (n - 1)
38 end
39 return value
40 end
41
42 function B.open(filename)
43 local file, err = io_open(filename, "rb")
44 if not file then
45 return nil, err
46 end
47 if file:read(2) ~= "BM" or not file:seek("cur", 8) then
48 return nil, "Invalid header"
49 end
50 local bits_offset, header_size = LE(file:read(4)), LE(file:read(4))
51 if header_size ~= 40 then
52 return nil, "Expected BITMAPINFOHEADER. Header size was " .. header_size
53 end
54 local width, height = LE(file:read(4)), LE(file:read(4))
55 local planes, bpp = LE(file:read(2)), LE(file:read(2))
56 if planes ~= 1 then
57 return nil, "Expected single colour plane"
58 end
59 if bpp ~= 8 then
60 return nil, "Expected 8 bit paletted image"
61 end
62 local compression = LE(file:read(4))
63 if compression ~= 0 then
64 return nil, "Expected uncompressed image"
65 end
66 file:seek("cur", 12)
67 local pal_size = LE(file:read(4))
68 if pal_size == 0 then
69 pal_size = 2 ^ bpp
70 end
71 file:seek("cur", 4)
72 local palette = {}
73 for pal_idx = 1, pal_size do
74 local bgr = file:read(3)
75 file:seek("cur", 1)
76 palette[pal_idx] = B.convertPal(bgr)
77 end
78 return setmetatable({
79 file = file,
80 bits_offset = bits_offset,
81 width = width,
82 height = height,
83 pal_size = pal_size,
84 palette = table_concat(palette),
85 }, mt)
86 end
87
88 function B.getPixel(bmp, x, y)
89 local file, width = bmp.file, bmp.width
90 if x < 0 or y < 0 or x >= width or y >= bmp.height then
91 return nil, "Invalid pixel"
92 end
93 local stride = width + ((4 - (width % 4)) % 4)
94 local offset = (bmp.height - 1 - y) * stride + x
95 if not file:seek("set", bmp.bits_offset + offset) then
96 return nil, "Invalid data offset"
97 end
98 return file:read(1)
99 end
100
101 function B.getPixels(bmp)
102 local file, width = bmp.file, bmp.width
103 if not file:seek("set", bmp.bits_offset) then
104 return nil, "Invalid data offset"
105 end
106 local rows = {}
107 local skip = (4 - (width % 4)) % 4
108 for y = bmp.height, 1, -1 do
109 rows[y] = file:read(width)
110 file:seek("cur", skip)
111 end
112 return table_concat(rows)
113 end
114
115 function B.getSubPixels(bmp, x, y, w, h)
116 local file, width = bmp.file, bmp.width
117 local stride = width + ((4 - (width % 4)) % 4)
118 local offset = (bmp.height - y - h) * stride + x
119 if not file:seek("set", bmp.bits_offset + offset) then
120 return nil, "Invalid data offset"
121 end
122 local rows = {}
123 local skip = stride - w
124 for y = h, 1, -1 do
125 rows[y] = file:read(w)
126 file:seek("cur", skip)
127 end
128 return table_concat(rows)
129 end
130
131 function B.convertPal(data)
132 return (data:gsub("...", string_reverse):gsub(".", function(c)
133 return string_char(math_floor(c:byte() / 255 * 63 + 0.5))
134 end))
135 end
136
137 return B