1 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 41 int getPaletteSize() { 42 return paletteSize; 43 } 44 45 51 PaletteData getPaletteData() { 52 RGB[] rgbs = new RGB[paletteSize]; 53 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 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 82 void validate(PngFileReadState readState, PngIhdrChunk headerChunk) { 83 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 103 if (getLength() % 3 != 0) SWT.error(SWT.ERROR_INVALID_IMAGE); 106 107 if (1 << headerChunk.getBitDepth() < paletteSize) { 111 SWT.error(SWT.ERROR_INVALID_IMAGE); 112 } 113 114 if (256 < paletteSize) SWT.error(SWT.ERROR_INVALID_IMAGE); 116 } 117 118 void contributeToString(StringBuffer buffer) { 119 buffer.append("\n\tPalette size:"); 120 buffer.append(paletteSize); 121 } 122 123 } 124 | Popular Tags |