KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > image > PngPlteChunk


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 package org.eclipse.swt.internal.image;
12
13
14 import org.eclipse.swt.*;
15 import org.eclipse.swt.graphics.*;
16
17 class PngPlteChunk extends PngChunk {
18     
19     int paletteSize;
20
21 PngPlteChunk(PaletteData palette) {
22     super(palette.getRGBs().length * 3);
23     paletteSize = length / 3;
24     setType(TYPE_PLTE);
25     setPaletteData(palette);
26     setCRC(computeCRC());
27 }
28
29 PngPlteChunk(byte[] reference){
30     super(reference);
31     paletteSize = length / 3;
32 }
33
34 int getChunkType() {
35     return CHUNK_PLTE;
36 }
37
38 /**
39  * Get the number of colors in this palette.
40  */

41 int getPaletteSize() {
42     return paletteSize;
43 }
44
45 /**
46  * Get a PaletteData object representing the colors
47  * stored in this PLTE chunk.
48  * The result should be cached as the PLTE chunk
49  * does not store the palette data created.
50  */

51 PaletteData getPaletteData() {
52     RGB[] rgbs = new RGB[paletteSize];
53 // int start = DATA_OFFSET;
54
// int end = DATA_OFFSET + length;
55
for (int i = 0; i < rgbs.length; i++) {
56         int offset = DATA_OFFSET + (i * 3);
57         int red = reference[offset] & 0xFF;
58         int green = reference[offset + 1] & 0xFF;
59         int blue = reference[offset + 2] & 0xFF;
60         rgbs[i] = new RGB(red, green, blue);
61     }
62     return new PaletteData(rgbs);
63 }
64
65 /**
66  * Set the data of a PLTE chunk to the colors
67  * stored in the specified PaletteData object.
68  */

69 void setPaletteData(PaletteData palette) {
70     RGB[] rgbs = palette.getRGBs();
71     for (int i = 0; i < rgbs.length; i++) {
72         int offset = DATA_OFFSET + (i * 3);
73         reference[offset] = (byte) rgbs[i].red;
74         reference[offset + 1] = (byte) rgbs[i].green;
75         reference[offset + 2] = (byte) rgbs[i].blue;
76     }
77 }
78
79 /**
80  * Answer whether the chunk is a valid PLTE chunk.
81  */

82 void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
83     // A PLTE chunk is invalid if no IHDR has been read or if any PLTE,
84
// IDAT, or IEND chunk has been read.
85
if (!readState.readIHDR
86         || readState.readPLTE
87         || readState.readTRNS
88         || readState.readIDAT
89         || readState.readIEND)
90     {
91         SWT.error(SWT.ERROR_INVALID_IMAGE);
92     } else {
93         readState.readPLTE = true;
94     }
95     
96     super.validate(readState, headerChunk);
97     
98     // Palettes cannot be included in grayscale images.
99
//
100
// Note: just ignore the palette.
101
// if (!headerChunk.getCanHavePalette()) SWT.error(SWT.ERROR_INVALID_IMAGE);
102

103     // Palette chunks' data fields must be event multiples
104
// of 3. Each 3-byte group represents an RGB value.
105
if (getLength() % 3 != 0) SWT.error(SWT.ERROR_INVALID_IMAGE);
106     
107     // Palettes cannot have more entries than 2^bitDepth
108
// where bitDepth is the bit depth of the image given
109
// in the IHDR chunk.
110
if (1 << headerChunk.getBitDepth() < paletteSize) {
111         SWT.error(SWT.ERROR_INVALID_IMAGE);
112     }
113     
114     // Palettes cannot have more than 256 entries.
115
if (256 < paletteSize) SWT.error(SWT.ERROR_INVALID_IMAGE);
116 }
117
118 void contributeToString(StringBuffer JavaDoc buffer) {
119     buffer.append("\n\tPalette size:");
120     buffer.append(paletteSize);
121 }
122
123 }
124
Popular Tags