2 -------------------------------------------------------------------------------
4 -- Purpose: Fix XPM files for use in Ribbon sample
5 -- Author: Peter Cawley
9 -- Copyright: (C) Copyright 2009, Peter Cawley
10 -- Licence: wxWindows Library Licence
11 -------------------------------------------------------------------------------
12 -- My preferred image editor (Paint Shop Pro 9) spits out XPM files, but with
14 -- 1) Specifies a 256 colour palette, even when less than 256 colours are used
15 -- 2) Transparency is replaced by a non-transparent colour
16 -- 3) Does not name the C array appropriately
17 -- 4) Array and strings not marked const
19 assert(_VERSION == "Lua 5.1", "Lua 5.1 is required")
20 local lpeg = require "lpeg"
24 local filename = assert(...,"Expected filename as first command line argument")
28 for i = 2, select('#', ...) do
30 if arg == "/?" or arg == "-?" or arg == "--help" then
31 print("Usage: filename [transparent=<colour>|(x,y)] [name=<array_name>] "..
33 print("In addition to the transparent colour and name changes, the "..
34 "palette will be also be optimised")
36 print(" in.xpm transparent=Gray100 -- Modifies in.xpm, replacing "..
37 "Gray100 with transparent")
38 print(" in.xpm transparent=(0,0) -- Modifies in.xpm, replacing "..
39 "whichever colour is at (0,0) with transparent")
40 print(" in.xpm name=out_xpm out=out.xpm -- Copies in.xpm to out.xpm, "..
41 "and changes the array name to out_xpm")
44 arg_transparent = arg:match"transparent=(.*)" or arg_transparent
45 arg_name = arg:match"name=(.*)" or arg_name
46 arg_out = arg:match"out=(.*)" or arg_out
52 local C, P, R, S, V = lpeg.C, lpeg.P, lpeg.R, lpeg.S, lpeg.V
54 local comment = P"/*" * (1 - P"*/") ^ 0 * P"*/"
55 local ws = (S" \r\n\t" + comment) ^ 0
56 local function tokens(...)
58 for i, arg in ipairs{...} do
59 if type(arg) == "table" then
62 result = result * P(arg) * ws
66 grammars.file = P { "xpm";
67 xpm = P"/* XPM */" * ws *
68 tokens("static",{"const"},"char","*",{"const"}) * V"name" *
69 tokens("[","]","=","{") * V"lines",
70 name = C(R("az","AZ","__") * R("az","AZ","09","__") ^ 0),
71 lines = Ct(V"line" ^ 1),
72 line = ws * P'"' * C((1 - P'"') ^ 0) * P'"' * (tokens"," + V"eof"),
73 eof = tokens("}",";") * P(1) ^ 0,
75 grammars.values = P { "values";
76 values = Ct(V"value" * (S" \r\n\t" ^ 1 * V"value") ^ 3),
77 value = C(R"09" ^ 1) / tonumber,
79 function make_remaining_grammars(cpp)
80 local char = R"\32\126" - S[['"\]] -- Most of lower ASCII
83 colour = colour * char
85 grammars.colour = P { "line";
86 line = C(colour) * Ct(Ct(ws * V"key" * ws * V"col") ^ 1),
87 key = C(P"g4" + S"msgc"),
88 col = V"name" + V"hex",
89 name = C(R("az","AZ","__") * R("az","AZ","09","__") ^ 0),
90 hex = C(P"#" * R("09","af","AF") ^ 3),
92 grammars.pixels = P { "line";
93 line = Ct(C(colour) ^ 1),
99 local file = assert(io.open(filename,"rt"))
100 local filedata = file:read"*a"
104 xpm.name, xpm.lines = grammars.file:match(filedata)
105 local values_table = assert(grammars.values:match(xpm.lines[1]))
106 xpm.width, xpm.height, xpm.ncolours, xpm.cpp = unpack(values_table)
107 make_remaining_grammars(xpm.cpp)
109 xpm.colours_full = {}
110 for i = 1, xpm.ncolours do
111 local name, data = grammars.colour:match(xpm.lines[1 + i])
113 for _, datum in ipairs(data) do
114 if datum[1] == "c" then
119 assert(colour, "No colour data for " .. name)
120 xpm.colours[name] = colour
121 xpm.colours_full[i] = {name = name, unpack(data)}
124 for y = 1, xpm.height do
125 xpm.pixels[y] = grammars.pixels:match(xpm.lines[1 + xpm.ncolours + y])
126 if not xpm.pixels[y] or #xpm.pixels[y] ~= xpm.width then
127 error("Line " .. y .. " is invalid")
133 local n_colours_used = 0
134 local colours_used = setmetatable({}, {__newindex = function(t, k, v)
135 n_colours_used = n_colours_used + 1
138 for y = 1, xpm.height do
139 for x = 1, xpm.width do
140 colours_used[xpm.pixels[y][x]] = true
143 if n_colours_used == xpm.ncolours then
146 local chars =" .abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567"
147 local cpp = (n_colours_used > #chars) and 2 or 1
149 local colour_map = setmetatable({}, {__index = function(t, k)
153 v = chars:sub(nalloc, nalloc)
155 local a, b = math.floor(nalloc / #chars) + 1, (nalloc % #chars) + 1
156 v = chars:sub(a, a) .. chars:sub(b, b)
161 for y = 1, xpm.height do
162 for x = 1, xpm.width do
163 xpm.pixels[y][x] = colour_map[xpm.pixels[y][x]]
166 local new_colours_full = {}
167 for i, colour in ipairs(xpm.colours_full) do
168 if colours_used[colour.name] then
169 colour.name = colour_map[colour.name]
170 new_colours_full[#new_colours_full + 1] = colour
173 xpm.colours_full = new_colours_full
174 local new_colours = {}
175 for name, value in pairs(xpm.colours) do
176 if colours_used[name] then
177 new_colours[colour_map[name]] = value
180 xpm.colours = new_colours
182 xpm.ncolours = nalloc
186 if arg_transparent then
188 local x, y = arg_transparent:match"[(](%d+),(%d+)[)]"
190 name = xpm.pixels[y + 1][x + 1]
192 for n, c in pairs(xpm.colours) do
193 if c == arg_transparent then
200 error("Cannot convert " .. arg_transparent .. " to transparent as the "..
201 "colour is not present in the file")
203 xpm.colours[name] = "None"
204 for i, colour in ipairs(xpm.colours_full) do
205 if colour.name == name then
206 for i, data in ipairs(colour) do
207 if data[1] == "c" then
218 xpm.name = arg_name or xpm.name
221 local file = assert(io.open(arg_out or filename, "wt"))
222 file:write"/* XPM */\n"
223 file:write("static const char *const " .. xpm.name .. "[] = {\n")
224 file:write(('"%i %i %i %i",\n'):format(xpm.width, xpm.height, xpm.ncolours,
226 for _, colour in ipairs(xpm.colours_full) do
227 file:write('"' .. colour.name)
228 for _, data in ipairs(colour) do
229 file:write(" " .. data[1] .. " " .. data[2])
233 for i, row in ipairs(xpm.pixels) do
234 file:write('"' .. table.concat(row) .. (i == xpm.height and '"\n' or '",\n'))