reader.go

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