diff --git a/README.md b/README.md index eadeb0d..1fd9c8a 100644 --- a/README.md +++ b/README.md @@ -11,13 +11,15 @@ Cram ships with a default command line interface implemented in C, but if you wi Command Line Usage ----- ```sh -Usage: cramcli input_dir output_dir atlas_name [--padding padding_value] [--notrim] [--dimension max_dimension] +Usage: cramcli input_dir output_dir atlas_name [--padding padding_value] [--premultiply] [--notrim] [--dimension max_dimension] ``` Cram CLI expects input images to be in PNG format and will output a PNG and a JSON metadata file that you can use to properly display the images in your game. Cram will recursively walk all the subdirectories of `input_dir` to generate your texture atlas. Padding is set to 0 by default. If you need to use linear filtering, set padding to at least 1. If you need to use texture compression, set padding to at least 4. +Premultiply is off by default. If you will be using linear filtering on these images, you should turn this on or you will get strange artifacts. + Trimming is on by default. Use `--notrim` if for some weird reason you want it off. Max dimension value is set to 8192 by default since that is a common max texture size for basically every GPU out there. Use `--dimension [max_dimension]` to override this maximum. diff --git a/tools/cli/main.c b/tools/cli/main.c index 54fdab0..f64845e 100644 --- a/tools/cli/main.c +++ b/tools/cli/main.c @@ -89,7 +89,7 @@ static void dirwalk(char *dir) void print_help() { - fprintf(stdout, "Usage: cram input_dir output_dir atlas_name [--padding padding_value] [--notrim] [--dimension max_dimension]"); + fprintf(stdout, "Usage: cram input_dir output_dir atlas_name [--padding padding_value] [--premultiply] [--notrim] [--dimension max_dimension]"); } uint8_t check_dir_exists(char *path) @@ -137,6 +137,8 @@ int main(int argc, char *argv[]) uint8_t *pixelData; int32_t width; int32_t height; + uint8_t premultiply; + uint8_t alpha; char *arg; char *inputDirPath = NULL; char *outputDirPath = NULL; @@ -156,6 +158,7 @@ int main(int argc, char *argv[]) createInfo.trim = 1; createInfo.maxDimension = 8192; createInfo.name = NULL; + premultiply = 0; if (argc < 2) { @@ -177,6 +180,10 @@ int main(int argc, char *argv[]) return 1; } } + else if (strcmp(arg, "--premultiply") == 0) + { + premultiply = 1; + } else if (strcmp(arg, "--notrim") == 0) { createInfo.trim = 0; @@ -245,6 +252,19 @@ int main(int argc, char *argv[]) /* output pixel data */ Cram_GetPixelData(context, &pixelData, &width, &height); + + if (premultiply) + { + for (i = 0; i < width * height * 4; i += 4) + { + alpha = pixelData[i + 3]; + + pixelData[i + 0] = (uint8_t) (((uint32_t) (pixelData[i + 0]) * alpha) / 255); + pixelData[i + 1] = (uint8_t) (((uint32_t) (pixelData[i + 1]) * alpha) / 255); + pixelData[i + 2] = (uint8_t) (((uint32_t) (pixelData[i + 2]) * alpha) / 255); + } + } + imageOutputFilename = malloc(strlen(outputDirPath) + strlen(createInfo.name) + 6); strcpy(imageOutputFilename, outputDirPath); strcat(imageOutputFilename, separatorString);