nitro¶
Support for the NDS Nitro file formats.
Includes NFTR fonts, NARC archives, the NCLR/NCGR/NSCR/NCER graphic formats and their raw NBFP/NBFC/NBFS variants, and the textures of NSBMD/NSBTX 3D models.
- hacktools.nitro.extractNSBMD(infolder, outfolder, extension='.nsbmd', readfunc=None)[source]¶
Extract the textures of all the NSBMD models in a folder to png.
- hacktools.nitro.repackNSBMD(workfolder, infolder, outfolder, extension='.nsbmd', readfunc=None, writefunc=None)[source]¶
Repack the textures of all the NSBMD models in a folder from png.
- Parameters:
workfolder (str) – Path of the folder with the png files to pack.
infolder (str) – Path of the folder with the original models.
outfolder (str) – Path of the folder the repacked models are written to.
extension (str) – Extension of the model files.
readfunc – Optional function called with each file name, returning whether the first palette color should be transparent.
writefunc – Optional function called with (file, nsbmd), returning the (fixtransp, checkalpha, zerotransp, backwards) options used when matching palette colors.
- Return type:
None
- hacktools.nitro.extractIMG(infolder, outfolder, extensions='.NCGR', readfunc=None)[source]¶
Extract all the Nitro graphics in a folder to png.
The palette, map and cell files matching each image name are loaded with
readNitroGraphic(). Images with a cell file are drawn withdrawNCER(), the others withdrawNCGR().- Parameters:
infolder (str) – Path of the folder to scan.
outfolder (str) – Path of the folder to extract to.
extensions – Extension or list of extensions to filter by.
readfunc – Optional function called with (infolder, file, extension), returning a (palettes, image, map, cell, width, height, mapfile, cellfile) tuple, for files that need custom loading.
- Return type:
None
- hacktools.nitro.repackIMG(workfolder, infolder, outfolder, extensions='.NCGR', readfunc=None, writefunc=None, clean=False)[source]¶
Repack all the Nitro graphics in a folder from png or psd files.
- Parameters:
workfolder (str) – Path of the folder with the png or psd files to pack.
infolder (str) – Path of the folder with the original files.
outfolder (str) – Path of the folder the repacked files are written to.
extensions – Extension or list of extensions to filter by.
readfunc – Optional function called with (infolder, file, extension), returning a (palettes, image, map, cell, width, height, mapfile, cellfile) tuple, for files that need custom loading.
writefunc – Optional function called with (workfolder, infolder, outfolder, file, image, palettes, map, cell, width, height), returning the same data updated, plus a transptile option.
clean (bool) – Whether to delete output files with no matching image, instead of copying the originals.
- Return type:
None
- class hacktools.nitro.FontNFTR[source]¶
Structure of an NFTR font.
- height¶
Line height of the font.
- width¶
Max character width of the font.
- plgcoffset¶
Offset of the PLGC section, holding the glyph images.
- hdwcoffset¶
Offset of the HDWC section, holding the glyph widths.
- pamcoffset¶
Offset of the first PAMC section, holding the character map.
- plgcsize¶
Size of the PLGC section.
- glyphwidth¶
Width of the glyph images.
- glyphheight¶
Height of the glyph images.
- glyphlength¶
Length in bytes of a single glyph image.
- depth¶
Bits per pixel of the glyph images.
- rotation¶
Rotation of the glyph images.
- tilenum¶
Number of glyphs in the font.
- firstcode¶
First glyph index from the HDWC section.
- lastcode¶
Last glyph index from the HDWC section.
- plgc¶
Glyph images, as a list of PIL images, only filled when requested with generateglyphs.
- colors¶
Grayscale palette generated from the depth.
- hdwc¶
List of HDWC entries.
- pamc¶
List of PAMC entries.
- glyphs¶
Dictionary of character -> FontGlyph.
- class hacktools.nitro.FontHDWC[source]¶
Width data of a single NFTR glyph.
- start¶
Left offset of the glyph.
- width¶
Width of the glyph.
- length¶
Horizontal space occupied by the glyph when drawn.
- class hacktools.nitro.FontPAMC[source]¶
Header of a single NFTR character map section.
- firstchar¶
First character code of the section.
- lastchar¶
Last character code of the section.
- type¶
Type of the section, 0 to 2.
- nextoffset¶
Offset of the next PAMC section, 0 for the last one.
- hacktools.nitro.readNFTR(file, generateglyphs=False, encoding='shift_jis')[source]¶
Read the glyph information of an NFTR font.
- hacktools.nitro.extractFontData(fontfiles, out)[source]¶
Write the length of the ASCII glyphs of some fonts to a binary file.
- Parameters:
fontfiles – Path of the font file, or a list of paths.
out (str) – Path of the output file.
- Return type:
None
- class hacktools.nitro.NARC[source]¶
Structure of a NARC archive.
- btaf¶
Offset of the BTAF section, holding the file offsets.
- btnf¶
Offset of the BTNF section, holding the file names.
- gmif¶
Offset of the GMIF section, holding the file data.
- files¶
List of files in the archive.
- class hacktools.nitro.NARCFile[source]¶
Structure of a single file entry in a NARC archive.
- start¶
Absolute offset of the file data.
- size¶
Size of the file.
- path¶
Directory part of the file path.
- name¶
Name of the file.
- fullname¶
Path and name of the file.
- hacktools.nitro.extractNARCFile(narcfile, outfolder)[source]¶
Extract all the files of a NARC archive to a folder.
- hacktools.nitro.extractNARC(narcfile, outfolder, narc)[source]¶
Extract all the files of an already parsed NARC archive.
- Parameters:
narcfile – Path of the NARC file.
outfolder (str) – Path of the folder to extract to.
narc (NARC) – Archive structure returned by
readNARC().
- Return type:
None
- hacktools.nitro.repackNARCFile(narcfilein, narcfileout, infolder)[source]¶
Repack a NARC archive, replacing the files found in a folder.
- hacktools.nitro.repackNARC(narcfilein, narcfileout, infolder, narc)[source]¶
Repack an already parsed NARC archive.
Files that exist in infolder replace the original ones, and the file table and section sizes are updated, so files can grow.
- Parameters:
narcfilein (str) – Path of the original NARC file.
narcfileout (str) – Path of the output NARC file.
infolder (str) – Path of the folder with the replacement files.
narc (NARC) – Archive structure returned by
readNARC().
- Return type:
None
- class hacktools.nitro.NCGR[source]¶
Structure of an NCGR tile file.
- width¶
Width of the image in pixels.
- height¶
Height of the image.
- bpp¶
Bits per pixel, 4 or 8.
- tilesize¶
Size of a single tile.
- tileoffset¶
Offset of the tile data.
- tilelen¶
Length of the tile data.
- lineal¶
Whether the tiles are stored in lineal order, instead of tiled.
- tiles¶
List of tiles, each a flat list of palette indexes.
- class hacktools.nitro.NSCR[source]¶
Structure of an NSCR tile map file.
- width¶
Width of the image in pixels.
- Type:
int
- height¶
Height of the image.
- Type:
int
- maplen¶
Length of the map data.
- Type:
int
- mapoffset¶
Offset of the map data.
- Type:
int
- maps¶
List of map entries.
- Type:
list[hacktools.nitro.Map]
- class hacktools.nitro.Map[source]¶
Structure of a single NSCR map entry.
- pal¶
Palette of the tile.
- xflip¶
Whether the tile is flipped horizontally.
- yflip¶
Whether the tile is flipped vertically.
- tile¶
Index of the tile.
- class hacktools.nitro.NCER[source]¶
Structure of an NCER cell file.
- banknum¶
Number of banks in the file.
- tbank¶
Bank type, 0x01 for extended banks with a bounding box.
- bankoffset¶
Offset of the bank data.
- blocksize¶
Tile offset shift of the banks.
- partitionoffset¶
Offset of the partition data, if any.
- maxpartitionsize¶
Max partition size from the partition data.
- firstpartitionoffset¶
Offset of the first partition.
- banks¶
List of banks in the file.
- class hacktools.nitro.Bank[source]¶
Structure of a single NCER bank, a group of cells forming a sprite.
- cellnum¶
Number of cells in the bank.
- cellinfo¶
Raw cell info value.
- celloffset¶
Offset of the cell data, relative to the end of the bank data.
- objoffset¶
Absolute offset of the cell data.
- partitionoffset¶
Offset of the bank partition.
- partitionsize¶
Size of the bank partition.
- cells¶
List of cells in the bank, sorted by drawing order.
- xmax¶
Right edge of the bounding box.
- ymax¶
Bottom edge of the bounding box.
- xmin¶
Left edge of the bounding box.
- ymin¶
Top edge of the bounding box.
- width¶
Width of the bank.
- height¶
Height of the bank.
- layernum¶
Number of layers used for psd exporting, computed so intersecting cells end up on different layers.
- duplicate¶
Whether the bank has the same cells as a previous one.
- class hacktools.nitro.Cell[source]¶
Structure of a single NCER cell, an OBJ entry of a bank.
- x¶
X position of the cell in the bank.
- y¶
Y position of the cell in the bank.
- width¶
Width of the cell, derived from shape and size.
- height¶
Height of the cell, derived from shape and size.
- numcell¶
Index of the cell in the bank.
- shape¶
OBJ shape attribute.
- size¶
OBJ size attribute.
- objoffset¶
Offset of the OBJ entry.
- tileoffset¶
Tile offset of the cell.
- rsflag¶
Whether rotation/scaling is enabled.
- objdisable¶
Whether the OBJ is disabled, without rsflag.
- doublesize¶
Whether the OBJ is double size, with rsflag.
- objmode¶
OBJ mode attribute.
- mosaic¶
Whether the mosaic effect is enabled.
- depth¶
Whether the cell is 8bpp instead of 4bpp.
- xflip¶
Whether the cell is flipped horizontally.
- yflip¶
Whether the cell is flipped vertically.
- selectparam¶
Rotation/scaling parameter selection, with rsflag.
- priority¶
Drawing priority of the cell.
- pal¶
Palette of the cell.
- layer¶
Layer used for psd exporting.
- hacktools.nitro.readNitroGraphic(palettefile, tilefile, mapfile, cellfile, ignorepalindex=False, ignoredupes=False)[source]¶
Read a Nitro graphic with all its related files.
The map and cell files are optional, the palette and tile ones aren’t.
- Parameters:
palettefile (str) – Path of the NCLR palette file.
tilefile (str) – Path of the NCGR tile file.
mapfile (str) – Path of the NSCR map file.
cellfile (str) – Path of the NCER cell file.
ignorepalindex (bool) – Whether to ignore the palette index section.
ignoredupes (bool) – Whether to skip marking duplicate cell banks.
- Returns:
A (palettes, ncgr, nscr, ncer, width, height) tuple, with None for the files that weren’t found.
- hacktools.nitro.readNCLR(nclrfile, ignoreindex=False)[source]¶
Read the palettes of an NCLR file.
- Parameters:
nclrfile (str) – Path of the NCLR file.
ignoreindex (bool) – Whether to ignore the palette index section.
- Returns:
A dictionary of palette index -> list of RGBA tuples.
- Return type:
dict
- hacktools.nitro.readNCGRTiles(ncgr, tiledata)[source]¶
Split raw tile data into the tiles of an NCGR structure.
- Parameters:
ncgr (NCGR) – Tile structure to fill.
tiledata (bytes) – Raw tile data.
- Return type:
None
- hacktools.nitro.readMapData(data)[source]¶
Parse a single 16-bit map entry.
- Parameters:
data (int) – Raw map entry value.
- Returns:
The parsed map entry.
- Return type:
- hacktools.nitro.getNCERCellSize(shape, size)[source]¶
Get the size of a cell from its OBJ shape and size attributes.
- Parameters:
shape (int) – OBJ shape attribute, 0 to 2.
size (int) – OBJ size attribute, 0 to 3.
- Returns:
The (width, height) of the cell.
- Return type:
tuple
- hacktools.nitro.readNCER(ncerfile, ignoredupes=False)[source]¶
Read the banks and cells of an NCER file.
The size and position of each bank is computed from its cells, and the layers used for psd exporting are assigned so intersecting cells end up on different layers.
- hacktools.nitro.tileToPixels(pixels, width, ncgr, tile, xflip, yflip, i, j, palette, pali, usetransp=True)[source]¶
Draw a single tile of an NCGR on an image.
- Parameters:
pixels – PIL pixel access object to draw on.
width (int) – Width of the image.
ncgr (NCGR) – Tile structure holding the tiles.
tile (int) – Index of the tile to draw.
xflip (bool) – Whether the tile is flipped horizontally.
yflip (bool) – Whether the tile is flipped vertically.
i (int) – Y position to draw at, in tiles.
j (int) – X position to draw at, in tiles.
palette (list) – Palette to use, as a list of RGBA tuples.
pali (int) – Index of the first palette color to use.
usetransp (bool) – Whether to skip drawing index 0, leaving it transparent.
- Returns:
The pixels object.
- hacktools.nitro.drawNCER(outfile, ncer, ncgr, palettes, usetransp=True, layered=False)[source]¶
Draw the banks of an NCER to a png file.
The banks are stacked vertically, with the palettes drawn on the right side. When layered is set and ImageMagick is available, a psd file is also created with each bank layer on its own named layer.
- Parameters:
outfile (str) – Path of the png file to create.
ncer (NCER) – Cell structure returned by
readNCER().ncgr (NCGR) – Tile structure returned by
readNCGR().palettes (dict) – Dictionary of palette index -> list of RGBA tuples.
usetransp (bool) – Whether to skip drawing index 0, leaving it transparent.
layered (bool) – Whether to create the psd file.
- Return type:
None
- hacktools.nitro.drawNCGR(outfile, nscr, ncgr, palettes, width, height, usetransp=True)[source]¶
Draw an NCGR to a png file, with the palettes on the right side.
- Parameters:
outfile (str) – Path of the png file to create.
nscr (NSCR) – Map structure returned by
readNSCR(), or None to draw the tiles sequentially.ncgr (NCGR) – Tile structure returned by
readNCGR().palettes (dict) – Dictionary of palette index -> list of RGBA tuples.
width (int) – Width of the image, 0xffff to assume a square image.
height (int) – Height of the image, 0xffff to assume a square image.
usetransp (bool) – Whether to skip drawing index 0, leaving it transparent.
- Return type:
None
- hacktools.nitro.writeNCGRData(f, bpp, index1, index2)[source]¶
Write two pixels of tile data, packed in one byte for 4bpp.
- Parameters:
f (Stream) – Stream to write to.
bpp (int) – Bits per pixel, 4 or 8.
index1 (int) – First palette index.
index2 (int) – Second palette index.
- Return type:
None
- hacktools.nitro.writeNCGRTile(f, pixels, width, ncgr, i, j, palette)[source]¶
Write a single tile of an NCGR from an image.
- Parameters:
f (Stream) – Stream to write to, seeked to the tile.
pixels – PIL pixel access object to read from.
width (int) – Width of the image.
ncgr (NCGR) – Tile structure the tile belongs to.
i (int) – Y position to read at, in tiles.
j (int) – X position to read at, in tiles.
palette (list) – Palette to use, as a list of RGBA tuples.
- Return type:
None
- hacktools.nitro.writeNCGR(file, ncgr, infile, palettes, width=-1, height=-1)[source]¶
Repack a png into an NCGR file with sequential tiles.
- Parameters:
file (str) – Path of the NCGR file to update.
ncgr (NCGR) – Tile structure returned by
readNCGR().infile (str) – Path of the png file to pack.
palettes (dict) – Dictionary of palettes, only the first one is used.
width (int) – Width of the image, or -1 to use the NCGR one.
height (int) – Height of the image, or -1 to use the NCGR one.
- Return type:
None
- hacktools.nitro.writeNSCR(file, ncgr, nscr, infile, palettes, width=-1, height=-1, skipfirst=False)[source]¶
Repack a png into an NCGR file, following its original map data.
Tiles are written back where the map entries point, without changing the map, so the png needs to keep the original tile layout. Flipped tiles are skipped.
- Parameters:
file (str) – Path of the NCGR file to update.
ncgr (NCGR) – Tile structure returned by
readNCGR().nscr (NSCR) – Map structure returned by
readNSCR().infile (str) – Path of the png file to pack.
palettes (dict) – Dictionary of palette index -> list of RGBA tuples.
width (int) – Width of the image, or -1 to use the NSCR one.
height (int) – Height of the image.
skipfirst (bool) – Whether to skip the first tile.
- Return type:
None
- hacktools.nitro.writeMappedNSCR(file, mapfile, ncgr, nscr, infile, palettes, width=-1, height=-1, transptile=False, writelen=True, useoldpal=False)[source]¶
Repack a png into an NCGR and NSCR pair, rebuilding the map.
Single map version of
writeMultiMappedNSCR(), see that for the parameters.
- hacktools.nitro.writeMultiMappedNSCR(file, mapfiles, ncgr, nscrs, infiles, palettes, width=-1, height=-1, transptile=False, writelen=True, useoldpal=False)[source]¶
Repack pngs into an NCGR and multiple NSCR maps, rebuilding them.
Tiles are deduplicated across all the maps, reusing already written tiles and their flipped versions, and each map is rebuilt with the best palette for each tile.
- Parameters:
file (str) – Path of the NCGR file to update.
mapfiles (list) – Paths of the NSCR files to update.
ncgr (NCGR) – Tile structure returned by
readNCGR().nscrs (list) – Map structures returned by
readNSCR().infiles (list) – Paths of the png files to pack.
palettes (dict) – Dictionary of palette index -> list of RGBA tuples.
width (int) – Width of the images, or -1 to use the NSCR ones.
height (int) – Height of the images, or -1 to use the NSCR ones.
transptile (bool) – Whether to reserve the first tile as a fully transparent one.
writelen (bool) – Whether to write the new tile data length in the NCGR.
useoldpal (bool) – Whether to keep the palette of the original map entries, instead of picking the best one.
- Return type:
None
- hacktools.nitro.searchTile(tile, tiles, tilesize=8)[source]¶
Search for a tile in a list, also trying its flipped versions.
- Parameters:
tile (list) – Tile to search for, as a flat list of palette indexes.
tiles (list) – List of tiles to search in.
tilesize (int) – Size of the tiles.
- Returns:
A (tile, xflip, yflip) tuple with the index of the matching tile, or -1 if not found, and the flips needed to match it.
- hacktools.nitro.writeNCER(file, ncerfile, ncgr, ncer, infile, palettes, width=0, height=0, appendTiles=False, checkRepeat=True, writelen=True, fixtransp=False, checkalpha=False, zerotransp=True)[source]¶
Repack a png or psd into the NCGR and NCER files of a sprite.
The image is expected in the layout drawn by
drawNCER(). For psd files, the bank layers are extracted with ImageMagick and matched by name. Flipped and duplicate cells are skipped.- Parameters:
file (str) – Path of the NCGR file to update.
ncerfile (str) – Path of the NCER file to update.
ncgr (NCGR) – Tile structure returned by
readNCGR().ncer (NCER) – Cell structure returned by
readNCER().infile (str) – Path of the png or psd file to pack.
palettes (dict) – Dictionary of palette index -> list of RGBA tuples.
width (int) – Unused.
height (int) – Unused.
appendTiles (bool) – Whether cells that changed can have new tiles appended at the end of the NCGR, updating their OBJ entries.
checkRepeat (bool) – Whether to skip tiles that were already written.
writelen (bool) – Whether to write the new tile count in the NCGR, when tiles were appended.
fixtransp (bool) – Whether to skip the first palette color when matching.
checkalpha (bool) – Whether the alpha channel counts when matching colors.
zerotransp (bool) – Whether fully transparent colors match index 0.
- Return type:
None
- hacktools.nitro.NSBMDbpp = [0, 8, 2, 4, 8, 2, 8, 16]¶
Bits per pixel of each NSBMD texture format.
- class hacktools.nitro.NSBMD[source]¶
Structure of the TEX0 block of an NSBMD or NSBTX file.
- textures¶
List of textures in the file.
- palettes¶
List of palettes in the file.
- blockoffset¶
Offset of the TEX0 block.
- blocksize¶
Size of the TEX0 block.
- blocklimit¶
Offset of the end of the TEX0 block.
- texdatasize¶
Size of the texture data.
- texdataoffset¶
Offset of the texture data.
- sptexsize¶
Size of the 4x4-texel texture data.
- sptexoffset¶
Offset of the 4x4-texel texture data.
- spdataoffset¶
Offset of the 4x4-texel palette index data.
- paldatasize¶
Size of the palette data.
- paldefoffset¶
Offset of the palette definitions.
- paldataoffset¶
Offset of the palette data.
- class hacktools.nitro.NSBMDTexture[source]¶
Structure of a single NSBMD texture.
- name¶
Name of the texture.
- offset¶
Offset of the texture data.
- width¶
Width of the texture.
- height¶
Height of the texture.
- size¶
Size of the texture data.
- data¶
Texture data.
- spdata¶
Palette index data, for 4x4-texel textures.
- class hacktools.nitro.NSBMDPalette[source]¶
Structure of a single NSBMD palette.
- name¶
Name of the palette.
- offset¶
Offset of the palette data.
- size¶
Size of the palette data.
- data¶
Palette colors, as a list of RGBA tuples.
- hacktools.nitro.readTEX0(nsbmd, f, zerotransp=False)[source]¶
Read the textures and palettes of a TEX0 block.
- hacktools.nitro.readNSBMD(nsbmdfile, zerotransp=False)[source]¶
Read the textures and palettes of an NSBMD model.
- hacktools.nitro.readNSBTX(nsbmdfile, zerotransp=False)[source]¶
Read the textures and palettes of an NSBTX file.
- hacktools.nitro.drawNSBMD(file, nsbmd, texi, drawpalette=True)[source]¶
Draw a single NSBMD texture to a png file.
All the texture formats are supported. The palette, if any, is drawn on the right side, unless drawpalette is unset.
- Parameters:
file (str) – Path of the png file to create.
nsbmd (NSBMD) – Structure returned by
readNSBMD().texi (int) – Index of the texture to draw.
drawpalette (bool) – Whether to draw the palette next to the texture, making the image 40 pixels wider and at least as tall as the palette.
- Return type:
None
- hacktools.nitro.writeNSBMD(file, nsbmd, texi, infile, fixtransp=False, checkalpha=False, zerotransp=True, backwards=False)[source]¶
Write a png image back into a single NSBMD texture.
The 4x4-texel (5) and direct color (7) formats are not supported.
- Parameters:
file (str) – Path of the model file to update.
nsbmd (NSBMD) – Structure returned by
readNSBMD().texi (int) – Index of the texture to write.
infile (str) – Path of the png file to pack.
fixtransp (bool) – Whether to skip the first palette color when matching.
checkalpha (bool) – Whether the alpha channel counts when matching colors.
zerotransp (bool) – Whether fully transparent colors match index 0.
backwards (bool) – Whether to search the palette backwards.
- Return type:
None
- hacktools.nitro.readManualCells(manualcells)[source]¶
Build an NCER structure from a manual cell description.
Used for images that have cell data hardcoded in the game instead of an NCER file.
- Parameters:
manualcells (list) – List of bank dictionaries, each holding a “cells” list of {“width”, “height”, “x”, “y”} dictionaries, and optional “pal” and “repeat” values.
- Returns:
The built cell structure.
- Return type:
- hacktools.nitro.readNitroGraphicNBFC(palettefile, tilefile, mapfile, lineal=False, bpp=0)[source]¶
Read a raw NBFP/NBFC/NBFS graphic.
- Parameters:
- Returns:
A (palettes, nbfc, nbfs) tuple, with None for the files that weren’t found.
- hacktools.nitro.readNitroGraphicNTFT(palettefile, tilefile, lineal=True)[source]¶
Read a raw NTFP/NTFT graphic.
- hacktools.nitro.readNBFP(ntfpfile, bpp=8)[source]¶
Read the palettes of a raw NBFP or NTFP file.
- Parameters:
ntfpfile (str) – Path of the palette file.
bpp (int) – Bits per pixel of the image the palette is for.
- Returns:
A dictionary of palette index -> list of RGBA tuples.
- Return type:
dict