### Get Image Description from Lilliput Decoder (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Returns a descriptive string of the image type, such as "JPEG" or "PNG", from a Lilliput Decoder. This provides a human-readable representation of the image format being processed. ```Go func (d lilliput.Decoder) Description() string ``` -------------------------------- ### Get Channels Per Pixel from PixelType Source: https://github.com/discord/lilliput/blob/master/README.md The `Channels` method of the `PixelType` type returns the number of color channels per pixel. For example, RGB images have 3 channels, while RGBA images have 4. ```go func (p lilliput.PixelType) Channels() int ``` -------------------------------- ### Get Orientation from ImageHeader Source: https://github.com/discord/lilliput/blob/master/README.md The `Orientation` method of the `ImageHeader` interface returns the image's orientation metadata. Currently, this is only reliably detected in JPEG images and indicates rotation or mirroring. ```go func (h lilliput.ImageHeader) Orientation() lilliput.ImageOrientation ``` -------------------------------- ### Get Image Height from ImageHeader Source: https://github.com/discord/lilliput/blob/master/README.md The `Height` method of the `ImageHeader` interface returns the height of the image in pixels. This metadata is crucial for understanding the image's dimensions. ```go func (h lilliput.ImageHeader) Height() int ``` -------------------------------- ### Get Image Width from ImageHeader Source: https://github.com/discord/lilliput/blob/master/README.md The `Width` method of the `ImageHeader` interface returns the width of the image in pixels. This is a fundamental piece of metadata obtained after decoding an image header. ```go func (h lilliput.ImageHeader) Width() int ``` -------------------------------- ### Get Image Header from Lilliput Decoder (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Reads and returns the image header from a Lilliput Decoder. The header contains essential image metadata such as dimensions and format. Returns an error if the image header is malformed, preventing further decoding. ```Go func (d lilliput.Decoder) Header() (lilliput.ImageHeader, error) ``` -------------------------------- ### Get Framebuffer Pixel Type (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Returns the `PixelType` enumeration representing the format of the pixel data within the Framebuffer. This is crucial for understanding how to interpret the raw pixel data. ```go func (f *lilliput.Framebuffer) PixelType() lilliput.PixelType ``` -------------------------------- ### Get Content Duration from Lilliput Decoder (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Returns the duration of the image content in time.Duration format. For static images and animated GIFs, this value is 0. For other animated formats, it represents the total playback time. ```Go func (d lilliput.Decoder) Duration() time.Duration ``` -------------------------------- ### Get Framebuffer Dimensions (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Retrieves the width and height of the pixel data currently held within a Framebuffer. These methods return the dimensions of the contained data, not the buffer's capacity. ```go func (f *lilliput.Framebuffer) Width() int ``` ```go func (f *lilliput.Framebuffer) Height() int ``` -------------------------------- ### Get Pixel Type from ImageHeader Source: https://github.com/discord/lilliput/blob/master/README.md The `PixelType` method of the `ImageHeader` interface returns the basic pixel type of the image. This provides information about the color format and structure of the image's pixels. ```go func (h lilliput.ImageHeader) PixelType() lilliput.PixelType ``` -------------------------------- ### Get Bits Per Pixel from PixelType Source: https://github.com/discord/lilliput/blob/master/README.md The `Depth` method of the `PixelType` type returns the number of bits used to represent each pixel. This is important for understanding the color depth and memory usage of the image. ```go func (p lilliput.PixelType) Depth() int ``` -------------------------------- ### Batch Image Resizing and Conversion (Go) Source: https://context7.com/discord/lilliput/llms.txt Performs batch resizing and format conversion of JPG images to WebP using Lilliput. It reuses `lilliput.ImageOps` and an output buffer for efficiency. Input images are read from `inputDir`, processed with specified options, and saved to `outputDir`. Handles potential errors during file reading, decoding, transformation, and writing. Outputs are normalized to 800x800 WebP with 85 quality. ```go package main import ( "fmt" "os" "path/filepath" "github.com/discord/lilliput" ) func batchResize(inputDir string, outputDir string) error { // Create single ImageOps instance for reuse ops := lilliput.NewImageOps(8192) defer ops.Close() // Reuse output buffer across all operations outputBuffer := make([]byte, 50*1024*1024) options := &lilliput.ImageOptions{ FileType: ".webp", Width: 800, Height: 800, ResizeMethod: lilliput.ImageOpsFit, NormalizeOrientation: true, EncodeOptions: map[int]int{ lilliput.WebpQuality: 85, }, } files, err := filepath.Glob(filepath.Join(inputDir, "*.jpg")) if err != nil { return err } for i, file := range files { inputData, err := os.ReadFile(file) if err != nil { fmt.Printf("Skipping %s: %v\n", file, err) continue } decoder, err := lilliput.NewDecoder(inputData) if err != nil { fmt.Printf("Skipping %s: %v\n", file, err) continue } // Transform image (reuses ops and outputBuffer) outputData, err := ops.Transform(decoder, options, outputBuffer) decoder.Close() // Important: close decoder after each use if err != nil { fmt.Printf("Failed to transform %s: %v\n", file, err) continue } outputName := filepath.Base(file) outputName = outputName[:len(outputName)-4] + ".webp" outputPath := filepath.Join(outputDir, outputName) if err := os.WriteFile(outputPath, outputData, 0644); err != nil { fmt.Printf("Failed to write %s: %v\n", outputPath, err) continue } fmt.Printf("[%d/%d] Processed %s -> %s (%d bytes)\n", i+1, len(files), file, outputPath, len(outputData)) // Optional: clear internal buffers periodically to free memory if i%100 == 0 { ops.Clear() } } return nil } ``` -------------------------------- ### Build Dependencies Script (OSX) (Shell) Source: https://github.com/discord/lilliput/blob/master/README.md A shell script designed to automate the building of Lilliput's dependencies on macOS. This script is provided to facilitate the integration of Lilliput as a standard Go package on OSX systems. ```shell #!/bin/bash # Build script for OSX dependencies ``` -------------------------------- ### ImageHeader API Source: https://github.com/discord/lilliput/blob/master/README.md Interface for retrieving basic metadata about an image. ```APIDOC ## ImageHeader API ### Description Provides an interface for accessing basic metadata of an image, typically obtained via `Decoder.Header()`. ### Methods #### `Width` ```go func (h lilliput.ImageHeader) Width() int ``` Returns the width of the image in pixels. #### `Height` ```go func (h lilliput.ImageHeader) Height() int ``` Returns the height of the image in pixels. #### `PixelType` ```go func (h lilliput.ImageHeader) PixelType() lilliput.PixelType ``` Returns the basic pixel type information for the image. #### `Orientation` ```go func (h lilliput.ImageHeader) Orientation() lilliput.ImageOrientation ``` Returns the metadata-based orientation of the image. Currently, this only detects orientation in JPEG images. A value of `1` indicates the default orientation; other values signify rotations or mirroring. ``` -------------------------------- ### Fit Framebuffer (Cropping Resize) (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Performs a cropping resize of the Framebuffer's content into a destination Framebuffer (`dst`) while preserving the aspect ratio. If the aspect ratios differ, the image is cropped equally from the edges. Returns an error if `dst` is too small. ```go func (f *lilliput.Framebuffer) Fit(width, height int, dst *lilliput.Framebuffer) error ``` -------------------------------- ### Create New Encoder (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Constructs a new Encoder object that writes compressed image data to a byte slice (`dst`). Requires a file extension (e.g., '.jpeg') and optionally a `Decoder` for formats like GIF. The `dst` slice is where the encoded output will be written. ```go func lilliput.NewEncoder(extension string, decodedBy lilliput.Decoder, dst []byte) (lilliput.Encoder, error) ``` -------------------------------- ### Build Dependencies Script (Linux) (Shell) Source: https://github.com/discord/lilliput/blob/master/README.md A shell script to automate the compilation of Lilliput's dependencies on Linux. This script is part of the effort to make Lilliput function seamlessly as a standard Go package on Linux environments. ```shell #!/bin/bash # Build script for Linux dependencies ``` -------------------------------- ### Automatic Image Format Detection and Conversion with Lilliput in Go Source: https://context7.com/discord/lilliput/llms.txt This Go code demonstrates how to automatically detect the format of an input image using Lilliput's decoder, which identifies formats from magic bytes. It then converts the image to an optimized output format (e.g., WebP from JPEG, PNG for transparency, or JPEG otherwise) with specified encoding options. The function utilizes 'path/filepath', 'os', 'fmt', and 'github.com/discord/lilliput'. ```go package main import ( "fmt" "os" "path/filepath" "github.com/discord/lilliput" ) func detectAndConvert(inputPath string) error { inputData, err := os.ReadFile(inputPath) if err != nil { return err } // Decoder automatically detects format from magic bytes decoder, err := lilliput.NewDecoder(inputData) if err != nil { return fmt.Errorf("unsupported format: %v", err) } defer decoder.Close() header, err := decoder.Header() if err != nil { return err } // Log detected format fmt.Printf("Detected: %s\n", decoder.Description()) fmt.Printf("Size: %dx%d\n", header.Width(), header.Height()) // Check codec information for videos if decoder.VideoCodec() != "" && decoder.VideoCodec() != "Unknown" { fmt.Printf("Video codec: %s\n", decoder.VideoCodec()) } if decoder.AudioCodec() != "" && decoder.AudioCodec() != "Unknown" { fmt.Printf("Audio codec: %s\n", decoder.AudioCodec()) } ops := lilliput.NewImageOps(4096) defer ops.Close() outputBuffer := make([]byte, 20*1024*1024) // Determine optimal output format based on input var outputFormat string var encodeOpts map[int]int switch decoder.Description() { case "JPEG": outputFormat = ".webp" // Convert JPEG to WebP for better compression encodeOpts = map[int]int{lilliput.WebpQuality: 85} case "PNG": if header.PixelType().Channels() == 4 { outputFormat = ".png" // Keep PNG for transparency encodeOpts = map[int]int{lilliput.PngCompression: 7} } else { outputFormat = ".jpeg" encodeOpts = map[int]int{lilliput.JpegQuality: 90} } case "WEBP": outputFormat = ".webp" encodeOpts = map[int]int{lilliput.WebpQuality: 85} default: outputFormat = ".jpeg" encodeOpts = map[int]int{lilliput.JpegQuality: 85} } options := &lilliput.ImageOptions{ FileType: outputFormat, Width: 1024, Height: 768, ResizeMethod: lilliput.ImageOpsFit, NormalizeOrientation: true, EncodeOptions: encodeOpts, } outputData, err := ops.Transform(decoder, options, outputBuffer) if err != nil { return err } outputPath := filepath.Base(inputPath) outputPath = outputPath[:len(outputPath)-len(filepath.Ext(outputPath))] + outputFormat if err := os.WriteFile(outputPath, outputData, 0644); err != nil { return err } fmt.Printf("Converted to %s: %d bytes\n", outputFormat, len(outputData)) return nil } ``` -------------------------------- ### Create New Framebuffer (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Initializes a new Framebuffer object with specified dimensions. The framebuffer will not contain any pixel data upon creation. It's a fundamental step before manipulating image data. ```go func lilliput.NewFramebuffer(width, height int) *lilliput.Framebuffer ``` -------------------------------- ### Manual Framebuffer Operations with Lilliput in Go Source: https://context7.com/discord/lilliput/llms.txt This Go code snippet demonstrates manual control over framebuffers for image processing. It decodes an image into a source framebuffer, applies orientation transformations, resizes it to a destination framebuffer using a 'fit' method, and then encodes the resized image into a JPEG format. Dependencies include the 'os', 'fmt', and 'github.com/discord/lilliput' packages. ```go package main import ( "fmt" "os" "github.com/discord/lilliput" ) func manualFramebufferProcessing() { inputData, _ := os.ReadFile("input.jpg") decoder, _ := lilliput.NewDecoder(inputData) defer decoder.Close() header, _ := decoder.Header() // Create framebuffer to hold decoded pixels srcFramebuffer := lilliput.NewFramebuffer(header.Width(), header.Height()) defer srcFramebuffer.Close() // Decode image into framebuffer err := decoder.DecodeTo(srcFramebuffer) if err != nil { fmt.Printf("Decode failed: %v\n", err) return } fmt.Printf("Decoded: %dx%d, %d channels\n", srcFramebuffer.Width(), srcFramebuffer.Height(), srcFramebuffer.PixelType().Channels()) // Apply orientation correction srcFramebuffer.OrientationTransform(header.Orientation()) // Create destination framebuffer for resize dstFramebuffer := lilliput.NewFramebuffer(800, 600) defer dstFramebuffer.Close() // Perform fit resize (crop to aspect ratio) err = srcFramebuffer.Fit(800, 600, dstFramebuffer) if err != nil { fmt.Printf("Resize failed: %v\n", err) return } // Encode the resized framebuffer outputBuffer := make([]byte, 10*1024*1024) encoder, err := lilliput.NewEncoder(".jpeg", decoder, outputBuffer, nil) if err != nil { fmt.Printf("Encoder creation failed: %v\n", err) return } defer encoder.Close() encodeOptions := map[int]int{ lilliput.JpegQuality: 85, } outputData, err := encoder.Encode(dstFramebuffer, encodeOptions) if err != nil { fmt.Printf("Encode failed: %v\n", err) return } os.WriteFile("manual_output.jpg", outputData, 0644) fmt.Printf("Manual processing complete: %d bytes\n", len(outputData)) } ``` -------------------------------- ### Transform Image Using ImageOps with Options Source: https://github.com/discord/lilliput/blob/master/README.md The `Transform` method resizes and encodes an image from a Decoder into a specified destination buffer. It requires `lilliput.ImageOptions` to define the output format, dimensions, resize method, orientation normalization, and encoding quality. The input decoder must not have `DecodeTo()` called previously. ```go func (o *lilliput.ImageOps) Transform(decoder lilliput.Decoder, opts *lilliput.ImageOptions, dst []byte) ([]byte, error) ``` -------------------------------- ### ImageOps API Source: https://github.com/discord/lilliput/blob/master/README.md Provides methods for image resizing and encoding using a reusable ImageOps object. ```APIDOC ## ImageOps API ### Description Provides methods for image resizing and encoding using a reusable `ImageOps` object. This object can be created once and reused for multiple operations, reducing memory allocations. ### Constructor #### `NewImageOps` ```go func lilliput.NewImageOps(dimension int) *lilliput.ImageOps ``` Creates an `ImageOps` object capable of operating on images up to `dimension x dimension` pixels. This object can be reused for multiple transformations. ### Methods #### `Transform` ```go func (o *lilliput.ImageOps) Transform(decoder lilliput.Decoder, opts *lilliput.ImageOptions, dst []byte) ([]byte, error) ``` Transforms the compressed image data from a `Decoder` object into the desired output format and size specified by `ImageOptions`. The `decoder` must not have `DecodeTo()` called on it prior to this call, but `decoder.Header()` can be called. The output is written into the `dst` byte slice, and the function returns a slice pointing to the actual end of the compressed image data within `dst`. **`lilliput.ImageOptions` Fields:** * `FileType` (string) - Required - The file extension type for the output image (e.g., ".jpeg"). * `Width` (int) - Optional - The desired width of the output image in pixels. * `Height` (int) - Optional - The desired height of the output image in pixels. * `ResizeMethod` (int) - Optional - Specifies the resizing behavior. Accepts `lilliput.ImageOpsNoResize` or `lilliput.ImageOpsFit` (cropping resize without stretching). * `NormalizeOrientation` (bool) - Optional - If `true`, the image orientation metadata (like JPEG EXIF) will be used to orient the output image correctly. * `EncodeOptions` (map[int]int) - Optional - Options for the encoding process, similar to `Encoder.Encode()`, controlling output quality. #### `Clear` ```go func (o *lilliput.ImageOps) Clear() ``` Clears all pixel data from the `ImageOps` object, freeing up memory used by previous operations. This is optional and not required between `Transform()` calls. #### `Close` ```go func (o *lilliput.ImageOps) Close() ``` Closes the `ImageOps` object and releases all associated resources. This method must be called when the `ImageOps` object is no longer needed. ``` -------------------------------- ### PixelType API Source: https://github.com/discord/lilliput/blob/master/README.md Provides methods for retrieving information about image pixel types. ```APIDOC ## PixelType API ### Description Provides methods for retrieving detailed information about an image's pixel type. ### Methods #### `Depth` ```go func (p lilliput.PixelType) Depth() int ``` Returns the number of bits used per pixel. #### `Channels` ```go func (p lilliput.PixelType) Channels() int ``` Returns the number of channels per pixel (e.g., 3 for RGB, 4 for RGBA). ``` -------------------------------- ### Go: Decode Image Metadata with Lilliput Decoder Source: https://context7.com/discord/lilliput/llms.txt This Go snippet demonstrates how to create a Lilliput decoder from image data, read image metadata such as dimensions, pixel type, and orientation, and check if the image is animated. It requires the `lilliput` library and standard Go packages for file I/O. ```go package main import ( "fmt" "os" "github.com/discord/lilliput" ) func main() { // Read image file into memory inputData, err := os.ReadFile("input.jpg") if err != nil { panic(err) } // Create decoder from byte buffer decoder, err := lilliput.NewDecoder(inputData) if err != nil { fmt.Printf("Error: %v\n", err) return } defer decoder.Close() // Read image metadata header, err := decoder.Header() if err != nil { fmt.Printf("Failed to read header: %v\n", err) return } // Access image properties fmt.Printf("Format: %s\n", decoder.Description()) fmt.Printf("Dimensions: %dx%d\n", header.Width(), header.Height()) fmt.Printf("Pixel Type: %d channels, %d bits\n", header.PixelType().Channels(), header.PixelType().Depth()) fmt.Printf("Orientation: %d\n", header.Orientation()) // Check for animation if header.IsAnimated() { fmt.Printf("Animated image with %d frames\n", header.NumFrames()) fmt.Printf("Duration: %v\n", decoder.Duration()) } } ``` -------------------------------- ### Create Lilliput Decoder from Byte Buffer (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Creates a new Lilliput Decoder object from a byte buffer containing compressed image data. Returns an error if the buffer's magic bytes do not match a supported image type. The decoder is used to access image metadata and pixel data. ```Go func lilliput.NewDecoder([]byte buf) (lilliput.Decoder, error) ``` -------------------------------- ### Advanced Encoding Options (Go) Source: https://context7.com/discord/lilliput/llms.txt Demonstrates advanced encoding options for JPEG and PNG formats using Lilliput. This includes setting JPEG quality, enabling progressive mode, and applying maximum compression for PNG. It requires input image files and outputs processed JPEG and PNG files. ```go package main import ( "fmt" "os" "github.com/discord/lilliput" ) func encodeWithAdvancedOptions() { inputData, _ := os.ReadFile("input.png") decoder, _ := lilliput.NewDecoder(inputData) defer decoder.Close() ops := lilliput.NewImageOps(8192) defer ops.Close() outputBuffer := make([]byte, 20*1024*1024) // JPEG encoding with progressive mode jpegOptions := &lilliput.ImageOptions{ FileType: ".jpeg", Width: 1200, Height: 900, ResizeMethod: lilliput.ImageOpsResize, // Stretch to exact size NormalizeOrientation: true, EncodeOptions: map[int]int{ lilliput.JpegQuality: 90, // Quality 90/100 lilliput.JpegProgressive: 1, // Progressive JPEG }, } outputJPEG, err := ops.Transform(decoder, jpegOptions, outputBuffer) if err != nil { fmt.Printf("JPEG encoding failed: %v\n", err) return } os.WriteFile("output.jpg", outputJPEG, 0644) // Reset decoder for second operation decoder2, _ := lilliput.NewDecoder(inputData) defer decoder2.Close() ops.Clear() // Clear internal buffers // PNG encoding with maximum compression pngOptions := &lilliput.ImageOptions{ FileType: ".png", Width: 1200, Height: 900, ResizeMethod: lilliput.ImageOpsFit, NormalizeOrientation: false, EncodeOptions: map[int]int{ lilliput.PngCompression: 9, // Maximum compression (0-9) }, } outputPNG, err := ops.Transform(decoder2, pngOptions, outputBuffer) if err != nil { fmt.Printf("PNG encoding failed: %v\n", err) return } os.WriteFile("output.png", outputPNG, 0644) fmt.Printf("JPEG: %d bytes, PNG: %d bytes\n", len(outputJPEG), len(outputPNG)) } ``` -------------------------------- ### Resize Framebuffer (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Resizes the Framebuffer's content into a destination Framebuffer (`dst`) with specified dimensions. This operation does not preserve aspect ratio and may fail if `dst` is not sufficiently large. It performs a direct resize to the target dimensions. ```go func (f *lilliput.Framebuffer) ResizeTo(width, height int, dst *lilliput.Framebuffer) error ``` -------------------------------- ### Go: Resize and Transcode Image to WebP with Lilliput ImageOps Source: https://context7.com/discord/lilliput/llms.txt This Go snippet shows how to use Lilliput's `ImageOps` to resize an image to 800x600 pixels, fix its orientation, and transcode it to WebP format with 85% quality. It requires the `lilliput` library and handles input/output file operations. The maximum image size is limited to 8192x8192 pixels. ```go package main import ( "fmt" "os" "github.com/discord/lilliput" ) func main() { // Read input image inputData, err := os.ReadFile("input.jpg") if err != nil { panic(err) } // Create decoder decoder, err := lilliput.NewDecoder(inputData) if err != nil { panic(err) } defer decoder.Close() // Create ImageOps for images up to 8192x8192 pixels ops := lilliput.NewImageOps(8192) defer ops.Close() // Allocate output buffer (50MB) outputBuffer := make([]byte, 50*1024*1024) // Configure transformation options options := &lilliput.ImageOptions{ FileType: ".webp", // Output format Width: 800, // Target width Height: 600, // Target height ResizeMethod: lilliput.ImageOpsFit, // Crop to fit without stretching NormalizeOrientation: true, // Fix EXIF rotation EncodeOptions: map[int]int{ lilliput.WebpQuality: 85, // Quality setting }, } // Perform transformation outputData, err := ops.Transform(decoder, options, outputBuffer) if err != nil { fmt.Printf("Transform failed: %v\n", err) return } // Write output file if err := os.WriteFile("output.webp", outputData, 0644); err != nil { fmt.Printf("Write failed: %v\n", err) return } fmt.Printf("Resized and transcoded to WebP: %d bytes\n", len(outputData)) } ``` -------------------------------- ### Apply Orientation Transform to Framebuffer (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Modifies the Framebuffer's pixel data by rotating and/or mirroring it according to the provided `ImageOrientation`. Applying the image's native orientation value normalizes the image to a default orientation. ```go func (f *lilliput.Framebuffer) OrientationTransform(orientation lilliput.ImageOrientation) ``` -------------------------------- ### Create and Reuse ImageOps for Efficient Image Transformations Source: https://github.com/discord/lilliput/blob/master/README.md The `NewImageOps` function creates an ImageOps object for resizing and encoding images. This object can be reused for multiple operations, reducing memory allocations. It operates on images up to a specified dimension. ```go func lilliput.NewImageOps(dimension int) *lilliput.ImageOps ``` -------------------------------- ### Handle Animated Images (Go) Source: https://context7.com/discord/lilliput/llms.txt Reads and transforms animated images like WebP or GIF, preserving animation. It allows setting maximum frames and duration for the output. This function requires the input animated image file and writes the processed animation to an output file. ```go package main import ( "fmt" "os" "time" "github.com/discord/lilliput" ) func main() { // Read animated WebP or GIF inputData, err := os.ReadFile("animated.webp") if err != nil { panic(err) } decoder, err := lilliput.NewDecoder(inputData) if err != nil { panic(err) } defer decoder.Close() ops := lilliput.NewImageOps(4096) defer ops.Close() outputBuffer := make([]byte, 100*1024*1024) // Larger buffer for animations options := &lilliput.ImageOptions{ FileType: ".webp", Width: 400, Height: 400, ResizeMethod: lilliput.ImageOpsFit, NormalizeOrientation: true, MaxEncodeFrames: 100, // Limit to 100 frames MaxEncodeDuration: time.Duration(10 * time.Second), // Max 10 seconds EncodeTimeout: time.Duration(60 * time.Second), // Timeout after 60s DisableAnimatedOutput: false, // Keep animation EncodeOptions: map[int]int{ lilliput.WebpQuality: 80, }, } // Transform preserves animation outputData, err := ops.Transform(decoder, options, outputBuffer) if err != nil { fmt.Printf("Transform failed: %v\n", err) return } os.WriteFile("output_animated.webp", outputData, 0644) fmt.Printf("Animated output: %d bytes\n", len(outputData)) } ``` -------------------------------- ### Decode Image to Framebuffer with Lilliput (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Fully decodes the image and writes its pixel data to the provided Framebuffer. If the image is animated, subsequent calls yield subsequent frames. io.EOF is returned when no more frames are available. Note: Users typically use ImageOps instead of calling DecodeTo directly. ```Go func (d lilliput.Decoder) DecodeTo(f *lilliput.Framebuffer) error ``` -------------------------------- ### Encode Framebuffer (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Encodes the provided Framebuffer into the destination byte slice specified during Encoder creation. It returns a potentially shorter slice pointing to the valid encoded data within `dst`. Optional encoding options (e.g., JPEG quality) can be provided. ```go func (e lilliput.Encoder) Encode(buffer lilliput.Framebuffer, opts map[int]int) ([]byte, error) ``` -------------------------------- ### HDR to SDR Conversion (Go) Source: https://context7.com/discord/lilliput/llms.txt Performs HDR to SDR conversion for images using Lilliput's `ForceSdr` option, enabling tone mapping. This function reads an HDR image, applies the conversion, and saves the output as an SDR PNG. It checks for and reports the presence of an ICC profile. ```go package main import ( "fmt" "os" "github.com/discord/lilliput" ) func convertHDRtoSDR() { // Read HDR image (e.g., from iPhone or modern camera) inputData, err := os.ReadFile("hdr_photo.jpg") if err != nil { panic(err) } decoder, err := lilliput.NewDecoder(inputData) if err != nil { panic(err) } defer decoder.Close() // Check ICC profile icc := decoder.ICC() if len(icc) > 0 { fmt.Printf("Image has ICC profile: %d bytes\n", len(icc)) } ops := lilliput.NewImageOps(8192) defer ops.Close() outputBuffer := make([]byte, 50*1024*1024) options := &lilliput.ImageOptions{ FileType: ".png", Width: 1920, Height: 1080, ResizeMethod: lilliput.ImageOpsFit, NormalizeOrientation: true, ForceSdr: true, // Enable HDR to SDR tone mapping EncodeOptions: map[int]int{ lilliput.PngCompression: 7, }, } // Transform with tone mapping outputData, err := ops.Transform(decoder, options, outputBuffer) if err != nil { fmt.Printf("Transform failed: %v\n", err) return } os.WriteFile("sdr_output.png", outputData, 0644) fmt.Printf("Converted HDR to SDR: %d bytes\n", len(outputData)) } ``` -------------------------------- ### Clear Framebuffer (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Resets the contents of an existing Framebuffer to zero, effectively clearing any previously stored pixel data. This is useful for preparing a Framebuffer for reuse or for ensuring a clean state. ```go func (f *lilliput.Framebuffer) Clear() ``` -------------------------------- ### Close Encoder (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Releases resources held by the Encoder. It is mandatory to call `.Close()` on an Encoder instance when it is no longer in use to ensure proper cleanup. ```go func (e lilliput.Encoder) Close() ``` -------------------------------- ### Close Framebuffer (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Releases any resources associated with the Framebuffer. It is essential to call `.Close()` on a Framebuffer object when it is no longer needed to prevent resource leaks. ```go func (f *lilliput.Framebuffer) Close() ``` -------------------------------- ### Close ImageOps Object and Release Resources Source: https://github.com/discord/lilliput/blob/master/README.md The `Close` method releases all resources held by the ImageOps object. It is essential to call `Close()` when the ImageOps object is no longer needed to prevent resource leaks. ```go func (o *lilliput.ImageOps) Close() ``` -------------------------------- ### Clear Image Data from ImageOps Object Source: https://github.com/discord/lilliput/blob/master/README.md The `Clear` method removes all pixel data from the ImageOps object, freeing memory. This is an optional step users can take if they wish to explicitly remove image data from memory between operations. ```go func (o *lilliput.ImageOps) Clear() ``` -------------------------------- ### Close Lilliput Decoder and Release Resources (Go) Source: https://github.com/discord/lilliput/blob/master/README.md Closes the Lilliput Decoder and releases any associated resources. It is crucial to call this method when the Decoder object is no longer needed to prevent resource leaks. ```Go func (d lilliput.Decoder) Close() ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.