ifeq ($(OSTYPE),cygwin)
# If you get a link error like this:
# ld: cannot find libpng.a: No such file or directory
#
# This is how I built my own libpng.a:
#
#   tar xJf libpng-1.6.39.tar.xz
#   cd libpng-1.6.39/
#   sed -e 's/CC = gcc/CC = x86_64-w64-mingw32-gcc/' scripts/makefile.msys | sed -e 's/AR_RC = ar/AR_RC = x86_64-w64-mingw32-ar/' | sed -e 's/RANLIB = ranlib/RANLIB = x86_64-w64-mingw32-ranlib/' > Makefile
#   make libpng.a
#
# Reason for doing this is because Mingw on Cygwin doesn't come with
# libpng.a, but we would like to statically link in that library to simplify
# distribution.  The steps above do that.
#
# Note that it does a sed hack to manually select the compiler, as opposed
# to using libpng's configure script, because the latter will choose "gcc"
# and produce binaries that depend on cygwin1.dll.
#
# Also note that the example instructions above builds a specific version of
# libpng, which might be different from the headers shipped by Cygwin.  As
# of 2023-02-07, Cygwin is using 1.6.37 headers, which is about 2 years
# behind 1.6.39, but the differences appear to be trivial.  So I went with
# the latest libpng to pick up the latest fixes.
cc = x86_64-w64-mingw32-gcc
cxx = x86_64-w64-mingw32-g++
cflags = -O2 -Wall -pedantic
lflags = libpng.a -static -lz -lm
rand_patch = -include rand_patch.h
strip = x86_64-w64-mingw32-strip
eext = .exe
else
ifeq ($(ASAN),1)
cc = clang
cxx = clang++
cflags = -O1 -Wall -pedantic -g -fsanitize=address -fno-omit-frame-pointer
strip = touch
else
cc = gcc
cxx = g++
cflags = -O2 -Wall -pedantic
strip = strip
endif
lflags = -lpng -lm
rand_patch =
eext =
endif

ange_source = ange.c
charlotte_source = charlotte.c
dorothy_source = dorothy.c
beatrice_source = beatrice.c
chise_source = chise.c

# {{{ Build targets.
targets = \
	ange1$(eext) \
	ange2$(eext) \
	charlotte1$(eext) \
	charlotte2$(eext) \
	dorothy1$(eext) \
	dorothy2$(eext) \
	beatrice1$(eext) \
	beatrice2$(eext) \
	chise1$(eext) \
	chise2$(eext) \
	stack_png$(eext) \
	concat_png_lr$(eext) \
	concat_png_tb$(eext) \
	make_png$(eext)

test_targets = \
	test_compile_all.passed \
	test_ange1a.passed test_ange2a.passed \
	test_ange1b.passed test_ange2b.passed \
	test_ange1c.passed test_ange2c.passed \
	test_ange1d.passed test_ange2d.passed \
	test_ange1_help_msg.passed test_ange2_help_msg.passed \
	test_ange1_read_error.passed test_ange2_read_error.passed \
	test_ange1_write_error.passed test_ange2_write_error.passed \
	test_charlotte1a.passed test_charlotte2a.passed \
	test_charlotte1b.passed test_charlotte2b.passed \
	test_charlotte1c.passed test_charlotte2c.passed \
	test_charlotte1d.passed test_charlotte2d.passed \
	test_charlotte1_help_msg.passed test_charlotte2_help_msg.passed \
	test_charlotte1_read_error.passed test_charlotte2_read_error.passed \
	test_charlotte1_write_error.passed test_charlotte2_write_error.passed \
	test_dorothy1a.passed test_dorothy2a.passed \
	test_dorothy1b.passed test_dorothy2b.passed \
	test_dorothy1c.passed test_dorothy2c.passed \
	test_dorothy1d.passed test_dorothy2d.passed \
	test_dorothy1_help_msg.passed test_dorothy2_help_msg.passed \
	test_dorothy1_read_error.passed test_dorothy2_read_error.passed \
	test_dorothy1_write_error.passed test_dorothy2_write_error.passed \
	test_beatrice1a.passed test_beatrice2a.passed \
	test_beatrice1b.passed test_beatrice2b.passed \
	test_beatrice1c.passed test_beatrice2c.passed \
	test_beatrice1d.passed test_beatrice2d.passed \
	test_beatrice1_help_msg.passed test_beatrice2_help_msg.passed \
	test_beatrice1_read_error.passed test_beatrice2_read_error.passed \
	test_beatrice1_write_error.passed test_beatrice2_write_error.passed \
	test_chise1a.passed test_chise2a.passed \
	test_chise1b.passed test_chise2b.passed \
	test_chise1c.passed test_chise2c.passed \
	test_chise1d.passed test_chise2d.passed \
	test_chise1_help_msg.passed test_chise2_help_msg.passed \
	test_chise1_read_error.passed test_chise2_read_error.passed \
	test_chise1_write_error.passed test_chise2_write_error.passed \
	test_chain1a.passed test_chain2a.passed \
	test_chain1b.passed test_chain2b.passed \
	test_embedded_data.passed test_incomplete_embedded_data.passed \
	test_stack_png.passed \
	test_concat_png_lr.passed \
	test_concat_png_tb.passed \
	test_make_png.passed \
	test_sample.png test_small_sample.png

all: $(targets)

test: $(test_targets)

# }}}

# {{{ Binaries.
ange1$(eext): $(ange_source)
	$(cc) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

ange2$(eext): $(ange_source)
	$(cxx) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

charlotte1$(eext): $(charlotte_source)
	$(cc) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

charlotte2$(eext): $(charlotte_source)
	$(cxx) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

dorothy1$(eext): $(dorothy_source)
	$(cc) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

dorothy2$(eext): $(dorothy_source)
	$(cxx) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

beatrice1$(eext): $(beatrice_source)
	$(cc) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

beatrice2$(eext): $(beatrice_source)
	$(cxx) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

chise1$(eext): $(chise_source)
	$(cc) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

chise2$(eext): $(chise_source)
	$(cxx) $(cflags) $(rand_patch) $< $(lflags) -o $@
	$(strip) $@

stack_png$(eext): stack_png.o png_cache.o
	$(cc) $(cflags) $^ $(lflags) -o $@
	$(strip) $@

concat_png_lr$(eext): concat_png_lr.o png_cache.o
	$(cc) $(cflags) $^ $(lflags) -o $@
	$(strip) $@

concat_png_tb$(eext): concat_png_tb.o png_cache.o
	$(cc) $(cflags) $^ $(lflags) -o $@
	$(strip) $@

stack_png.o: stack_png.c png_cache.h
	$(cc) $(cflags) -c $< -o $@

concat_png_lr.o: concat_png_lr.c png_cache.h
	$(cc) $(cflags) -c $< -o $@

concat_png_tb.o: concat_png_tb.c png_cache.h
	$(cc) $(cflags) -c $< -o $@

png_cache.o: png_cache.c png_cache.h
	$(cc) $(cflags) -c $< -o $@

make_png$(eext): make_png.c
	$(cc) $(cflags) $< $(lflags) -o $@
	$(strip) $@

verify_split$(eext): verify_split.c
	$(cc) $(cflags) $< $(lflags) -o $@

# }}}

# {{{ Test images.
lizard.png: lizard.jpg
	djpeg $< | pnmtopng -compression 9 > $@

small.png: $(ange_source) $(charlotte_source) $(dorothy_source) $(beatrice_source) $(chise_source)
	cat $^ | ruby > $@

white.png:
	ppmmake rgb:ff/ff/ff 1920 1080 | pnmtopng > $@

faint_orange.png: make_png$(eext)
	./make_png$(eext) 509 1021 251 127 61 7 > $@

random.png:
	perl -e 'print "P6\n256 128\n255\n"; for($$i=0;$$i<256*128*3;$$i++){print chr(rand(255));}' | pnmtopng > $@

# }}}


# {{{ Tests.
test_compile_all.passed:
	bash ./test_compile_all.sh && touch $@


test_ange1a.passed: test_split_png.sh ange1$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_ange2a.passed: test_split_png.sh ange2$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_ange1b.passed: test_split_png.sh ange1$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_ange2b.passed: test_split_png.sh ange2$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_ange1c.passed: test_split_png.sh ange1$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_ange2c.passed: test_split_png.sh ange2$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_ange1d.passed: test_split_png.sh ange1$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_ange2d.passed: test_split_png.sh ange2$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_ange1_help_msg.passed: test_help_msg.sh ange1$(eext)
	bash $^ && touch $@

test_ange2_help_msg.passed: test_help_msg.sh ange2$(eext)
	bash $^ && touch $@

test_ange1_read_error.passed: test_read_error.sh ange1$(eext)
	bash $^ && touch $@

test_ange2_read_error.passed: test_read_error.sh ange2$(eext)
	bash $^ && touch $@

test_ange1_write_error.passed: test_write_error.sh ange1$(eext) random.png
	bash $^ && touch $@

test_ange2_write_error.passed: test_write_error.sh ange2$(eext) random.png
	bash $^ && touch $@


test_charlotte1a.passed: test_split_png.sh charlotte1$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_charlotte2a.passed: test_split_png.sh charlotte2$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_charlotte1b.passed: test_split_png.sh charlotte1$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_charlotte2b.passed: test_split_png.sh charlotte2$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_charlotte1c.passed: test_split_png.sh charlotte1$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_charlotte2c.passed: test_split_png.sh charlotte2$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_charlotte1d.passed: test_split_png.sh charlotte1$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_charlotte2d.passed: test_split_png.sh charlotte2$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_charlotte1_help_msg.passed: test_help_msg.sh charlotte1$(eext)
	bash $^ && touch $@

test_charlotte2_help_msg.passed: test_help_msg.sh charlotte2$(eext)
	bash $^ && touch $@

test_charlotte1_read_error.passed: test_read_error.sh charlotte1$(eext)
	bash $^ && touch $@

test_charlotte2_read_error.passed: test_read_error.sh charlotte2$(eext)
	bash $^ && touch $@

test_charlotte1_write_error.passed: test_write_error.sh charlotte1$(eext) random.png
	bash $^ && touch $@

test_charlotte2_write_error.passed: test_write_error.sh charlotte2$(eext) random.png
	bash $^ && touch $@


test_dorothy1a.passed: test_split_png.sh dorothy1$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_dorothy2a.passed: test_split_png.sh dorothy2$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_dorothy1b.passed: test_split_png.sh dorothy1$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_dorothy2b.passed: test_split_png.sh dorothy2$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_dorothy1c.passed: test_split_png.sh dorothy1$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_dorothy2c.passed: test_split_png.sh dorothy2$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_dorothy1d.passed: test_split_png.sh dorothy1$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_dorothy2d.passed: test_split_png.sh dorothy2$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_dorothy1_help_msg.passed: test_help_msg.sh dorothy1$(eext)
	bash $^ && touch $@

test_dorothy2_help_msg.passed: test_help_msg.sh dorothy2$(eext)
	bash $^ && touch $@

test_dorothy1_read_error.passed: test_read_error.sh dorothy1$(eext)
	bash $^ && touch $@

test_dorothy2_read_error.passed: test_read_error.sh dorothy2$(eext)
	bash $^ && touch $@

test_dorothy1_write_error.passed: test_write_error.sh dorothy1$(eext) random.png
	bash $^ && touch $@

test_dorothy2_write_error.passed: test_write_error.sh dorothy2$(eext) random.png
	bash $^ && touch $@


test_beatrice1a.passed: test_split_png.sh beatrice1$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_beatrice2a.passed: test_split_png.sh beatrice2$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_beatrice1b.passed: test_split_png.sh beatrice1$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_beatrice2b.passed: test_split_png.sh beatrice2$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_beatrice1c.passed: test_split_png.sh beatrice1$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_beatrice2c.passed: test_split_png.sh beatrice2$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_beatrice1d.passed: test_split_png.sh beatrice1$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_beatrice2d.passed: test_split_png.sh beatrice2$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_beatrice1_help_msg.passed: test_help_msg.sh beatrice1$(eext)
	bash $^ && touch $@

test_beatrice2_help_msg.passed: test_help_msg.sh beatrice2$(eext)
	bash $^ && touch $@

test_beatrice1_read_error.passed: test_read_error.sh beatrice1$(eext)
	bash $^ && touch $@

test_beatrice2_read_error.passed: test_read_error.sh beatrice2$(eext)
	bash $^ && touch $@

test_beatrice1_write_error.passed: test_write_error.sh beatrice1$(eext) random.png
	bash $^ && touch $@

test_beatrice2_write_error.passed: test_write_error.sh beatrice2$(eext) random.png
	bash $^ && touch $@


test_chise1a.passed: test_split_png.sh chise1$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_chise2a.passed: test_split_png.sh chise2$(eext) verify_split$(eext) stack_png$(eext) lizard.png
	bash $^ && touch $@

test_chise1b.passed: test_split_png.sh chise1$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_chise2b.passed: test_split_png.sh chise2$(eext) verify_split$(eext) stack_png$(eext) white.png
	bash $^ && touch $@

test_chise1c.passed: test_split_png.sh chise1$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_chise2c.passed: test_split_png.sh chise2$(eext) verify_split$(eext) stack_png$(eext) faint_orange.png
	bash $^ && touch $@

test_chise1d.passed: test_split_png.sh chise1$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_chise2d.passed: test_split_png.sh chise2$(eext) verify_split$(eext) stack_png$(eext) random.png
	bash $^ && touch $@

test_chise1_help_msg.passed: test_help_msg.sh chise1$(eext)
	bash $^ && touch $@

test_chise2_help_msg.passed: test_help_msg.sh chise2$(eext)
	bash $^ && touch $@

test_chise1_read_error.passed: test_read_error.sh chise1$(eext)
	bash $^ && touch $@

test_chise2_read_error.passed: test_read_error.sh chise2$(eext)
	bash $^ && touch $@

test_chise1_write_error.passed: test_write_error.sh chise1$(eext) random.png
	bash $^ && touch $@

test_chise2_write_error.passed: test_write_error.sh chise2$(eext) random.png
	bash $^ && touch $@


test_chain_expected_a.ppm: lizard.png
	pngtopam $< | ppmtoppm > $@

test_chain_expected_b.ppm: white.png
	pngtopam $< | ppmtoppm > $@


test_chain1a.passed: test_chain1a_stacked.png test_chain_expected_a.ppm
	pngtopam $< | ppmtoppm | diff -q test_chain_expected_a.ppm - && touch $@

test_chain1a_stacked.png: stack_png$(eext) test_chain1a_output1a.png test_chain1a_output2a.png test_chain1a_output3a.png test_chain1a_output4a.png test_chain1a_output5a.png test_chain1a_output5b.png
	./$^ > $@

test_chain1a_output5a.png: test_chain1a_output4b.png charlotte1$(eext)
	./charlotte1$(eext) $< test_chain1a_output5a.png test_chain1a_output5b.png

test_chain1a_output5b.png: test_chain1a_output5a.png

test_chain1a_output4a.png: test_chain1a_output3b.png ange1$(eext)
	./ange1$(eext) $< test_chain1a_output4a.png test_chain1a_output4b.png

test_chain1a_output4b.png: test_chain1a_output4a.png

test_chain1a_output3a.png: test_chain1a_output2b.png dorothy1$(eext)
	./dorothy1$(eext) $< test_chain1a_output3a.png test_chain1a_output3b.png

test_chain1a_output3b.png: test_chain1a_output3a.png

test_chain1a_output2a.png: test_chain1a_output1b.png beatrice1$(eext)
	./beatrice1$(eext) $< test_chain1a_output2a.png test_chain1a_output2b.png

test_chain1a_output2b.png: test_chain1a_output2a.png

test_chain1a_output1a.png: lizard.png chise1$(eext)
	./chise1$(eext) $< test_chain1a_output1a.png test_chain1a_output1b.png

test_chain1a_output1b.png: test_chain1a_output1a.png

test_chain1b.passed: test_chain1b_stacked.png test_chain_expected_b.ppm
	pngtopam $< | ppmtoppm | diff -q test_chain_expected_b.ppm - && touch $@

test_chain1b_stacked.png: stack_png$(eext) test_chain1b_output1a.png test_chain1b_output2a.png test_chain1b_output3a.png test_chain1b_output4a.png test_chain1b_output5a.png test_chain1b_output5b.png
	./$^ > $@

test_chain1b_output5a.png: test_chain1b_output4b.png charlotte1$(eext)
	./charlotte1$(eext) $< test_chain1b_output5a.png test_chain1b_output5b.png

test_chain1b_output5b.png: test_chain1b_output5a.png

test_chain1b_output4a.png: test_chain1b_output3b.png ange1$(eext)
	./ange1$(eext) $< test_chain1b_output4a.png test_chain1b_output4b.png

test_chain1b_output4b.png: test_chain1b_output4a.png

test_chain1b_output3a.png: test_chain1b_output2b.png dorothy1$(eext)
	./dorothy1$(eext) $< test_chain1b_output3a.png test_chain1b_output3b.png

test_chain1b_output3b.png: test_chain1b_output3a.png

test_chain1b_output2a.png: test_chain1b_output1b.png beatrice1$(eext)
	./beatrice1$(eext) $< test_chain1b_output2a.png test_chain1b_output2b.png

test_chain1b_output2b.png: test_chain1b_output2a.png

test_chain1b_output1a.png: white.png chise1$(eext)
	./chise1$(eext) $< test_chain1b_output1a.png test_chain1b_output1b.png

test_chain1b_output1b.png: test_chain1b_output1a.png


test_chain2a.passed: test_chain2a_stacked.png test_chain_expected_a.ppm
	pngtopam $< | ppmtoppm | diff -q test_chain_expected_a.ppm - && touch $@

test_chain2a_stacked.png: stack_png$(eext) test_chain2a_output1a.png test_chain2a_output2a.png test_chain2a_output3a.png test_chain2a_output4a.png test_chain2a_output5a.png test_chain2a_output5b.png
	./$^ > $@

test_chain2a_output5a.png: test_chain2a_output4b.png charlotte2$(eext)
	./charlotte2$(eext) $< test_chain2a_output5a.png test_chain2a_output5b.png

test_chain2a_output5b.png: test_chain2a_output5a.png

test_chain2a_output4a.png: test_chain2a_output3b.png ange2$(eext)
	./ange2$(eext) $< test_chain2a_output4a.png test_chain2a_output4b.png

test_chain2a_output4b.png: test_chain2a_output4a.png

test_chain2a_output3a.png: test_chain2a_output2b.png dorothy2$(eext)
	./dorothy2$(eext) $< test_chain2a_output3a.png test_chain2a_output3b.png

test_chain2a_output3b.png: test_chain2a_output3a.png

test_chain2a_output2a.png: test_chain2a_output1b.png beatrice2$(eext)
	./beatrice2$(eext) $< test_chain2a_output2a.png test_chain2a_output2b.png

test_chain2a_output2b.png: test_chain2a_output2a.png

test_chain2a_output1a.png: lizard.png chise2$(eext)
	./chise2$(eext) $< test_chain2a_output1a.png test_chain2a_output1b.png

test_chain2a_output1b.png: test_chain2a_output1a.png

test_chain2b.passed: test_chain2b_stacked.png test_chain_expected_b.ppm
	pngtopam $< | ppmtoppm | diff -q test_chain_expected_b.ppm - && touch $@

test_chain2b_stacked.png: stack_png$(eext) test_chain2b_output1a.png test_chain2b_output2a.png test_chain2b_output3a.png test_chain2b_output4a.png test_chain2b_output5a.png test_chain2b_output5b.png
	./$^ > $@

test_chain2b_output5a.png: test_chain2b_output4b.png charlotte2$(eext)
	./charlotte2$(eext) $< test_chain2b_output5a.png test_chain2b_output5b.png

test_chain2b_output5b.png: test_chain2b_output5a.png

test_chain2b_output4a.png: test_chain2b_output3b.png ange2$(eext)
	./ange2$(eext) $< test_chain2b_output4a.png test_chain2b_output4b.png

test_chain2b_output4b.png: test_chain2b_output4a.png

test_chain2b_output3a.png: test_chain2b_output2b.png dorothy2$(eext)
	./dorothy2$(eext) $< test_chain2b_output3a.png test_chain2b_output3b.png

test_chain2b_output3b.png: test_chain2b_output3a.png

test_chain2b_output2a.png: test_chain2b_output1b.png beatrice2$(eext)
	./beatrice2$(eext) $< test_chain2b_output2a.png test_chain2b_output2b.png

test_chain2b_output2b.png: test_chain2b_output2a.png

test_chain2b_output1a.png: white.png chise2$(eext)
	./chise2$(eext) $< test_chain2b_output1a.png test_chain2b_output1b.png

test_chain2b_output1b.png: test_chain2b_output1a.png


test_embedded_data.passed: test_embedded_data.sh $(ange_source) $(charlotte_source) $(dorothy_source) $(beatrice_source) $(chise_source)
	bash $^ && touch $@

test_incomplete_embedded_data.passed: test_incomplete_embedded_data.sh $(ange_source) $(charlotte_source) $(dorothy_source) $(beatrice_source) $(chise_source)
	bash $^ && touch $@


test_stack_png.passed: \
	test_opaque1.passed \
	test_opaque2.passed \
	test_transparent.passed \
	test_single_gradient.passed \
	test_double_gradient1.passed \
	test_double_gradient2.passed \
	test_double_gradient3.passed \
	test_triple_gradient1.passed \
	test_triple_gradient2.passed \
	test_triple_gradient3.passed \
	test_triple_gradient4.passed \
	test_stack_png_dup.passed
	touch $@

test_opaque1.passed: test_stack_png.sh stack_png$(eext) test_black.pgm test_square0.pgm test_white.pgm
	bash $^ && touch $@

test_opaque2.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_black.pgm test_white.pgm
	bash $^ && touch $@

test_transparent.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_white.pgm test_black.pgm
	bash $^ && touch $@

test_single_gradient.passed: test_stack_png.sh stack_png$(eext) test_black.pgm test_white.pgm test_square0.pgm
	bash $^ && touch $@

test_double_gradient1.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_white.pgm test_square1.pgm
	bash $^ && touch $@

test_double_gradient2.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_white.pgm test_square2.pgm
	bash $^ && touch $@

test_double_gradient3.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_white.pgm test_square3.pgm
	bash $^ && touch $@

test_triple_gradient1.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_square1.pgm test_square2.pgm
	bash $^ && touch $@

test_triple_gradient2.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_square1.pgm test_square3.pgm
	bash $^ && touch $@

test_triple_gradient3.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_square2.pgm test_square1.pgm
	bash $^ && touch $@

test_triple_gradient4.passed: test_stack_png.sh stack_png$(eext) test_square0.pgm test_square2.pgm test_square3.pgm
	bash $^ && touch $@

test_stack_png_dup.passed: test_stack_png_dup.sh stack_png$(eext)
	bash $^ && touch $@

test_black.pgm:
	ppmmake rgb:0/0/0 256 256 | ppmtopgm > $@

test_white.pgm:
	ppmmake rgb:ff/ff/ff 256 256 | ppmtopgm > $@

test_square0.pgm: test_line.pgm
	pnmcat -tb $< $< | pamstretch -xscale=1 -yscale=128 > $@

test_square1.pgm: test_square0.pgm
	pamflip -rotate90 $< > $@

test_square2.pgm: test_square0.pgm
	pamflip -leftright $< > $@

test_square3.pgm: test_square0.pgm
	pamflip -rotate270 $< > $@

test_line.pgm:
	perl -e 'print "P5\n256 1\n255\n"; print chr foreach 0..255;' > $@


test_concat_png_lr.passed: test_concat_png_lr.sh concat_png_lr$(eext) make_png$(eext)
	bash $^ && touch $@

test_concat_png_tb.passed: test_concat_png_tb.sh concat_png_tb$(eext) make_png$(eext)
	bash $^ && touch $@


test_make_png.passed: test_make_png.sh make_png$(eext)
	bash $^ && touch $@


test_sample.png: concat_png_tb$(eext) test_sample_row1.png test_sample_row2.png
	./$^ > $@

test_sample_row1.png: concat_png_lr$(eext) test_sample_charlotte1.png test_sample_ange1.png test_sample_dorothy1.png test_sample_beatrice1.png test_sample_chise1.png
	./$^ > $@

test_sample_row2.png: concat_png_lr$(eext) test_sample_charlotte2.png test_sample_ange2.png test_sample_dorothy2.png test_sample_beatrice2.png test_sample_chise2.png
	./$^ > $@

test_sample_charlotte1.png: charlotte1$(eext) make_png$(eext) stack_png$(eext) white.png
	./charlotte1$(eext) white.png test_sample_charlotte1a.png test_sample_charlotte1b.png
	./make_png$(eext) 1920 1080 0 0 0 | ./stack_png$(eext) - test_sample_charlotte1a.png > $@

test_sample_ange1.png: ange1$(eext) make_png$(eext) stack_png$(eext) white.png
	./ange1$(eext) white.png test_sample_ange1a.png test_sample_ange1b.png
	./make_png$(eext) 1920 1080 0 0 128 | ./stack_png$(eext) - test_sample_ange1a.png > $@

test_sample_dorothy1.png: dorothy1$(eext) make_png$(eext) stack_png$(eext) white.png
	./dorothy1$(eext) white.png test_sample_dorothy1a.png test_sample_dorothy1b.png
	./make_png$(eext) 1920 1080 0 128 0 | ./stack_png$(eext) - test_sample_dorothy1a.png > $@

test_sample_beatrice1.png: beatrice1$(eext) make_png$(eext) stack_png$(eext) white.png
	./beatrice1$(eext) white.png test_sample_beatrice1a.png test_sample_beatrice1b.png
	./make_png$(eext) 1920 1080 128 128 0 | ./stack_png$(eext) - test_sample_beatrice1a.png > $@

test_sample_chise1.png: chise1$(eext) make_png$(eext) stack_png$(eext) white.png
	./chise1$(eext) white.png test_sample_chise1a.png test_sample_chise1b.png
	./make_png$(eext) 1920 1080 128 0 0 | ./stack_png$(eext) - test_sample_chise1a.png > $@

test_sample_charlotte2.png: charlotte2$(eext) make_png$(eext) stack_png$(eext) white.png
	./charlotte2$(eext) white.png test_sample_charlotte2a.png test_sample_charlotte2b.png
	./make_png$(eext) 1920 1080 0 0 0 | ./stack_png$(eext) - test_sample_charlotte2a.png > $@

test_sample_ange2.png: ange2$(eext) make_png$(eext) stack_png$(eext) white.png
	./ange2$(eext) white.png test_sample_ange2a.png test_sample_ange2b.png
	./make_png$(eext) 1920 1080 0 0 128 | ./stack_png$(eext) - test_sample_ange2a.png > $@

test_sample_dorothy2.png: dorothy2$(eext) make_png$(eext) stack_png$(eext) white.png
	./dorothy2$(eext) white.png test_sample_dorothy2a.png test_sample_dorothy2b.png
	./make_png$(eext) 1920 1080 0 128 0 | ./stack_png$(eext) - test_sample_dorothy2a.png > $@

test_sample_beatrice2.png: beatrice2$(eext) make_png$(eext) stack_png$(eext) white.png
	./beatrice2$(eext) white.png test_sample_beatrice2a.png test_sample_beatrice2b.png
	./make_png$(eext) 1920 1080 128 128 0 | ./stack_png$(eext) - test_sample_beatrice2a.png > $@

test_sample_chise2.png: chise2$(eext) make_png$(eext) stack_png$(eext) white.png
	./chise2$(eext) white.png test_sample_chise2a.png test_sample_chise2b.png
	./make_png$(eext) 1920 1080 128 0 0 | ./stack_png$(eext) - test_sample_chise2a.png > $@


test_small_sample.png: concat_png_tb$(eext) test_small_sample_row1.png test_small_sample_row2.png
	./$^ > $@

test_small_sample_row1.png: concat_png_lr$(eext) test_small_sample_charlotte1.png test_small_sample_ange1.png test_small_sample_dorothy1.png test_small_sample_beatrice1.png test_small_sample_chise1.png
	./$^ > $@

test_small_sample_row2.png: concat_png_lr$(eext) test_small_sample_charlotte2.png test_small_sample_ange2.png test_small_sample_dorothy2.png test_small_sample_beatrice2.png test_small_sample_chise2.png
	./$^ > $@

test_small_sample_charlotte1.png: charlotte1$(eext) concat_png_tb$(eext) small.png
	./charlotte1$(eext) small.png test_small_sample_charlotte1a.png test_small_sample_charlotte1b.png
	./concat_png_tb$(eext) test_small_sample_charlotte1a.png test_small_sample_charlotte1b.png > $@

test_small_sample_ange1.png: ange1$(eext) concat_png_tb$(eext) small.png
	./ange1$(eext) small.png test_small_sample_ange1a.png test_small_sample_ange1b.png
	./concat_png_tb$(eext) test_small_sample_ange1a.png test_small_sample_ange1b.png > $@

test_small_sample_dorothy1.png: dorothy1$(eext) concat_png_tb$(eext) small.png
	./dorothy1$(eext) small.png test_small_sample_dorothy1a.png test_small_sample_dorothy1b.png
	./concat_png_tb$(eext) test_small_sample_dorothy1a.png test_small_sample_dorothy1b.png > $@

test_small_sample_beatrice1.png: beatrice1$(eext) concat_png_tb$(eext) small.png
	./beatrice1$(eext) small.png test_small_sample_beatrice1a.png test_small_sample_beatrice1b.png
	./concat_png_tb$(eext) test_small_sample_beatrice1a.png test_small_sample_beatrice1b.png > $@

test_small_sample_chise1.png: chise1$(eext) concat_png_tb$(eext) small.png
	./chise1$(eext) small.png test_small_sample_chise1a.png test_small_sample_chise1b.png
	./concat_png_tb$(eext) test_small_sample_chise1a.png test_small_sample_chise1b.png > $@

test_small_sample_charlotte2.png: charlotte2$(eext) concat_png_tb$(eext) small.png
	./charlotte2$(eext) small.png test_small_sample_charlotte2a.png test_small_sample_charlotte2b.png
	./concat_png_tb$(eext) test_small_sample_charlotte2a.png test_small_sample_charlotte2b.png > $@

test_small_sample_ange2.png: ange2$(eext) concat_png_tb$(eext) small.png
	./ange2$(eext) small.png test_small_sample_ange2a.png test_small_sample_ange2b.png
	./concat_png_tb$(eext) test_small_sample_ange2a.png test_small_sample_ange2b.png > $@

test_small_sample_dorothy2.png: dorothy2$(eext) concat_png_tb$(eext) small.png
	./dorothy2$(eext) small.png test_small_sample_dorothy2a.png test_small_sample_dorothy2b.png
	./concat_png_tb$(eext) test_small_sample_dorothy2a.png test_small_sample_dorothy2b.png > $@

test_small_sample_beatrice2.png: beatrice2$(eext) concat_png_tb$(eext) small.png
	./beatrice2$(eext) small.png test_small_sample_beatrice2a.png test_small_sample_beatrice2b.png
	./concat_png_tb$(eext) test_small_sample_beatrice2a.png test_small_sample_beatrice2b.png > $@

test_small_sample_chise2.png: chise2$(eext) concat_png_tb$(eext) small.png
	./chise2$(eext) small.png test_small_sample_chise2a.png test_small_sample_chise2b.png
	./concat_png_tb$(eext) test_small_sample_chise2a.png test_small_sample_chise2b.png > $@

# }}}



clean:
	-rm -f $(targets) verify_split$(eext) *.o lizard.png small.png white.png faint_orange.png random.png
	-rm -f test_*.pgm test_*.ppm test_*.png test_*.passed

backup:
	-rm -f backup.tar.gz
	tar czf backup.tar.gz *.c *.h *.sh *.rb *.map *.txt Makefile
