bmp

package module
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 18, 2023 License: MIT Imports: 6 Imported by: 0

README

BMP Decoding and Encoding in Go

bmp-go is a Go package aiming to provide abilities to decode BMP image files having 4, 8, 16, 24 and 32 bits color depths. It supports RLE decoding which might be applied as a compression method to 4 bits and 8 bits BMP images. The decoder outputs an image.Image with an *image.RGBA format.

This could also be used to directly encode an *image.RGBA format and writes it into an io.Writer as a 32 bits RGBA color BMP file. See sample usage.

Sample BMP images with various formats can be found in /images directory. The specifications for encoding and decoding are from https://en.wikipedia.org/wiki/BMP_file_format. The code within this repo is easy to read and modify to suit your needs.

Installation

Install with go get -u github.com/dvertx/bmp-go or by manually cloning this repository into $GOPATH/src/github.com/dvertx/

Sample Usage

Test both, decoding and encoding, with:

package main

import (
	"bufio"
	"bytes"
	"fmt"
	"image/png"
	"os"

	"github.com/dvertx/bmp-go"
)

func main() {
	f, err := os.Open("anyold.bmp")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer f.Close()

	img, err := bmp.Decode(f)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	pngbuffer := new(bytes.Buffer)
	pngwriter := bufio.NewWriter(pngbuffer)
	png.Encode(pngwriter, img)
	pngwriter.Flush()
	err = os.WriteFile("test.png", pngbuffer.Bytes(), 0644)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	out, err := os.Create("newfile.bmp")
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
	defer out.Close()

	err = bmp.Encode(out, img)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Decode

func Decode(r io.Reader) (image.Image, error)

func Encode added in v1.0.0

func Encode(w io.Writer, img image.Image) error

Types

type BMPHeader

type BMPHeader struct {
	Magic         uint16
	FileSize      uint32
	Reserved1     uint16
	Reserved2     uint16
	Offset        uint32
	DibHeaderSize uint32
	Width         int32
	Height        int32
	Planes        uint16
	Bpp           uint16
	Compression   uint32
	ImageSize     uint32
	Xppm          int32
	Yppm          int32
	Colors        uint32
	ImportantClr  uint32
}

type BitFields added in v0.9.1

type BitFields struct {
	RedMask   uint32
	GreenMask uint32
	BlueMask  uint32
	AlphaMask uint32
}

type Bitmap

type Bitmap struct {
	Width  int32
	Height int32
	Pixels [][]Pixel
}

type Color

type Color struct {
	B uint8
	G uint8
	R uint8
	A uint8
}

type ColorTable

type ColorTable struct {
	Rows []Color
}

type Pixel

type Pixel struct {
	ColorIndex byte
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL