diff dwt/internal/image/PngEncoder.d @ 213:36f5cb12e1a2

Update to SWT 3.4M7
author Frank Benoit <benoit@tionex.de>
date Sat, 17 May 2008 17:34:28 +0200
parents 57151e2793a2
children fd9c62a2998e
line wrap: on
line diff
--- a/dwt/internal/image/PngEncoder.d	Mon May 05 00:12:38 2008 +0200
+++ b/dwt/internal/image/PngEncoder.d	Sat May 17 17:34:28 2008 +0200
@@ -1,4 +1,4 @@
-/*******************************************************************************
+/*******************************************************************************
  * Copyright (c) 2000, 2006 IBM Corporation and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
@@ -15,10 +15,12 @@
 import dwt.internal.image.LEDataOutputStream;
 import dwt.internal.image.PngDeflater;
 import dwt.dwthelper.ByteArrayOutputStream;
+import dwt.dwthelper.OutputStream;
 import dwt.DWT;
 import dwt.graphics.ImageData;
 import dwt.graphics.ImageLoader;
 import dwt.graphics.RGB;
+import dwt.internal.Compatibility;
 import dwt.internal.image.PngChunk;
 
 import tango.core.Exception;
@@ -236,21 +238,23 @@
 void writeImageData() {
 
     ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
+    OutputStream os = Compatibility.newDeflaterOutputStream(baos);
+    if (os is null) os = baos;
 
     if (colorType is 3) {
 
-        int[] lineData = new int[width];
+        byte[] lineData = new byte[width];
 
         for (int y = 0; y < height; y++) {
 
-            byte filter[] = [0];
-            baos.write(filter, 0, 1);
+            int filter = 0;
+            os.write(filter);
 
             data.getPixels(0, y, width, lineData, 0);
 
             for (int x = 0; x < lineData.length; x++) {
 
-                baos.write(cast(byte) lineData[x]);
+                os.write(lineData[x]);
 
             }
 
@@ -261,7 +265,10 @@
     else {
 
         int[] lineData = new int[width];
-        byte[] alphaData = new byte[width];
+        byte[] alphaData = null;
+        if (colorType is 6) {
+            alphaData = new byte[width];
+        }
 
         int redMask = data.palette.redMask;
         int redShift = data.palette.redShift;
@@ -272,8 +279,8 @@
 
         for (int y = 0; y < height; y++) {
 
-            byte filter[] = [0];
-            baos.write(filter, 0, 1);
+            int filter = 0;
+            os.write(filter);
 
             data.getPixels(0, y, width, lineData, 0);
 
@@ -292,14 +299,12 @@
                 int b = pixel & blueMask;
                 b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
 
-                byte pixels[] = [cast(byte) r, cast(byte) g, cast(byte) b];
-                baos.write(pixels, 0, 3);
+                os.write(r);
+                os.write(g);
+                os.write(b);
 
                 if (colorType is 6) {
-
-                    byte alpha[] = [alphaData[x]];
-                    baos.write(alpha, 0, 1);
-
+                    os.write(alphaData[x]);
                 }
 
             }
@@ -308,8 +313,14 @@
 
     }
 
-    PngDeflater deflater = new PngDeflater();
-    byte[] compressed = deflater.deflate(baos.toByteArray());
+    os.flush();
+    os.close();
+
+    byte[] compressed = baos.toByteArray();
+    if (os is baos) {
+        PngDeflater deflater = new PngDeflater();
+        compressed = deflater.deflate(compressed);
+    }
 
     writeChunk(TAG_IDAT, compressed);