diff dwt/internal/image/FileFormat.d @ 34:5123b17c98ef

Ported dwt.events.*, dwt.graphics.GC, Region, dwt.internal.image.*
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sun, 14 Sep 2008 01:45:57 +0200
parents a9ab4c738ed8
children d8635bb48c7c
line wrap: on
line diff
--- a/dwt/internal/image/FileFormat.d	Fri Sep 12 13:53:21 2008 +0200
+++ b/dwt/internal/image/FileFormat.d	Sun Sep 14 01:45:57 2008 +0200
@@ -7,17 +7,33 @@
  *
  * Contributors:
  *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
  *******************************************************************************/
-module dwt.internal.image;
-
+module dwt.internal.image.FileFormat;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import dwt.dwthelper.utils;
+
+public import dwt.graphics.ImageLoader;
+public import dwt.graphics.ImageData;
+public import dwt.internal.image.LEDataInputStream;
+public import dwt.internal.image.LEDataOutputStream;
 
 import dwt.DWT;
-import dwt.graphics.ImageData;
-import dwt.graphics.ImageLoader;
+
+public import dwt.dwthelper.InputStream;
+public import dwt.dwthelper.OutputStream;
+
+import dwt.internal.image.GIFFileFormat;
+import dwt.internal.image.WinBMPFileFormat;
+import dwt.internal.image.WinICOFileFormat;
+import dwt.internal.image.TIFFFileFormat;
+import dwt.internal.image.OS2BMPFileFormat;
+import dwt.internal.image.JPEGFileFormat;
+import dwt.internal.image.PNGFileFormat;
+
+import tango.core.Exception;
+import tango.core.Tuple;
 
 /**
  * Abstract factory class for loading/unloading images from files or streams
@@ -25,10 +41,10 @@
  *
  */
 public abstract class FileFormat {
-    static final String FORMAT_PACKAGE = "dwt.internal.image"; //$NON-NLS-1$
-    static final String FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$
-    static final String[] FORMATS = {"WinBMP", "WinBMP", "GIF", "WinICO", "JPEG", "PNG", "TIFF", "OS2BMP"}; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$//$NON-NLS-8$
-    
+    static const String FORMAT_PACKAGE = "dwt.internal.image"; //$NON-NLS-1$
+    static const String FORMAT_SUFFIX = "FileFormat"; //$NON-NLS-1$
+    static const String[] FORMATS = [ "WinBMP"[], "WinBMP", "GIF", "WinICO", "JPEG", "PNG", "TIFF", "OS2BMP" ]; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$//$NON-NLS-5$ //$NON-NLS-6$//$NON-NLS-7$//$NON-NLS-8$
+    alias Tuple!( WinBMPFileFormat, WinBMPFileFormat, GIFFileFormat, WinICOFileFormat, JPEGFileFormat, PNGFileFormat, TIFFFileFormat, OS2BMPFileFormat ) TFormats;
     LEDataInputStream inputStream;
     LEDataOutputStream outputStream;
     ImageLoader loader;
@@ -45,17 +61,16 @@
 /**
  * Read the specified input stream, and return the
  * device independent image array represented by the stream.
- */ 
+ */
 public ImageData[] loadFromStream(LEDataInputStream stream) {
     try {
         inputStream = stream;
         return loadFromByteStream();
+    } catch (IOException e) {
+        DWT.error(DWT.ERROR_IO, e);
+        return null;
     } catch (Exception e) {
-        if ( null !is cast(IOException)e ) {
-            DWT.error(DWT.ERROR_IO, e);
-        } else {
-            DWT.error(DWT.ERROR_INVALID_IMAGE, e);
-        }
+        DWT.error(DWT.ERROR_INVALID_IMAGE, e);
         return null;
     }
 }
@@ -63,24 +78,19 @@
 /**
  * Read the specified input stream using the specified loader, and
  * return the device independent image array represented by the stream.
- */ 
-public static ImageData[] load(InputStream is, ImageLoader loader) {
+ */
+public static ImageData[] load(InputStream istr, ImageLoader loader) {
     FileFormat fileFormat = null;
-    LEDataInputStream stream = new LEDataInputStream(is);
-    bool isSupported = false;   
-    for (int i = 1; i < FORMATS.length; i++) {
-        if (FORMATS[i] !is null) {
-            try {
-                Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[i] + FORMAT_SUFFIX);
-                fileFormat = cast(FileFormat) clazz.newInstance();
-                if (fileFormat.isFileFormat(stream)) {
-                    isSupported = true;
-                    break;
-                }
-            } catch (ClassNotFoundException e) {
-                FORMATS[i] = null;
-            } catch (Exception e) {
+    LEDataInputStream stream = new LEDataInputStream(istr);
+    bool isSupported = false;    
+    foreach( TFormat; TFormats ){
+        try{
+            fileFormat = new TFormat();
+            if (fileFormat.isFileFormat(stream)) {
+                isSupported = true;
+                break;
             }
+        } catch (Exception e) {
         }
     }
     if (!isSupported) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT);
@@ -91,7 +101,7 @@
 /**
  * Write the device independent image array stored in the specified loader
  * to the specified output stream using the specified file format.
- */ 
+ */
 public static void save(OutputStream os, int format, ImageLoader loader) {
     if (format < 0 || format >= FORMATS.length) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT);
     if (FORMATS[format] is null) DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT);
@@ -100,8 +110,11 @@
     LEDataOutputStream stream = new LEDataOutputStream(os);
     FileFormat fileFormat = null;
     try {
-        Class clazz = Class.forName(FORMAT_PACKAGE + '.' + FORMATS[format] + FORMAT_SUFFIX);
-        fileFormat = cast(FileFormat) clazz.newInstance();
+        foreach( idx, TFormat; TFormats ){
+            if( idx is format ){
+                fileFormat = new TFormat();
+            }
+        }
     } catch (Exception e) {
         DWT.error(DWT.ERROR_UNSUPPORTED_FORMAT);
     }
@@ -109,6 +122,7 @@
         switch (loader.data[0].depth) {
             case 8: fileFormat.compression = 1; break;
             case 4: fileFormat.compression = 2; break;
+            default:
         }
     }
     fileFormat.unloadIntoStream(loader, stream);
@@ -119,7 +133,7 @@
 /**
  * Write the device independent image array stored in the specified loader
  * to the specified output stream.
- */ 
+ */
 public void unloadIntoStream(ImageLoader loader, LEDataOutputStream stream) {
     try {
         outputStream = stream;