trick/gfxconvert

↩ back to overview

Module for converting between PNG and raw GBA/NDS image data.

Types

GfxColor = distinct uint16

15-bit (5 bits per channel) BGR color. Each component has a value between 0 and 31.

The 16th bit has no meaning on the GBA, but in this library it is used to represent empty/transparent pixels.

The r, g and b components can be accessed using the provided getters and setters.

GfxPalette = seq[GfxColor]
A list of colors used by a graphic.
GfxBpp = enum
  gfx2bpp = 2, ## Not used by the GBA hardware, but some games may wish
                ## to store graphics in a compact 2bpp paletted format.
  gfx4bpp = 4,              ## 16-color GBA graphics format.
  gfx8bpp = 8                ## 256-color GBA graphics format.
Bits-per-pixel specifier.
GfxLayout = enum
  gfxBitmap,                ## Image is a simple matrix of pixels
  gfxTiles                   ## Image is broken into 8x8px tiles
GfxInfo = object
  pal*: GfxPalette ## Defines the colors used by the image.
                   ## When building the palette via `pngToBin`, it's recommended to
                   ## initialize this to `@[clrEmpty]` so that the first encountered
                   ## color will be given index 1.
                   ## Transparent pixels will always be given index 0.
  bpp*: GfxBpp               ## How many bits-per-pixel are used by this graphic.
  layout*: GfxLayout         ## Bitmap or tiled arrangement.
  width*: int                ## Image width (only needed for `binToPng`, will be set by `pngToBin`)
  height*: int               ## Image height (unneeded, will be set by `pngToBin`)
  palGroup*: int ## An optional tag which you could use to check whether two
                 ## graphics are sharing a palette
  
A specification for how to read or write a paletted image. This is used by the pngToBin or binToPng functions.

Consts

clrEmpty = 32768'u
A color which is different to all other colors due to the unused bit. It's recommended to use this as the first item in any palette.

Procs

proc `==`(a, b: GfxColor): bool {...}{.borrow.}
proc `$`(c: GfxColor): string {...}{.raises: [], tags: [].}
proc readPal(palFile: string): GfxPalette {...}{.raises: [IOError],
    tags: [ReadIOEffect].}
Read a palette from a binary file.
proc writePal(palFile: string; pal: GfxPalette) {...}{.
    raises: [IOError, Exception, OSError], tags: [WriteIOEffect].}
Write a palette to a binary file.
proc binToPng(data: string; conf: GfxInfo): PNG
Convert raw GBA/NDS graphics data to a PNG object
proc writeFile(filename: string; png: PNG)
Write a PNG object to a file
proc readPng(filename: string): PNG[string] {...}{.
    raises: [IOError, OSError, PNGError, NZError, Exception],
    tags: [ReadIOEffect, WriteIOEffect].}
Load a PNG object instead of the less-flexible PNGResult that nimPNG usually returns.
proc pngToBin(filename: string; conf: var GfxInfo; buildPal: bool): string {...}{.
    raises: [IOError, OSError, PNGError, NZError, Exception],
    tags: [ReadIOEffect, WriteIOEffect].}

Load a PNG by filename and convert to GBA/NDS image data.

Pixels are returned as a string of raw binary data. Fields of conf will be modified to include any additional data such as image width.

If buildPal is true, unique colors will be added to conf.pal. Otherwise new colors are considered an error.

Funcs

func rgb15(r, g, b: int): GfxColor {...}{.deprecated: "Use rgb5 instead", inline,
                                     raises: [], tags: [].}
Deprecated: Use rgb5 instead
func rgb5(r, g, b: int): GfxColor {...}{.inline, raises: [], tags: [].}
Create a 15-bit BGR color from 5-bit components Each component should be from 0 to 31.
func rgb8(r, g, b: int): GfxColor {...}{.inline, raises: [], tags: [].}
Create a 15-bit BGR color, using 8-bit components
func rgb8(rgb: int): GfxColor {...}{.inline, raises: [], tags: [].}
Create a 15-bit BGR color from a 24-bit RGB color of the form 0xRRGGBB
func r(color: GfxColor): int {...}{.inline, raises: [], tags: [].}
func r=(color: var GfxColor; r: int) {...}{.inline, raises: [], tags: [].}
Red component of a 15-bit color.
func g(color: GfxColor): int {...}{.inline, raises: [], tags: [].}
func g=(color: var GfxColor; g: int) {...}{.inline, raises: [], tags: [].}
Green component of a 15-bit color.
func b(color: GfxColor): int {...}{.inline, raises: [], tags: [].}
func b=(color: var GfxColor; b: int) {...}{.inline, raises: [], tags: [].}
Blue component of a 15-bit color.
func swap4bpp(ab: uint8): uint8 {...}{.inline, raises: [], tags: [].}

Swap the low and high nybbles within a byte. e.g.

swap4bpp(0x12) == 0x21
This is needed because, in 4bpp graphics on the GBA, each byte represents two pixels, whereby the lower 4 bits define the color of the left pixel and the higher 4 bits define the color of the right pixel.

func swap2bpp(abcd: uint8): uint8 {...}{.inline, raises: [], tags: [].}

Swap all pairs of bits within a byte. e.g.

swap2bpp(0b00_01_10_11) == 0b11_10_01_00
May be useful for games that store graphics in a 2bpp format.

Iterators

iterator tileEncode(width, height: int; bpp = gfx8bpp; tileWidth = 8;
                    tileHeight = 8): tuple[destIndex, srcIndex: int] {...}{.
    raises: [], tags: [].}
Used to unravel a tiled space into a linear array. If each index in the space contains more than one pixel of data, the bpp option can be specified.