diff dwt/internal/image/PngIhdrChunk.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 1a8b3cb347e0
children
line wrap: on
line diff
--- a/dwt/internal/image/PngIhdrChunk.d	Fri Sep 12 13:53:21 2008 +0200
+++ b/dwt/internal/image/PngIhdrChunk.d	Sun Sep 14 01:45:57 2008 +0200
@@ -7,46 +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.PngIhdrChunk;
+
+import dwt.dwthelper.utils;
 
 
 import dwt.DWT;
 import dwt.graphics.PaletteData;
 import dwt.graphics.RGB;
+import dwt.internal.image.PngFileReadState;
+import dwt.internal.image.PngIhdrChunk;
+import dwt.internal.image.PngChunk;
+import tango.text.convert.Format;
 
 class PngIhdrChunk : PngChunk {
-    static final int IHDR_DATA_LENGTH = 13;
-    
-    static final int WIDTH_DATA_OFFSET = DATA_OFFSET + 0;
-    static final int HEIGHT_DATA_OFFSET = DATA_OFFSET + 4;
-    static final int BIT_DEPTH_OFFSET = DATA_OFFSET + 8;
-    static final int COLOR_TYPE_OFFSET = DATA_OFFSET + 9;
-    static final int COMPRESSION_METHOD_OFFSET = DATA_OFFSET + 10;
-    static final int FILTER_METHOD_OFFSET = DATA_OFFSET + 11;
-    static final int INTERLACE_METHOD_OFFSET = DATA_OFFSET + 12;
-    
-    static final byte COLOR_TYPE_GRAYSCALE = 0;
-    static final byte COLOR_TYPE_RGB = 2;
-    static final byte COLOR_TYPE_PALETTE = 3;
-    static final byte COLOR_TYPE_GRAYSCALE_WITH_ALPHA = 4;
-    static final byte COLOR_TYPE_RGB_WITH_ALPHA = 6;
-    
-    static final int INTERLACE_METHOD_NONE = 0;
-    static final int INTERLACE_METHOD_ADAM7 = 1;
-    
-    static final int FILTER_NONE = 0;
-    static final int FILTER_SUB = 1;
-    static final int FILTER_UP = 2;
-    static final int FILTER_AVERAGE = 3;
-    static final int FILTER_PAETH = 4;
-    
-    static final byte[] ValidBitDepths = {1, 2, 4, 8, 16};
-    static final byte[] ValidColorTypes = {0, 2, 3, 4, 6};
-    
+    static const int IHDR_DATA_LENGTH = 13;
+
+    static const int WIDTH_DATA_OFFSET = DATA_OFFSET + 0;
+    static const int HEIGHT_DATA_OFFSET = DATA_OFFSET + 4;
+    static const int BIT_DEPTH_OFFSET = DATA_OFFSET + 8;
+    static const int COLOR_TYPE_OFFSET = DATA_OFFSET + 9;
+    static const int COMPRESSION_METHOD_OFFSET = DATA_OFFSET + 10;
+    static const int FILTER_METHOD_OFFSET = DATA_OFFSET + 11;
+    static const int INTERLACE_METHOD_OFFSET = DATA_OFFSET + 12;
+
+    static const byte COLOR_TYPE_GRAYSCALE = 0;
+    static const byte COLOR_TYPE_RGB = 2;
+    static const byte COLOR_TYPE_PALETTE = 3;
+    static const byte COLOR_TYPE_GRAYSCALE_WITH_ALPHA = 4;
+    static const byte COLOR_TYPE_RGB_WITH_ALPHA = 6;
+
+    static const int INTERLACE_METHOD_NONE = 0;
+    static const int INTERLACE_METHOD_ADAM7 = 1;
+
+    static const int FILTER_NONE = 0;
+    static const int FILTER_SUB = 1;
+    static const int FILTER_UP = 2;
+    static const int FILTER_AVERAGE = 3;
+    static const int FILTER_PAETH = 4;
+
+    static const byte[] ValidBitDepths = [ cast(byte)1, 2, 4, 8, 16];
+    static const byte[] ValidColorTypes = [ cast(byte)0, 2, 3, 4, 6];
+
     int width, height;
     byte bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod;
-    
+
 this(int width, int height, byte bitDepth, byte colorType, byte compressionMethod, byte filterMethod, byte interlaceMethod) {
     super(IHDR_DATA_LENGTH);
     setType(TYPE_IHDR);
@@ -63,7 +71,7 @@
 /**
  * Construct a PNGChunk using the reference bytes
  * given.
- */ 
+ */
 this(byte[] reference) {
     super(reference);
     if (reference.length <= IHDR_DATA_LENGTH) DWT.error(DWT.ERROR_INVALID_IMAGE);
@@ -76,7 +84,7 @@
     interlaceMethod = reference[INTERLACE_METHOD_OFFSET];
 }
 
-int getChunkType() {
+override int getChunkType() {
     return CHUNK_IHDR;
 }
 
@@ -212,28 +220,28 @@
 /**
  * Answer whether the chunk is a valid IHDR chunk.
  */
-void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
+override void validate(PngFileReadState readState, PngIhdrChunk headerChunk) {
     // An IHDR chunk is invalid if any other chunk has
     // been read.
-    if (readState.readIHDR 
+    if (readState.readIHDR
         || readState.readPLTE
         || readState.readIDAT
-        || readState.readIEND) 
+        || readState.readIEND)
     {
         DWT.error(DWT.ERROR_INVALID_IMAGE);
     } else {
         readState.readIHDR = true;
     }
-    
+
     super.validate(readState, headerChunk);
-    
+
     if (length !is IHDR_DATA_LENGTH) DWT.error(DWT.ERROR_INVALID_IMAGE);
     if (compressionMethod !is 0) DWT.error(DWT.ERROR_INVALID_IMAGE);
     if (interlaceMethod !is INTERLACE_METHOD_NONE &&
         interlaceMethod !is INTERLACE_METHOD_ADAM7) {
             DWT.error(DWT.ERROR_INVALID_IMAGE);
     }
-    
+
     bool colorTypeIsValid = false;
     for (int i = 0; i < ValidColorTypes.length; i++) {
         if (ValidColorTypes[i] is colorType) {
@@ -251,15 +259,15 @@
         }
     }
     if (!bitDepthIsValid) DWT.error(DWT.ERROR_INVALID_IMAGE);
-    
-    if ((colorType is COLOR_TYPE_RGB 
+
+    if ((colorType is COLOR_TYPE_RGB
         || colorType is COLOR_TYPE_RGB_WITH_ALPHA
         || colorType is COLOR_TYPE_GRAYSCALE_WITH_ALPHA)
-        && bitDepth < 8) 
+        && bitDepth < 8)
     {
             DWT.error(DWT.ERROR_INVALID_IMAGE);
     }
-    
+
     if (colorType is COLOR_TYPE_PALETTE && bitDepth > 8) {
         DWT.error(DWT.ERROR_INVALID_IMAGE);
     }
@@ -268,11 +276,11 @@
 String getColorTypeString() {
     switch (colorType) {
         case COLOR_TYPE_GRAYSCALE:              return "Grayscale";
-        case COLOR_TYPE_RGB:                    return "RGB";       
+        case COLOR_TYPE_RGB:                    return "RGB";
         case COLOR_TYPE_PALETTE:                return "Palette";
         case COLOR_TYPE_GRAYSCALE_WITH_ALPHA:   return "Grayscale with Alpha";
         case COLOR_TYPE_RGB_WITH_ALPHA:         return "RGB with Alpha";
-        default:                                return "Unknown - " + colorType;
+        default:                                return "Unknown - " ~ cast(char)colorType;
     }
 }
 
@@ -295,21 +303,9 @@
     }
 }
 
-void contributeToString(StringBuffer buffer) {
-    buffer.append("\n\tWidth: ");
-    buffer.append(width);
-    buffer.append("\n\tHeight: ");
-    buffer.append(height);
-    buffer.append("\n\tBit Depth: ");
-    buffer.append(bitDepth);
-    buffer.append("\n\tColor Type: ");
-    buffer.append(getColorTypeString());
-    buffer.append("\n\tCompression Method: ");
-    buffer.append(compressionMethod);
-    buffer.append("\n\tFilter Method: ");
-    buffer.append(getFilterMethodString());
-    buffer.append("\n\tInterlace Method: ");
-    buffer.append(getInterlaceMethodString());
+override String contributeToString() {
+    return Format( "\n\tWidth: {}\n\tHeight: {}\n\tBit Depth: {}\n\tColor Type: {}\n\tCompression Method: {}\n\tFilter Method: {}\n\tInterlace Method: {}",
+        width, height, bitDepth, getColorTypeString(), compressionMethod, getFilterMethodString(), getInterlaceMethodString() );
 }
 
 bool getMustHavePalette() {
@@ -317,7 +313,7 @@
 }
 
 bool getCanHavePalette() {
-    return colorType !is COLOR_TYPE_GRAYSCALE && 
+    return colorType !is COLOR_TYPE_GRAYSCALE &&
         colorType !is COLOR_TYPE_GRAYSCALE_WITH_ALPHA;
 }
 
@@ -383,7 +379,7 @@
     int max = (1 << depth) - 1;
     int delta = 255 / max;
     int gray = 0;
-    RGB[] rgbs = new RGB[max + 1]; 
+    RGB[] rgbs = new RGB[max + 1];
     for (int i = 0; i <= max; i++) {
         rgbs[i] = new RGB(gray, gray, gray);
         gray += delta;
@@ -401,7 +397,7 @@
             return new PaletteData(0xFF0000, 0xFF00, 0xFF);
         default:
             return null;
-    }   
+    }
 }