-
Notifications
You must be signed in to change notification settings - Fork 5
/
mediacopy.sh
executable file
·350 lines (289 loc) · 10.5 KB
/
mediacopy.sh
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/bash -e
# Save current screen from a VT340 as a sixel screenshot in print.six.
# THIS WORKS ON A GENUINE VT340, BUT COULD USE SOME POLISHING.
# For this to work, the VT340 probably has to have the following
# setting changed in the Setup menu:
#
# 1. Printer Set-Up -> Sixel Graphics Level = level 2
# Questions:
#
# * How do I inquire the geometry of the graphics screen?
# TODO:
# * Figure out why sometimes the VT340 will erroneously send a byte
# with the eighth bit set high, send a few more characters, and then
# pause for a long time before transmitting again. Test image
# tty.jpg triggers this glitch.
#
# Note: flakey cable is not the issue as I have the same trouble
# using the DECconnect port as with RS232.
#
# Next step: Glitched character isn't always the same, but is it
# always at the same spot(s) for a given image? It appears to be.
# What about at a different baudrate?
# * When image has finished saving, make sure it is a level 2 image.
# If not, show the commands needed to rescale to 1:1 aspect ratio.
# (Also, give a message suggesting that the user change the printing
# mode to level2 in the VT340 Set-Up screen).
#
# * (Add DPI data from the SSU to the PNG image instead of discarding it.)
#
# * Command line args should allow percentage or even row & column,
# not require pixel coords for geometry.
#
# * Negative geometry offsets should not presume the screen is 800x480 pixels.
#
# * Investigate if there is some secret, undocumented way to enable
# level 2 printing from the application side.
#
# * Rewrite this in C using lex/antlr or the routine in
# vt340test/charset/uplineload/mediacopy.c
#
########################################
main() {
### Defaults
decgpbm_flag=low # low: Do not print background by default
hostcomm=2 # 2: Send sixels to the terminal's host (not printer) comm port.
outputfile="print.six" # Default output filename is print.six.
errorfile=/dev/null # No debugging file by default. (try --debug --small)
trimheader="" # Set this to remove the VT340 sixel header.
pngoutput="" # Set this write PNG format, not sixel.
# Print full screen by default.
X1=-1; Y1=-1; X2=-1; Y2=-1
# Change defaults based on command line arguments.
parseargs "$@"
# Send the escape sequence to tell terminal to send the screen as sixels
sendmediacopy
# Redirect printing to the outputfile, debugging to errorfile.
exec >"$outputfile" 2>"$errorfile"
# Receive data from terminal
if [[ $hostcomm == 2 ]]; then receivesixeldata; fi |
if [[ $pngoutput ]]; then convert six:- png:-; else cat; fi
# TODO: Check if image got saved to print.six correctly as Level 2.
# If it didn't, then use ImageMagick to convert the image to the
# proper aspect ratio. (E.g., convert -sample 50%x100%).
# Note: trimheader shouldn't be necessary, but some programs can't
# handle the header that a VT340 sends for sixel media copies.
# They ought to be "ANSI" compatible and ignore unknown escape
# sequences.
}
parseargs() {
# Handle command line arguments
while [[ "$1" ]]; do
case "$1" in
--transparent|--46l) decgpbm_flag=low; shift ;;
--opaque|--46h) decgpbm_flag=high; shift ;;
--printer) hostcomm=0; shift ;;
--host) hostcomm=2; shift ;;
--out*|-o)
outputfile="${2}"
shift 2
if [[ $outputfile =~ png$ ]]; then
pngoutput=Yup
trimheader=Yup
fi
;;
--debug|-debug) DEBUG=yup;
shift
;;
--small) # For debugging, we can send just a small cropped part.
# (100x100, ~30 seconds)
X1=350; Y1=190; X2=449; Y2=289;
shift
;;
--above|-a) # Snapshot of anything above cursor position
echo -n $'\r' # Move cursor to beginning of line.
X1=4095; Y1=0; X2=-1; Y2=-1;
shift
;;
-t|--trim-header) trimheader="Yup"; shift ;; # For ImageMagick
-T|--no-trim-header) trimheader=""; shift ;; # \compatible sixel
-p|--png) pngoutput="Yup"; trimheader="Yup"
outputfile="${outputfile%.six}.png";
shift
;;
-P|--sixel) pngoutput="" ; shift ;;
# Handle geometry, e.g., 300x200+50+75
--geometry|-g) shift ;;
*x[0-9]*|*[0-9]x*|*+[0-9]*|*-[0-9]*)
read X1 Y1 X2 Y2 < <(parsegeometry "$1")
shift
;;
*) shift ;; # Ignore unknown fnords.
esac
done
if [[ "$DEBUG" ]]; then
errorfile="print.debug"
fi
portname=([0]="Printer" [2]="Host comm")
declare -A bgname
bgname=([high]="Print background" [low]="Transparent background")
if [[ "$DEBUG" ]]; then
if (( X1>=0 && X2<0 )); then echo -n $'\e7'; fi # Save cursor
cat <<-EOF >&2
DEBUG is on.
DECGPBM is $decgpbm_flag (${bgname[$decgpbm_flag]})
MediaCopy to ${portname[hostcomm]} port.
EOF
if (( X1<0 )); then
echo "Region to copy is entire screen." >&2
elif (( X2<0 )); then
echo "Region to copy is ($X1, $Y1) to cursor." >&2
echo -n $'\e8' # Restore cursor
else
echo "Region to copy is ($X1, $Y1) to ($X2, $Y2)." >&2
fi
fi
}
parsegeometry() {
# Given input of the form 256x192+50+75, (WIDTHxHEIGHT+X+Y)
# print four numbers X1 Y1 X2 Y2.
# X1 Y1: upper left coordinate, X2 Y2: lower right coordinate.
# Coordinates are zero-based, so 800x480+0+0 -> 0 0 799 479
# X, Y is taken as the coordinate of upper left corner normally.
# However, if X or Y is negative, it specifies the location of the
# right/lower corner offset from the rightmost/bottom edge,
# respectively. For example: 100x100-0-0 would copy the region
# from (700, 380) to (799, 479).
# A geometry specified as +X+Y (without the usual WIDTHxHEIGHT),
# will output "X Y -1 -1". This is useful for taking a screenshot
# from the current cursor position to (X, Y).
# TODO: This routine should detect the size of the screen instead
# of presuming the vt340's resolution (800x480) is correct.
# However, it is not a major bug as it only comes into play when
# negative offsets are specified.
local g="$1"
local -i w=0 h=0 x=0 y=0
if [[ ${g:0:1} =~ [0-9] ]]; then
# First number is width. Truncate anything after x, + or -
g2=${g%%x*}
g2=${g2%%+*}
w=${g2%%-*}
g=${g#$w} # Remove width from input
fi
if [[ ${g:0:1} == "x" ]]; then
# Next number is height
g=${g:1} # Skip the x
g2=${g%%+*}
h=${g2%%-*}
g=${g#$h} # Remove height from input
fi
local xsign=${g:0:1}
if [[ $xsign == "+" || $xsign == "-" ]]; then
# Next number is x offset
g=${g:1} # Skip the sign
g2=${g%%+*} # Remove y offset
x=${g2%%-*}
g=${g#$x} # Remove x value from input
fi
local ysign=${g:0:1}
if [[ $ysign == "+" || $ysign == "-" ]]; then
# Final number is y offset
g=${g:1} # Skip the sign
y=${g}
g=${g#$y} # Remove y from input
fi
local -i x2=x+w-1
local -i y2=y+h-1
if [[ "$xsign" == "-" ]]; then
x2=800-x-1
x=800-w-x
fi
if [[ "$ysign" == "-" ]]; then
y2=480-y-1
y=480-h-y
fi
if (( w <= 0 || h <= 0 )); then # No WxH specified, only +X+Y
if [[ "$xsign" == "-" ]]; then x=x2; fi
if [[ "$ysign" == "-" ]]; then y=y2; fi
x2=-1; y2=-1
fi
echo "$x $y $x2 $y2"
}
sendmediacopy() {
# Enable host flow-control in case the VT340 overwhelms it with data. :)
stty ixoff
# REGIS Screen Hard Copy with no parameters sends the whole screen,
# offset 50 pixels to the right. We use P[0,0] to disable the offset.
# Full screen on VT340 is equivalent to X1=0; Y1=0; X2=799; Y2=479
if (( X1<0 )); then
# No coordinates, defaults to fullscreen.
REGIS_H="S(H(P[0,0]))"
elif (( X1>=0 && X2<0 )); then
# One coordinate to copy to the cursor.
REGIS_H="S(H(P[0,0])[$X1,$Y1])"
else
# Two coords set the region to capture.
REGIS_H="S(H(P[0,0])[$X1,$Y1][$X2,$Y2])"
fi
CSI=$'\e[' # Control Sequence Introducer
DCS=$'\eP' # Device Control String
ST=$'\e\\' # String Terminator
FF=$'\f' # Form Feed
echo -n ${CSI}'?'$hostcomm'i' # (MC) 2: Send graphics to host comm
# 0: Send graphics to printer port
# DECGEPM: Graphics Expanded Print Mode (only for Level 1 Graphics)
#echo -n ${CSI}'?43l' # Compressed: 6" x 3" printout, 800x240
echo -n ${CSI}'?43h' # Expanded: 12" x 8" printout, 1600x480
# DECGPCM: Print Graphics Color Mode
#echo -n ${CSI}'?44l' # Print in black and white
echo -n ${CSI}'?44h' # Print in color
# DECGPCS: Print Graphics Color Syntax
#echo -n ${CSI}'?45l' # Print using HLS colors
echo -n ${CSI}'?45h' # Print using RGB colors (ImageMagick reqs)
# DECGPBM: Print Graphics Background Mode (always on for level 1 graphics)
if [[ $decgpbm_flag == "low" ]]; then
echo -n ${CSI}'?46l' # Do not send background (transparent bg)
else
echo -n ${CSI}'?46h' # Include background when printing
fi
# DECGRPM: Graphics Rotated Print Mode (90 degrees counterclockwise)
echo -n ${CSI}'?47l' # Use compress or expand to fit on printer.
#echo -n ${CSI}'?47h' # Rotate image CCW. 8" x 12" printout
# Send a hard copy using REGIS
echo -n ${DCS}'p' # Enter REGIS mode
echo -n ${REGIS_H} # Send hard copy sequence
echo -n ${ST} # Exit REGIS mode
}
receivesixeldata() {
# Receive sixel data until Esc \ is seen on stdin.
while read -r -s -d $'\e'; do
if [[ -z "$trimheader" ]]; then echo -n "$REPLY"$'\e'; fi
read -r -s -N1 next_char
cat -v >&2 <<<"Found in header $REPLY Esc $next_char"
if [[ "$next_char" == "P" ]]; then break; fi
if [[ -z "$trimheader" ]]; then echo -n "$next_char"; fi
done
echo "Found Esc P (DCS String Start)" >&2
if [[ "$trimheader" ]]; then echo -n $'\e'; fi
echo -n P
# Read until the escape-backslash that ends sixel data
while read -r -s -d $'\e' until_esc; do
echo "Reading data" >&2
echo -n "$until_esc"
# Allow multiple Esc chars in a row
second_char=$'\e'
while [[ "$second_char" == $'\e' ]]; do
echo -n $'\e'
read -r -s -N1 second_char
done
# Okay, at this point, we have just read an Esc followed by a
# second character that is not an escape.
# Is it the end of the sixel graphics?
if [[ "$second_char" == '\' ]]; then
echo -n '\'
break;
fi
# Nope, so write the data and keep going
echo -n "$second_char"
if LANG=C egrep -q "[^[:print:][:cntrl:]]" <<<"$REPLY"; then
# Debugging: check for non ASCII characters
echo >&2
echo 'WARNING: 8-bit Glitch. Received data with the eighth bit set high.'>&2
fi
# Debugging: log data to print.debug
date +"%s: " | tr -d '\n' >&2
echo -n "$REPLY" | sed $'s/\e/Esc /g' | cat -v >&2
done
echo "Finished reading data" >&2
}
main "$@"