diff dwt/internal/image/PngChunk.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 e831403a80a9
children
line wrap: on
line diff
--- a/dwt/internal/image/PngChunk.d	Fri Sep 12 13:53:21 2008 +0200
+++ b/dwt/internal/image/PngChunk.d	Sun Sep 14 01:45:57 2008 +0200
@@ -7,42 +7,54 @@
  *
  * 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.PngChunk;
 
 
-import java.io.IOException;
+import dwt.DWT;
+import dwt.internal.image.LEDataInputStream;
+import dwt.internal.image.PngFileReadState;
+import dwt.internal.image.PngIhdrChunk;
+import dwt.internal.image.PngPlteChunk;
+import dwt.internal.image.PngIdatChunk;
+import dwt.internal.image.PngIendChunk;
+import dwt.internal.image.PngTrnsChunk;
+import dwt.dwthelper.utils;
 
-import dwt.DWT;
+import tango.core.Exception;
+import tango.text.convert.Format;
 
-class PngChunk : Object {
+class PngChunk {
     byte[] reference;
 
-    static final int LENGTH_OFFSET = 0;
-    static final int TYPE_OFFSET = 4;
-    static final int DATA_OFFSET = 8;
+    static const int LENGTH_OFFSET = 0;
+    static const int TYPE_OFFSET = 4;
+    static const int DATA_OFFSET = 8;
 
-    static final int TYPE_FIELD_LENGTH = 4;
-    static final int LENGTH_FIELD_LENGTH = 4;
-    static final int MIN_LENGTH = 12;
+    static const int TYPE_FIELD_LENGTH = 4;
+    static const int LENGTH_FIELD_LENGTH = 4;
+    static const int MIN_LENGTH = 12;
 
-    static final int CHUNK_UNKNOWN = -1;
+    static const int CHUNK_UNKNOWN = -1;
     // Critical chunks.
-    static final int CHUNK_IHDR = 0;
-    static final int CHUNK_PLTE = 1;
-    static final int CHUNK_IDAT = 2;
-    static final int CHUNK_IEND = 3;
+    static const int CHUNK_IHDR = 0;
+    static const int CHUNK_PLTE = 1;
+    static const int CHUNK_IDAT = 2;
+    static const int CHUNK_IEND = 3;
     // Non-critical chunks.
-    static final int CHUNK_tRNS = 5;
-    
-    static final byte[] TYPE_IHDR = {cast(byte) 'I', cast(byte) 'H', cast(byte) 'D', cast(byte) 'R'};
-    static final byte[] TYPE_PLTE = {cast(byte) 'P', cast(byte) 'L', cast(byte) 'T', cast(byte) 'E'};
-    static final byte[] TYPE_IDAT = {cast(byte) 'I', cast(byte) 'D', cast(byte) 'A', cast(byte) 'T'};
-    static final byte[] TYPE_IEND = {cast(byte) 'I', cast(byte) 'E', cast(byte) 'N', cast(byte) 'D'};
-    static final byte[] TYPE_tRNS = {cast(byte) 't', cast(byte) 'R', cast(byte) 'N', cast(byte) 'S'};
-    
-    static final int[] CRC_TABLE;
-    static {
+    static const int CHUNK_tRNS = 5;
+
+    static const byte[] TYPE_IHDR = cast(byte[])"IHDR";//{(byte) 'I', (byte) 'H', (byte) 'D', (byte) 'R'};
+    static const byte[] TYPE_PLTE = cast(byte[])"PLTE";//{(byte) 'P', (byte) 'L', (byte) 'T', (byte) 'E'};
+    static const byte[] TYPE_IDAT = cast(byte[])"IDAT";//{(byte) 'I', (byte) 'D', (byte) 'A', (byte) 'T'};
+    static const byte[] TYPE_IEND = cast(byte[])"IEND";//{(byte) 'I', (byte) 'E', (byte) 'N', (byte) 'D'};
+    static const byte[] TYPE_tRNS = cast(byte[])"tRNS";//{(byte) 't', (byte) 'R', (byte) 'N', (byte) 'S'};
+
+    static const int[] CRC_TABLE;
+    //public static void static_this() {
+    static this() {
         CRC_TABLE = new int[256];
         for (int i = 0; i < 256; i++) {
             CRC_TABLE[i] = i;
@@ -53,17 +65,16 @@
                     CRC_TABLE[i] = 0xEDB88320 ^ ((CRC_TABLE[i] >> 1) & 0x7FFFFFFF);
                 }
             }
-        }   
+        }
     }
-    
+
     int length;
-    
+
 /**
  * Construct a PngChunk using the reference bytes
  * given.
- */ 
+ */
 this(byte[] reference) {
-    super();
     setReference(reference);
     if (reference.length < LENGTH_OFFSET + LENGTH_FIELD_LENGTH) DWT.error(DWT.ERROR_INVALID_IMAGE);
     length = getInt32(LENGTH_OFFSET);
@@ -72,7 +83,7 @@
 /**
  * Construct a PngChunk with the specified number of
  * data bytes.
- */ 
+ */
 this(int dataLength) {
     this(new byte[MIN_LENGTH + dataLength]);
     setLength(dataLength);
@@ -80,14 +91,14 @@
 
 /**
  * Get the PngChunk's reference byteArray;
- */ 
+ */
 byte[] getReference() {
     return reference;
 }
 
 /**
  * Set the PngChunk's reference byteArray;
- */ 
+ */
 void setReference(byte[] reference) {
     this.reference = reference;
 }
@@ -95,18 +106,18 @@
 /**
  * Get the 16-bit integer from the reference byte
  * array at the given offset.
- */ 
+ */
 int getInt16(int offset) {
     int answer = 0;
     answer |= (reference[offset] & 0xFF) << 8;
     answer |= (reference[offset + 1] & 0xFF);
-    return answer;  
+    return answer;
 }
 
 /**
  * Set the 16-bit integer in the reference byte
  * array at the given offset.
- */ 
+ */
 void setInt16(int offset, int value) {
     reference[offset] = cast(byte) ((value >> 8) & 0xFF);
     reference[offset + 1] = cast(byte) (value & 0xFF);
@@ -115,20 +126,20 @@
 /**
  * Get the 32-bit integer from the reference byte
  * array at the given offset.
- */ 
+ */
 int getInt32(int offset) {
     int answer = 0;
     answer |= (reference[offset] & 0xFF) << 24;
     answer |= (reference[offset + 1] & 0xFF) << 16;
     answer |= (reference[offset + 2] & 0xFF) << 8;
     answer |= (reference[offset + 3] & 0xFF);
-    return answer;  
+    return answer;
 }
 
 /**
  * Set the 32-bit integer in the reference byte
  * array at the given offset.
- */ 
+ */
 void setInt32(int offset, int value) {
     reference[offset] = cast(byte) ((value >> 24) & 0xFF);
     reference[offset + 1] = cast(byte) ((value >> 16) & 0xFF);
@@ -139,7 +150,7 @@
 /**
  * Get the length of the data component of this chunk.
  * This is not the length of the entire chunk.
- */ 
+ */
 int getLength() {
     return length;
 }
@@ -147,7 +158,7 @@
 /**
  * Set the length of the data component of this chunk.
  * This is not the length of the entire chunk.
- */ 
+ */
 void setLength(int value) {
     setInt32(LENGTH_OFFSET, value);
     length = value;
@@ -159,14 +170,14 @@
  * The first byte is upper case if the chunk is critical.
  * The second byte is upper case if the chunk is publicly defined.
  * The third byte must be upper case.
- * The fourth byte is upper case if the chunk is unsafe to copy. 
+ * The fourth byte is upper case if the chunk is unsafe to copy.
  * Public chunk types are defined by the PNG Development Group.
- */ 
+ */
 byte[] getTypeBytes() {
     byte[] type = new byte[4];
     System.arraycopy(reference, TYPE_OFFSET, type, 0, TYPE_FIELD_LENGTH);
     return type;
-}   
+}
 
 /**
  * Set the chunk type. This is a four byte value.
@@ -174,9 +185,9 @@
  * The first byte is upper case if the chunk is critical.
  * The second byte is upper case if the chunk is publicly defined.
  * The third byte must be upper case.
- * The fourth byte is upper case if the chunk is unsafe to copy. 
+ * The fourth byte is upper case if the chunk is unsafe to copy.
  * Public chunk types are defined by the PNG Development Group.
- */ 
+ */
 void setType(byte[] value) {
     if (value.length !is TYPE_FIELD_LENGTH) {
         DWT.error (DWT.ERROR_INVALID_ARGUMENT);
@@ -268,12 +279,12 @@
         if (reference[TYPE_OFFSET + i] !is array[i]){
             return false;
         }
-    }   
+    }
     return true;
 }
 
 bool isCritical() {
-    char c = cast(wchar) getTypeBytes()[0]; 
+    char c = cast(char) getTypeBytes()[0];
     return 'A' <= c && c <= 'Z';
 }
 
@@ -282,7 +293,7 @@
     if (typeMatchesArray(TYPE_PLTE)) return CHUNK_PLTE;
     if (typeMatchesArray(TYPE_IDAT)) return CHUNK_IDAT;
     if (typeMatchesArray(TYPE_IEND)) return CHUNK_IEND;
-    if (typeMatchesArray(TYPE_tRNS)) return CHUNK_tRNS; 
+    if (typeMatchesArray(TYPE_tRNS)) return CHUNK_tRNS;
     return CHUNK_UNKNOWN;
 }
 
@@ -297,14 +308,15 @@
         int result = stream.read(headerBytes, 0, headerLength);
         stream.unread(headerBytes);
         if (result !is headerLength) return null;
-        
+
         PngChunk tempChunk = new PngChunk(headerBytes);
-        
+
         int chunkLength = tempChunk.getSize();
         byte[] chunk = new byte[chunkLength];
+
         result = stream.read(chunk, 0, chunkLength);
         if (result !is chunkLength) return null;
-    
+
         switch (tempChunk.getChunkType()) {
             case CHUNK_IHDR:
                 return new PngIhdrChunk(chunk);
@@ -318,7 +330,7 @@
                 return new PngTrnsChunk(chunk);
             default:
                 return new PngChunk(chunk);
-        }       
+        }
     } catch (IOException e) {
         return null;
     }
@@ -329,21 +341,21 @@
  */
 void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
     if (reference.length < MIN_LENGTH) DWT.error(DWT.ERROR_INVALID_IMAGE);
-    
+
     byte[] type = getTypeBytes();
-    
+
     // The third character MUST be upper case.
-    char c = cast(wchar) type[2];
+    char c = cast(char) type[2];
     if (!('A' <= c && c <= 'Z')) DWT.error(DWT.ERROR_INVALID_IMAGE);
-    
+
     // All characters must be letters.
     for (int i = 0; i < TYPE_FIELD_LENGTH; i++) {
-        c = cast(wchar) type[i];
+        c = cast(char) type[i];
         if (!(('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'))) {
             DWT.error(DWT.ERROR_INVALID_IMAGE);
         }
     }
-    
+
     // The stored CRC must match the data's computed CRC.
     if (!checkCRC()) DWT.error(DWT.ERROR_INVALID_IMAGE);
 }
@@ -352,31 +364,23 @@
  * Provided so that subclasses can override and add
  * data to the toString() call.
  */
-void contributeToString(StringBuffer buffer) {}
+String contributeToString() {
+    return "";
+}
 
 /**
- * Returns a String containing a concise, human-readable
+ * Returns a string containing a concise, human-readable
  * description of the receiver.
  *
- * @return a String representation of the event
+ * @return a string representation of the event
  */
-public String toString() {
-    StringBuffer buffer = new StringBuffer();
-    buffer.append("{");
-    buffer.append("\n\tLength: ");
-    buffer.append(getLength());
-    buffer.append("\n\tType: ");
-    byte[] type = getTypeBytes();
-    for(int i = 0; i < type.length; i++) {
-        buffer.append(cast(wchar) type[i]);
-    }
-    
-    contributeToString(buffer);
-    
-    buffer.append("\n\tCRC: ");
-    buffer.append(Integer.toHexString(getCRC()));
-    buffer.append("\n}");
-    return buffer.toString();
+public override String toString() {
+    String buffer = Format( "{\n\tLength: {}\n\tType: {}{}\n\tCRC: {:X}\n}",
+        getLength(),
+        cast(String) getTypeBytes(),
+        contributeToString(),
+        getCRC());
+    return buffer;
 }
 
 }