Debian Codegraph @

src/uk/me/parabola/imgfmt/app/typ/ColourInfo.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2011.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13 package uk.me.parabola.imgfmt.app.typ;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import uk.me.parabola.imgfmt.app.BitWriter;
21 import uk.me.parabola.imgfmt.app.ImgFileWriter;
22 import uk.me.parabola.imgfmt.app.Writeable;
23
24 /**
25 * Holds colour information for elements in the typ file.
26 *
27 * The Colour information can relate to a bitmap or solid shapes.
28 *
29 * @author Steve Ratcliffe
30 */
31 public class ColourInfo implements Writeable, AlphaAdder {
32 private static final int S_NIGHT = 1;
33 private static final int S_DAY_TRANSPARENT = 0x2;
34 private static final int S_NIGHT_TRANSPARENT = 0x4;
35 private static final int S_HAS_BITMAP = 0x8;
36
37 private int numberOfColours;
38 private int numberOfSolidColours;
39
40 private boolean hasBitmap;
41 private boolean hasBorder;
42 private final List<RgbWithTag> colours = new ArrayList<>();
43 private final Map<String, Integer> indexMap = new HashMap<>();
44
45 private char width;
46 private char height;
47 private char charsPerPixel;
48
49 private boolean simple = true;
50 private char colourMode;
51
52 /**
53 * Add a colour for this element.
54 * @param tag The xpm tag that represents the colour.
55 * @param rgb The actual colour.
56 */
57 public void addColour(String tag, Rgb rgb) {
58 RgbWithTag cwt = new RgbWithTag(tag, rgb);
59 colours.add(cwt);
60 }
61
62 /**
63 * Add a transparent colour. Convenience routine.
64 */
65 public void addTransparent(String colourTag) {
66 addColour(colourTag, new Rgb(0, 0, 0, 0));
67 }
68
69 public void setHasBitmap(boolean hasBitmap) {
70 this.hasBitmap = hasBitmap;
71 }
72
73 /**
74 * The colour scheme in use. This is a bitmask that has the following bits:
75 * 0 - Has night colour
76 * 1 - day background colour is transparent
77 * 2 - night background colour is transparent
78 * 3 - has bitmap
79 *
80 * If there is no night colour, then set the night background colour bit to be the same as
81 * the day one.
82 *
83 * @return The colour scheme bitmask. The term colour scheme is historical, it doesn't really
84 * describe it.
85 */
86 public int getColourScheme() {
87 if (numberOfColours == 0)
88 numberOfColours = colours.size();
89
90 int scheme = 0;
91 if (hasBitmap)
92 scheme |= S_HAS_BITMAP;
93
94 if (numberOfColours == 4)
95 scheme |= S_NIGHT;
96
97 if (!hasBitmap && !hasBorder && numberOfColours == 2)
98 scheme |= S_NIGHT | S_DAY_TRANSPARENT | S_NIGHT_TRANSPARENT;
99
100 if (numberOfColours < 2 || colours.get(1).isTransparent())
101 scheme |= S_DAY_TRANSPARENT;
102 if (numberOfColours == 4 && (colours.get(3).isTransparent()))
103 scheme |= S_NIGHT_TRANSPARENT;
104
105 if ((scheme & S_NIGHT) == 0)
106 if ((scheme & S_DAY_TRANSPARENT) != 0)
107 scheme |= S_NIGHT_TRANSPARENT;
108
109 return scheme;
110 }
111
112 /**
113 * Get the number of bits per pixel that will be used in the written bitmap.
114 *
115 * This depends on the colour mode and number of colours to be represented.
116 */
117 public int getBitsPerPixel() {
118 if (simple)
119 return 1;
120
121 // number of colours includes the transparent pixel in colormode=0x10 so this
122 // works for all colour modes.
123 int nc = numberOfColours;
124 if (nc == 0)
125 return 24;
126 else if (nc < 2)
127 return 1;
128 else if (nc < 4)
129 return 2;
130 else if (nc < 16)
131 return 4;
132 else
133 return 8;
134 }
135
136 /**
137 * Write out the colours only.
138 */
139 public void write(ImgFileWriter writer) {
140 if (colourMode == 0x20) {
141 writeColours20(writer);
142 } else {
143 for (Rgb rgb : colours) {
144 if (!rgb.isTransparent())
145 rgb.write(writer, (byte) 0x10);
146 }
147 }
148 }
149
150 /**
151 * Write out the colours in the colormode=x20 case.
152 */
153 private void writeColours20(ImgFileWriter writer) {
154 BitWriter bw = new BitWriter();
155 for (Rgb rgb : colours) {
156 bw.putn(rgb.getB(), 8);
157 bw.putn(rgb.getG(), 8);
158 bw.putn(rgb.getR(), 8);
159
160 int alpha = 0xff - rgb.getA();
161 alpha = alphaRound4(alpha);
162
163 bw.putn(alpha, 4);
164 }
165 writer.put(bw.getBytes(), 0, bw.getLength());
166 }
167
168 /**
169 * Round alpha value to four bits.
170 * @param alpha The original alpha value eg 0xf0.
171 * @return Rounded alpha to four bits eg 0xe.
172 */
173 static int alphaRound4(int alpha) {
174 int top = (alpha >> 4) & 0xf;
175 int low = alpha & 0xf;
176
177 int diff = low-top;
178 if (diff > 8)
179 top++;
180 else if (diff < -8)
181 top--;
182 return top;
183 }
184
185 public int getIndex(String tag) {
186 Integer ind = indexMap.get(tag);
187
188 // If this is a simple bitmap (for line or polygon), then the foreground colour is
189 // first and so has index 0, but we want the foreground to have index 1, so reverse.
190 if (simple)
191 ind = ~ind;
192
193 return ind;
194 }
195
196 public void setWidth(int width) {
197 this.width = (char) width;
198 }
199
200 public void setHeight(int height) {
201 this.height = (char) height;
202 }
203
204 public void setNumberOfColours(int numberOfColours) {
205 this.numberOfColours = numberOfColours;
206 }
207
208 public void setCharsPerPixel(int charsPerPixel) {
209 this.charsPerPixel = (char) (charsPerPixel == 0 ? 1 : charsPerPixel);
210 }
211
212 public int getNumberOfColours() {
213 return numberOfColours;
214 }
215
216 public int getNumberOfSColoursForCM() {
217 if (colourMode == 0x10)
218 return numberOfSolidColours;
219 else
220 return numberOfColours;
221 }
222
223 public int getCharsPerPixel() {
224 return charsPerPixel;
225 }
226
227 public int getHeight() {
228 return height;
229 }
230
231 public int getWidth() {
232 return width;
233 }
234
235 public int getColourMode() {
236 return colourMode;
237 }
238
239 public void setColourMode(int colourMode) {
240 this.colourMode = (char) colourMode;
241 }
242
243 public void setSimple(boolean simple) {
244 this.simple = simple;
245 }
246
247 public void setHasBorder(boolean hasBorder) {
248 this.hasBorder = hasBorder;
249 }
250
251 /**
252 * Replace the last pixel with a pixel with the same colour components and the given
253 * alpha.
254 *
255 * This is used when the alpha value is specified separately to the colour values in the
256 * input file.
257 * @param alpha The alpha value to be added to the pixel. This is a real alpha, not a transparency.
258 */
259 public void addAlpha(int alpha) {
260 int last = colours.size();
261 RgbWithTag rgb = colours.get(last - 1);
262 rgb = new RgbWithTag(rgb, alpha);
263 colours.set(last - 1, rgb);
264 }
265
266 /**
267 * Analyse the colour pallet and normalise it.
268 *
269 * Try to work out what is required from the supplied colour pallet and set the colour mode
270 * and rearrange transparent pixels if necessary to be in the proper place.
271 *
272 * At the end we build the index from colour tag to pixel index.
273 *
274 * @param simple If this is a line or polygon.
275 * @return A string describing the validation failure.
276 */
277 public String analyseColours(boolean simple) {
278 setSimple(simple);
279
280 if (simple) {
281 // There can be up to four colours, no partial transparency, and a max of one transparent pixel
282 // in each of the day/night sections.
283
284 if (numberOfColours > 4)
285 return "Too many colours for a line or polygon";
286 if (numberOfColours == 0)
287 return "Line or polygon cannot have zero colours";
288
289 // Putting the transparent pixel first is common, so reverse if found
290 if (colours.get(0).isTransparent()) {
291 if (numberOfColours < 2)
292 return "Only colour cannot be transparent for line or polygon";
293 swapColour(0, 1);
294 }
295 if (numberOfColours > 2 && colours.get(2).isTransparent()) {
296 if (numberOfColours < 4)
297 return "Only colour cannot be transparent for line or polygon";
298 swapColour(2, 3);
299 }
300
301 // There can only be one transparent pixel per colour pair
302 if (numberOfColours > 1 && colours.get(0).isTransparent())
303 return "Both day foreground and background are transparent";
304 if (numberOfColours > 3 && colours.get(2).isTransparent())
305 return "Both night foreground and background are transparent";
306
307 } else {
308 int transIndex = 0; // index of last transparent pixel, only used when there is only one
309 int nTrans = 0; // completely transparent
310 int nAlpha = 0; // partially transparent
311 int count = 0; // total number of colours
312 for (RgbWithTag rgb : colours) {
313 if (rgb.isTransparent()) {
314 nTrans++;
315 transIndex = count;
316 }
317
318 if (rgb.getA() != 0xff && rgb.getA() != 0)
319 nAlpha++;
320 count++;
321 }
322
323 if (nAlpha > 0 || (count > 0 && count == nTrans)) {
324 // If there is any partial transparency we need colour mode 0x20
325 // Also if there is only one pixel and it is transparent, since otherwise there would be zero
326 // solid colours and that is a special case used to indicate a true colour pixmap.
327 colourMode = 0x20;
328
329 } else if (nTrans == 1) {
330 colourMode = 0x10;
331
332 // Ensure the transparent pixel is at the end
333 RgbWithTag rgb = colours.remove(transIndex);
334 colours.add(rgb);
335 }
336 }
337
338 int count = 0;
339 for (RgbWithTag rgb : colours) {
340 indexMap.put(rgb.getTag(), count++);
341 if (!rgb.isTransparent())
342 numberOfSolidColours++;
343 }
344
345 return null;
346 }
347
348 private void swapColour(int c1, int c2) {
349 RgbWithTag tmp = colours.get(c1);
350 colours.set(c1, colours.get(c2));
351 colours.set(c2, tmp);
352 }
353 }