viewgif.ps 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. % Copyright (C) 1989, 1992, 1993, 1998 Aladdin Enterprises. All rights reserved.
  2. %
  3. % This file is part of AFPL Ghostscript.
  4. %
  5. % AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  6. % distributor accepts any responsibility for the consequences of using it, or
  7. % for whether it serves any particular purpose or works at all, unless he or
  8. % she says so in writing. Refer to the Aladdin Free Public License (the
  9. % "License") for full details.
  10. %
  11. % Every copy of AFPL Ghostscript must include a copy of the License, normally
  12. % in a plain ASCII text file named PUBLIC. The License grants you the right
  13. % to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14. % conditions described in the License. Among other things, the License
  15. % requires that the copyright notice and this notice be preserved on all
  16. % copies.
  17. % $Id: viewgif.ps,v 1.2 2000/09/19 18:29:11 lpd Exp $
  18. % viewgif.ps
  19. % Display a GIF file.
  20. /read1 % <file> read1 <int>
  21. { read pop
  22. } bind def
  23. /read2 % <file> read2 <int>
  24. { dup read1 exch read1 8 bitshift add
  25. } bind def
  26. /readGIFheader % <file> readGIFheader <dict>
  27. { 20 dict begin
  28. dup 6 string readstring pop
  29. dup (GIF87a) eq exch (GIF89a) eq or not
  30. { (Not a GIF file.\n) print cleartomark stop
  31. } if
  32. dup read2 /Width exch def
  33. dup read2 /Height exch def
  34. dup read1
  35. dup 128 ge /GlobalColor exch def
  36. dup -4 bitshift 7 and 1 add /BitsPerPixel exch def %***BOGUS?***
  37. dup 8 and 0 ne /PaletteSorted exch def
  38. 7 and 1 add dup /BitsPerPixel exch def
  39. 1 exch bitshift /PaletteSize exch def
  40. dup read1 /BackgroundIndex exch def
  41. dup read1 15 add 64 div /AspectRatio exch def
  42. GlobalColor
  43. { PaletteSize 3 mul string readstring pop
  44. /GlobalPalette exch def
  45. } if
  46. currentdict end
  47. } bind def
  48. /readGIFimageHeader % <file> readGIFimageHeader <dict>
  49. % Note: GIF header must be on dict stack
  50. { 10 dict begin
  51. { dup read1
  52. dup (!) 0 get ne { exit } if pop % extension
  53. dup read1 pop
  54. { dup read1 dup 0 eq { pop exit } if { dup read1 pop } repeat
  55. } loop
  56. } loop
  57. (,) 0 get ne
  58. { (Not a GIF image.\n) print stop
  59. } if
  60. dup read2 /Left exch def
  61. dup read2 /Top exch def
  62. dup read2 /Width exch def
  63. dup read2 /Height exch def
  64. dup read1
  65. dup 128 ge /LocalColor exch def
  66. dup 64 and 0 ne /Interlaced exch def
  67. LocalColor
  68. { 7 and 1 add /BitsPerPixel exch def
  69. 1 BitsPerPixel bitshift 3 mul string readstring pop
  70. /Palette exch def
  71. }
  72. { pop pop /Palette GlobalPalette def
  73. }
  74. ifelse
  75. currentdict end
  76. } bind def
  77. /imageGIF % <imagedict> imageGIF
  78. { /ImageOut where
  79. { pop
  80. % We know BitsPerComponent = 8, Decode = [0 255].
  81. % and there is only a single data source which is
  82. % either a filter or a string whose size is exactly
  83. % the width of the row.
  84. dup /DataSource get dup type /stringtype eq
  85. { ImageOut exch writestring
  86. }
  87. { pop dup /Width get string
  88. 1 index /Height get
  89. { 1 index /DataSource get 1 index readstring pop
  90. ImageOut exch writestring
  91. }
  92. repeat pop pop
  93. }
  94. ifelse
  95. }
  96. { image
  97. }
  98. ifelse
  99. } bind def
  100. /viewGIF % <file|string> viewGIF -
  101. { save 20 dict begin
  102. /saved exch def
  103. dup type /stringtype eq { (r) file } if
  104. /F exch def
  105. /ImageOutFile where { /ImageOut ImageOutFile (w) file def } if
  106. F readGIFheader /Header exch def
  107. currentdict Header end begin begin
  108. DEBUG { Header { exch == == } forall (----------------\n) print flush } if
  109. F readGIFimageHeader /ImageHeader exch def
  110. currentdict ImageHeader end begin begin
  111. DEBUG { ImageHeader { exch == == } forall (----------------\n) print flush } if
  112. /D F
  113. << /InitialCodeLength F read1
  114. /FirstBitLowOrder true
  115. /BlockData true
  116. /EarlyChange 0
  117. >> /LZWDecode filter def
  118. [/Indexed /DeviceRGB 1 BitsPerPixel bitshift 1 sub Palette] setcolorspace
  119. matrix currentmatrix
  120. 0 1 3 { 2 copy get dup 0 ne { dup abs div } if 3 copy put pop pop } for
  121. setmatrix
  122. << /ImageType 1
  123. /ImageMatrix [1 0 0 -1 0 Height]
  124. /BitsPerComponent 8
  125. /Decode [0 255]
  126. Interlaced
  127. { /Width Width /Height 1
  128. /row Width string def
  129. /DataSource row
  130. >> /I exch def
  131. /inter % <num> <denom> inter -
  132. { /denom exch def /num exch def
  133. gsave
  134. /lines Height denom 1 sub add num sub denom idiv def
  135. 0 1 lines 1 sub {
  136. Height exch denom mul num add sub
  137. I /ImageMatrix get 5 3 -1 roll put
  138. D row readstring pop pop
  139. I imageGIF
  140. } for
  141. grestore
  142. }
  143. bind def
  144. 0 8 inter
  145. 4 8 inter
  146. 2 4 inter
  147. 1 2 inter
  148. }
  149. { /Width Width /Height Height
  150. /DataSource D
  151. >> imageGIF
  152. }
  153. ifelse
  154. saved end end end restore
  155. } bind def