reader.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. )
  132. var b [1024]byte
  133. if _, err := io.ReadFull(r, b[:fileHeaderLen+infoHeaderLen]); err != nil {
  134. return image.Config{}, 0, false, err
  135. }
  136. if string(b[:2]) != "BM" {
  137. return image.Config{}, 0, false, errors.New("bmp: invalid format")
  138. }
  139. offset := readUint32(b[10:14])
  140. if readUint32(b[14:18]) != infoHeaderLen {
  141. return image.Config{}, 0, false, ErrUnsupported
  142. }
  143. width := int(int32(readUint32(b[18:22])))
  144. height := int(int32(readUint32(b[22:26])))
  145. if height < 0 {
  146. height, topDown = -height, true
  147. }
  148. if width < 0 || height < 0 {
  149. return image.Config{}, 0, false, ErrUnsupported
  150. }
  151. // We only support 1 plane, 8 or 24 bits per pixel and no compression.
  152. planes, bpp, compression := readUint16(b[26:28]), readUint16(b[28:30]), readUint32(b[30:34])
  153. if planes != 1 || compression != 0 {
  154. return image.Config{}, 0, false, ErrUnsupported
  155. }
  156. switch bpp {
  157. case 8:
  158. if offset != fileHeaderLen+infoHeaderLen+256*4 {
  159. return image.Config{}, 0, false, ErrUnsupported
  160. }
  161. _, err = io.ReadFull(r, b[:256*4])
  162. if err != nil {
  163. return image.Config{}, 0, false, err
  164. }
  165. pcm := make(color.Palette, 256)
  166. for i := range pcm {
  167. // BMP images are stored in BGR order rather than RGB order.
  168. // Every 4th byte is padding.
  169. pcm[i] = color.RGBA{b[4*i+2], b[4*i+1], b[4*i+0], 0xFF}
  170. }
  171. return image.Config{ColorModel: pcm, Width: width, Height: height}, 8, topDown, nil
  172. case 24:
  173. if offset != fileHeaderLen+infoHeaderLen {
  174. return image.Config{}, 0, false, ErrUnsupported
  175. }
  176. return image.Config{ColorModel: color.RGBAModel, Width: width, Height: height}, 24, topDown, nil
  177. case 32:
  178. if offset != fileHeaderLen+infoHeaderLen {
  179. return image.Config{}, 0, false, ErrUnsupported
  180. }
  181. return image.Config{ColorModel: color.RGBAModel, Width: width, Height: height}, 32, topDown, nil
  182. }
  183. return image.Config{}, 0, false, ErrUnsupported
  184. }
  185. func init() {
  186. image.RegisterFormat("bmp", "BM????\x00\x00\x00\x00", Decode, DecodeConfig)
  187. }