You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by lb...@apache.org on 2014/01/13 00:59:42 UTC

svn commit: r1557632 - /xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/ps/PSImageUtils.java

Author: lbernardo
Date: Sun Jan 12 23:59:42 2014
New Revision: 1557632

URL: http://svn.apache.org/r1557632
Log:
FOP-1801: conversion B&W GIF=>PDF creates PDF with colorspace RGB if FOP0.95 and Gray if FOP0.20.5; patch submitted by Thanasis Giannimaras

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=1557632&r1=1557631&r2=1557632&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 Sun Jan 12 23:59:42 2014
@@ -33,6 +33,7 @@ import java.awt.image.RenderedImage;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.util.Arrays;
 
 import org.apache.commons.io.IOUtils;
 
@@ -348,26 +349,42 @@ public class PSImageUtils {
         if ((cm instanceof IndexColorModel)) {
             ColorSpace cs = cm.getColorSpace();
             IndexColorModel im = (IndexColorModel)cm;
-            gen.write("[/Indexed " + getColorSpaceName(cs));
+            boolean isDeviceGray;
             int c = im.getMapSize();
+            int[] palette = new int[c];
+            im.getRGBs(palette);
+            byte[] reds = new byte[c];
+            byte[] greens = new byte[c];
+            byte[] blues = new byte[c];
+            im.getReds(reds);
+            im.getGreens(greens);
+            im.getBlues(blues);
             int hival = c - 1;
             if (hival > 4095) {
                 throw new UnsupportedOperationException("hival must not go beyond 4095");
             }
+            isDeviceGray = Arrays.equals(reds, blues) && Arrays.equals(blues, greens);
+            if (isDeviceGray) {
+                gen.write("[/Indexed " + "/DeviceGray");
+            } else {
+                gen.write("[/Indexed " + getColorSpaceName(cs));
+            }
             gen.writeln(" " + Integer.toString(hival));
             gen.write("  <");
-            int[] palette = new int[c];
-            im.getRGBs(palette);
-            for (int i = 0; i < c; i++) {
-                if (i > 0) {
-                    if ((i % 8) == 0) {
-                        gen.newLine();
-                        gen.write("   ");
-                    } else {
-                        gen.write(" ");
+            if (isDeviceGray) {
+                gen.write(toHexString(blues));
+            } else {
+                for (int i = 0; i < c; i++) {
+                    if (i > 0) {
+                        if ((i % 8) == 0) {
+                            gen.newLine();
+                            gen.write("   ");
+                        } else {
+                            gen.write(" ");
+                        }
                     }
+                    gen.write(rgb2Hex(palette[i]));
                 }
-                gen.write(rgb2Hex(palette[i]));
             }
             gen.writeln(">");
             gen.writeln("] setcolorspace");
@@ -376,6 +393,17 @@ public class PSImageUtils {
         }
     }
 
+    static String toHexString(byte[] color) {
+        char[] hexChars = new char[color.length * 2];
+        int x;
+        for (int i = 0; i < color.length; i++) {
+            x = color[i] & 0xFF;
+            hexChars[i * 2] = HEX[x >>> 4];
+            hexChars[i * 2 + 1] = HEX[x & 0x0F];
+        }
+        return new String(hexChars);
+    }
+
     static void writeImageCommand(RenderedImage img,
             PSDictionary imageDict, PSGenerator gen) throws IOException {
         ImageEncodingHelper helper = new ImageEncodingHelper(img, true);



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org