PageRenderTime 60ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/third_party/gofrontend/libgo/go/image/gif/reader.go

http://github.com/axw/llgo
Go | 494 lines | 373 code | 42 blank | 79 comment | 123 complexity | 0c8763c85b680fc8121cc11a8ac4c2b8 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. // Copyright 2011 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. // Package gif implements a GIF image decoder and encoder.
  5. //
  6. // The GIF specification is at http://www.w3.org/Graphics/GIF/spec-gif89a.txt.
  7. package gif
  8. import (
  9. "bufio"
  10. "compress/lzw"
  11. "errors"
  12. "fmt"
  13. "image"
  14. "image/color"
  15. "io"
  16. )
  17. var (
  18. errNotEnough = errors.New("gif: not enough image data")
  19. errTooMuch = errors.New("gif: too much image data")
  20. errBadPixel = errors.New("gif: invalid pixel value")
  21. )
  22. // If the io.Reader does not also have ReadByte, then decode will introduce its own buffering.
  23. type reader interface {
  24. io.Reader
  25. io.ByteReader
  26. }
  27. // Masks etc.
  28. const (
  29. // Fields.
  30. fColorTable = 1 << 7
  31. fInterlace = 1 << 6
  32. fColorTableBitsMask = 7
  33. // Graphic control flags.
  34. gcTransparentColorSet = 1 << 0
  35. gcDisposalMethodMask = 7 << 2
  36. )
  37. // Disposal Methods.
  38. const (
  39. DisposalNone = 0x01
  40. DisposalBackground = 0x02
  41. DisposalPrevious = 0x03
  42. )
  43. // Section indicators.
  44. const (
  45. sExtension = 0x21
  46. sImageDescriptor = 0x2C
  47. sTrailer = 0x3B
  48. )
  49. // Extensions.
  50. const (
  51. eText = 0x01 // Plain Text
  52. eGraphicControl = 0xF9 // Graphic Control
  53. eComment = 0xFE // Comment
  54. eApplication = 0xFF // Application
  55. )
  56. // decoder is the type used to decode a GIF file.
  57. type decoder struct {
  58. r reader
  59. // From header.
  60. vers string
  61. width int
  62. height int
  63. loopCount int
  64. delayTime int
  65. backgroundIndex byte
  66. disposalMethod byte
  67. // From image descriptor.
  68. imageFields byte
  69. // From graphics control.
  70. transparentIndex byte
  71. hasTransparentIndex bool
  72. // Computed.
  73. globalColorTable color.Palette
  74. // Used when decoding.
  75. delay []int
  76. disposal []byte
  77. image []*image.Paletted
  78. tmp [1024]byte // must be at least 768 so we can read color table
  79. }
  80. // blockReader parses the block structure of GIF image data, which
  81. // comprises (n, (n bytes)) blocks, with 1 <= n <= 255. It is the
  82. // reader given to the LZW decoder, which is thus immune to the
  83. // blocking. After the LZW decoder completes, there will be a 0-byte
  84. // block remaining (0, ()), which is consumed when checking that the
  85. // blockReader is exhausted.
  86. type blockReader struct {
  87. r reader
  88. slice []byte
  89. err error
  90. tmp [256]byte
  91. }
  92. func (b *blockReader) Read(p []byte) (int, error) {
  93. if b.err != nil {
  94. return 0, b.err
  95. }
  96. if len(p) == 0 {
  97. return 0, nil
  98. }
  99. if len(b.slice) == 0 {
  100. var blockLen uint8
  101. blockLen, b.err = b.r.ReadByte()
  102. if b.err != nil {
  103. return 0, b.err
  104. }
  105. if blockLen == 0 {
  106. b.err = io.EOF
  107. return 0, b.err
  108. }
  109. b.slice = b.tmp[:blockLen]
  110. if _, b.err = io.ReadFull(b.r, b.slice); b.err != nil {
  111. return 0, b.err
  112. }
  113. }
  114. n := copy(p, b.slice)
  115. b.slice = b.slice[n:]
  116. return n, nil
  117. }
  118. // decode reads a GIF image from r and stores the result in d.
  119. func (d *decoder) decode(r io.Reader, configOnly bool) error {
  120. // Add buffering if r does not provide ReadByte.
  121. if rr, ok := r.(reader); ok {
  122. d.r = rr
  123. } else {
  124. d.r = bufio.NewReader(r)
  125. }
  126. err := d.readHeaderAndScreenDescriptor()
  127. if err != nil {
  128. return err
  129. }
  130. if configOnly {
  131. return nil
  132. }
  133. for {
  134. c, err := d.r.ReadByte()
  135. if err != nil {
  136. return err
  137. }
  138. switch c {
  139. case sExtension:
  140. if err = d.readExtension(); err != nil {
  141. return err
  142. }
  143. case sImageDescriptor:
  144. m, err := d.newImageFromDescriptor()
  145. if err != nil {
  146. return err
  147. }
  148. useLocalColorTable := d.imageFields&fColorTable != 0
  149. if useLocalColorTable {
  150. m.Palette, err = d.readColorTable(d.imageFields)
  151. if err != nil {
  152. return err
  153. }
  154. } else {
  155. if d.globalColorTable == nil {
  156. return errors.New("gif: no color table")
  157. }
  158. m.Palette = d.globalColorTable
  159. }
  160. if d.hasTransparentIndex && int(d.transparentIndex) < len(m.Palette) {
  161. if !useLocalColorTable {
  162. // Clone the global color table.
  163. m.Palette = append(color.Palette(nil), d.globalColorTable...)
  164. }
  165. m.Palette[d.transparentIndex] = color.RGBA{}
  166. }
  167. litWidth, err := d.r.ReadByte()
  168. if err != nil {
  169. return err
  170. }
  171. if litWidth < 2 || litWidth > 8 {
  172. return fmt.Errorf("gif: pixel size in decode out of range: %d", litWidth)
  173. }
  174. // A wonderfully Go-like piece of magic.
  175. br := &blockReader{r: d.r}
  176. lzwr := lzw.NewReader(br, lzw.LSB, int(litWidth))
  177. defer lzwr.Close()
  178. if _, err = io.ReadFull(lzwr, m.Pix); err != nil {
  179. if err != io.ErrUnexpectedEOF {
  180. return err
  181. }
  182. return errNotEnough
  183. }
  184. // Both lzwr and br should be exhausted. Reading from them should
  185. // yield (0, io.EOF).
  186. //
  187. // The spec (Appendix F - Compression), says that "An End of
  188. // Information code... must be the last code output by the encoder
  189. // for an image". In practice, though, giflib (a widely used C
  190. // library) does not enforce this, so we also accept lzwr returning
  191. // io.ErrUnexpectedEOF (meaning that the encoded stream hit io.EOF
  192. // before the LZW decoder saw an explict end code), provided that
  193. // the io.ReadFull call above successfully read len(m.Pix) bytes.
  194. // See https://golang.org/issue/9856 for an example GIF.
  195. if n, err := lzwr.Read(d.tmp[:1]); n != 0 || (err != io.EOF && err != io.ErrUnexpectedEOF) {
  196. if err != nil {
  197. return err
  198. }
  199. return errTooMuch
  200. }
  201. if n, err := br.Read(d.tmp[:1]); n != 0 || err != io.EOF {
  202. if err != nil {
  203. return err
  204. }
  205. return errTooMuch
  206. }
  207. // Check that the color indexes are inside the palette.
  208. if len(m.Palette) < 256 {
  209. for _, pixel := range m.Pix {
  210. if int(pixel) >= len(m.Palette) {
  211. return errBadPixel
  212. }
  213. }
  214. }
  215. // Undo the interlacing if necessary.
  216. if d.imageFields&fInterlace != 0 {
  217. uninterlace(m)
  218. }
  219. d.image = append(d.image, m)
  220. d.delay = append(d.delay, d.delayTime)
  221. d.disposal = append(d.disposal, d.disposalMethod)
  222. // The GIF89a spec, Section 23 (Graphic Control Extension) says:
  223. // "The scope of this extension is the first graphic rendering block
  224. // to follow." We therefore reset the GCE fields to zero.
  225. d.delayTime = 0
  226. d.hasTransparentIndex = false
  227. case sTrailer:
  228. if len(d.image) == 0 {
  229. return io.ErrUnexpectedEOF
  230. }
  231. return nil
  232. default:
  233. return fmt.Errorf("gif: unknown block type: 0x%.2x", c)
  234. }
  235. }
  236. }
  237. func (d *decoder) readHeaderAndScreenDescriptor() error {
  238. _, err := io.ReadFull(d.r, d.tmp[:13])
  239. if err != nil {
  240. return err
  241. }
  242. d.vers = string(d.tmp[:6])
  243. if d.vers != "GIF87a" && d.vers != "GIF89a" {
  244. return fmt.Errorf("gif: can't recognize format %s", d.vers)
  245. }
  246. d.width = int(d.tmp[6]) + int(d.tmp[7])<<8
  247. d.height = int(d.tmp[8]) + int(d.tmp[9])<<8
  248. if fields := d.tmp[10]; fields&fColorTable != 0 {
  249. d.backgroundIndex = d.tmp[11]
  250. // readColorTable overwrites the contents of d.tmp, but that's OK.
  251. if d.globalColorTable, err = d.readColorTable(fields); err != nil {
  252. return err
  253. }
  254. }
  255. // d.tmp[12] is the Pixel Aspect Ratio, which is ignored.
  256. return nil
  257. }
  258. func (d *decoder) readColorTable(fields byte) (color.Palette, error) {
  259. n := 1 << (1 + uint(fields&fColorTableBitsMask))
  260. _, err := io.ReadFull(d.r, d.tmp[:3*n])
  261. if err != nil {
  262. return nil, fmt.Errorf("gif: short read on color table: %s", err)
  263. }
  264. j, p := 0, make(color.Palette, n)
  265. for i := range p {
  266. p[i] = color.RGBA{d.tmp[j+0], d.tmp[j+1], d.tmp[j+2], 0xFF}
  267. j += 3
  268. }
  269. return p, nil
  270. }
  271. func (d *decoder) readExtension() error {
  272. extension, err := d.r.ReadByte()
  273. if err != nil {
  274. return err
  275. }
  276. size := 0
  277. switch extension {
  278. case eText:
  279. size = 13
  280. case eGraphicControl:
  281. return d.readGraphicControl()
  282. case eComment:
  283. // nothing to do but read the data.
  284. case eApplication:
  285. b, err := d.r.ReadByte()
  286. if err != nil {
  287. return err
  288. }
  289. // The spec requires size be 11, but Adobe sometimes uses 10.
  290. size = int(b)
  291. default:
  292. return fmt.Errorf("gif: unknown extension 0x%.2x", extension)
  293. }
  294. if size > 0 {
  295. if _, err := io.ReadFull(d.r, d.tmp[:size]); err != nil {
  296. return err
  297. }
  298. }
  299. // Application Extension with "NETSCAPE2.0" as string and 1 in data means
  300. // this extension defines a loop count.
  301. if extension == eApplication && string(d.tmp[:size]) == "NETSCAPE2.0" {
  302. n, err := d.readBlock()
  303. if n == 0 || err != nil {
  304. return err
  305. }
  306. if n == 3 && d.tmp[0] == 1 {
  307. d.loopCount = int(d.tmp[1]) | int(d.tmp[2])<<8
  308. }
  309. }
  310. for {
  311. n, err := d.readBlock()
  312. if n == 0 || err != nil {
  313. return err
  314. }
  315. }
  316. }
  317. func (d *decoder) readGraphicControl() error {
  318. if _, err := io.ReadFull(d.r, d.tmp[:6]); err != nil {
  319. return fmt.Errorf("gif: can't read graphic control: %s", err)
  320. }
  321. flags := d.tmp[1]
  322. d.disposalMethod = (flags & gcDisposalMethodMask) >> 2
  323. d.delayTime = int(d.tmp[2]) | int(d.tmp[3])<<8
  324. if flags&gcTransparentColorSet != 0 {
  325. d.transparentIndex = d.tmp[4]
  326. d.hasTransparentIndex = true
  327. }
  328. return nil
  329. }
  330. func (d *decoder) newImageFromDescriptor() (*image.Paletted, error) {
  331. if _, err := io.ReadFull(d.r, d.tmp[:9]); err != nil {
  332. return nil, fmt.Errorf("gif: can't read image descriptor: %s", err)
  333. }
  334. left := int(d.tmp[0]) + int(d.tmp[1])<<8
  335. top := int(d.tmp[2]) + int(d.tmp[3])<<8
  336. width := int(d.tmp[4]) + int(d.tmp[5])<<8
  337. height := int(d.tmp[6]) + int(d.tmp[7])<<8
  338. d.imageFields = d.tmp[8]
  339. // The GIF89a spec, Section 20 (Image Descriptor) says:
  340. // "Each image must fit within the boundaries of the Logical
  341. // Screen, as defined in the Logical Screen Descriptor."
  342. bounds := image.Rect(left, top, left+width, top+height)
  343. if bounds != bounds.Intersect(image.Rect(0, 0, d.width, d.height)) {
  344. return nil, errors.New("gif: frame bounds larger than image bounds")
  345. }
  346. return image.NewPaletted(bounds, nil), nil
  347. }
  348. func (d *decoder) readBlock() (int, error) {
  349. n, err := d.r.ReadByte()
  350. if n == 0 || err != nil {
  351. return 0, err
  352. }
  353. return io.ReadFull(d.r, d.tmp[:n])
  354. }
  355. // interlaceScan defines the ordering for a pass of the interlace algorithm.
  356. type interlaceScan struct {
  357. skip, start int
  358. }
  359. // interlacing represents the set of scans in an interlaced GIF image.
  360. var interlacing = []interlaceScan{
  361. {8, 0}, // Group 1 : Every 8th. row, starting with row 0.
  362. {8, 4}, // Group 2 : Every 8th. row, starting with row 4.
  363. {4, 2}, // Group 3 : Every 4th. row, starting with row 2.
  364. {2, 1}, // Group 4 : Every 2nd. row, starting with row 1.
  365. }
  366. // uninterlace rearranges the pixels in m to account for interlaced input.
  367. func uninterlace(m *image.Paletted) {
  368. var nPix []uint8
  369. dx := m.Bounds().Dx()
  370. dy := m.Bounds().Dy()
  371. nPix = make([]uint8, dx*dy)
  372. offset := 0 // steps through the input by sequential scan lines.
  373. for _, pass := range interlacing {
  374. nOffset := pass.start * dx // steps through the output as defined by pass.
  375. for y := pass.start; y < dy; y += pass.skip {
  376. copy(nPix[nOffset:nOffset+dx], m.Pix[offset:offset+dx])
  377. offset += dx
  378. nOffset += dx * pass.skip
  379. }
  380. }
  381. m.Pix = nPix
  382. }
  383. // Decode reads a GIF image from r and returns the first embedded
  384. // image as an image.Image.
  385. func Decode(r io.Reader) (image.Image, error) {
  386. var d decoder
  387. if err := d.decode(r, false); err != nil {
  388. return nil, err
  389. }
  390. return d.image[0], nil
  391. }
  392. // GIF represents the possibly multiple images stored in a GIF file.
  393. type GIF struct {
  394. Image []*image.Paletted // The successive images.
  395. Delay []int // The successive delay times, one per frame, in 100ths of a second.
  396. LoopCount int // The loop count.
  397. // Disposal is the successive disposal methods, one per frame. For
  398. // backwards compatibility, a nil Disposal is valid to pass to EncodeAll,
  399. // and implies that each frame's disposal method is 0 (no disposal
  400. // specified).
  401. Disposal []byte
  402. // Config is the global color table (palette), width and height. A nil or
  403. // empty-color.Palette Config.ColorModel means that each frame has its own
  404. // color table and there is no global color table. Each frame's bounds must
  405. // be within the rectangle defined by the two points (0, 0) and
  406. // (Config.Width, Config.Height).
  407. //
  408. // For backwards compatibility, a zero-valued Config is valid to pass to
  409. // EncodeAll, and implies that the overall GIF's width and height equals
  410. // the first frame's bounds' Rectangle.Max point.
  411. Config image.Config
  412. // BackgroundIndex is the background index in the global color table, for
  413. // use with the DisposalBackground disposal method.
  414. BackgroundIndex byte
  415. }
  416. // DecodeAll reads a GIF image from r and returns the sequential frames
  417. // and timing information.
  418. func DecodeAll(r io.Reader) (*GIF, error) {
  419. var d decoder
  420. if err := d.decode(r, false); err != nil {
  421. return nil, err
  422. }
  423. gif := &GIF{
  424. Image: d.image,
  425. LoopCount: d.loopCount,
  426. Delay: d.delay,
  427. Disposal: d.disposal,
  428. Config: image.Config{
  429. ColorModel: d.globalColorTable,
  430. Width: d.width,
  431. Height: d.height,
  432. },
  433. BackgroundIndex: d.backgroundIndex,
  434. }
  435. return gif, nil
  436. }
  437. // DecodeConfig returns the global color model and dimensions of a GIF image
  438. // without decoding the entire image.
  439. func DecodeConfig(r io.Reader) (image.Config, error) {
  440. var d decoder
  441. if err := d.decode(r, true); err != nil {
  442. return image.Config{}, err
  443. }
  444. return image.Config{
  445. ColorModel: d.globalColorTable,
  446. Width: d.width,
  447. Height: d.height,
  448. }, nil
  449. }
  450. func init() {
  451. image.RegisterFormat("gif", "GIF8?a", Decode, DecodeConfig)
  452. }