Blog
ak's java blog
Eclipse Applications
[Download from sourceforge.jp] akJ OptipngWrapper
Other Java
dhw2png
ordrumbox(JP)
Tips

Links
EclipseCon 2005
many great tutorials pdfs.
Eclipse RCP
eclipsepowered
---
Japanese(日本語) Site
[EMAIL]

Creative Commons

Digimemo A501 to PNG File using dhw2ps.pl

at first really thanks to open Linux Converter for DigiMemo A501 nice code.


What is this

This is Java program which convert from ps files which is created by dhw2ps.pl to png files.

Download

You need Java2.

download digimemo2png.zip

it contain jar and sample datas.

known bugs.

pixel location little wrong.maybe 50 pixel of left side can't show.

how to use.

now tested only dhw2ps.pl version0.1 on Windows XP SP2

convert dhw files to ps.

you download dhw2ps.pl

dhw2ps.pl  --ps input1.dhw input2.dhw

convert ps files to png .

java -cp digimemo.jar DigiPSToPng c:\ input1.dhw input2.dhw

c:\ is output directoryname. you can chage freely.


file name is must ends dhw.becouse of batch action.

and output file is alwasy overwritten.

sampledatas

it's contained in zip files.

.png created by this program.


.dhw

.ps


.pdf (I use e to convert from ps to pdf:using pstoedit.)


codes

it's contained in zip files.

Javacode

DigiPSToPng.java

import java.util.ArrayList;
import java.util.List;

import java.io.File;

import javax.imageio.ImageIO;

import java.awt.BasicStroke;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.QuadCurve2D;
import java.awt.image.BufferedImage;

/*
 * Created on 2005/03/28
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 * 
 * this version really junk.
 * ignore color and pixelsize.
 * image size is fixed as width:426*2 height:599*2
 */

/**
 * 
 *
 */
public class DigiPSToPng{

public static int SIZE_X=420;
public static int SIZE_Y=595;



int multiple=2;
int limit=200;
int w=15/multiple;
int h=15/multiple;
int mapx=SIZE_X*multiple;
int mapy=SIZE_Y*multiple;


    public static void main(String[] args){
        DigiPSToPng test = new DigiPSToPng(args);

        
    }
    
    File outputDir;
    
    public void printUse(){
        System.out.println("DigiPSToPng outputdir [input1.dhw input2.dhw ..]");
    }
    
    public DigiPSToPng(String args[]){
        if(args.length<2){
            printUse();
            return;
        }
        
        outputDir=new File(args[0]);
        if(outputDir.isFile()){
            printUse();
            return;
        }
        
        DigimemoPSParser parser=new DigimemoPSParser();
        for(int k=1;k<args.length;k++){
        
        File input=new File(args[k]);
        int last=input.getName().toLowerCase().lastIndexOf(".dhw");
        if(last!=-1){
        String fileName=input.getName().substring(0,last);
        List list=parser.parse(new File(input.getParentFile(),input.getName()+".ps"));//don't support .PS
        

        int boundx=SIZE_X*multiple;
        int boundy=SIZE_Y*multiple;
        BufferedImage image=new BufferedImage(boundx,boundy,BufferedImage.TYPE_INT_RGB);
        
        
        Graphics2D g2 = (Graphics2D)image.createGraphics();
       
        g2.setPaint(Color.black);
        
        g2.setBackground(Color.white);
        g2.clearRect(0,0,boundx,boundy);
        
        g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        
        
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
        BasicStroke roundStroke = new BasicStroke((float)0.5*multiple, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
        
        g2.setStroke(roundStroke);
       
        for(int i=0;i<list.size();i++){
            Point[] pts=(Point[]) list.get(i);
            draw(g2,pts);
        }

       
        
        boolean result = false;

        try {
            result = ImageIO.write(image, "png", new File(outputDir,fileName+".png"));
        } catch (Exception e) {
            e.printStackTrace();
            result = false;
        }
        }
        }
        
    }
   
    /**
     * @param pts
     */

    
    private void drawP(Graphics2D g,Point[] pts) {
        int w=20/multiple;
        int h=20/multiple;
        int mapx=426*multiple;
        int mapy=599*multiple;
       
        List pointList=splitValidPoint(pts);
        for (int i = 0; i < pointList.size(); i++) {
            Point[] dpt=(Point[]) pointList.get(i);
            if(dpt.length==1){
                g.drawLine(dpt[0].x,dpt[0].y,dpt[0].x,dpt[0].y);
            }else{
                for(int j=0;j<dpt.length-1;j++){
                    if(j+2<dpt.length){
                        g.draw(new QuadCurve2D.Double(dpt[j].x, dpt[j].y, dpt[j+1].x, dpt[j+1].y, dpt[j+2].x, dpt[j+2].y));
                       
                    }else{
                       
                        g.draw(new Line2D.Double(dpt[j].x, dpt[j].y, dpt[j+1].x, dpt[j+1].y));
                    }
                }
            }
        }
    }
    
    private List splitValidPoint(Point pts[]){
        //sometime happen problem.
        List list=new ArrayList();
        if(pts.length==0){
            return list;
        }
        
        List tmpP=new ArrayList();
        tmpP.add(convertPt(pts[0]));
        
        for(int i=1;i<pts.length;i++){
            int x=Math.abs(pts[i].x-pts[i-1].x);
            int y=Math.abs(pts[i].y-pts[i-1].y);
            if(x+y<limit){
                tmpP.add(convertPt(pts[i]));
            }else{
                if(tmpP.size()>0){
                list.add(tmpP.toArray(new Point[tmpP.size()]));
                tmpP.removeAll(tmpP);
                }
            }
        }
        
        if(tmpP.size()>0){
            list.add(tmpP.toArray(new Point[tmpP.size()]));
        }
        
        return list;
    }
    
    /**
     * @param point
     * @return
     */
    private Point convertPt(Point pts) {
        int dx1=(int)pts.x/w;
        
        int dy1=(int)mapy-(int)pts.y/h;
        
       return new Point(dx1,dy1);
    }


    
    private void draw(Graphics2D g,Point[] pts){
        List pointList=splitValidPoint(pts);
        for (int i = 0; i < pointList.size(); i++) {
            Point[] dpt=(Point[]) pointList.get(i);
            drawPoints(g,dpt);
        }
    	
    }
    private void drawPoints(Graphics2D g,Point[] pts) {
       
       
      
       GeneralPath rect1 = new GeneralPath();
       
    
     
       rect1.moveTo(pts[0].x,pts[0].y);
       
       
       for(int i=0;i<pts.length-1;i++){
           int x=Math.abs(pts[i].x-pts[i+1].x);
           int y=Math.abs(pts[i].y-pts[i+1].y);
           
           
           
           int tmpLimit=200;
           if(x+y<tmpLimit){
               if(i==0){
                   
               }else{
                   rect1.lineTo(pts[i+1].x,pts[i+1].y);
               }
               }else{
                  // System.out.println("line");
                   for(int k=0;k<pts.length;k++){
                 //      System.out.println("over:"+pts[k].x+","+pts[k].y);
                   }
                 
                  rect1.moveTo(pts[i+1].x,pts[i+1].y);
                  }
          
       }
       g.draw(rect1); 
       
       
    }
    
}

DigimemoPSParser.java

import java.util.ArrayList;
import java.util.List;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import java.awt.Point;

/*
 * Created on 2005/03/28
 * Author aki@www.xucker.jpn.org
 * License Apache2.0 or Common Public License
 */

/**
 * 
 *
 */
public class DigimemoPSParser {
public List parse(File file){
    List list=new ArrayList();
    BufferedReader reader;
    try {
        reader = new BufferedReader(new FileReader(file));
        
        String line;
        List tmpList=new ArrayList();
        while((line=reader.readLine())!=null){
            if(line.equals("E")){
                Point[] pts=(Point[]) tmpList.toArray(new Point[tmpList.size()]);
                list.add(pts);
                tmpList.removeAll(tmpList);
            }else if(line.length()>0 && Character.isDigit(line.charAt(0))){
                String[] vs=line.split(" ");
                if(vs.length==3){
                Point pt=new Point(Integer.parseInt(vs[0]),Integer.parseInt(vs[1]));
                tmpList.add(pt);
                }else{
                    System.out.println(line);
                }
            }
        }
        
        
        reader.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   
    return list;
}
}

bat files.

if you create shortcut you drag and drop,create png files.

sorry,I don't know how to write sh version.

perl dhw2ps.pl --ps %*

java -cp digimemo.jar DigiPSToPng c:\tmp %*