imacon

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2025 License: MIT Imports: 10 Imported by: 0

README

Imacon

Image Context Output

Imacon is a golang module for creating image representation of context data.

This module is built to solve the problem of dynamically generating images that represent various types of context data (text, images) in a image representation format that multimodel AI can use it as context input.

Features

  • Render text blocks with word wrapping.
  • Load and display JPEG and PNG images.
  • Auto-tiling of multiple objects to fit within a specified canvas size.
  • Waterfall layout for arranging objects efficiently.
  • Support for nested panes to create complex layouts.
  • Custom canvas size and font settings.

Installation

go get github.com/dannykok/imacon

Requirements

  • Go 1.24.1 or higher

Usage

Basic Example
package main

import (
    "os"
    "github.com/dannykok/imacon"
)

func main() {
    // Create engine with configuration
    eng := imacon.New(imacon.Config{
        MaxCanvasWidth:  2048, // The maximum width of the output image
        MaxCanvasHeight: 2048, // The maximum height of the output image
        FontSize:        32, // optional
    })

    // Scene is the root struct that holds the main Pane struct
    // Create text block
    textBlock := imacon.NewTextBlock("Imacon: Image Representation of Context Data", imacon.TextBlockOpts{TextWrap: true})
    
    // Create pane with objects
    pane := imacon.NewPane([]imacon.Tileable{textBlock}, 0, 0, 0)
    
    // Create scene with main pane
    scene := imacon.NewScene(pane)

    // Render the scene
    canvas, err := eng.Render(&scene)
    if err != nil {
        panic(err)
    }

    // Save to file
    f, _ := os.Create("output.png")
    defer f.Close()
    canvas.ToPng(f)
}
Working with Images
// Load an image
// NewImageBlock accepts an io.Reader which can be a file or network stream
file, _ := os.Open("path/to/image.jpg")
defer file.Close()
imgBlock, _ := imacon.NewImageBlock(file, "My Image")

// Create scene with image with default option values
pane := imacon.NewPane([]imacon.Tileable{imgBlock}, 0, 0, 0)
scene := imacon.NewScene(pane)
Create a auto waterfall layout
// Imacon automatically tiles multiple objects to fit the canvas
textBlock := imacon.NewTextBlock("Remarks", imacon.TextBlockOpts{})
pane := imacon.NewPane([]imacon.Tileable{
    imgBlock1,
    imgBlock2,
    textBlock,
    imgBlock3,
}, 0, 0, 0)
scene := imacon.NewScene(pane)
Custom Pane layout

For more control over the layout, you can use NewPaneWithShape to define a custom column-based layout:

// Create text blocks
title := imacon.NewTextBlock("Character Sheet", imacon.TextBlockOpts{TextWrap: true})
description := imacon.NewTextBlock("Details about the character...", imacon.TextBlockOpts{TextWrap: true})

// Define a custom shape with 3 columns using NewShapeWithObjects
shape := imacon.NewShapeWithObjects([]imacon.Column{
    // First column - text descriptions
    {
        Objects: []imacon.Tileable{title, description},
    },
    // Second column - first set of images
    {
        Objects: []imacon.Tileable{img1, img2},
    },
    // Third column - second set of images
    {
        Objects: []imacon.Tileable{img3, img4},
    },
})

// Create pane with the custom shape
// Parameters: shape, colWidth, colPad, rowPad
pane := imacon.NewPaneWithShape(shape, 480, imacon.DefaultColPad, imacon.DefaultMinPad)
scene := imacon.NewScene(pane)

Testing

go test -v ./...

Test outputs will be saved to test_output/ directory.

Dependencies

Documentation

Index

Constants

View Source
const (
	DefaultOuterPad    = 24.0  // The default outer padding around the canvas
	DefaultMinPad      = 12.0  // The minimum padding between tiles
	DefaultLineSpacing = 1.5   // The default line spacing for text rendering
	DefaultLabelPad    = 3.0   // The default padding between image and its label
	DefaultMinFontSize = 12.0  // The default minimum font size
	DefaultMaxFontSize = 32.0  // The default maximum font size
	DefaultColWidth    = 720.0 // The default column width for tiling
	DefaultColPad      = 24.0  // The default padding between columns
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Canvas

type Canvas struct {
	Width  int         // The width of the canvas in pixels.
	Height int         // The height of the canvas in pixels.
	Raw    image.Image // The raw image data of the canvas.
}

Canvas represents the rendered image canvas.

func (*Canvas) ToJpeg

func (c *Canvas) ToJpeg(writer io.Writer, options *jpeg.Options) error

ToJpeg encodes the canvas image to JPEG format and writes it to the provided writer.

func (*Canvas) ToPng

func (c *Canvas) ToPng(writer io.Writer) error

ToPng encodes the canvas image to PNG format and writes it to the provided writer.

type Column added in v0.2.0

type Column struct {
	Objects []Tileable
}

Column represents a single column in the pane, containing multiple tileable objects. The column width will follow Pane.ColWidth when rendered.

func (Column) Height added in v0.2.0

func (c Column) Height(ctx *gg.Context, colWidth float64, rowPad float64) float64

type Config

type Config struct {
	MaxCanvasWidth  int         // The maximum width of the canvas to compose images on.
	MaxCanvasHeight int         // The maximum height of the canvas to compose images on.
	FgColor         color.Color // The foreground color used for text and shapes.
	BgColor         color.Color // The background color of the canvas.
	FontSize        float64     // The default font size for text rendering.
}

The configuration options for the Imacon rendering engine.

type Drawable

type Drawable interface {
	Draw(ctx *gg.Context, cw float64, ch float64) // Draw the object onto the given context with specified canvas width and height
}

Drawable defines the behavior of objects that can be drawn onto the scene.

type Engine

type Engine struct {
	// contains filtered or unexported fields
}

func New

func New(cfg Config) *Engine

func (*Engine) Render

func (e *Engine) Render(scene *Scene) (*Canvas, error)

Render generates a canvas by rendering the provided scene according to the engine's configuration.

type ImageBlock

type ImageBlock struct {
	// Representation of an image, with a custom label for identification.
	Image image.Image
	Label *TextBlock
}

func NewImageBlock

func NewImageBlock(file io.Reader, label string) (*ImageBlock, error)

func (*ImageBlock) Draw

func (i *ImageBlock) Draw(ctx *gg.Context, cw float64, ch float64)

func (*ImageBlock) IntrinsicSize

func (i *ImageBlock) IntrinsicSize(ctx *gg.Context, expectedWidth float64, expectedHeight float64) (float64, float64)

type Pane

type Pane struct {
	Objects      []Tileable // The objects within the pane, which can be TextBlocks or ImageBlocks
	PlannedShape *Shape     // The planned shape of the pane after layout calculation
	ColWidth     float64    // The fixed column width for tiling
	ColPad       float64    // The padding between columns
	RowPad       float64    // The padding between tiles in a column
}

Pane represents a container that holds multiple tileable objects (TextBlocks or ImageBlocks) and manages their layout.

func NewPane added in v0.2.0

func NewPane(objects []Tileable, colWidth float64, colPad float64, rowPad float64) *Pane

func NewPaneWithShape added in v0.2.0

func NewPaneWithShape(Shape *Shape, colWidth float64, colPad float64, rowPad float64) *Pane

func (*Pane) Draw

func (p *Pane) Draw(ctx *gg.Context, cw float64, ch float64)

func (*Pane) DrawShape added in v0.2.0

func (p *Pane) DrawShape(ctx *gg.Context, shape Shape)

Draw the pane onto the given context based on the provided shape.

func (*Pane) IntrinsicSize

func (p *Pane) IntrinsicSize(ctx *gg.Context, expectedWidth float64, expectedHeight float64) (float64, float64)

func (*Pane) Shape

func (p *Pane) Shape(ctx *gg.Context) (Shape, Size)

Calculate and return the shape of the column layout of the pane. The algorithm finds the smallest footprint of canvas that can fit all objects in the pane.

type Scene

type Scene struct {
	Main *Pane // The main pane that holds all the objects to be rendered.

}

Scene represents the overall image composition, containing panes and their layout properties.

func NewScene

func NewScene(main *Pane) *Scene

type Shape

type Shape struct {
	Columns []Column // The columns in the pane
}

Shape stores the layout shape of the pane in terms of columns and rows. It's a temporary view of underlying objects calculated using the greedy algorithm to fit into the best canvas size.

func NewShape added in v0.2.0

func NewShape(colCount int) *Shape

func NewShapeWithObjects added in v0.2.0

func NewShapeWithObjects(columns []Column) *Shape

type Size added in v0.2.0

type Size struct {
	Width  float64
	Height float64
}

type TextBlock

type TextBlock struct {
	// Representation of a plain-text.
	Text string
	Opts TextBlockOpts
}

func NewTextBlock

func NewTextBlock(text string, opts TextBlockOpts) *TextBlock

func (*TextBlock) Draw

func (t *TextBlock) Draw(ctx *gg.Context, cw float64, ch float64)

func (*TextBlock) IntrinsicSize

func (t *TextBlock) IntrinsicSize(ctx *gg.Context, expectedWidth float64, expectedHeight float64) (float64, float64)

type TextBlockOpts

type TextBlockOpts struct {
	TextWrap bool // Whether to wrap text if it exceeds the pane width
}

type TileProxy added in v0.2.0

type TileProxy struct {
	Object Tileable // The actual tileable object being proxied
	Size   Size     // The pre-calculated size of the tile
}

TileProxy is a placeholder tileable object used for layout calculations, with pre-calculated size.

func (*TileProxy) Draw added in v0.2.0

func (t *TileProxy) Draw(ctx *gg.Context, cw float64, ch float64)

func (*TileProxy) IntrinsicSize added in v0.2.0

func (t *TileProxy) IntrinsicSize(ctx *gg.Context, expectedWidth float64, expectedHeight float64) (float64, float64)

type Tileable

type Tileable interface {
	Drawable
	IntrinsicSize(ctx *gg.Context, expectedWidth float64, expectedHeight float64) (float64, float64) // return the intrinsic width and height of the object
}

Tilable specific the tiling behavior of the pane within the window

Jump to

Keyboard shortcuts

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