aboutsummaryrefslogtreecommitdiff
path: root/dla2img.sh
blob: ba97d956fc3d78a58c37b5cba1a25945b1a8fdac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/sh

# This script deliberately written with old-school shell syntax, to
# support as many Bourne-like shells as possible. Do not complain that
# backticks are deprecated: this is supposed to (potentially) work
# on ancient shells that might exist on e.g. an Atari ST or Amiga. It
# *does* work on a wide variety of modern shells.

# The only required external commands we call are magick, rm, and cat.
# If stat is present, it will be called (but no problem, if it's
# missing).

# Tested with ImageMagick 7.1, but doesn't use any fancy features so
# it should work with any fairly recent ImageMagick.

SELF="`basename $0`"

# This line may get changed by the Makefile (do not hand-edit):
VERSION=0.3.1

# allow overriding magick path via environment.
if [ "$MAGICK" = "" ]; then
	MAGICK="magick"
fi

# only change this if the save file size changes in dla.xex.
SIZE="176x170"

# ...and if you change that, change this too:
FILESIZE="3740"

INFILE="$1"
OUTFILE="$2"

if [ -z "$INFILE" -o -z "$OUTFILE" -o "$INFILE" = "-h" -o "$INFILE" = "--help" ]; then
	cat <<EOF
$SELF v$VERSION - convert dla.xex saved files to image files.

Usage: $SELF [dla-file] [image-file] <magick-arguments ...>

[dla-file] is a file created by the Save option in dla.xex.

[image-file] is the output file: any image file that ImageMagick
knows how to create. Image type is determined by filename extension,
e.g. "filename.png" or "filename.bmp". Try "magick -list format|less"
to see the the full list.

If any arguments after the [image-file] are present, they will be
passed to the magick executable as-is. The most useful argument here
is probably -trim, which will automatically crop the black borders
around the image. See the ImageMagick documentation for the full list
of supported arguments.

See also: https://slackware.uk/~urchlay/repos/dla-asm
EOF
	exit 0
fi

shift
shift

if [ ! -e "$INFILE" ]; then
	cat "$INFILE" > /dev/null # to get an error like "No such file or directory"
	exit 1
fi

# if "stat" is missing, e.g. because we're on a non-GNU system that
# lacks the command, just skip the file size check (magick will error
# out instead; less user-friendly but no big deal).
if stat /dev/null >/dev/null 2>&1; then
	if [ "`stat -L -c '%s' "$INFILE"`" != "$FILESIZE" ]; then
		echo "$SELF: '$INFILE' not a DLA file (size != $FILESIZE)." 1>&2
		exit 1
	fi
fi

rm -f "$OUTFILE"

# do the conversion... if $OUTFILE has an unknown extension, we will
# not get an error, and the conversion will succeed (the output will
# just be a copy of the input!)
# The "eval" is here to make this script work on the original Bourne
# shell (yes, the one from V7 UNIX). Seriously. Even with spaces
# in the arguments!
eval "$MAGICK convert -size '$SIZE' -depth 1 "$@" gray:'$INFILE' '$OUTFILE'"

# if magick fails, exit with its error status (it already printed its
# error messages to stderr).
S="$?"
if [ "$S" != "0" ]; then
	echo "$SELF: conversion failed (invalid output file extension?)." 1>&2
	exit "$S"
fi

# if magick "succeeds" but the output file doesn't exist, complain.
if [ ! -e "$OUTFILE" ]; then
	echo "$SELF: conversion failed (probably '$INFILE' is not a DLA) file)." 1>&2
	exit 1;
fi

# if magick "succeeds" but the output is identical to the input, let
# the user know.
if cmp "$INFILE" "$OUTFILE" >/dev/null 2>&1; then
	rm -f "$OUTFILE"
	echo "$SELF: Unsupported filename extension; see ImageMagick docs." 1>&2
	exit 1
fi

exit 0