root/dwt/internal/image/PngPlteChunk.d

Revision 314:0e2b4fed7a0f, 3.8 kB (checked in by Frank Benoit <benoit@tionex.de>, 3 months ago)

sync dwt/internal/image with dwt-linux

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