reader.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, allowAlpha 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. if !allowAlpha {
  98. p[i+3] = 0xFF
  99. }
  100. }
  101. }
  102. return rgba, nil
  103. }
  104. // Decode reads a BMP image from r and returns it as an image.Image.
  105. // Limitation: The file must be 8, 24 or 32 bits per pixel.
  106. func Decode(r io.Reader) (image.Image, error) {
  107. c, bpp, topDown, allowAlpha, err := decodeConfig(r)
  108. if err != nil {
  109. return nil, err
  110. }
  111. switch bpp {
  112. case 8:
  113. return decodePaletted(r, c, topDown)
  114. case 24:
  115. return decodeRGB(r, c, topDown)
  116. case 32:
  117. return decodeNRGBA(r, c, topDown, allowAlpha)
  118. }
  119. panic("unreachable")
  120. }
  121. // DecodeConfig returns the color model and dimensions of a BMP image without
  122. // decoding the entire image.
  123. // Limitation: The file must be 8, 24 or 32 bits per pixel.
  124. func DecodeConfig(r io.Reader) (image.Config, error) {
  125. config, _, _, _, err := decodeConfig(r)
  126. return config, err
  127. }
  128. func decodeConfig(r io.Reader) (config image.Config, bitsPerPixel int, topDown bool, allowAlpha bool, err error) {
  129. // We only support those BMP images with one of the following DIB headers:
  130. // - BITMAPINFOHEADER (40 bytes)
  131. // - BITMAPV4HEADER (108 bytes)
  132. // - BITMAPV5HEADER (124 bytes)
  133. const (
  134. fileHeaderLen = 14
  135. infoHeaderLen = 40
  136. v4InfoHeaderLen = 108
  137. v5InfoHeaderLen = 124
  138. )
  139. var b [1024]byte
  140. if _, err := io.ReadFull(r, b[:fileHeaderLen+4]); err != nil {
  141. if err == io.EOF {
  142. err = io.ErrUnexpectedEOF
  143. }
  144. return image.Config{}, 0, false, false, err
  145. }
  146. if string(b[:2]) != "BM" {
  147. return image.Config{}, 0, false, false, errors.New("bmp: invalid format")
  148. }
  149. offset := readUint32(b[10:14])
  150. infoLen := readUint32(b[14:18])
  151. if infoLen != infoHeaderLen && infoLen != v4InfoHeaderLen && infoLen != v5InfoHeaderLen {
  152. return image.Config{}, 0, false, false, ErrUnsupported
  153. }
  154. if _, err := io.ReadFull(r, b[fileHeaderLen+4:fileHeaderLen+infoLen]); err != nil {
  155. if err == io.EOF {
  156. err = io.ErrUnexpectedEOF
  157. }
  158. return image.Config{}, 0, false, false, err
  159. }
  160. width := int(int32(readUint32(b[18:22])))
  161. height := int(int32(readUint32(b[22:26])))
  162. if height < 0 {
  163. height, topDown = -height, true
  164. }
  165. if width < 0 || height < 0 {
  166. return image.Config{}, 0, false, false, ErrUnsupported
  167. }
  168. // We only support 1 plane and 8, 24 or 32 bits per pixel and no
  169. // compression.
  170. planes, bpp, compression := readUint16(b[26:28]), readUint16(b[28:30]), readUint32(b[30:34])
  171. // if compression is set to BI_BITFIELDS, but the bitmask is set to the default bitmask
  172. // that would be used if compression was set to 0, we can continue as if compression was 0
  173. if compression == 3 && infoLen > infoHeaderLen &&
  174. readUint32(b[54:58]) == 0xff0000 && readUint32(b[58:62]) == 0xff00 &&
  175. readUint32(b[62:66]) == 0xff && readUint32(b[66:70]) == 0xff000000 {
  176. compression = 0
  177. }
  178. if planes != 1 || compression != 0 {
  179. return image.Config{}, 0, false, false, ErrUnsupported
  180. }
  181. switch bpp {
  182. case 8:
  183. if offset != fileHeaderLen+infoLen+256*4 {
  184. return image.Config{}, 0, false, false, ErrUnsupported
  185. }
  186. _, err = io.ReadFull(r, b[:256*4])
  187. if err != nil {
  188. return image.Config{}, 0, false, false, err
  189. }
  190. pcm := make(color.Palette, 256)
  191. for i := range pcm {
  192. // BMP images are stored in BGR order rather than RGB order.
  193. // Every 4th byte is padding.
  194. pcm[i] = color.RGBA{b[4*i+2], b[4*i+1], b[4*i+0], 0xFF}
  195. }
  196. return image.Config{ColorModel: pcm, Width: width, Height: height}, 8, topDown, false, nil
  197. case 24:
  198. if offset != fileHeaderLen+infoLen {
  199. return image.Config{}, 0, false, false, ErrUnsupported
  200. }
  201. return image.Config{ColorModel: color.RGBAModel, Width: width, Height: height}, 24, topDown, false, nil
  202. case 32:
  203. if offset != fileHeaderLen+infoLen {
  204. return image.Config{}, 0, false, false, ErrUnsupported
  205. }
  206. // 32 bits per pixel is possibly RGBX (X is padding) or RGBA (A is
  207. // alpha transparency). However, for BMP images, "Alpha is a
  208. // poorly-documented and inconsistently-used feature" says
  209. // https://source.chromium.org/chromium/chromium/src/+/bc0a792d7ebc587190d1a62ccddba10abeea274b:third_party/blink/renderer/platform/image-decoders/bmp/bmp_image_reader.cc;l=621
  210. //
  211. // That goes on to say "BITMAPV3HEADER+ have an alpha bitmask in the
  212. // info header... so we respect it at all times... [For earlier
  213. // (smaller) headers we] ignore alpha in Windows V3 BMPs except inside
  214. // ICO files".
  215. //
  216. // "Ignore" means to always set alpha to 0xFF (fully opaque):
  217. // https://source.chromium.org/chromium/chromium/src/+/bc0a792d7ebc587190d1a62ccddba10abeea274b:third_party/blink/renderer/platform/image-decoders/bmp/bmp_image_reader.h;l=272
  218. //
  219. // Confusingly, "Windows V3" does not correspond to BITMAPV3HEADER, but
  220. // instead corresponds to the earlier (smaller) BITMAPINFOHEADER:
  221. // https://source.chromium.org/chromium/chromium/src/+/bc0a792d7ebc587190d1a62ccddba10abeea274b:third_party/blink/renderer/platform/image-decoders/bmp/bmp_image_reader.cc;l=258
  222. //
  223. // This Go package does not support ICO files and the (infoLen >
  224. // infoHeaderLen) condition distinguishes BITMAPINFOHEADER (40 bytes)
  225. // vs later (larger) headers.
  226. allowAlpha = infoLen > infoHeaderLen
  227. return image.Config{ColorModel: color.RGBAModel, Width: width, Height: height}, 32, topDown, allowAlpha, nil
  228. }
  229. return image.Config{}, 0, false, false, ErrUnsupported
  230. }
  231. func init() {
  232. image.RegisterFormat("bmp", "BM????\x00\x00\x00\x00", Decode, DecodeConfig)
  233. }