decode.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. // +build go1.6
  5. package webp
  6. import (
  7. "bytes"
  8. "errors"
  9. "image"
  10. "image/color"
  11. "io"
  12. "golang.org/x/image/riff"
  13. "golang.org/x/image/vp8"
  14. "golang.org/x/image/vp8l"
  15. )
  16. var errInvalidFormat = errors.New("webp: invalid format")
  17. var (
  18. fccALPH = riff.FourCC{'A', 'L', 'P', 'H'}
  19. fccVP8 = riff.FourCC{'V', 'P', '8', ' '}
  20. fccVP8L = riff.FourCC{'V', 'P', '8', 'L'}
  21. fccVP8X = riff.FourCC{'V', 'P', '8', 'X'}
  22. fccWEBP = riff.FourCC{'W', 'E', 'B', 'P'}
  23. )
  24. func decode(r io.Reader, configOnly bool) (image.Image, image.Config, error) {
  25. formType, riffReader, err := riff.NewReader(r)
  26. if err != nil {
  27. return nil, image.Config{}, err
  28. }
  29. if formType != fccWEBP {
  30. return nil, image.Config{}, errInvalidFormat
  31. }
  32. var (
  33. alpha []byte
  34. alphaStride int
  35. wantAlpha bool
  36. widthMinusOne uint32
  37. heightMinusOne uint32
  38. buf [10]byte
  39. )
  40. for {
  41. chunkID, chunkLen, chunkData, err := riffReader.Next()
  42. if err == io.EOF {
  43. err = errInvalidFormat
  44. }
  45. if err != nil {
  46. return nil, image.Config{}, err
  47. }
  48. switch chunkID {
  49. case fccALPH:
  50. if !wantAlpha {
  51. return nil, image.Config{}, errInvalidFormat
  52. }
  53. wantAlpha = false
  54. // Read the Pre-processing | Filter | Compression byte.
  55. if _, err := io.ReadFull(chunkData, buf[:1]); err != nil {
  56. if err == io.EOF {
  57. err = errInvalidFormat
  58. }
  59. return nil, image.Config{}, err
  60. }
  61. alpha, alphaStride, err = readAlpha(chunkData, widthMinusOne, heightMinusOne, buf[0]&0x03)
  62. if err != nil {
  63. return nil, image.Config{}, err
  64. }
  65. unfilterAlpha(alpha, alphaStride, (buf[0]>>2)&0x03)
  66. case fccVP8:
  67. if wantAlpha || int32(chunkLen) < 0 {
  68. return nil, image.Config{}, errInvalidFormat
  69. }
  70. d := vp8.NewDecoder()
  71. d.Init(chunkData, int(chunkLen))
  72. fh, err := d.DecodeFrameHeader()
  73. if err != nil {
  74. return nil, image.Config{}, err
  75. }
  76. if configOnly {
  77. return nil, image.Config{
  78. ColorModel: color.YCbCrModel,
  79. Width: fh.Width,
  80. Height: fh.Height,
  81. }, nil
  82. }
  83. m, err := d.DecodeFrame()
  84. if err != nil {
  85. return nil, image.Config{}, err
  86. }
  87. if alpha != nil {
  88. return &image.NYCbCrA{
  89. YCbCr: *m,
  90. A: alpha,
  91. AStride: alphaStride,
  92. }, image.Config{}, nil
  93. }
  94. return m, image.Config{}, nil
  95. case fccVP8L:
  96. if wantAlpha || alpha != nil {
  97. return nil, image.Config{}, errInvalidFormat
  98. }
  99. if configOnly {
  100. c, err := vp8l.DecodeConfig(chunkData)
  101. return nil, c, err
  102. }
  103. m, err := vp8l.Decode(chunkData)
  104. return m, image.Config{}, err
  105. case fccVP8X:
  106. if chunkLen != 10 {
  107. return nil, image.Config{}, errInvalidFormat
  108. }
  109. if _, err := io.ReadFull(chunkData, buf[:10]); err != nil {
  110. return nil, image.Config{}, err
  111. }
  112. const (
  113. animationBit = 1 << 1
  114. xmpMetadataBit = 1 << 2
  115. exifMetadataBit = 1 << 3
  116. alphaBit = 1 << 4
  117. iccProfileBit = 1 << 5
  118. )
  119. if buf[0] != alphaBit {
  120. return nil, image.Config{}, errors.New("webp: non-Alpha VP8X is not implemented")
  121. }
  122. widthMinusOne = uint32(buf[4]) | uint32(buf[5])<<8 | uint32(buf[6])<<16
  123. heightMinusOne = uint32(buf[7]) | uint32(buf[8])<<8 | uint32(buf[9])<<16
  124. if configOnly {
  125. return nil, image.Config{
  126. ColorModel: color.NYCbCrAModel,
  127. Width: int(widthMinusOne) + 1,
  128. Height: int(heightMinusOne) + 1,
  129. }, nil
  130. }
  131. wantAlpha = true
  132. default:
  133. return nil, image.Config{}, errInvalidFormat
  134. }
  135. }
  136. }
  137. func readAlpha(chunkData io.Reader, widthMinusOne, heightMinusOne uint32, compression byte) (
  138. alpha []byte, alphaStride int, err error) {
  139. switch compression {
  140. case 0:
  141. w := int(widthMinusOne) + 1
  142. h := int(heightMinusOne) + 1
  143. alpha = make([]byte, w*h)
  144. if _, err := io.ReadFull(chunkData, alpha); err != nil {
  145. return nil, 0, err
  146. }
  147. return alpha, w, nil
  148. case 1:
  149. // Read the VP8L-compressed alpha values. First, synthesize a 5-byte VP8L header:
  150. // a 1-byte magic number, a 14-bit widthMinusOne, a 14-bit heightMinusOne,
  151. // a 1-bit (ignored, zero) alphaIsUsed and a 3-bit (zero) version.
  152. // TODO(nigeltao): be more efficient than decoding an *image.NRGBA just to
  153. // extract the green values to a separately allocated []byte. Fixing this
  154. // will require changes to the vp8l package's API.
  155. if widthMinusOne > 0x3fff || heightMinusOne > 0x3fff {
  156. return nil, 0, errors.New("webp: invalid format")
  157. }
  158. alphaImage, err := vp8l.Decode(io.MultiReader(
  159. bytes.NewReader([]byte{
  160. 0x2f, // VP8L magic number.
  161. uint8(widthMinusOne),
  162. uint8(widthMinusOne>>8) | uint8(heightMinusOne<<6),
  163. uint8(heightMinusOne >> 2),
  164. uint8(heightMinusOne >> 10),
  165. }),
  166. chunkData,
  167. ))
  168. if err != nil {
  169. return nil, 0, err
  170. }
  171. // The green values of the inner NRGBA image are the alpha values of the
  172. // outer NYCbCrA image.
  173. pix := alphaImage.(*image.NRGBA).Pix
  174. alpha = make([]byte, len(pix)/4)
  175. for i := range alpha {
  176. alpha[i] = pix[4*i+1]
  177. }
  178. return alpha, int(widthMinusOne) + 1, nil
  179. }
  180. return nil, 0, errInvalidFormat
  181. }
  182. func unfilterAlpha(alpha []byte, alphaStride int, filter byte) {
  183. if len(alpha) == 0 || alphaStride == 0 {
  184. return
  185. }
  186. switch filter {
  187. case 1: // Horizontal filter.
  188. for i := 1; i < alphaStride; i++ {
  189. alpha[i] += alpha[i-1]
  190. }
  191. for i := alphaStride; i < len(alpha); i += alphaStride {
  192. // The first column is equivalent to the vertical filter.
  193. alpha[i] += alpha[i-alphaStride]
  194. for j := 1; j < alphaStride; j++ {
  195. alpha[i+j] += alpha[i+j-1]
  196. }
  197. }
  198. case 2: // Vertical filter.
  199. // The first row is equivalent to the horizontal filter.
  200. for i := 1; i < alphaStride; i++ {
  201. alpha[i] += alpha[i-1]
  202. }
  203. for i := alphaStride; i < len(alpha); i++ {
  204. alpha[i] += alpha[i-alphaStride]
  205. }
  206. case 3: // Gradient filter.
  207. // The first row is equivalent to the horizontal filter.
  208. for i := 1; i < alphaStride; i++ {
  209. alpha[i] += alpha[i-1]
  210. }
  211. for i := alphaStride; i < len(alpha); i += alphaStride {
  212. // The first column is equivalent to the vertical filter.
  213. alpha[i] += alpha[i-alphaStride]
  214. // The interior is predicted on the three top/left pixels.
  215. for j := 1; j < alphaStride; j++ {
  216. c := int(alpha[i+j-alphaStride-1])
  217. b := int(alpha[i+j-alphaStride])
  218. a := int(alpha[i+j-1])
  219. x := a + b - c
  220. if x < 0 {
  221. x = 0
  222. } else if x > 255 {
  223. x = 255
  224. }
  225. alpha[i+j] += uint8(x)
  226. }
  227. }
  228. }
  229. }
  230. // Decode reads a WEBP image from r and returns it as an image.Image.
  231. func Decode(r io.Reader) (image.Image, error) {
  232. m, _, err := decode(r, false)
  233. if err != nil {
  234. return nil, err
  235. }
  236. return m, err
  237. }
  238. // DecodeConfig returns the color model and dimensions of a WEBP image without
  239. // decoding the entire image.
  240. func DecodeConfig(r io.Reader) (image.Config, error) {
  241. _, c, err := decode(r, true)
  242. return c, err
  243. }
  244. func init() {
  245. image.RegisterFormat("webp", "RIFF????WEBPVP8", Decode, DecodeConfig)
  246. }