comparison dwt/internal/image/GIFFileFormat.d @ 213:36f5cb12e1a2

Update to SWT 3.4M7
author Frank Benoit <benoit@tionex.de>
date Sat, 17 May 2008 17:34:28 +0200
parents ab60f3309436
children fd9c62a2998e
comparison
equal deleted inserted replaced
212:ab60f3309436 213:36f5cb12e1a2
20 import dwt.DWT; 20 import dwt.DWT;
21 import dwt.graphics.ImageData; 21 import dwt.graphics.ImageData;
22 import dwt.graphics.ImageLoaderEvent; 22 import dwt.graphics.ImageLoaderEvent;
23 import dwt.graphics.ImageLoader; 23 import dwt.graphics.ImageLoader;
24 import tango.core.Exception; 24 import tango.core.Exception;
25 import dwt.dwthelper.System;
26 import dwt.dwthelper.utils; 25 import dwt.dwthelper.utils;
26
27 27
28 final class GIFFileFormat : FileFormat { 28 final class GIFFileFormat : FileFormat {
29 String signature; 29 String signature;
30 int screenWidth, screenHeight, backgroundPixel, bitsPerPixel, defaultDepth; 30 int screenWidth, screenHeight, backgroundPixel, bitsPerPixel, defaultDepth;
31 int disposalMethod = 0; 31 int disposalMethod = 0;
60 override bool isFileFormat(LEDataInputStream stream) { 60 override bool isFileFormat(LEDataInputStream stream) {
61 try { 61 try {
62 byte[3] signature; 62 byte[3] signature;
63 stream.read(signature); 63 stream.read(signature);
64 stream.unread(signature); 64 stream.unread(signature);
65 return cast(String)signature == "GIF"; //$NON-NLS-1$ 65 return signature[0] is 'G' && signature[1] is 'I' && signature[2] is 'F';
66 } catch (Exception e) { 66 } catch (Exception e) {
67 return false; 67 return false;
68 } 68 }
69 } 69 }
70 70
71 /** 71 /**
72 * Load the GIF image(s) stored in the input stream. 72 * Load the GIF image(s) stored in the input stream.
73 * Return an array of ImageData representing the image(s). 73 * Return an array of ImageData representing the image(s).
74 */ 74 */
75 override ImageData[] loadFromByteStream() { 75 override ImageData[] loadFromByteStream() {
76 byte[3] signatureBytes; 76 byte[3] signature;
77 byte[3] versionBytes; 77 byte[3] versionBytes;
78 byte[7] block; 78 byte[7] block;
79 try { 79 try {
80 inputStream.read(signatureBytes); 80 inputStream.read(signature);
81 signature = cast(String)signatureBytes.dup; 81 if (!(signature[0] is 'G' && signature[1] is 'I' && signature[2] is 'F'))
82 if (signature != "GIF") //$NON-NLS-1$
83 DWT.error(DWT.ERROR_INVALID_IMAGE); 82 DWT.error(DWT.ERROR_INVALID_IMAGE);
84 83
85 inputStream.read(versionBytes); 84 inputStream.read(versionBytes);
86 85
87 inputStream.read(block); 86 inputStream.read(block);
311 byte[] readApplicationExtension() { 310 byte[] readApplicationExtension() {
312 try { 311 try {
313 // Read size of block = 0x0B. 312 // Read size of block = 0x0B.
314 inputStream.read(); 313 inputStream.read();
315 // Read application identifier. 314 // Read application identifier.
316 byte[] applicationBytes = new byte[8]; 315 byte[] application = new byte[8];
317 inputStream.read(applicationBytes); 316 inputStream.read(application);
318 String application = cast(String)(applicationBytes.dup);
319 // Read authentication code. 317 // Read authentication code.
320 byte[] authenticationBytes = new byte[3]; 318 byte[] authentication = new byte[3];
321 inputStream.read(authenticationBytes); 319 inputStream.read(authentication);
322 String authentication = cast(String)(authenticationBytes.dup);
323 // Read application data. 320 // Read application data.
324 byte[] data = new byte[0]; 321 byte[] data = new byte[0];
325 byte[] block = new byte[255]; 322 byte[] block = new byte[255];
326 int size = inputStream.read(); 323 int size = inputStream.read();
327 while ((size > 0) && (inputStream.read(block, 0, size) !is -1)) { 324 while ((size > 0) && (inputStream.read(block, 0, size) !is -1)) {
331 System.arraycopy(block, 0, data, oldData.length, size); 328 System.arraycopy(block, 0, data, oldData.length, size);
332 //data ~= block[ 0 .. size ]; 329 //data ~= block[ 0 .. size ];
333 size = inputStream.read(); 330 size = inputStream.read();
334 } 331 }
335 // Look for the NETSCAPE 'repeat count' field for an animated GIF. 332 // Look for the NETSCAPE 'repeat count' field for an animated GIF.
336 if (application=="NETSCAPE" && authentication=="2.0" && data[0] is 01) { //$NON-NLS-1$ //$NON-NLS-2$ 333 bool netscape =
334 application[0] is 'N' &&
335 application[1] is 'E' &&
336 application[2] is 'T' &&
337 application[3] is 'S' &&
338 application[4] is 'C' &&
339 application[5] is 'A' &&
340 application[6] is 'P' &&
341 application[7] is 'E';
342 bool authentic =
343 authentication[0] is '2' &&
344 authentication[1] is '.' &&
345 authentication[2] is '0';
346 if (netscape && authentic && data[0] is 01) { //$NON-NLS-1$ //$NON-NLS-2$
337 repeatCount = (data[1] & 0xFF) | ((data[2] & 0xFF) << 8); 347 repeatCount = (data[1] & 0xFF) | ((data[2] & 0xFF) << 8);
338 loader.repeatCount = repeatCount; 348 loader.repeatCount = repeatCount;
339 } 349 }
340 return data; 350 return data;
341 } catch (Exception e) { 351 } catch (Exception e) {