zippy1978 / ghost4j

Java wrapper for Ghostscript C API + PS/PDF document handling API
http://www.ghost4j.org
GNU Lesser General Public License v3.0
64 stars 38 forks source link

Sample code on website no longer works #32

Open tebruno99 opened 10 years ago

tebruno99 commented 10 years ago

I had use the following code instead of the sample provided otherwise the JVM would crash.

package org.ghost4j.example;

import java.awt.image.RenderedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

import org.ghost4j.Ghostscript;
import org.ghost4j.GhostscriptException;
import org.ghost4j.display.ImageWriterDisplayCallback;

/**
 * Example showing how to setup a display callback (to interract with the display) for the Ghostscript interpreter.
 * In this example, a simple ImageWriterDisplayCallback is used as callback: it converts page rasters into images and stores them.
 * @author Gilles Grousset (gi.grousset@gmail.com)
 */
public class DisplayCallbackExample {

    public static void main(String[] args) {

        //get Ghostscript instance
        Ghostscript gs = Ghostscript.getInstance();

        //create display callback (capture display output pages as images)
        ImageWriterDisplayCallback displayCallback = new ImageWriterDisplayCallback();

        //set display callback
        gs.setDisplayCallback(displayCallback);

        //prepare Ghostscript interpreter parameters with display device
        String[] gsArgs = new String[9];
        gsArgs[0] = "-dQUIET";
        gsArgs[1] = "-dNOPAUSE";
        gsArgs[2] = "-dBATCH";
        gsArgs[3] = "-dSAFER";
        gsArgs[4] = "-sDEVICE=display";
        gsArgs[5] = "-sDisplayHandle=0";
        gsArgs[6] = "-dDisplayFormat=16#804";
        gsArgs[7] = "-f";
        gsArgs[8] = "input.ps";

        //run PostScript (also works with PDF) and exit interpreter
        try {
            gs.initialize(gsArgs);
            gs.exit();
        } catch (GhostscriptException e) {
            System.out.println("ERROR: " + e.getMessage());
        }

        //write images captured by the display callback to the disk in PNG format
        try {
            for (int i = 0; i < displayCallback.getImages().size(); i++) {
                ImageIO.write((RenderedImage) displayCallback.getImages().get(i), "png", new File((i + 1) + ".png"));
            }
        } catch (IOException e) {
            System.out.println("ERROR: " + e.getMessage());
        }

    }
}
zippy1978 commented 10 years ago

Hi,

Thank you.

Could you tell me what you changed and where the JVM crashed in the code ?

tebruno99 commented 10 years ago

Ghost4j: 0.4.6, 0.5.0, 0.5.1 and 0.5.2-SNAPSHOT Ghostscript: 9.10 and 9.14 Java: Java6 (Latest), Java7 (Latest)

On OSX I get: Invalid memory access of location 0x0 rip=0x0 at Ghostscript.java (runFile(String fileName): GhostscriptLibrary.instance.gsapi_run_file(getNativeInstanceByRef() .getValue(), fileName, 0, exitCode);

On Linux: # SIGSEGV (0xb) at pc=0x0000000000000000, pid=17329, tid=139761385789184

At: Ghostscript.java:runFile(String fileName) GhostscriptLibrary.instance.gsapi_run_file(getNativeInstanceByRef() .getValue(), fileName, 0, exitCode);

Linux Core Dump & Log: http://www.naveoss.com/ghost4j/issue32/core.zip http://www.naveoss.com/ghost4j/issue32/hs_err_pid11567.log Failing Sample: http://www.naveoss.com/ghost4j/issue32/MainConverter_SEGV.java

Fixed Sample: http://www.naveoss.com/ghost4j/issue32/MainConverter_FIXED.java

tebruno99 commented 10 years ago

The same applies to the other sample that uses runFile

zippy1978 commented 10 years ago

Thank you.... Something probably changed for runFile in the native API. I will have a look