decode.go 6.9 KB

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