JAVAOUT=$(addprefix classes/, \
	net/cscott/pcbmill/SVGOutput.class \
	net/cscott/pcbmill/SVGParser.class \
        net/cscott/pcbmill/Healer.class \
        net/cscott/pcbmill/DXFOutput.class \
        gnu/java/awt/geom/GeneralPath.class \
        gnu/java/awt/geom/CubicCurve2D.class \
        gnu/java/awt/geom/QuadCurve2D.class \
        gnu/java/awt/geom/Tolerance.class \
        gnu/java/awt/geom/Line2D.class \
        net/cscott/mallard/Contour.class \
        net/cscott/mallard/Assemble.class \
        net/cscott/mallard/BaseColorSpace.class \
        net/cscott/mallard/LabColorSpace.class \
        net/cscott/mallard/LuvColorSpace.class \
	)
JAVAIN=$(addsuffix .java,$(basename $(notdir $(JAVAOUT))))
LIBS=lib/batik.jar lib/jutil.jar lib/java-getopt-1.0.9.jar lib/epsgraphics.jar
empty:=
space:= $(empty) $(empty)
CPATH=$(subst $(space),:,$(strip classes $(LIBS)))

export JSR14DISTR=$(HOME)/jsr14-ea-2_4a1
JSR14=$(JSR14DISTR)/scripts

# default assembly flags: just show/emit regions; don't scale or trim.
ASSEMBLY_FLAGS=-d

# my glass color list.
GLASS:=	-g 9971-125 -g 9971-110-2 -g 9971-121 -g 9971-130-8 -g 9971-171 \
	-g 9971-152 -g 9971-136 -g 9971-161 -g 9971-533-1 -g 9971-142 \
	-g 9971-337-2 -g 9971-325-2 -g 9971-327-6 -g 9971-339-6 \
	-g 9971-622-7 -g 9971-833-91 -g 9971-823-72 -g 9971-533-1 \
	-g 9971-136 -g 9971-123H
#	-g 9971-307 -g 9976-WO191 -g 9971-CINNB -g 9977-5502SP
# not found: 9976-WO58 -> image is 9976-wo-58.jpg
# not found: 9977-1313HS -> image is 9977-1313hs.gif (!)

all: test.out.dxf

clean:
	-$(RM) *.pbm *.ras *.svg *.ele *.node *.edge *.neigh *.poly
	-$(RM) -rf classes

# Image pre-processing rules.
%.pbm: %.pgm
	pgmtopbm -threshold $< > $@
%.pbm: %.tiff # tiff is *much* smaller!
	convert $< $@
%.pbm: %.gif
	convert $< $@
%.svg: %.pbm
	potrace -o $@ -r 72 -s $<

# Java compilation rules
$(JAVAOUT): $(JAVAIN)
	mkdir -p classes
	$(JSR14)/javac -classpath $(CPATH) -source 1.5 -d classes -g $^
# Outline->.Poly file
%.poly: %.svg $(JAVAOUT)
	$(JSR14)/java -ea -cp $(CPATH) net.cscott.mallard.Contour \
		$(CONTOUR_FLAGS) $< $@

# Voronoi Skeletonization Rules.
triangle/triangle:
	$(MAKE) -C triangle

%.1.poly %.1.ele %.1.node %.1.edge %.1.neigh: %.poly triangle/triangle
	triangle/triangle -nespLj $*
.SECONDARY: %.poly
.PRECIOUS: %.poly

# Delauney triangulation->DXF
%.out.dxf: %.1.ele $(JAVAOUT)
	$(JSR14)/java -Xmx1G -ea -cp $(CPATH) net.cscott.mallard.Assemble \
		$(ASSEMBLY_FLAGS) $(DISPLAY_FLAG) $(GLASS) $*
%.out.png: %.1.ele $(JAVAOUT)
	$(JSR14)/java -Xmx1G -ea -cp $(CPATH) net.cscott.mallard.Assemble \
		-f png $(ASSEMBLY_FLAGS) $(DISPLAY_FLAG) $(GLASS) $*
%.out.svg: %.1.ele $(JAVAOUT)
	$(JSR14)/java -Xmx1G -ea -cp $(CPATH) net.cscott.mallard.Assemble \
		-f svg $(ASSEMBLY_FLAGS) $(DISPLAY_FLAG) $(GLASS) $*
%.out.eps: %.1.ele $(JAVAOUT)
	$(JSR14)/java -Xmx1G -ea -cp $(CPATH) net.cscott.mallard.Assemble \
		-f eps $(ASSEMBLY_FLAGS) $(DISPLAY_FLAG) $(GLASS) $*
# scale eps to fit on a letter-sized page.
%.ps: %.eps
	epsffit -c -m -s 36 36 540 756 $< $@
%.pdf: %.ps
	ps2pdf $< $@

# was -s 17.7722x48.5016 -t 2.24973+30.0872
MALLARD1_IMAGE=$(if $(wildcard mallard1.tiff),mallard1.tiff,mallard1.jpg)
mallard1-outline.out.%: ASSEMBLY_FLAGS=-s 17.773x48.5023 -t 14x11+2.24973+30.088 -o .0625 -c .1 --color-image $(MALLARD1_IMAGE)
# was 18.988x33.8261, t 14x11+1.99869+16.769
MALLARD2_IMAGE=$(if $(wildcard mallard2.tiff),mallard2.tiff,mallard2.jpg)
mallard2-outline.out.%: ASSEMBLY_FLAGS=-s 19.185374x33.826876 -t 14x11+2.19156+16.7675 -o .0625 -c .1 --color-image $(MALLARD2_IMAGE)
duck1.out.%: ASSEMBLY_FLAGS=-s 6.75x6 -t 9x6.3134-1.48949-.218438 -o .0625 -c .1
test.out.%: ASSEMBLY_FLAGS=-o .0625 -c .1 --color-image test-color.jpg
test.out.dxf: DISPLAY_FLAG=-d


# misc.
ChangeLog:
	if [ -d CVS ]; then \
	  $(RM) $@; \
	  rcs2log > $@ ; \
	fi

dist:	mallard.tgz
mallard.tgz:	Makefile ChangeLog COPYING AUTHORS NEWS README \
	$(JAVAIN) $(LIBS) \
	triangle/makefile triangle/README triangle/A.poly \
	triangle/showme.c triangle/triangle.c triangle/triangle.h \
	triangle/tricall.c \
	$(wildcard *.tiff) test-color.jpg mallard1.jpg mallard2.jpg
	tar -czf $@ $^
mallard.jar: $(JAVAOUT)
	cd classes ; jar cf ../$@ gnu net
upload: mallard.tgz
	scp $^ catfish.csail.mit.edu:public_html/Projects/Mallard
