annotate dwt/internal/image/PngIhdrChunk.d @ 2:57151e2793a2

More common modules from dwt-linux
author Frank Benoit <benoit@tionex.de>
date Wed, 23 Jan 2008 12:01:46 +0100
parents
children 9a64a7781bab
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
1 /*******************************************************************************
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
2 * Copyright (c) 2000, 2006 IBM Corporation and others.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
3 * All rights reserved. This program and the accompanying materials
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
4 * are made available under the terms of the Eclipse Public License v1.0
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
5 * which accompanies this distribution, and is available at
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
6 * http://www.eclipse.org/legal/epl-v10.html
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
7 *
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
8 * Contributors:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
9 * IBM Corporation - initial API and implementation
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
10 * Port to the D programming language:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
11 * Frank Benoit <benoit@tionex.de>
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
12 *******************************************************************************/
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
13 module dwt.internal.image.PngIhdrChunk;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
14
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
15
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
16 import dwt.DWT;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
17 import dwt.graphics.PaletteData;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
18 import dwt.graphics.RGB;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
19 import dwt.internal.image.PngFileReadState;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
20 import dwt.internal.image.PngIhdrChunk;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
21 import dwt.internal.image.PngChunk;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
22 import tango.text.convert.Format;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
23
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
24 class PngIhdrChunk : PngChunk {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
25 static const int IHDR_DATA_LENGTH = 13;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
26
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
27 static const int WIDTH_DATA_OFFSET = DATA_OFFSET + 0;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
28 static const int HEIGHT_DATA_OFFSET = DATA_OFFSET + 4;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
29 static const int BIT_DEPTH_OFFSET = DATA_OFFSET + 8;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
30 static const int COLOR_TYPE_OFFSET = DATA_OFFSET + 9;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
31 static const int COMPRESSION_METHOD_OFFSET = DATA_OFFSET + 10;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
32 static const int FILTER_METHOD_OFFSET = DATA_OFFSET + 11;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
33 static const int INTERLACE_METHOD_OFFSET = DATA_OFFSET + 12;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
34
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
35 static const byte COLOR_TYPE_GRAYSCALE = 0;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
36 static const byte COLOR_TYPE_RGB = 2;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
37 static const byte COLOR_TYPE_PALETTE = 3;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
38 static const byte COLOR_TYPE_GRAYSCALE_WITH_ALPHA = 4;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
39 static const byte COLOR_TYPE_RGB_WITH_ALPHA = 6;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
40
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
41 static const int INTERLACE_METHOD_NONE = 0;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
42 static const int INTERLACE_METHOD_ADAM7 = 1;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
43
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
44 static const int FILTER_NONE = 0;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
45 static const int FILTER_SUB = 1;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
46 static const int FILTER_UP = 2;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
47 static const int FILTER_AVERAGE = 3;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
48 static const int FILTER_PAETH = 4;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
49
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
50 static const byte[] ValidBitDepths = [ cast(byte)1, 2, 4, 8, 16];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
51 static const byte[] ValidColorTypes = [ cast(byte)0, 2, 3, 4, 6];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
52
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
53 int width, height;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
54 byte bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
55
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
56 this(int width, int height, byte bitDepth, byte colorType, byte compressionMethod, byte filterMethod, byte interlaceMethod) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
57 super(IHDR_DATA_LENGTH);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
58 setType(TYPE_IHDR);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
59 setWidth(width);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
60 setHeight(height);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
61 setBitDepth(bitDepth);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
62 setColorType(colorType);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
63 setCompressionMethod(compressionMethod);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
64 setFilterMethod(filterMethod);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
65 setInterlaceMethod(interlaceMethod);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
66 setCRC(computeCRC());
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
67 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
68
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
69 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
70 * Construct a PNGChunk using the reference bytes
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
71 * given.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
72 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
73 this(byte[] reference) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
74 super(reference);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
75 if (reference.length <= IHDR_DATA_LENGTH) DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
76 width = getInt32(WIDTH_DATA_OFFSET);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
77 height = getInt32(HEIGHT_DATA_OFFSET);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
78 bitDepth = reference[BIT_DEPTH_OFFSET];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
79 colorType = reference[COLOR_TYPE_OFFSET];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
80 compressionMethod = reference[COMPRESSION_METHOD_OFFSET];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
81 filterMethod = reference[FILTER_METHOD_OFFSET];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
82 interlaceMethod = reference[INTERLACE_METHOD_OFFSET];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
83 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
84
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
85 int getChunkType() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
86 return CHUNK_IHDR;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
87 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
88
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
89 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
90 * Get the image's width in pixels.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
91 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
92 int getWidth() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
93 return width;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
94 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
95
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
96 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
97 * Set the image's width in pixels.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
98 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
99 void setWidth(int value) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
100 setInt32(WIDTH_DATA_OFFSET, value);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
101 width = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
102 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
103
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
104 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
105 * Get the image's height in pixels.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
106 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
107 int getHeight() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
108 return height;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
109 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
110
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
111 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
112 * Set the image's height in pixels.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
113 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
114 void setHeight(int value) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
115 setInt32(HEIGHT_DATA_OFFSET, value);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
116 height = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
117 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
118
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
119 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
120 * Get the image's bit depth.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
121 * This is limited to the values 1, 2, 4, 8, or 16.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
122 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
123 byte getBitDepth() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
124 return bitDepth;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
125 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
126
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
127 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
128 * Set the image's bit depth.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
129 * This is limited to the values 1, 2, 4, 8, or 16.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
130 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
131 void setBitDepth(byte value) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
132 reference[BIT_DEPTH_OFFSET] = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
133 bitDepth = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
134 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
135
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
136 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
137 * Get the image's color type.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
138 * This is limited to the values:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
139 * 0 - Grayscale image.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
140 * 2 - RGB triple.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
141 * 3 - Palette.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
142 * 4 - Grayscale with Alpha channel.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
143 * 6 - RGB with Alpha channel.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
144 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
145 byte getColorType() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
146 return colorType;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
147 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
148
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
149 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
150 * Set the image's color type.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
151 * This is limited to the values:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
152 * 0 - Grayscale image.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
153 * 2 - RGB triple.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
154 * 3 - Palette.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
155 * 4 - Grayscale with Alpha channel.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
156 * 6 - RGB with Alpha channel.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
157 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
158 void setColorType(byte value) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
159 reference[COLOR_TYPE_OFFSET] = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
160 colorType = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
161 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
162
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
163 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
164 * Get the image's compression method.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
165 * This value must be 0.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
166 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
167 byte getCompressionMethod() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
168 return compressionMethod;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
169 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
170
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
171 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
172 * Set the image's compression method.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
173 * This value must be 0.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
174 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
175 void setCompressionMethod(byte value) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
176 reference[COMPRESSION_METHOD_OFFSET] = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
177 compressionMethod = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
178 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
179
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
180 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
181 * Get the image's filter method.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
182 * This value must be 0.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
183 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
184 byte getFilterMethod() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
185 return filterMethod;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
186 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
187
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
188 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
189 * Set the image's filter method.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
190 * This value must be 0.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
191 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
192 void setFilterMethod(byte value) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
193 reference[FILTER_METHOD_OFFSET] = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
194 filterMethod = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
195 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
196
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
197 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
198 * Get the image's interlace method.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
199 * This value is limited to:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
200 * 0 - No interlacing used.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
201 * 1 - Adam7 interlacing used.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
202 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
203 byte getInterlaceMethod() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
204 return interlaceMethod;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
205 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
206
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
207 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
208 * Set the image's interlace method.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
209 * This value is limited to:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
210 * 0 - No interlacing used.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
211 * 1 - Adam7 interlacing used.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
212 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
213 void setInterlaceMethod(byte value) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
214 reference[INTERLACE_METHOD_OFFSET] = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
215 interlaceMethod = value;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
216 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
217
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
218 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
219 * Answer whether the chunk is a valid IHDR chunk.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
220 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
221 void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
222 // An IHDR chunk is invalid if any other chunk has
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
223 // been read.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
224 if (readState.readIHDR
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
225 || readState.readPLTE
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
226 || readState.readIDAT
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
227 || readState.readIEND)
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
228 {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
229 DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
230 } else {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
231 readState.readIHDR = true;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
232 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
233
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
234 super.validate(readState, headerChunk);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
235
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
236 if (length !is IHDR_DATA_LENGTH) DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
237 if (compressionMethod !is 0) DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
238 if (interlaceMethod !is INTERLACE_METHOD_NONE &&
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
239 interlaceMethod !is INTERLACE_METHOD_ADAM7) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
240 DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
241 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
242
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
243 bool colorTypeIsValid = false;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
244 for (int i = 0; i < ValidColorTypes.length; i++) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
245 if (ValidColorTypes[i] is colorType) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
246 colorTypeIsValid = true;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
247 break;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
248 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
249 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
250 if (!colorTypeIsValid) DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
251
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
252 bool bitDepthIsValid = false;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
253 for (int i = 0; i < ValidBitDepths.length; i++) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
254 if (ValidBitDepths[i] is bitDepth) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
255 bitDepthIsValid = true;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
256 break;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
257 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
258 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
259 if (!bitDepthIsValid) DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
260
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
261 if ((colorType is COLOR_TYPE_RGB
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
262 || colorType is COLOR_TYPE_RGB_WITH_ALPHA
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
263 || colorType is COLOR_TYPE_GRAYSCALE_WITH_ALPHA)
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
264 && bitDepth < 8)
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
265 {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
266 DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
267 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
268
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
269 if (colorType is COLOR_TYPE_PALETTE && bitDepth > 8) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
270 DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
271 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
272 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
273
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
274 char[] getColorTypeString() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
275 switch (colorType) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
276 case COLOR_TYPE_GRAYSCALE: return "Grayscale";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
277 case COLOR_TYPE_RGB: return "RGB";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
278 case COLOR_TYPE_PALETTE: return "Palette";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
279 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA: return "Grayscale with Alpha";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
280 case COLOR_TYPE_RGB_WITH_ALPHA: return "RGB with Alpha";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
281 default: return "Unknown - " ~ cast(char)colorType;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
282 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
283 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
284
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
285 char[] getFilterMethodString() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
286 switch (filterMethod) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
287 case FILTER_NONE: return "None";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
288 case FILTER_SUB: return "Sub";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
289 case FILTER_UP: return "Up";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
290 case FILTER_AVERAGE: return "Average";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
291 case FILTER_PAETH: return "Paeth";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
292 default: return "Unknown";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
293 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
294 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
295
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
296 char[] getInterlaceMethodString() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
297 switch (interlaceMethod) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
298 case INTERLACE_METHOD_NONE: return "Not Interlaced";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
299 case INTERLACE_METHOD_ADAM7: return "Interlaced - ADAM7";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
300 default: return "Unknown";
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
301 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
302 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
303
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
304 override char[] contributeToString() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
305 return Format( "\n\tWidth: {}\n\tHeight: {}\n\tBit Depth: {}\n\tColor Type: {}\n\tCompression Method: {}\n\tFilter Method: {}\n\tInterlace Method: {}",
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
306 width, height, bitDepth, getColorTypeString(), compressionMethod, getFilterMethodString(), getInterlaceMethodString() );
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
307 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
308
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
309 bool getMustHavePalette() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
310 return colorType is COLOR_TYPE_PALETTE;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
311 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
312
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
313 bool getCanHavePalette() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
314 return colorType !is COLOR_TYPE_GRAYSCALE &&
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
315 colorType !is COLOR_TYPE_GRAYSCALE_WITH_ALPHA;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
316 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
317
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
318 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
319 * Answer the pixel size in bits based on the color type
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
320 * and bit depth.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
321 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
322 int getBitsPerPixel() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
323 switch (colorType) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
324 case COLOR_TYPE_RGB_WITH_ALPHA:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
325 return 4 * bitDepth;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
326 case COLOR_TYPE_RGB:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
327 return 3 * bitDepth;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
328 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
329 return 2 * bitDepth;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
330 case COLOR_TYPE_GRAYSCALE:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
331 case COLOR_TYPE_PALETTE:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
332 return bitDepth;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
333 default:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
334 DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
335 return 0;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
336 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
337 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
338
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
339 /**
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
340 * Answer the pixel size in bits based on the color type
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
341 * and bit depth.
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
342 */
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
343 int getSwtBitsPerPixel() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
344 switch (colorType) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
345 case COLOR_TYPE_RGB_WITH_ALPHA:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
346 case COLOR_TYPE_RGB:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
347 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
348 return 24;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
349 case COLOR_TYPE_GRAYSCALE:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
350 case COLOR_TYPE_PALETTE:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
351 return Math.min(bitDepth, 8);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
352 default:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
353 DWT.error(DWT.ERROR_INVALID_IMAGE);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
354 return 0;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
355 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
356 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
357
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
358 int getFilterByteOffset() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
359 if (bitDepth < 8) return 1;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
360 return getBitsPerPixel() / 8;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
361 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
362
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
363 bool usesDirectColor() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
364 switch (colorType) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
365 case COLOR_TYPE_GRAYSCALE:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
366 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
367 case COLOR_TYPE_RGB:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
368 case COLOR_TYPE_RGB_WITH_ALPHA:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
369 return true;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
370 default:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
371 return false;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
372 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
373 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
374
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
375 PaletteData createGrayscalePalette() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
376 int depth = Math.min(bitDepth, 8);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
377 int max = (1 << depth) - 1;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
378 int delta = 255 / max;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
379 int gray = 0;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
380 RGB[] rgbs = new RGB[max + 1];
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
381 for (int i = 0; i <= max; i++) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
382 rgbs[i] = new RGB(gray, gray, gray);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
383 gray += delta;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
384 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
385 return new PaletteData(rgbs);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
386 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
387
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
388 PaletteData getPaletteData() {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
389 switch (colorType) {
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
390 case COLOR_TYPE_GRAYSCALE:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
391 return createGrayscalePalette();
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
392 case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
393 case COLOR_TYPE_RGB:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
394 case COLOR_TYPE_RGB_WITH_ALPHA:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
395 return new PaletteData(0xFF0000, 0xFF00, 0xFF);
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
396 default:
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
397 return null;
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
398 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
399 }
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
400
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
401
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
402
57151e2793a2 More common modules from dwt-linux
Frank Benoit <benoit@tionex.de>
parents:
diff changeset
403 }