1 11 package org.eclipse.swt.internal.image; 12 13 14 import org.eclipse.swt.*; 15 import org.eclipse.swt.graphics.*; 16 import java.io.*; 17 18 22 final class TIFFFileFormat extends FileFormat { 23 24 boolean isFileFormat(LEDataInputStream stream) { 25 try { 26 byte[] header = new byte[4]; 27 stream.read(header); 28 stream.unread(header); 29 if (header[0] != header[1]) return false; 30 if (!(header[0] == 0x49 && header[2] == 42 && header[3] == 0) && 31 !(header[0] == 0x4d && header[2] == 0 && header[3] == 42)) { 32 return false; 33 } 34 return true; 35 } catch (Exception e) { 36 return false; 37 } 38 } 39 40 ImageData[] loadFromByteStream() { 41 byte[] header = new byte[8]; 42 boolean isLittleEndian; 43 ImageData[] images = new ImageData[0]; 44 TIFFRandomFileAccess file = new TIFFRandomFileAccess(inputStream); 45 try { 46 file.read(header); 47 if (header[0] != header[1]) SWT.error(SWT.ERROR_INVALID_IMAGE); 48 if (!(header[0] == 0x49 && header[2] == 42 && header[3] == 0) && 49 !(header[0] == 0x4d && header[2] == 0 && header[3] == 42)) { 50 SWT.error(SWT.ERROR_INVALID_IMAGE); 51 } 52 isLittleEndian = header[0] == 0x49; 53 int offset = isLittleEndian ? 54 (header[4] & 0xFF) | ((header[5] & 0xFF) << 8) | ((header[6] & 0xFF) << 16) | ((header[7] & 0xFF) << 24) : 55 (header[7] & 0xFF) | ((header[6] & 0xFF) << 8) | ((header[5] & 0xFF) << 16) | ((header[4] & 0xFF) << 24); 56 file.seek(offset); 57 TIFFDirectory directory = new TIFFDirectory(file, isLittleEndian, loader); 58 ImageData image = directory.read(); 59 60 images = new ImageData[] {image}; 61 } catch (IOException e) { 62 SWT.error(SWT.ERROR_IO, e); 63 } 64 return images; 65 } 66 67 void unloadIntoByteStream(ImageLoader loader) { 68 70 ImageData image = loader.data[0]; 71 TIFFDirectory directory = new TIFFDirectory(image); 72 try { 73 directory.writeToStream(outputStream); 74 } catch (IOException e) { 75 SWT.error(SWT.ERROR_IO, e); 76 } 77 } 78 79 } 80 | Popular Tags |