25
|
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 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.internal.image.PngPlteChunk;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17
|
|
18 import org.eclipse.swt.SWT;
|
|
19 import org.eclipse.swt.graphics.PaletteData;
|
|
20 import org.eclipse.swt.graphics.RGB;
|
|
21 import org.eclipse.swt.internal.image.PngChunk;
|
|
22 import org.eclipse.swt.internal.image.PngFileReadState;
|
|
23 import org.eclipse.swt.internal.image.PngIhdrChunk;
|
|
24
|
|
25
|
|
26 class PngPlteChunk : PngChunk {
|
|
27
|
|
28 int paletteSize;
|
|
29
|
|
30 this(PaletteData palette) {
|
|
31 super(palette.getRGBs().length * 3);
|
|
32 paletteSize = length / 3;
|
|
33 setType(TYPE_PLTE);
|
|
34 setPaletteData(palette);
|
|
35 setCRC(computeCRC());
|
|
36 }
|
|
37
|
|
38 this(byte[] reference){
|
|
39 super(reference);
|
|
40 paletteSize = length / 3;
|
|
41 }
|
|
42
|
|
43 override int getChunkType() {
|
|
44 return CHUNK_PLTE;
|
|
45 }
|
|
46
|
|
47 /**
|
|
48 * Get the number of colors in this palette.
|
|
49 */
|
|
50 int getPaletteSize() {
|
|
51 return paletteSize;
|
|
52 }
|
|
53
|
|
54 /**
|
|
55 * Get a PaletteData object representing the colors
|
|
56 * stored in this PLTE chunk.
|
|
57 * The result should be cached as the PLTE chunk
|
|
58 * does not store the palette data created.
|
|
59 */
|
|
60 PaletteData getPaletteData() {
|
|
61 RGB[] rgbs = new RGB[paletteSize];
|
|
62 // int start = DATA_OFFSET;
|
|
63 // int end = DATA_OFFSET + length;
|
|
64 for (int i = 0; i < rgbs.length; i++) {
|
|
65 int offset = DATA_OFFSET + (i * 3);
|
|
66 int red = reference[offset] & 0xFF;
|
|
67 int green = reference[offset + 1] & 0xFF;
|
|
68 int blue = reference[offset + 2] & 0xFF;
|
|
69 rgbs[i] = new RGB(red, green, blue);
|
|
70 }
|
|
71 return new PaletteData(rgbs);
|
|
72 }
|
|
73
|
|
74 /**
|
|
75 * Set the data of a PLTE chunk to the colors
|
|
76 * stored in the specified PaletteData object.
|
|
77 */
|
|
78 void setPaletteData(PaletteData palette) {
|
|
79 RGB[] rgbs = palette.getRGBs();
|
|
80 for (int i = 0; i < rgbs.length; i++) {
|
|
81 int offset = DATA_OFFSET + (i * 3);
|
|
82 reference[offset] = cast(byte) rgbs[i].red;
|
|
83 reference[offset + 1] = cast(byte) rgbs[i].green;
|
|
84 reference[offset + 2] = cast(byte) rgbs[i].blue;
|
|
85 }
|
|
86 }
|
|
87
|
|
88 /**
|
|
89 * Answer whether the chunk is a valid PLTE chunk.
|
|
90 */
|
|
91 override void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
|
|
92 // A PLTE chunk is invalid if no IHDR has been read or if any PLTE,
|
|
93 // IDAT, or IEND chunk has been read.
|
|
94 if (!readState.readIHDR
|
|
95 || readState.readPLTE
|
|
96 || readState.readTRNS
|
|
97 || readState.readIDAT
|
|
98 || readState.readIEND)
|
|
99 {
|
|
100 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
101 } else {
|
|
102 readState.readPLTE = true;
|
|
103 }
|
|
104
|
|
105 super.validate(readState, headerChunk);
|
|
106
|
|
107 // Palettes cannot be included in grayscale images.
|
|
108 //
|
|
109 // Note: just ignore the palette.
|
|
110 // if (!headerChunk.getCanHavePalette()) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
111
|
|
112 // Palette chunks' data fields must be event multiples
|
|
113 // of 3. Each 3-byte group represents an RGB value.
|
|
114 if (getLength() % 3 !is 0) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
115
|
|
116 // Palettes cannot have more entries than 2^bitDepth
|
|
117 // where bitDepth is the bit depth of the image given
|
|
118 // in the IHDR chunk.
|
|
119 if (1 << headerChunk.getBitDepth() < paletteSize) {
|
|
120 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
121 }
|
|
122
|
|
123 // Palettes cannot have more than 256 entries.
|
|
124 if (256 < paletteSize) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
125 }
|
|
126
|
|
127 override String contributeToString() {
|
|
128 return Format("\n\tPalette size:{}", paletteSize );
|
|
129 }
|
|
130
|
|
131 }
|