/
rgbview
executable file
·56 lines (52 loc) · 2.11 KB
/
rgbview
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
#!/usr/src/jcomeauictx/casperscript/bin/cs -S --
# adapted from https://stackoverflow.com/a/15167678/493161
/inches {72 mul} def
/pagewidth currentpagedevice /PageSize get aload pop /pageheight exch def def
(page width and height: ) print [pagewidth pageheight] ==
/infile argc 1 gt {argv 1 get} {(/dev/urandom)} ifelse def
/imagewidth argc 2 gt {argv 2 get cvi} {pagewidth 1 inches sub} ifelse def
/imageheight argc 3 gt
{argv 3 get cvi}
{[ infile status ] 1 get dup 0 gt
{imagewidth div 3 div cvi dup (height calculated from file size: ) print =}
{pageheight 1 inches sub}
ifelse}
ifelse def
/bits 8 def # maybe someday make it support different bits/sample
/flip argc 4 gt {argv 4 get} {(none)} ifelse def # flip (v), flop (h), both
128 string (displaying %s with width %d, height %d, %d bits per color)
[infile imagewidth imageheight bits] sprintf {==} {pop} ifelse
/rgbimage
<<
/ImageType 1
/Width imagewidth
/Height imageheight
/ImageMatrix <<
(none) [imagewidth 0 0 imageheight 0 0]
(flip) [imagewidth 0 0 imageheight neg 0 imageheight]
(flop) [imagewidth neg 0 0 imageheight imagewidth 0]
(both) [imagewidth neg 0 0 imageheight neg imagewidth imageheight]
>> flip get
/MultipleDataSources false
/DataSource infile (r) file
/BitsPerComponent bits
/Decode [0 1 0 1 0 1]
/Interpolate false
>> def
save
# RGB image data is, presumably, ordinarily stored top to bottom, left to
# right; but PostScript draws from bottom to top unless the CTM (Current
# Transformation Matrix), ImageMatrix, or `scale` operator change this.
# It turns out the images in the PDF files I'm using already expect the
# bottom-to-top rendering, having the bytes for the bottom line at the
# very end of the file.
0.5 inches 0.5 inches moveto currentpoint translate
# fill the page as best we can, keeping the 1:1 aspect ratio
/imageratio imageheight imagewidth div def
/pageratio pageheight pagewidth div def
pagewidth 1 inches sub dup imageratio mul 2 copy scale [0 0] astore
128 string (scale: %f %f) 3 -1 roll sprintf {==} {pop} ifelse
rgbimage
/DeviceRGB setcolorspace image
restore
showpage