decode.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. seenVP8X 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 seenVP8X {
  107. return nil, image.Config{}, errInvalidFormat
  108. }
  109. seenVP8X = true
  110. if chunkLen != 10 {
  111. return nil, image.Config{}, errInvalidFormat
  112. }
  113. if _, err := io.ReadFull(chunkData, buf[:10]); err != nil {
  114. return nil, image.Config{}, err
  115. }
  116. const (
  117. animationBit = 1 << 1
  118. xmpMetadataBit = 1 << 2
  119. exifMetadataBit = 1 << 3
  120. alphaBit = 1 << 4
  121. iccProfileBit = 1 << 5
  122. )
  123. wantAlpha = (buf[0] & alphaBit) != 0
  124. widthMinusOne = uint32(buf[4]) | uint32(buf[5])<<8 | uint32(buf[6])<<16
  125. heightMinusOne = uint32(buf[7]) | uint32(buf[8])<<8 | uint32(buf[9])<<16
  126. if configOnly {
  127. if wantAlpha {
  128. return nil, image.Config{
  129. ColorModel: color.NYCbCrAModel,
  130. Width: int(widthMinusOne) + 1,
  131. Height: int(heightMinusOne) + 1,
  132. }, nil
  133. }
  134. return nil, image.Config{
  135. ColorModel: color.YCbCrModel,
  136. Width: int(widthMinusOne) + 1,
  137. Height: int(heightMinusOne) + 1,
  138. }, nil
  139. }
  140. }
  141. }
  142. }
  143. func readAlpha(chunkData io.Reader, widthMinusOne, heightMinusOne uint32, compression byte) (
  144. alpha []byte, alphaStride int, err error) {
  145. switch compression {
  146. case 0:
  147. w := int(widthMinusOne) + 1
  148. h := int(heightMinusOne) + 1
  149. alpha = make([]byte, w*h)
  150. if _, err := io.ReadFull(chunkData, alpha); err != nil {
  151. return nil, 0, err
  152. }
  153. return alpha, w, nil
  154. case 1:
  155. // Read the VP8L-compressed alpha values. First, synthesize a 5-byte VP8L header:
  156. // a 1-byte magic number, a 14-bit widthMinusOne, a 14-bit heightMinusOne,
  157. // a 1-bit (ignored, zero) alphaIsUsed and a 3-bit (zero) version.
  158. // TODO(nigeltao): be more efficient than decoding an *image.NRGBA just to
  159. // extract the green values to a separately allocated []byte. Fixing this
  160. // will require changes to the vp8l package's API.
  161. if widthMinusOne > 0x3fff || heightMinusOne > 0x3fff {
  162. return nil, 0, errors.New("webp: invalid format")
  163. }
  164. alphaImage, err := vp8l.Decode(io.MultiReader(
  165. bytes.NewReader([]byte{
  166. 0x2f, // VP8L magic number.
  167. uint8(widthMinusOne),
  168. uint8(widthMinusOne>>8) | uint8(heightMinusOne<<6),
  169. uint8(heightMinusOne >> 2),
  170. uint8(heightMinusOne >> 10),
  171. }),
  172. chunkData,
  173. ))
  174. if err != nil {
  175. return nil, 0, err
  176. }
  177. // The green values of the inner NRGBA image are the alpha values of the
  178. // outer NYCbCrA image.
  179. pix := alphaImage.(*image.NRGBA).Pix
  180. alpha = make([]byte, len(pix)/4)
  181. for i := range alpha {
  182. alpha[i] = pix[4*i+1]
  183. }
  184. return alpha, int(widthMinusOne) + 1, nil
  185. }
  186. return nil, 0, errInvalidFormat
  187. }
  188. func unfilterAlpha(alpha []byte, alphaStride int, filter byte) {
  189. if len(alpha) == 0 || alphaStride == 0 {
  190. return
  191. }
  192. switch filter {
  193. case 1: // Horizontal filter.
  194. for i := 1; i < alphaStride; i++ {
  195. alpha[i] += alpha[i-1]
  196. }
  197. for i := alphaStride; i < len(alpha); i += alphaStride {
  198. // The first column is equivalent to the vertical filter.
  199. alpha[i] += alpha[i-alphaStride]
  200. for j := 1; j < alphaStride; j++ {
  201. alpha[i+j] += alpha[i+j-1]
  202. }
  203. }
  204. case 2: // Vertical filter.
  205. // The first row is equivalent to the horizontal filter.
  206. for i := 1; i < alphaStride; i++ {
  207. alpha[i] += alpha[i-1]
  208. }
  209. for i := alphaStride; i < len(alpha); i++ {
  210. alpha[i] += alpha[i-alphaStride]
  211. }
  212. case 3: // Gradient filter.
  213. // The first row is equivalent to the horizontal filter.
  214. for i := 1; i < alphaStride; i++ {
  215. alpha[i] += alpha[i-1]
  216. }
  217. for i := alphaStride; i < len(alpha); i += alphaStride {
  218. // The first column is equivalent to the vertical filter.
  219. alpha[i] += alpha[i-alphaStride]
  220. // The interior is predicted on the three top/left pixels.
  221. for j := 1; j < alphaStride; j++ {
  222. c := int(alpha[i+j-alphaStride-1])
  223. b := int(alpha[i+j-alphaStride])
  224. a := int(alpha[i+j-1])
  225. x := a + b - c
  226. if x < 0 {
  227. x = 0
  228. } else if x > 255 {
  229. x = 255
  230. }
  231. alpha[i+j] += uint8(x)
  232. }
  233. }
  234. }
  235. }
  236. // Decode reads a WEBP image from r and returns it as an image.Image.
  237. func Decode(r io.Reader) (image.Image, error) {
  238. m, _, err := decode(r, false)
  239. if err != nil {
  240. return nil, err
  241. }
  242. return m, err
  243. }
  244. // DecodeConfig returns the color model and dimensions of a WEBP image without
  245. // decoding the entire image.
  246. func DecodeConfig(r io.Reader) (image.Config, error) {
  247. _, c, err := decode(r, true)
  248. return c, err
  249. }
  250. func init() {
  251. image.RegisterFormat("webp", "RIFF????WEBPVP8", Decode, DecodeConfig)
  252. }