reader.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 bmp implements a BMP image decoder and encoder.
  5. //
  6. // The BMP specification is at http://www.digicamsoft.com/bmp/bmp.html.
  7. package bmp // import "golang.org/x/image/bmp"
  8. import (
  9. "errors"
  10. "image"
  11. "image/color"
  12. "io"
  13. )
  14. // ErrUnsupported means that the input BMP image uses a valid but unsupported
  15. // feature.
  16. var ErrUnsupported = errors.New("bmp: unsupported BMP image")
  17. func readUint16(b []byte) uint16 {
  18. return uint16(b[0]) | uint16(b[1])<<8
  19. }
  20. func readUint32(b []byte) uint32 {
  21. return uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
  22. }
  23. // decodePaletted reads an 8 bit-per-pixel BMP image from r.
  24. // If topDown is false, the image rows will be read bottom-up.
  25. func decodePaletted(r io.Reader, c image.Config, topDown bool) (image.Image, error) {
  26. paletted := image.NewPaletted(image.Rect(0, 0, c.Width, c.Height), c.ColorModel.(color.Palette))
  27. if c.Width == 0 || c.Height == 0 {
  28. return paletted, nil
  29. }
  30. var tmp [4]byte
  31. y0, y1, yDelta := c.Height-1, -1, -1
  32. if topDown {
  33. y0, y1, yDelta = 0, c.Height, +1
  34. }
  35. for y := y0; y != y1; y += yDelta {
  36. p := paletted.Pix[y*paletted.Stride : y*paletted.Stride+c.Width]
  37. if _, err := io.ReadFull(r, p); err != nil {
  38. return nil, err
  39. }
  40. // Each row is 4-byte aligned.
  41. if c.Width%4 != 0 {
  42. _, err := io.ReadFull(r, tmp[:4-c.Width%4])
  43. if err != nil {
  44. return nil, err
  45. }
  46. }
  47. }
  48. return paletted, nil
  49. }
  50. // decodeRGB reads a 24 bit-per-pixel BMP image from r.
  51. // If topDown is false, the image rows will be read bottom-up.
  52. func decodeRGB(r io.Reader, c image.Config, topDown bool) (image.Image, error) {
  53. rgba := image.NewRGBA(image.Rect(0, 0, c.Width, c.Height))
  54. if c.Width == 0 || c.Height == 0 {
  55. return rgba, nil
  56. }
  57. // There are 3 bytes per pixel, and each row is 4-byte aligned.
  58. b := make([]byte, (3*c.Width+3)&^3)
  59. y0, y1, yDelta := c.Height-1, -1, -1
  60. if topDown {
  61. y0, y1, yDelta = 0, c.Height, +1
  62. }
  63. for y := y0; y != y1; y += yDelta {
  64. if _, err := io.ReadFull(r, b); err != nil {
  65. return nil, err
  66. }
  67. p := rgba.Pix[y*rgba.Stride : y*rgba.Stride+c.Width*4]
  68. for i, j := 0, 0; i < len(p); i, j = i+4, j+3 {
  69. // BMP images are stored in BGR order rather than RGB order.
  70. p[i+0] = b[j+2]
  71. p[i+1] = b[j+1]
  72. p[i+2] = b[j+0]
  73. p[i+3] = 0xFF
  74. }
  75. }
  76. return rgba, nil
  77. }
  78. // decodeNRGBA reads a 32 bit-per-pixel BMP image from r.
  79. // If topDown is false, the image rows will be read bottom-up.
  80. func decodeNRGBA(r io.Reader, c image.Config, topDown bool) (image.Image, error) {
  81. rgba := image.NewNRGBA(image.Rect(0, 0, c.Width, c.Height))
  82. if c.Width == 0 || c.Height == 0 {
  83. return rgba, nil
  84. }
  85. y0, y1, yDelta := c.Height-1, -1, -1
  86. if topDown {
  87. y0, y1, yDelta = 0, c.Height, +1
  88. }
  89. for y := y0; y != y1; y += yDelta {
  90. p := rgba.Pix[y*rgba.Stride : y*rgba.Stride+c.Width*4]
  91. if _, err := io.ReadFull(r, p); err != nil {
  92. return nil, err
  93. }
  94. for i := 0; i < len(p); i += 4 {
  95. // BMP images are stored in BGRA order rather than RGBA order.
  96. p[i+0], p[i+2] = p[i+2], p[i+0]
  97. }
  98. }
  99. return rgba, nil
  100. }
  101. // Decode reads a BMP image from r and returns it as an image.Image.
  102. // Limitation: The file must be 8, 24 or 32 bits per pixel.
  103. func Decode(r io.Reader) (image.Image, error) {
  104. c, bpp, topDown, err := decodeConfig(r)
  105. if err != nil {
  106. return nil, err
  107. }
  108. switch bpp {
  109. case 8:
  110. return decodePaletted(r, c, topDown)
  111. case 24:
  112. return decodeRGB(r, c, topDown)
  113. case 32:
  114. return decodeNRGBA(r, c, topDown)
  115. }
  116. panic("unreachable")
  117. }
  118. // DecodeConfig returns the color model and dimensions of a BMP image without
  119. // decoding the entire image.
  120. // Limitation: The file must be 8, 24 or 32 bits per pixel.
  121. func DecodeConfig(r io.Reader) (image.Config, error) {
  122. config, _, _, err := decodeConfig(r)
  123. return config, err
  124. }
  125. func decodeConfig(r io.Reader) (config image.Config, bitsPerPixel int, topDown bool, err error) {
  126. // We only support those BMP images that are a BITMAPFILEHEADER
  127. // immediately followed by a BITMAPINFOHEADER.
  128. const (
  129. fileHeaderLen = 14
  130. infoHeaderLen = 40
  131. v4InfoHeaderLen = 108
  132. v5InfoHeaderLen = 124
  133. )
  134. var b [1024]byte
  135. if _, err := io.ReadFull(r, b[:fileHeaderLen+4]); err != nil {
  136. return image.Config{}, 0, false, err
  137. }
  138. if string(b[:2]) != "BM" {
  139. return image.Config{}, 0, false, errors.New("bmp: invalid format")
  140. }
  141. offset := readUint32(b[10:14])
  142. infoLen := readUint32(b[14:18])
  143. if infoLen != infoHeaderLen && infoLen != v4InfoHeaderLen && infoLen != v5InfoHeaderLen {
  144. return image.Config{}, 0, false, ErrUnsupported
  145. }
  146. if _, err := io.ReadFull(r, b[fileHeaderLen+4:fileHeaderLen+infoLen]); err != nil {
  147. return image.Config{}, 0, false, err
  148. }
  149. width := int(int32(readUint32(b[18:22])))
  150. height := int(int32(readUint32(b[22:26])))
  151. if height < 0 {
  152. height, topDown = -height, true
  153. }
  154. if width < 0 || height < 0 {
  155. return image.Config{}, 0, false, ErrUnsupported
  156. }
  157. // We only support 1 plane and 8, 24 or 32 bits per pixel and no
  158. // compression.
  159. planes, bpp, compression := readUint16(b[26:28]), readUint16(b[28:30]), readUint32(b[30:34])
  160. // if compression is set to BITFIELDS, but the bitmask is set to the default bitmask
  161. // that would be used if compression was set to 0, we can continue as if compression was 0
  162. if compression == 3 && infoLen > infoHeaderLen &&
  163. readUint32(b[54:58]) == 0xff0000 && readUint32(b[58:62]) == 0xff00 &&
  164. readUint32(b[62:66]) == 0xff && readUint32(b[66:70]) == 0xff000000 {
  165. compression = 0
  166. }
  167. if planes != 1 || compression != 0 {
  168. return image.Config{}, 0, false, ErrUnsupported
  169. }
  170. switch bpp {
  171. case 8:
  172. if offset != fileHeaderLen+infoLen+256*4 {
  173. return image.Config{}, 0, false, ErrUnsupported
  174. }
  175. _, err = io.ReadFull(r, b[:256*4])
  176. if err != nil {
  177. return image.Config{}, 0, false, err
  178. }
  179. pcm := make(color.Palette, 256)
  180. for i := range pcm {
  181. // BMP images are stored in BGR order rather than RGB order.
  182. // Every 4th byte is padding.
  183. pcm[i] = color.RGBA{b[4*i+2], b[4*i+1], b[4*i+0], 0xFF}
  184. }
  185. return image.Config{ColorModel: pcm, Width: width, Height: height}, 8, topDown, nil
  186. case 24:
  187. if offset != fileHeaderLen+infoLen {
  188. return image.Config{}, 0, false, ErrUnsupported
  189. }
  190. return image.Config{ColorModel: color.RGBAModel, Width: width, Height: height}, 24, topDown, nil
  191. case 32:
  192. if offset != fileHeaderLen+infoLen {
  193. return image.Config{}, 0, false, ErrUnsupported
  194. }
  195. return image.Config{ColorModel: color.RGBAModel, Width: width, Height: height}, 32, topDown, nil
  196. }
  197. return image.Config{}, 0, false, ErrUnsupported
  198. }
  199. func init() {
  200. image.RegisterFormat("bmp", "BM????\x00\x00\x00\x00", Decode, DecodeConfig)
  201. }