#include "cram.h" #include "dirent.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "stb_image_write.h" #define MAX_DIR_LENGTH 2048 static Cram_Context *context; #ifdef _WIN32 #define SEPARATOR "\\" #endif #ifdef __unix__ #define SEPARATOR "/" #endif static const char* GetFilenameExtension(const char *filename) { const char *dot = strrchr(filename, '.'); if (!dot || dot == filename) { return ""; } return dot + 1; } /* Mostly taken from K&R C 2nd edition page 182 */ static void dirwalk(char *dir) { dirent *dp; DIR *dfd; char subname[2048]; if ((dfd = opendir(dir)) == NULL) { fprintf(stderr, "Can't open %s\n", dir); return; } while ((dp = readdir(dfd)) != NULL) { if ( strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0 ) { continue; } sprintf(subname, "%s%s%s", dir, SEPARATOR, dp->d_name); if (dp->d_type == DT_DIR) { dirwalk(subname); } else { if (strcmp(GetFilenameExtension(subname), "png") == 0) { Cram_AddFile(context, subname); } else { fprintf(stdout, "skipping %s\n", subname); } } } closedir(dfd); } int main(int argc, char *argv[]) { Cram_ContextCreateInfo createInfo; uint8_t *pixelData; uint32_t width; uint32_t height; if (argc < 2) { fprintf(stderr, "Must provide directory!\n"); return 1; } createInfo.padding = 0; createInfo.trim = 1; createInfo.maxDimension = 8192; createInfo.name = "test"; context = Cram_Init(&createInfo); dirwalk(argv[1]); Cram_Pack(context); Cram_GetPixelData(context, &pixelData, &width, &height); stbi_write_png( "output.png", width, height, 4, pixelData, width * 4 ); Cram_Destroy(context); return 0; }