Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
561 views
in Technique[技术] by (71.8m points)

go - Generate 1 bit depth BMP image

I need to create a mono bitmap image with a colour depth of 1 bit, for example:

$ file exampleMono.bmp 
> exampleMono.bmp: PC bitmap, Windows 3.x format, 640 x 1000 x 1

In Go, I'm able to generate a simple bmp image with the following:

tempPal := color.Palette([]color.Color{
    color.Black, color.White,
})
img := image.NewPaletted(image.Rect(640, 1000,0,0), tempPal)
filename := fmt.Sprintf("img-%d.bmp", time.Now().Unix())
file, _ := os.Create(filename)
bmp.Encode(file, img)
file.close

However, the resulting bmp file has a colour depth of 8-bits.

$ file img-1611574755.bmp 
> img-1611574755.bmp: PC bitmap, Windows 3.x format, 640 x 1000 x 8

I've had a look through the bmp, image and draw packages in Go and haven't found anything that appears to provide the ability to set the colour depth to 1 bit. Is it possible to achieve what I need in Go and if so, how?

question from:https://stackoverflow.com/questions/65884109/generate-1-bit-depth-bmp-image

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As you can see from here down; golang.org/x/image/bmp's Encode() does not support bpp=1 in any of image modes.

It should be easy to take pieces from Encode() and write your own one-purpose encoding function as BMP file format and all its variants are well documented. BMP file format specs: on Wikipedia or FileFormat.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...