Mercurial > projects > dwt-mac
comparison dwt/internal/image/PngPlteChunk.d @ 0:380af2bdd8e5
Upload of whole dwt tree
author | Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com> |
---|---|
date | Sat, 09 Aug 2008 17:00:02 +0200 |
parents | |
children | 1a8b3cb347e0 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:380af2bdd8e5 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2006 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 *******************************************************************************/ | |
11 module dwt.internal.image; | |
12 | |
13 | |
14 import dwt.DWT; | |
15 import dwt.graphics.PaletteData; | |
16 import dwt.graphics.RGB; | |
17 | |
18 class PngPlteChunk : PngChunk { | |
19 | |
20 int paletteSize; | |
21 | |
22 PngPlteChunk(PaletteData palette) { | |
23 super(palette.getRGBs().length * 3); | |
24 paletteSize = length / 3; | |
25 setType(TYPE_PLTE); | |
26 setPaletteData(palette); | |
27 setCRC(computeCRC()); | |
28 } | |
29 | |
30 PngPlteChunk(byte[] reference){ | |
31 super(reference); | |
32 paletteSize = length / 3; | |
33 } | |
34 | |
35 int getChunkType() { | |
36 return CHUNK_PLTE; | |
37 } | |
38 | |
39 /** | |
40 * Get the number of colors in this palette. | |
41 */ | |
42 int getPaletteSize() { | |
43 return paletteSize; | |
44 } | |
45 | |
46 /** | |
47 * Get a PaletteData object representing the colors | |
48 * stored in this PLTE chunk. | |
49 * The result should be cached as the PLTE chunk | |
50 * does not store the palette data created. | |
51 */ | |
52 PaletteData getPaletteData() { | |
53 RGB[] rgbs = new RGB[paletteSize]; | |
54 // int start = DATA_OFFSET; | |
55 // int end = DATA_OFFSET + length; | |
56 for (int i = 0; i < rgbs.length; i++) { | |
57 int offset = DATA_OFFSET + (i * 3); | |
58 int red = reference[offset] & 0xFF; | |
59 int green = reference[offset + 1] & 0xFF; | |
60 int blue = reference[offset + 2] & 0xFF; | |
61 rgbs[i] = new RGB(red, green, blue); | |
62 } | |
63 return new PaletteData(rgbs); | |
64 } | |
65 | |
66 /** | |
67 * Set the data of a PLTE chunk to the colors | |
68 * stored in the specified PaletteData object. | |
69 */ | |
70 void setPaletteData(PaletteData palette) { | |
71 RGB[] rgbs = palette.getRGBs(); | |
72 for (int i = 0; i < rgbs.length; i++) { | |
73 int offset = DATA_OFFSET + (i * 3); | |
74 reference[offset] = (byte) rgbs[i].red; | |
75 reference[offset + 1] = (byte) rgbs[i].green; | |
76 reference[offset + 2] = (byte) rgbs[i].blue; | |
77 } | |
78 } | |
79 | |
80 /** | |
81 * Answer whether the chunk is a valid PLTE chunk. | |
82 */ | |
83 void validate(PngFileReadState readState, PngIhdrChunk headerChunk) { | |
84 // A PLTE chunk is invalid if no IHDR has been read or if any PLTE, | |
85 // IDAT, or IEND chunk has been read. | |
86 if (!readState.readIHDR | |
87 || readState.readPLTE | |
88 || readState.readTRNS | |
89 || readState.readIDAT | |
90 || readState.readIEND) | |
91 { | |
92 DWT.error(DWT.ERROR_INVALID_IMAGE); | |
93 } else { | |
94 readState.readPLTE = true; | |
95 } | |
96 | |
97 super.validate(readState, headerChunk); | |
98 | |
99 // Palettes cannot be included in grayscale images. | |
100 // | |
101 // Note: just ignore the palette. | |
102 // if (!headerChunk.getCanHavePalette()) DWT.error(DWT.ERROR_INVALID_IMAGE); | |
103 | |
104 // Palette chunks' data fields must be event multiples | |
105 // of 3. Each 3-byte group represents an RGB value. | |
106 if (getLength() % 3 !is 0) DWT.error(DWT.ERROR_INVALID_IMAGE); | |
107 | |
108 // Palettes cannot have more entries than 2^bitDepth | |
109 // where bitDepth is the bit depth of the image given | |
110 // in the IHDR chunk. | |
111 if (1 << headerChunk.getBitDepth() < paletteSize) { | |
112 DWT.error(DWT.ERROR_INVALID_IMAGE); | |
113 } | |
114 | |
115 // Palettes cannot have more than 256 entries. | |
116 if (256 < paletteSize) DWT.error(DWT.ERROR_INVALID_IMAGE); | |
117 } | |
118 | |
119 void contributeToString(StringBuffer buffer) { | |
120 buffer.append("\n\tPalette size:"); | |
121 buffer.append(paletteSize); | |
122 } | |
123 | |
124 } |