2023-03-03
mgl-texatlas - Texture atlas generator
mgl-texatlas [options] image-files
mgl-texatlas is a tool for combining multiple images into one image of a specified size.
Multiple images to be combined are specified in the image-files argument. The tool outputs the combined images and location data to access the original images. The location data is output in binary and JSON format.
Currently, only PNG is supported as the image file format for both input and output.
The tool outputs the following files.
texture[d].png
The combined image. The [d] part is a number greater than or equal to 0. If the input images do not fit into the specified size, they are output as multiple images with sequential numbers.
texture_loc.bin
A binary file of location data. This file contains information about the coordinates where the input image is placed on the texture. See the BINARY FORMAT section for the storage format.
texture_loc.json
Location data is output in JSON format. The content is equivalent to that of a binary file.
The size of the output image and the "texture" part of the filename can be changed optionally. See OPTIONS section for details.
--hash-seed
Specifies the seed value of the hash generation function to be used for location names. If not specified, 0xA3F6C23E is used. Use this option to change the seed value in case of hash value collision.
--help
Display simple help and exit.
--margin, -m
Specifies the spacing in dots between images to be combined, between 0 and 32. If not specified, 1 is used.
--name, -n
Specifies the name of the output file. If not specified, "texture" is used.
--output, -o
Specifies the output directory. If not specified, the output is directed to the current directory.
--size, -s
Specify the size of the output image. Specify the width and height separated by ‘x’. If not specified, 1024x1024 is used.
--verbose
Display more messages.
--version, -v
Display the version information and exit.
0
Succeeded.
otherwise
Failed.
If you want to further divide the input image into smaller areas, you can add these areas to the location data by preparing a CSV file with the same name.
If the input file is "image.png", the corresponding CSV file name is "image.csv". The two files must be located in the same directory.
CSV files must be in RFC 4180 compliant format. Use ',' as the delimiter and save the file with CR+LF as the linefeed code. Although not recommended, non-ASCII characters can also be handled by using UTF-8 encoding.
The fields in the CSV file are, in order, name, X-coordinate, Y-coordinate, width, and height. For example, to treat a 16x16 area at coordinates (8, 8) as a "sub_layer", the specification is as follows
Example: image.csv
sub_layer,8,8,16,16
In this case, the name "image/sub_layer" is added to the location data.
The binary data of the location data output by the converter is stored in the following order.
All values are little-endian in byte order.
The header is 24 bytes in length and contains the following parameters.
+0 hashSeed (4Byte)
The seed value for the hash calculation used by this file. The value specified by the --hash-seed option is stored in this value. See the HASH ALGORITHM section for the hash generation algorithm used by mgl-texatlas.
+4 identify (4Byte)
It is an identifier to confirm that the file is a location data file, and contains the hashed value of the string "TextureAtlasLocations" using hashSeed. This value is used to verify that the file is location data and also to verify that the hash generation algorithm calculates the intended value.
+8 revision (4Byte)
Revision information for this binary format. It is always 0 now and will be added in the future when the format is changed.
+12 flags (4Byte)
This area is reserved for future use and is not currently in use.
+16 imageCount (4Byte)
This value represents the number of textures handled by the location data.
+20 locationCount (4Byte)
The number of locations stored in this file.
The location data consists of 28 bytes per location and is stored as the number of locationCount in the header. The location data is stored in ascending order of the hash value. The contents of the location data are as follows.
+0 hash (4byte)
A hashed value of the location name. Binary files use hash values to identify locations, which are calculated based on the input file path. See the HASH ALGORITHM section for the hash generation algorithm.
+4 imageIndex (4byte)
The output image number. This value indicates the number of the target area in the sequentially numbered images.
+8 x (4byte)
X-coordinate value of the target area.
+12 y (4byte)
Y-coordinate value of the target area.
+16 width (4byte)
The width of the target area.
+20 height (4byte)
The height of the target area.
+24 flags (4byte)
This area is reserved for future use and is not currently in use.
The footer is 4 bytes and contains a hashed 32-bit value of the string "EndOfRecord". If this value cannot be read after all elements have been read, parsing of the location data may have failed.
mgl-texatlas uses the FNV1a algorithm for hash generation. The following is an example of a C++ implementation of this algorithm.
constexpr uint32_t kFNV1aPrime32 = 0x1000193;
constexpr uint32_t FNV1a(
const char *str,
const uint32_t seed) noexcept
{
if (str[0] == '\0')
{
return seed;
}
return FNV1a(
&str[1],
(seed ^ uint32_t(str[0])) * kFNV1aPrime32);
}