image/

directory
v0.28.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: BSD-3-Clause

README

tinygo.org/x/drivers/image

This is an image package that uses less RAM to run on a microcontroller. Unlike Go's original image package, image.Decode() does not return image.Image.

Instead, a callback can be set to process the data corresponding to the image.

How to use

First, use SetCallback() to set the callback. Then call png.Decode() or jpeg.Decode(). The callback will be called as many times as necessary to load the image.

SetCallback() needs to be given a Buffer to handle the callback and the actual function to be called. The data []uint16 in the callback is in RGB565 format.

The io.Reader to pass to Decode() specifies the binary data of the image.

func drawPng(display *ili9341.Device) error {
	p := strings.NewReader(pngImage)
	png.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
		err := display.DrawRGBBitmap(x, y, data[:w*h], w, h)
		if err != nil {
			errorMessage(fmt.Errorf("error drawPng: %s", err))
		}
	})

	return png.Decode(p)
}
func drawJpeg(display *ili9341.Device) error {
	p := strings.NewReader(jpegImage)
	jpeg.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
		err := display.DrawRGBBitmap(x, y, data[:w*h], w, h)
		if err != nil {
			errorMessage(fmt.Errorf("error drawJpeg: %s", err))
		}
	})

	return jpeg.Decode(p)
}

How to create an image

The following program will output an image binary like the one in images.go.

go run ./cmd/convert2bin ./path/to/png_or_jpg.png

Examples

An example can be found below. Processing jpegs requires a minimum of 32KB of RAM.

Directories

Path Synopsis
internal
compress/flate
Package flate implements the DEFLATE compressed data format, described in RFC 1951.
Package flate implements the DEFLATE compressed data format, described in RFC 1951.
compress/zlib
Package zlib implements reading and writing of zlib format compressed data, as specified in RFC 1950.
Package zlib implements reading and writing of zlib format compressed data, as specified in RFC 1950.
imageutil
Package imageutil contains code shared by image-related packages.
Package imageutil contains code shared by image-related packages.
Package jpeg implements a JPEG image decoder and encoder.
Package jpeg implements a JPEG image decoder and encoder.
Package png implements a PNG image decoder and encoder.
Package png implements a PNG image decoder and encoder.

Jump to

Keyboard shortcuts

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