reximage

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2025 License: MIT Imports: 6 Imported by: 1

README

reximage

A Go package for importing/exporting REXPaint's .xp images

REXPaint is a fabulous program for creating ASCII art, made by ultra-famous roguelike developer Kyzrati. This package allows you to import and export image data to/from the .xp file format produced by REXPaint for use in your project. It was made as part of the larger Tyumi engine, but doesn't depend on it at all and is fine to be used standalone!

Documentation

Obtain this package in the usual way:

go get github.com/bennicholls/reximage

Importing
image, err := reximage.Import(pathname)

ensuring that pathname is a string describing a path to an .xp file. It will return an error if the pathname is invalid or the file cannot be read for some other reason.

Access the cell data using:

cell, err := image.GetCell(x, y)

where x and y are coordinates to a cell in the image (bounded by image.Width, image.Height). It will throw an error if (x,y) is not in bounds or if the image hasn't been imported yet. My engine Tyumi was originally built around SDL, so reximage follows SDL's convention of setting the (0,0) coordinate in the top-left of the image.

cell consists of a Glyph (ASCII codepoint) and RGB components for both foreground and background colours. Each colour component is 8bits (0-255). You can extract 32bit colours using some helper functions:

foregroundRGBA, backgroundRGBA := cell.RGBA()
foregroundARGB, backgroundARGB := cell.ARGB()

I imagine these are the most popular colour formats but I don't have the research to back that up. Of course, if your program uses a different colour format you can access the individual components and form the colour yourself.

Exporting

You can also create your own image and export it to the .xp format. First initialize an image:

var image reximage.ImageData
image.Init(width, height)

The cells in the image are initialized to REXPaint's default cell state. Then create cells with the visuals you want and set them with image.SetCell. Some helper functions are provided for setting cell colours with different colour formats.

var cell reximage.CellData
cell.Glyph = 88 // An 'X'
cell.SetColoursARGB(0xFFFFFFFF, 0xFF000000) // foreground white, background black
image.SetCell(0, 0, cell) // (0, 0) is the (x, y) coordinate of the top left cell in the image

If you use a different colour format than ARGB or RGBA then you'll have to set the cell's R,G,B components yourself. Once your image has been filled with cell data you can export it to a file:

err := reximage.Export(image, "some_file_path.xp")

If the file already exists it will be overwritten. If the export process fails Export will return an error.

Complete(-ish) documentation can be found on GoDoc.org.

Limitations

REXPaint supports saving images with up to 9 layers, but at the moment reximage does not retain layer data in xp files. On import, images with multiple layers are painted bottom to top and the final composed image is returned. Exported images are limited to 1 layer, encoding multiple layers is not yet implemented.

Future

This is all my engine needs at the moment so I'm not sure what else would be helpful here. On the off-chance someone else is using this, please let me know if there's anything you think it could use.

License

reximage is licensed under the MIT license (see LICENSE file).

Documentation

Overview

A package for decoding and handling .xp files produced by Kyzrati's fabulous REXPaint program, the gold-standard in ASCII art drawing programs. It can be found at www.gridsagegames.com/rexpaint.

reximage is part of the Tyumi engine by Benjamin Nicholls, but feel free to use it as a standalone package!

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Export added in v1.1.0

func Export(image ImageData, path string) (err error)

Export encodes an image as an .xp file and writes to disk at the specified path. If a file already exists at that location it is overwritten.

Types

type CellData

type CellData struct {
	Glyph uint32 // ASCII code for glyph
	R_f   uint8  // Foreground Colour - Red channel
	G_f   uint8  // Foreground Colour - Green channel
	B_f   uint8  // Foreground Colour - Blue channel
	R_b   uint8  // Background Colour - Red channel
	G_b   uint8  // Background Colour - Green channel
	B_b   uint8  // Background Colour - Blue channel
}

CellData holds the decoded data for a single cell. Colours are split into uint8 components so the user can combine them into whatever colour format they need. Some popular colour format conversion functions are provided as well.

func (CellData) ARGB

func (cd CellData) ARGB() (fore, back uint32)

ARGB returns the foreground and background colours of the cell in ARGB format. Alpha in this case is always set to maximum (255).

func (*CellData) Clear added in v1.1.0

func (cd *CellData) Clear()

Clear removes the cell's glyph and sets the cell's colours to the rexpaint default. A cleared cell will be considered undrawn.

func (CellData) RGBA

func (cd CellData) RGBA() (fore, back uint32)

RGBA returns the foreground and background colours of the cell in RGBA format. Alpha in this case is always set to maximum (255).

func (*CellData) SetColoursARGB added in v1.1.0

func (cd *CellData) SetColoursARGB(fore, back uint32)

SetColoursRGBA sets the colours of the cell, interpreting the input uint32 colours in ARGB8888 format. If background alpha is 0, the cell is set to undrawn. Foreground alpha is ignored.

func (*CellData) SetColoursRGBA added in v1.1.0

func (cd *CellData) SetColoursRGBA(fore, back uint32)

SetColoursRGBA sets the colours of the cell, interpreting the input uint32 colours in RGBA8888 format. If background alpha is 0, the cell is set to undrawn. Foreground alpha is ignored.

func (CellData) Undrawn

func (cd CellData) Undrawn() bool

Undrawn returns whether the cell is "undrawn" or empty, which in XP files is identified by the background colour (255, 0, 255).

type ImageData

type ImageData struct {
	Width  int
	Height int
	Cells  []CellData //will have Width*Height Elements
}

ImageData is the struct holding the decoded and exported image data.

func Import

func Import(path string) (image ImageData, err error)

Import imports an image from the xp file at the provided path. Returns the Imagedata and an error. If an error is present, ImageData will be no good.

func (ImageData) GetCell

func (id ImageData) GetCell(x, y int) (cd CellData, err error)

GetCell returns the CellData at coordinate (x, y) of the decoded image, with (0,0) at the top-left of the image.

func (*ImageData) Init added in v1.1.0

func (id *ImageData) Init(width, height int)

Init initializes an image with the provided size, setting all cells to the REXPaint default.

func (*ImageData) SetCell added in v1.1.0

func (id *ImageData) SetCell(x, y int, cell CellData) (err error)

SetCell sets the cell at (x, y) to cell. (0, 0) is considered to be the top-left of the image, with positive y values moving down.

Jump to

Keyboard shortcuts

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