Debian Codegraph @

src/uk/me/parabola/imgfmt/app/typ/TrueImage.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 uk.me.parabola.imgfmt.app.BitWriter;
16 import uk.me.parabola.imgfmt.app.ImgFileWriter;
17
18 /**
19 * A true colour image.
20 *
21 * The image is represented by an array of int, with each int in RGBA format.
22 *
23 * @author Steve Ratcliffe
24 */
25 public class TrueImage implements Image {
26 private final ColourInfo colourInfo;
27 private final int[] image;
28
29 // If this is mode 16, then the transparent colour is set.
30 private int transparentPixel;
31
32 public TrueImage(ColourInfo colourInfo, int[] image) {
33 analyzeColours(image, colourInfo);
34 this.colourInfo = colourInfo;
35 this.image = image;
36 }
37
38 /**
39 * Write out the image. It is a set of pixel values that are full RGB values, rather than
40 * table driven as in the other image type. If the colour mode is 32 the colours have an
41 * extra 4 bit opacity value following.
42 *
43 * If the colour mode is 16, then the transparent pixel is written just before the image
44 * itself.
45 */
46 public void write(ImgFileWriter writer) {
47 int width = colourInfo.getWidth();
48 int height = colourInfo.getHeight();
49
50 int mode = colourInfo.getColourMode();
51
52 // For mode 16, the transparent pixel precedes the pixmap data.
53 if (mode == 16) {
54 writer.put1u(transparentPixel>>8);
55 writer.put1u(transparentPixel>>16);
56 writer.put1u(transparentPixel>>24);
57 }
58
59 boolean hasAlpha = mode == 32;
60
61 // Unlike the xpm based images, the true-colour image format does not appear to
62 // have any padding so write as a continuous block.
63 BitWriter bitWriter = new BitWriter();
64 for (int h = 0; h < height; h++) {
65
66 for (int w = 0; w < width; w++) {
67 int col = image[h * width + w];
68
69 bitWriter.putn(col>>8 & 0xff, 8);
70 bitWriter.putn(col>>16 & 0xff, 8);
71 bitWriter.putn(col>>24 & 0xff, 8);
72
73 if (hasAlpha) {
74 int alpha = 0xff - (col & 0xff);
75 alpha = ColourInfo.alphaRound4(alpha);
76 bitWriter.putn(alpha, 4);
77 }
78 }
79 }
80 writer.put(bitWriter.getBytes(), 0, bitWriter.getLength());
81 }
82
83 /**
84 * Analyze the colours and determine if this should be a mode 16 or 32 image.
85 *
86 * By default it will be a mode 0 image. If there is any transparency the appropriate
87 * colour mode will be selected.
88 *
89 * @param image An images as an array of integers. Each integer is a colour in RGBA format.
90 * @param colourInfo The colour mode will be set in this.
91 */
92 private void analyzeColours(int[] image, ColourInfo colourInfo) {
93 boolean hasTransparent = false;
94 boolean hasAlpha = false;
95
96 int nPixels = colourInfo.getWidth() * colourInfo.getHeight();
97 for (int i = 0; i < nPixels; i++) {
98 int col = image[i];
99 int a = col & 0xff;
100 if (a == 0) {
101 // Completely transparent, change all transparent pixels to the same value
102 if (hasTransparent)
103 image[i] = transparentPixel;
104 else
105 transparentPixel = image[i];
106 hasTransparent = true;
107 } else if (a < 255) {
108 // Partially transparent
109 hasAlpha = true;
110 }
111 }
112
113 if (hasAlpha)
114 colourInfo.setColourMode(32);
115 else if (hasTransparent)
116 colourInfo.setColourMode(16);
117 }
118 }