tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/image/README.md (about)

     1  # tinygo.org/x/drivers/image
     2  
     3  This is an image package that uses less RAM to run on a microcontroller.
     4  Unlike Go's original image package, `image.Decode()` does not return `image.Image`.
     5  
     6  Instead, a callback can be set to process the data corresponding to the image.
     7  
     8  ## How to use
     9  
    10  First, use `SetCallback()` to set the callback.
    11  Then call `png.Decode()` or `jpeg.Decode()`.
    12  The callback will be called as many times as necessary to load the image.
    13  
    14  `SetCallback()` needs to be given a Buffer to handle the callback and the actual function to be called.
    15  The `data []uint16` in the callback is in RGB565 format.
    16  
    17  The `io.Reader` to pass to `Decode()` specifies the binary data of the image.
    18  
    19  ```go
    20  func drawPng(display *ili9341.Device) error {
    21  	p := strings.NewReader(pngImage)
    22  	png.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
    23  		err := display.DrawRGBBitmap(x, y, data[:w*h], w, h)
    24  		if err != nil {
    25  			errorMessage(fmt.Errorf("error drawPng: %s", err))
    26  		}
    27  	})
    28  
    29  	return png.Decode(p)
    30  }
    31  ```
    32  
    33  ```go
    34  func drawJpeg(display *ili9341.Device) error {
    35  	p := strings.NewReader(jpegImage)
    36  	jpeg.SetCallback(buffer[:], func(data []uint16, x, y, w, h, width, height int16) {
    37  		err := display.DrawRGBBitmap(x, y, data[:w*h], w, h)
    38  		if err != nil {
    39  			errorMessage(fmt.Errorf("error drawJpeg: %s", err))
    40  		}
    41  	})
    42  
    43  	return jpeg.Decode(p)
    44  }
    45  ```
    46  
    47  ## How to create an image
    48  
    49  The following program will output an image binary like the one in [images.go](./examples/ili9341/slideshow/images.go).  
    50  
    51  ```
    52  go run ./cmd/convert2bin ./path/to/png_or_jpg.png
    53  ```
    54  
    55  ## Examples
    56  
    57  An example can be found below.
    58  Processing jpegs requires a minimum of 32KB of RAM.
    59  
    60  * [./examples/ili9341/slideshow](./examples/ili9341/slideshow)