From c9de48782eb067b3aef30b1c8946d8a0a040a7e0 Mon Sep 17 00:00:00 2001 From: evrhel Date: Tue, 21 Jul 2020 15:01:18 -0700 Subject: Colors now being displayed correctly after getting image Fixed drawing colors coming from ghostscript. Next: need to migrate the current project into a jar file instead of a runnable application. The viewer will use the Java library. --- .../gsjava/src/com/artifex/gsjava/Document.java | 4 +-- demos/java/gsjava/src/com/artifex/gsjava/Main.java | 26 +------------------ demos/java/gsjava/src/com/artifex/gsjava/Page.java | 30 ++++++++++++---------- 3 files changed, 20 insertions(+), 40 deletions(-) diff --git a/demos/java/gsjava/src/com/artifex/gsjava/Document.java b/demos/java/gsjava/src/com/artifex/gsjava/Document.java index 243f08a72..b67bb639c 100644 --- a/demos/java/gsjava/src/com/artifex/gsjava/Document.java +++ b/demos/java/gsjava/src/com/artifex/gsjava/Document.java @@ -31,7 +31,7 @@ public class Document implements List { System.err.println("Failed to set display callback"); return null; } - final int format = GS_COLORS_RGB | GS_DISPLAY_DEPTH_8 | GS_DISPLAY_LITTLEENDIAN | GS_DISPLAY_UNUSED_FIRST; + final int format = GS_COLORS_RGB | GS_DISPLAY_DEPTH_8 | GS_DISPLAY_BIGENDIAN; final String[] gargs = { "gs", "-dNOPAUSE", "-dSAFER", "-I%rom%Resource%/Init/", "-dBATCH", "-r72", "-sDEVICE=display", @@ -75,7 +75,7 @@ public class Document implements List { System.out.println("Copies = "+ copies); System.out.println("Flush = " + flush); byte[] data = (byte[])pimage.toArrayNoConvert(); - pages.add(new Page(data, pageWidth, pageHeight, BufferedImage.TYPE_4BYTE_ABGR)); + pages.add(new Page(data, pageWidth, pageHeight, pageRaster, BufferedImage.TYPE_3BYTE_BGR)); return 0; } } diff --git a/demos/java/gsjava/src/com/artifex/gsjava/Main.java b/demos/java/gsjava/src/com/artifex/gsjava/Main.java index b4b149010..9927a9a1f 100644 --- a/demos/java/gsjava/src/com/artifex/gsjava/Main.java +++ b/demos/java/gsjava/src/com/artifex/gsjava/Main.java @@ -60,35 +60,11 @@ public class Main { return; } - final int format = GS_COLORS_RGB | GS_DISPLAY_DEPTH_8 | GS_DISPLAY_LITTLEENDIAN; // 22 0 236 - final File file = new File("blue.pdf"); + final File file = new File("tiger.eps"); if (!file.exists()) throw new FileNotFoundException(file.getAbsolutePath()); - final File ofile = new File("image.tiff"); - - ///final String[] gargs = { "gs", "-dNOPAUSE", "-dSAFER", - // "-I%rom%Resource%/Init/", - // "-dBATCH", "-r72", "-sDEVICE=display", - // "-dDisplayFormat=" + format, - // "-f", - // file.getAbsolutePath() }; - - /*String[] gargs = { "gs", "-dNOPAUSE", "-dSAFER", - "-I%rom%Resource%/Init/", - "-dBATCH", "-r72", "-sDEVICE=tiff24nc", "-o", ofile.getAbsolutePath(), - "-f", - file.getAbsolutePath() };*/ - //final String[] gargs = { "gs", "-Z#", "-h" }; - //System.out.println("args = " + Arrays.toString(gargs)); - //code = gsapi_init_with_args(instanceRef.value, gargs); - //if (code != GS_ERROR_OK) { - // System.err.println("Failed to gsapi_init_with_args (code = " + code + ")"); - //} else { - // System.out.println("gsapi_init_with_args success"); - //} - Document doc = Document.loadFromFile(instanceRef.value, file); DisplayWindow win = new DisplayWindow(new Dimension(800, 600)); win.renderPage(doc.get(0)); diff --git a/demos/java/gsjava/src/com/artifex/gsjava/Page.java b/demos/java/gsjava/src/com/artifex/gsjava/Page.java index fd8a15e8f..bf0204de9 100644 --- a/demos/java/gsjava/src/com/artifex/gsjava/Page.java +++ b/demos/java/gsjava/src/com/artifex/gsjava/Page.java @@ -4,10 +4,16 @@ import java.awt.Dimension; import java.awt.Graphics; import java.awt.Image; import java.awt.Point; +import java.awt.color.ColorSpace; import java.awt.image.BufferedImage; +import java.awt.image.ColorModel; +import java.awt.image.ComponentColorModel; import java.awt.image.DataBuffer; import java.awt.image.DataBufferByte; +import java.awt.image.PixelInterleavedSampleModel; import java.awt.image.Raster; +import java.awt.image.SampleModel; +import java.awt.image.WritableRaster; import java.io.File; import java.io.IOException; @@ -23,25 +29,23 @@ public class Page { this.image = image; } - public Page(final byte[] data, final int width, final int height, final int format) { - this.image = new BufferedImage(width, height, format); - this.image.setData(Raster.createRaster(this.image.getSampleModel(), - new DataBufferByte(data, data.length), new Point())); + public Page(final byte[] data, final int width, final int height, final int raster, final int format) { + DataBuffer dataBuffer = new DataBufferByte(data, data.length); + WritableRaster wraster = Raster.createInterleavedRaster(dataBuffer, width, height, raster, 3, new int[] { 0, 1, 2 }, new Point()); + + // this.image = new BufferedImage(width, height, format); + //this.image.setData(Raster.createRaster(this.image.getSampleModel(), + // new DataBufferByte(data, data.length), new Point())); + ColorModel model = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8 }, + false, false, ColorModel.OPAQUE, DataBuffer.TYPE_BYTE); + + this.image = new BufferedImage(model, wraster, false, null); try { ImageIO.write(this.image, "PNG", new File("test.png")); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } - //BufferedImage img = null; - //try { - // img = ImageIO.read(new File("image.png")); - //} catch (IOException e) { - // System.out.println("Failed to load image: " + e); - // e.printStackTrace(); - // } finally { - // this.image = img; - // } } public void render(final RenderParams params) { -- cgit v1.2.1