xmlgraphics-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jerem...@apache.org
Subject svn commit: r600559 - /xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java
Date Mon, 03 Dec 2007 14:54:09 GMT
Author: jeremias
Date: Mon Dec  3 06:54:06 2007
New Revision: 600559

URL: http://svn.apache.org/viewvc?rev=600559&view=rev
Log:
PostScript:
Cut peak memory-consumption to half for non-writing-optimizable images since no additional
buffer is necessary anymore. It's faster, too.
Added optimized writing for plain non-alpha RGB images.

Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java?rev=600559&r1=600558&r2=600559&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java Mon Dec
 3 06:54:06 2007
@@ -23,6 +23,7 @@
 import java.awt.color.ColorSpace;
 import java.awt.geom.Rectangle2D;
 import java.awt.image.ColorModel;
+import java.awt.image.ComponentColorModel;
 import java.awt.image.DataBuffer;
 import java.awt.image.DataBufferByte;
 import java.awt.image.IndexColorModel;
@@ -519,14 +520,40 @@
         }
         
         private void writeRGBTo(OutputStream out) throws IOException {
-            int[] tmpMap = getRGB(img, 0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
-            // Should take care of the ColorSpace and bitsPerPixel
-            for (int y = 0, my = img.getHeight(); y < my; y++) {
-                for (int x = 0, mx = img.getWidth(); x < mx; x++) {
-                    int p = tmpMap[y * mx + x];
-                    int r = (p >> 16) & 0xFF;
-                    int g = (p >> 8) & 0xFF;
-                    int b = (p) & 0xFF;
+            Raster raster = img.getData();
+            Object data;
+            int nbands = raster.getNumBands();
+            int dataType = raster.getDataBuffer().getDataType();
+            switch (dataType) {
+            case DataBuffer.TYPE_BYTE:
+                data = new byte[nbands];
+                break;
+            case DataBuffer.TYPE_USHORT:
+                data = new short[nbands];
+                break;
+            case DataBuffer.TYPE_INT:
+                data = new int[nbands];
+                break;
+            case DataBuffer.TYPE_FLOAT:
+                data = new float[nbands];
+                break;
+            case DataBuffer.TYPE_DOUBLE:
+                data = new double[nbands];
+                break;
+            default:
+                throw new IllegalArgumentException("Unknown data buffer type: "+
+                                                   dataType);
+            }
+            
+            ColorModel colorModel = img.getColorModel();
+            int w = img.getWidth();
+            int h = img.getHeight();
+            for (int y = 0; y < h; y++) {
+                for (int x = 0; x < w; x++) {
+                    int rgb = colorModel.getRGB(raster.getDataElements(x, y, data));
+                    int r = (rgb >> 16) & 0xFF;
+                    int g = (rgb >> 8) & 0xFF;
+                    int b = (rgb) & 0xFF;
                     out.write(r);
                     out.write(g);
                     out.write(b);
@@ -539,9 +566,10 @@
             ColorSpace cs = cm.getColorSpace();
             int tilesX = img.getNumXTiles();
             int tilesY = img.getNumYTiles();
+            int colorComponents = cm.getNumColorComponents();
+            int components = cm.getNumComponents();
             boolean multiTile = (tilesX != 1 || tilesY != 1); 
-            if (cm.getNumComponents() == 1 && cs.getType() == ColorSpace.TYPE_GRAY
&& !multiTile) {
-                System.out.println("gray");
+            if (components == 1 && cs.getType() == ColorSpace.TYPE_GRAY &&
!multiTile) {
                 Raster raster = img.getTile(0, 0);
                 DataBuffer buffer = raster.getDataBuffer();
                 if (buffer instanceof DataBufferByte) {
@@ -560,6 +588,19 @@
                     return true;
                 }
             }
+            if (!multiTile && cm instanceof ComponentColorModel
+                    && components == 3 && colorComponents == 3) {
+                ComponentColorModel ccm = (ComponentColorModel)cm;
+                Raster raster = img.getTile(0, 0);
+                DataBuffer buffer = raster.getDataBuffer();
+                if (buffer instanceof DataBufferByte
+                        && buffer.getOffset() == 0
+                        && buffer.getNumBanks() == 1) {
+                    DataBufferByte byteBuffer = (DataBufferByte)buffer;
+                    out.write(byteBuffer.getData());
+                    return true;
+                }
+            }
             return false;
         }
         
@@ -575,24 +616,6 @@
             return null; //No implicit filters with RenderedImage instances
         }
         
-    }
-    
-    private static byte[] getBitmapBytes(RenderedImage img) {
-        int[] tmpMap = getRGB(img, 0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth());
-        // Should take care of the ColorSpace and bitsPerPixel
-        byte[] bitmaps = new byte[img.getWidth() * img.getHeight() * 3];
-        for (int y = 0, my = img.getHeight(); y < my; y++) {
-            for (int x = 0, mx = img.getWidth(); x < mx; x++) {
-                int p = tmpMap[y * mx + x];
-                int r = (p >> 16) & 0xFF;
-                int g = (p >> 8) & 0xFF;
-                int b = (p) & 0xFF;
-                bitmaps[3 * (y * mx + x)] = (byte)(r & 0xFF);
-                bitmaps[3 * (y * mx + x) + 1] = (byte)(g & 0xFF);
-                bitmaps[3 * (y * mx + x) + 2] = (byte)(b & 0xFF);
-            }
-        }
-        return bitmaps;
     }
     
     /**



---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org


Mime
View raw message