From c5c7df6c91d3418a66838a6e6012b9ebc3bc77d9 Mon Sep 17 00:00:00 2001 From: cosmonaut Date: Tue, 19 Jul 2022 17:27:56 -0700 Subject: [PATCH] power of two --- src/cram.c | 72 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 44 insertions(+), 28 deletions(-) diff --git a/src/cram.c b/src/cram.c index aa4a9e0..07169a9 100644 --- a/src/cram.c +++ b/src/cram.c @@ -106,34 +106,19 @@ typedef struct Cram_Internal_Context Cram_AtlasData *atlasData; } Cram_Internal_Context; -/* API functions */ +/* Internal functions */ -uint32_t Wellspring_LinkedVersion(void) +/* https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 */ +static uint32_t Cram_Internal_NextPowerOfTwo(uint32_t v) { - return WELLSPRING_COMPILED_VERSION; -} - -Cram_Context* Cram_Init(Cram_ContextCreateInfo *createInfo) -{ - Cram_Internal_Context *context = Cram_malloc(sizeof(Cram_Internal_Context)); - - context->name = Cram_strdup(createInfo->name); - - context->width = createInfo->maxDimension; - context->height = createInfo->maxDimension; - - context->padding = createInfo->padding; - context->trim = createInfo->trim; - - context->images = Cram_malloc(INITIAL_DATA_CAPACITY * sizeof(Cram_Image)); - context->imageCapacity = INITIAL_DATA_CAPACITY; - context->imageCount = 0; - - context->atlasData = Cram_malloc(sizeof(Cram_AtlasData)); - - context->pixels = NULL; - - return (Cram_Context*) context; + v--; + v |= v >> 1; + v |= v >> 2; + v |= v >> 4; + v |= v >> 8; + v |= v >> 16; + v++; + return v; } static uint8_t Cram_Internal_IsImageEqual(Cram_Image *a, Cram_Image *b) @@ -220,6 +205,36 @@ static int8_t Cram_Internal_CopyPixels( return 0; } +/* API functions */ + +uint32_t Wellspring_LinkedVersion(void) +{ + return WELLSPRING_COMPILED_VERSION; +} + +Cram_Context* Cram_Init(Cram_ContextCreateInfo *createInfo) +{ + Cram_Internal_Context *context = Cram_malloc(sizeof(Cram_Internal_Context)); + + context->name = Cram_strdup(createInfo->name); + + context->width = createInfo->maxDimension; + context->height = createInfo->maxDimension; + + context->padding = createInfo->padding; + context->trim = createInfo->trim; + + context->images = Cram_malloc(INITIAL_DATA_CAPACITY * sizeof(Cram_Image)); + context->imageCapacity = INITIAL_DATA_CAPACITY; + context->imageCount = 0; + + context->atlasData = Cram_malloc(sizeof(Cram_AtlasData)); + + context->pixels = NULL; + + return (Cram_Context*) context; +} + void Cram_AddFile(Cram_Context *context, const char *path) { Cram_Internal_Context *internalContext = (Cram_Internal_Context*) context; @@ -378,6 +393,7 @@ int8_t Cram_Pack(Cram_Context *context) } } + /* TODO: replace this with something that uses MaxRects algorithm */ stbrp_pack_rects(&rectPackContext, rects, numRects); numRects = 0; @@ -404,8 +420,8 @@ int8_t Cram_Pack(Cram_Context *context) } } - internalContext->width = maxWidth; - internalContext->height = maxHeight; + internalContext->width = Cram_Internal_NextPowerOfTwo(maxWidth); + internalContext->height = Cram_Internal_NextPowerOfTwo(maxHeight); internalContext->pixels = Cram_realloc(internalContext->pixels, internalContext->width * internalContext->height * 4); Cram_memset(internalContext->pixels, 0, internalContext->width * internalContext->height * 4);