You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

189 lines
7.7 KiB

1 month ago
  1. % Copyright (C) 2001-2023 Artifex Software, Inc.
  2. % All Rights Reserved.
  3. %
  4. % This software is provided AS-IS with no warranty, either express or
  5. % implied.
  6. %
  7. % This software is distributed under license and may not be copied,
  8. % modified or distributed except as expressly authorized under the terms
  9. % of the license contained in the file LICENSE in this distribution.
  10. %
  11. % Refer to licensing information at http://www.artifex.com or contact
  12. % Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  13. % CA 94129, USA, for further information.
  14. %
  15. % viewcmyk.ps
  16. % Display a raw CMYK file.
  17. % Requires the colorimage operator.
  18. % If SCALE is defined, maps input pixels to output pixels with that scale;
  19. % if SCALE is undefined, scales the image to fit the page.
  20. % If BITS is defined, it is the number of bits per sample (1,2,4,8);
  21. % if BITS is undefined, its default value is 1.
  22. % Colorspace defaults to cmyk, but -dGray or -dRGB can change it
  23. /viewraw { % <filename> <ncomp> <width> viewraw -
  24. 20 dict begin
  25. % Default ncomp is 4 == CMYK
  26. /w exch def
  27. /fname exch def
  28. /F fname (r) file def % the raw file
  29. /f /F load def % the usual DataSource
  30. /bpc /BITS where { pop BITS } { 1 } ifelse def
  31. /ncomp
  32. /Gray where
  33. { pop 1 /CS /DeviceGray def } % DeviceGray is 1 component
  34. { /RGB where
  35. { pop 3 BITS 8 ge % DeviceRGB is 3 component, use Indexed for 1, 2, 4 BITS
  36. { /CS /DeviceRGB def }
  37. {
  38. BITS 4 eq {
  39. /P 4096 3 mul string def % the palette
  40. 0 1 15 { /r exch def
  41. 0 1 15 { /g exch def
  42. 0 1 15 { /b exch def
  43. r 256 mul g 16 mul add b add 3 mul % base of the triplet
  44. P 1 index r 17 mul put
  45. P 1 index 1 add g 17 mul put
  46. P exch 2 add b 17 mul put
  47. } for
  48. } for
  49. } for
  50. /CS
  51. [ /Indexed /DeviceRGB 4095 P ] def
  52. /BITS 12 def % change to 4 bit indexed
  53. % redefine the DataSource to pack the 16-bit values into 12-bit
  54. % The 'proc' returns 2 12-bit pixels in 3 bytes
  55. % This proc is needed for the output from -sDEVICE=bitrgb -dGrayValues=16
  56. /S3 3 string def
  57. /f {
  58. F read
  59. {
  60. 256 mul F read
  61. pop add 16 mul S3 0 2 index 256 div cvi put
  62. 240 and F read
  63. { add S3 exch 1 exch put S3 2 F read pop put }
  64. { S3 exch 1 exch put S3 2 0 put }
  65. ifelse
  66. S3
  67. }
  68. { () }
  69. ifelse
  70. } bind def
  71. } if
  72. BITS 2 eq {
  73. /CS
  74. [ /Indexed /DeviceRGB 255 <
  75. 000000 000055 0000AA 0000FF
  76. 005500 005555 0055AA 0055FF
  77. 00AA00 00AA55 00AAAA 00AAFF
  78. 00FF00 00FF55 00FFAA 00FFFF
  79. 550000 550055 5500AA 5500FF
  80. 555500 555555 5555AA 5555FF
  81. 55AA00 55AA55 55AAAA 55AAFF
  82. 55FF00 55FF55 55FFAA 55FFFF
  83. AA0000 AA0055 AA00AA AA00FF
  84. AA5500 AA5555 AA55AA AA55FF
  85. AAAA00 AAAA55 AAAAAA AAAAFF
  86. AAFF00 AAFF55 AAFFAA AAFFFF
  87. FF0000 FF0055 FF00AA FF00FF
  88. FF5500 FF5555 FF55AA FF55FF
  89. FFAA00 FFAA55 FFAAAA FFAAFF
  90. FFFF00 FFFF55 FFFFAA FFFFFF
  91. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  92. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  93. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  94. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  95. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  96. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  97. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  98. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  99. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  100. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  101. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  102. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  103. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  104. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  105. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  106. 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000 000000
  107. >
  108. ] def
  109. /BITS 8 def % change to 4 bit indexed
  110. } if
  111. BITS 1 eq {
  112. /CS
  113. [ /Indexed /DeviceRGB 15 <
  114. 000000 0000FF 00FF00 00FFFF FF0000 FF00FF FFFF00 FFFFFF
  115. 000000 0000FF 00FF00 00FFFF FF0000 FF00FF FFFF00 FFFFFF
  116. >
  117. ] def
  118. /BITS 4 def % change to 4 bit indexed
  119. } if
  120. }
  121. ifelse
  122. }
  123. { 4 /CS /DeviceCMYK def } % DeviceCMYK is 4 component
  124. ifelse
  125. }
  126. ifelse
  127. def
  128. % Calculate Height from file length and width
  129. mark fname status pop pop pop /flen exch def cleartomark
  130. % NB: bitrgb writes 4 bits when BITS=1, 8 bits when BITS=2, 16 bits when BITS=4
  131. % presumably to keep values on nice boundaries this takes some fudging
  132. /h flen w bpc ncomp dup 3 eq bpc 8 lt and { 1 add } if mul mul 7 add 8 idiv idiv def
  133. %% (Dimensions: ) print [w h] == flush
  134. % Set up scaling.
  135. /SCALE where {
  136. pop
  137. % Map pixels SCALE-for-1. Assume orthogonal transformation.
  138. SCALE 1 0 dtransform add abs div
  139. SCALE 0 1 dtransform add abs div
  140. } {
  141. % Scale the image (uniformly) to fit the page.
  142. clippath pathbbox pop pop translate
  143. pathbbox 3 -1 roll sub h div
  144. 3 1 roll exch sub w div .min dup
  145. } ifelse scale
  146. %% w h bpc [1 0 0 -1 0 h] f false ncomp colorimage
  147. CS setcolorspace
  148. << /ImageType 1 /Width w /Height h /ImageMatrix [1 0 0 -1 0 h]
  149. /MultipleDataSources false /DataSource /f load /BitsPerComponent BITS
  150. /Decode
  151. bpc 1 eq { % inverted sense for 1 bit per component
  152. [ [0] [ 1 0 ] [0] [ 0 15 ] [ 0 1 0 1 0 1 0 1 ] ] ncomp get
  153. } if
  154. bpc 2 eq {
  155. [ [0] [ 0 1 ] [0] [ 0 255 ] [ 0 1 0 1 0 1 0 1 ] ] ncomp get
  156. } if
  157. bpc 4 eq {
  158. [ [0] [ 0 1 ] [0] [ 0 4095 ] [ 1 0 1 0 1 0 1 0 ] ] ncomp get
  159. } if
  160. bpc 8 ge {
  161. [ [0] [ 0 1 ] [0] [ 0 1 0 1 0 1 ] [ 1 0 1 0 1 0 1 0 ] ] ncomp get
  162. } if
  163. >> image
  164. showpage
  165. F closefile
  166. end
  167. } bind def
  168. % If the program was invoked from the command line, run it now.
  169. [ .shellarguments {
  170. counttomark 2 eq {
  171. cvi viewraw
  172. } {
  173. cleartomark
  174. (Usage: gs -- viewraw filename.raw width\n) print
  175. ( e.g.: gs -- viewraw my.raw 2550\n) print flush
  176. (From version 9.50 you must supply permissions for this program to read the input file(s)\n) print flush
  177. (either by using -dNOSAFER or by supplying --permit-file-read=<filename>\n) = flush
  178. } ifelse
  179. } {
  180. pop
  181. } ifelse