Debian Codegraph @

netx/net/sourceforge/jnlp/tools/ico/impl/IcoHeaderEntry.java

raw
1 package net.sourceforge.jnlp.tools.ico.impl;
2
3 /*
4 Copyright (C) 2015 Red Hat, Inc.
5
6 This file is part of IcedTea.
7
8 IcedTea is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 IcedTea is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with IcedTea; see the file COPYING. If not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301 USA.
22
23 Linking this library statically or dynamically with other modules is
24 making a combined work based on this library. Thus, the terms and
25 conditions of the GNU General Public License cover the whole
26 combination.
27
28 As a special exception, the copyright holders of this library give you
29 permission to link this library with independent modules to produce an
30 executable, regardless of the license terms of these independent
31 modules, and to copy and distribute the resulting executable under
32 terms of your choice, provided that you also meet, for each linked
33 independent module, the terms and conditions of the license of that
34 module. An independent module is a module which is not derived from
35 or based on this library. If you modify this library, you may extend
36 this exception to your version of the library, but you are not
37 obligated to do so. If you do not wish to do so, delete this
38 exception statement from your version. */
39 import java.io.IOException;
40 import javax.imageio.stream.ImageInputStream;
41
42 public class IcoHeaderEntry {
43
44 private int width;
45 private int height;
46 private int colorCount;
47 private final int reserved;
48 private final int planes; //should be 1 but I met quite a lot of 0
49 private final int bitCount;
50 private final int sizeInBytes; // InfoHeader + ANDbitmap + XORbitmap
51 private final int fileOffset; //FilePos, where InfoHeader starts
52
53 public IcoHeaderEntry(ImageInputStream src) throws IOException, IcoException {
54 width = src.read();
55 height = src.read();
56 colorCount = src.read();
57 // sentence "Number of Colors (2,16, 0=256) " is form doubnet
58 //unluckily, both 0==0 and 0==256 does exists
59 //going with doubnet by default
60 if (colorCount == 0) {
61 colorCount = 256;
62 }
63 reserved = src.read();
64 planes = src.readUnsignedShort();
65 isIcoHeader();
66 bitCount = src.readUnsignedShort();
67 sizeInBytes = src.readInt();
68 fileOffset = src.readInt();
69 }
70
71 private IcoHeaderEntry(int width, int height, int colorCount, int planes, int bitCount, int sizeInBytes, int fileOffset) {
72 this.width = width;
73 this.height = height;
74 this.colorCount = colorCount;
75 this.reserved = 0;
76 this.planes = planes;
77 this.bitCount = bitCount;
78 this.sizeInBytes = sizeInBytes;
79 this.fileOffset = fileOffset;
80 }
81
82 private IcoHeaderEntry provideMonochromeHeader() {
83 //each bit in byte stores 8 pixels values
84 return new IcoHeaderEntry(width, height, 1, planes, 1, width * height / 8, fileOffset + sizeInBytes);
85 }
86
87 private void isIcoHeader() throws IcoException {
88 if (reserved != 0 || (planes != 1 && planes != 0)) {
89 throw new IcoException("Invalid header. Expected 0 and 1(0?), got " + reserved + " and " + planes);
90 }
91 }
92
93 /**
94 * @return the colorCount
95 */
96 int getColorCount() {
97 return colorCount;
98 }
99
100 /**
101 * @param colorCount the colorCount to set
102 */
103 void setColorCount(int colorCount) {
104 this.colorCount = colorCount;
105 }
106
107 /**
108 * @return the width
109 */
110 public int getWidth() {
111 return width;
112 }
113
114 /**
115 * @param width the width to set
116 */
117 void setWidth(int width) {
118 this.width = width;
119 }
120
121 /**
122 * @return the height
123 */
124 public int getHeight() {
125 return height;
126 }
127
128 /**
129 * @param height the height to set
130 */
131 void setHeight(int height) {
132 this.height = height;
133 }
134
135 int getSizeInBytes() {
136 return sizeInBytes;
137 }
138
139 int getFileOffset() {
140 return fileOffset;
141 }
142
143 }