viewgif.ps 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. % Copyright (C) 1989, 1992, 1993, 1998 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This software is provided AS-IS with no warranty, either express or
  4. % implied.
  5. %
  6. % This software is distributed under license and may not be copied,
  7. % modified or distributed except as expressly authorized under the terms
  8. % of the license contained in the file LICENSE in this distribution.
  9. %
  10. % For more information about licensing, please refer to
  11. % http://www.ghostscript.com/licensing/. For information on
  12. % commercial licensing, go to http://www.artifex.com/licensing/ or
  13. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  14. % San Rafael, CA 94903, U.S.A., +1(415)492-9861.
  15. % $Id: viewgif.ps,v 1.5 2003/04/12 18:08:18 ray Exp $
  16. % viewgif.ps
  17. % Display a GIF file.
  18. /read1 % <file> read1 <int>
  19. { read pop
  20. } bind def
  21. /read2 % <file> read2 <int>
  22. { dup read1 exch read1 8 bitshift add
  23. } bind def
  24. /readGIFheader % <file> readGIFheader <dict>
  25. { 20 dict begin
  26. dup 6 string readstring pop
  27. dup (GIF87a) eq exch (GIF89a) eq or not
  28. { (Not a GIF file.\n) print cleartomark stop
  29. } if
  30. dup read2 /Width exch def
  31. dup read2 /Height exch def
  32. dup read1
  33. dup 128 ge /GlobalColor exch def
  34. dup -4 bitshift 7 and 1 add /BitsPerPixel exch def %***BOGUS?***
  35. dup 8 and 0 ne /PaletteSorted exch def
  36. 7 and 1 add dup /BitsPerPixel exch def
  37. 1 exch bitshift /PaletteSize exch def
  38. dup read1 /BackgroundIndex exch def
  39. dup read1 15 add 64 div /AspectRatio exch def
  40. GlobalColor
  41. { PaletteSize 3 mul string readstring pop
  42. /GlobalPalette exch def
  43. } if
  44. currentdict end
  45. } bind def
  46. /readGIFimageHeader % <file> readGIFimageHeader <dict>
  47. % Note: GIF header must be on dict stack
  48. { 10 dict begin
  49. { dup read1
  50. dup (!) 0 get ne { exit } if pop % extension
  51. dup read1 pop
  52. { dup read1 dup 0 eq { pop exit } if { dup read1 pop } repeat
  53. } loop
  54. } loop
  55. (,) 0 get ne
  56. { (Not a GIF image.\n) print stop
  57. } if
  58. dup read2 /Left exch def
  59. dup read2 /Top exch def
  60. dup read2 /Width exch def
  61. dup read2 /Height exch def
  62. dup read1
  63. dup 128 ge /LocalColor exch def
  64. dup 64 and 0 ne /Interlaced exch def
  65. LocalColor
  66. { 7 and 1 add /BitsPerPixel exch def
  67. 1 BitsPerPixel bitshift 3 mul string readstring pop
  68. /Palette exch def
  69. }
  70. { pop pop /Palette GlobalPalette def
  71. }
  72. ifelse
  73. currentdict end
  74. } bind def
  75. /imageGIF % <imagedict> imageGIF
  76. { /ImageOut where
  77. { pop
  78. % We know BitsPerComponent = 8, Decode = [0 255].
  79. % and there is only a single data source which is
  80. % either a filter or a string whose size is exactly
  81. % the width of the row.
  82. dup /DataSource get dup type /stringtype eq
  83. { ImageOut exch writestring
  84. }
  85. { pop dup /Width get string
  86. 1 index /Height get
  87. { 1 index /DataSource get 1 index readstring pop
  88. ImageOut exch writestring
  89. }
  90. repeat pop pop
  91. }
  92. ifelse
  93. }
  94. { image
  95. }
  96. ifelse
  97. } bind def
  98. /viewGIF % <file|string> viewGIF -
  99. { save 20 dict begin
  100. /saved exch def
  101. dup type /stringtype eq { (r) file } if
  102. /F exch def
  103. /ImageOutFile where { /ImageOut ImageOutFile (w) file def } if
  104. F readGIFheader /Header exch def
  105. currentdict Header end begin begin
  106. VGIFDEBUG { Header { exch == == } forall (----------------\n) print flush } if
  107. F readGIFimageHeader /ImageHeader exch def
  108. currentdict ImageHeader end begin begin
  109. VGIFDEBUG { ImageHeader { exch == == } forall (----------------\n) print flush } if
  110. /D F
  111. << /InitialCodeLength F read1
  112. /FirstBitLowOrder true
  113. /BlockData true
  114. /EarlyChange 0
  115. >> /LZWDecode filter def
  116. [/Indexed /DeviceRGB 1 BitsPerPixel bitshift 1 sub Palette] setcolorspace
  117. matrix currentmatrix
  118. 0 1 3 { 2 copy get dup 0 ne { dup abs div } if 3 copy put pop pop } for
  119. setmatrix
  120. << /ImageType 1
  121. /ImageMatrix [1 0 0 -1 0 Height]
  122. /BitsPerComponent 8
  123. /Decode [0 255]
  124. Interlaced
  125. { /Width Width /Height 1
  126. /row Width string def
  127. /DataSource row
  128. >> /I exch def
  129. /inter % <num> <denom> inter -
  130. { /denom exch def /num exch def
  131. gsave
  132. /lines Height denom 1 sub add num sub denom idiv def
  133. 0 1 lines 1 sub {
  134. Height exch denom mul num add sub
  135. I /ImageMatrix get 5 3 -1 roll put
  136. D row readstring pop pop
  137. I imageGIF
  138. } for
  139. grestore
  140. }
  141. bind def
  142. 0 8 inter
  143. 4 8 inter
  144. 2 4 inter
  145. 1 2 inter
  146. }
  147. { /Width Width /Height Height
  148. /DataSource D
  149. >> imageGIF
  150. }
  151. ifelse
  152. saved end end end restore
  153. } bind def