fix some buggy output

pull/1/head
cosmonaut 2022-07-22 16:59:12 -07:00
parent 6faceb9d16
commit 6a6629f4c7
4 changed files with 45 additions and 48 deletions

View File

@ -105,8 +105,8 @@ typedef struct Cram_ImageData
int32_t width;
int32_t height;
int32_t offsetX;
int32_t offsetY;
int32_t trimOffsetX;
int32_t trimOffsetY;
int32_t untrimmedWidth;
int32_t untrimmedHeight;
} Cram_ImageData;

View File

@ -59,7 +59,8 @@ struct Cram_Image
{
char *name;
Rect originalRect;
Rect rect;
Rect trimmedRect;
Rect packedRect;
Cram_Image *duplicateOf;
uint8_t *pixels; /* Will be NULL if duplicateOf is not NULL! */
size_t hash;
@ -124,9 +125,9 @@ static uint32_t Cram_Internal_NextPowerOfTwo(uint32_t v)
static uint8_t Cram_Internal_IsImageEqual(Cram_Image *a, Cram_Image *b)
{
int32_t i;
if (a->hash == b->hash && a->rect.w == b->rect.w && a->rect.h == b->rect.h)
if (a->hash == b->hash && a->trimmedRect.w == b->trimmedRect.w && a->trimmedRect.h == b->trimmedRect.h)
{
for (i = 0; i < a->rect.w * a->rect.h * 4; i += 1)
for (i = 0; i < a->trimmedRect.w * a->trimmedRect.h * 4; i += 1)
{
if (a->pixels[i] != b->pixels[i])
{
@ -532,7 +533,6 @@ void Cram_AddFile(Cram_Context *context, const char *path)
}
image = Cram_malloc(sizeof(Cram_Image));
internalContext->images[internalContext->imageCount] = image;
image->name = Cram_Internal_GetImageName(path);
@ -592,29 +592,29 @@ void Cram_AddFile(Cram_Context *context, const char *path)
}
}
image->rect.x = leftTrim;
image->rect.y = topTrim;
image->rect.w = rightTrim - leftTrim;
image->rect.h = bottomTrim - topTrim;
image->trimmedRect.x = leftTrim;
image->trimmedRect.y = topTrim;
image->trimmedRect.w = rightTrim - leftTrim;
image->trimmedRect.h = bottomTrim - topTrim;
}
else
{
image->rect = image->originalRect;
image->trimmedRect = image->originalRect;
}
/* copy and free source pixels */
image->pixels = Cram_malloc(image->rect.w * image->rect.h * 4);
image->pixels = Cram_malloc(image->trimmedRect.w * image->trimmedRect.h * 4);
Rect dstRect;
dstRect.x = 0;
dstRect.y = 0;
dstRect.w = image->rect.w;
dstRect.h = image->rect.h;
Cram_Internal_CopyPixels((uint32_t*) image->pixels, image->rect.w, (uint32_t*) pixels, width, &dstRect, &image->rect);
dstRect.w = image->trimmedRect.w;
dstRect.h = image->trimmedRect.h;
Cram_Internal_CopyPixels((uint32_t*) image->pixels, image->trimmedRect.w, (uint32_t*) pixels, width, &dstRect, &image->trimmedRect);
stbi_image_free(pixels);
/* hash */
image->hash = stbds_hash_bytes(image->pixels, image->rect.w * image->rect.h * 4, 0);
image->hash = stbds_hash_bytes(image->pixels, image->trimmedRect.w * image->trimmedRect.h * 4, 0);
/* check if this is a duplicate */
image->duplicateOf = NULL;
@ -625,7 +625,7 @@ void Cram_AddFile(Cram_Context *context, const char *path)
if (Cram_Internal_IsImageEqual(image, internalContext->images[i]))
{
/* this is duplicate data! */
image->duplicateOf = image;
image->duplicateOf = internalContext->images[i];
Cram_free(image->pixels);
image->pixels = NULL;
break;
@ -633,6 +633,7 @@ void Cram_AddFile(Cram_Context *context, const char *path)
}
}
internalContext->images[internalContext->imageCount] = image;
internalContext->imageCount += 1;
}
@ -672,8 +673,8 @@ int8_t Cram_Pack(Cram_Context *context)
{
packerRect = &packerRects[numRects];
packerRect->w = internalContext->images[i]->rect.w + internalContext->padding;
packerRect->h = internalContext->images[i]->rect.h + internalContext->padding;
packerRect->w = internalContext->images[i]->trimmedRect.w + internalContext->padding;
packerRect->h = internalContext->images[i]->trimmedRect.h + internalContext->padding;
numRects += 1;
}
@ -691,8 +692,10 @@ int8_t Cram_Pack(Cram_Context *context)
{
packerRect = &packerRects[numRects];
internalContext->images[i]->rect.x = packerRect->x;
internalContext->images[i]->rect.y = packerRect->y;
internalContext->images[i]->packedRect.x = packerRect->x;
internalContext->images[i]->packedRect.y = packerRect->y;
internalContext->images[i]->packedRect.w = internalContext->images[i]->trimmedRect.w;
internalContext->images[i]->packedRect.h = internalContext->images[i]->trimmedRect.h;
maxWidth = Cram_max(maxWidth, packerRect->x + packerRect->w);
maxHeight = Cram_max(maxHeight, packerRect->y + packerRect->h);
@ -711,21 +714,21 @@ int8_t Cram_Pack(Cram_Context *context)
{
if (!internalContext->images[i]->duplicateOf)
{
dstRect.x = internalContext->images[i]->rect.x;
dstRect.y = internalContext->images[i]->rect.y;
dstRect.w = internalContext->images[i]->rect.w;
dstRect.h = internalContext->images[i]->rect.h;
dstRect.x = internalContext->images[i]->packedRect.x;
dstRect.y = internalContext->images[i]->packedRect.y;
dstRect.w = internalContext->images[i]->trimmedRect.w;
dstRect.h = internalContext->images[i]->trimmedRect.h;
srcRect.x = 0;
srcRect.y = 0;
srcRect.w = internalContext->images[i]->rect.w;
srcRect.h = internalContext->images[i]->rect.h;
srcRect.w = internalContext->images[i]->trimmedRect.w;
srcRect.h = internalContext->images[i]->trimmedRect.h;
Cram_Internal_CopyPixels(
(uint32_t*) internalContext->pixels,
internalContext->width,
(uint32_t*) internalContext->images[i]->pixels,
internalContext->images[i]->rect.w,
internalContext->images[i]->trimmedRect.w,
&dstRect,
&srcRect
);
@ -740,17 +743,17 @@ int8_t Cram_Pack(Cram_Context *context)
image = internalContext->images[i];
}
internalContext->imageDatas[i].x = image->rect.x;
internalContext->imageDatas[i].y = image->rect.y;
internalContext->imageDatas[i].width = image->rect.w;
internalContext->imageDatas[i].height = image->rect.h;
internalContext->imageDatas[i].x = image->packedRect.x;
internalContext->imageDatas[i].y = image->packedRect.y;
internalContext->imageDatas[i].width = image->trimmedRect.w;
internalContext->imageDatas[i].height = image->trimmedRect.h;
internalContext->imageDatas[i].offsetX = image->rect.x - image->originalRect.x;
internalContext->imageDatas[i].offsetY = image->rect.y - image->originalRect.y;
internalContext->imageDatas[i].trimOffsetX = image->trimmedRect.x - image->originalRect.x;
internalContext->imageDatas[i].trimOffsetY = image->trimmedRect.y - image->originalRect.y;
internalContext->imageDatas[i].untrimmedWidth = image->originalRect.w;
internalContext->imageDatas[i].untrimmedHeight = image->originalRect.h;
internalContext->imageDatas[i].name = strdup(image->name);
internalContext->imageDatas[i].name = strdup(internalContext->images[i]->name);
}
Cram_free(packerRects);

View File

@ -64,25 +64,19 @@ void JsonBuilder_Internal_RemoveTrailingComma(JsonBuilder *builder)
void JsonBuilder_AppendProperty(JsonBuilder *builder, char *propertyName, char *propertyString, uint8_t isString)
{
size_t lineLength;
lineLength = strlen(propertyName) + strlen(propertyString) + 6;
if (isString)
{
lineLength += 2;
}
JsonBuilder_Internal_MaybeExpand(builder, lineLength);
JsonBuilder_Internal_Indent(builder);
JsonBuilder_Internal_MaybeExpand(builder, strlen(propertyName) + 4);
builder->index += sprintf(&builder->string[builder->index], "\"%s\": ", propertyName);
if (isString)
{
JsonBuilder_Internal_MaybeExpand(builder, strlen(propertyString) + 4);
builder->index += sprintf(&builder->string[builder->index], "\"%s\",\n", propertyString);
}
else
{
JsonBuilder_Internal_MaybeExpand(builder, strlen(propertyString) + 2);
builder->index += sprintf(&builder->string[builder->index], "%s,\n", propertyString);
}
}
@ -102,7 +96,7 @@ void JsonBuilder_AppendIntProperty(JsonBuilder *builder, char *propertyName, int
void JsonBuilder_StartObject(JsonBuilder *builder)
{
JsonBuilder_Internal_Indent(builder);
JsonBuilder_Internal_MaybeExpand(builder, 1);
JsonBuilder_Internal_MaybeExpand(builder, 2);
builder->index += sprintf(&builder->string[builder->index], "{\n");
builder->indentLevel += 1;
}
@ -142,7 +136,7 @@ void JsonBuilder_Finish(JsonBuilder *builder)
builder->indentLevel = 0;
JsonBuilder_Internal_RemoveTrailingComma(builder);
JsonBuilder_Internal_MaybeExpand(builder, 3);
JsonBuilder_Internal_MaybeExpand(builder, 2);
builder->index += sprintf(&builder->string[builder->index], "}\n");
}

View File

@ -221,8 +221,8 @@ int main(int argc, char *argv[])
JsonBuilder_AppendIntProperty(jsonBuilder, "Y", imageDatas[i].y);
JsonBuilder_AppendIntProperty(jsonBuilder, "W", imageDatas[i].width);
JsonBuilder_AppendIntProperty(jsonBuilder, "H", imageDatas[i].height);
JsonBuilder_AppendIntProperty(jsonBuilder, "OffsetX", imageDatas[i].offsetX);
JsonBuilder_AppendIntProperty(jsonBuilder, "OffsetY", imageDatas[i].offsetY);
JsonBuilder_AppendIntProperty(jsonBuilder, "TrimOffsetX", imageDatas[i].trimOffsetX);
JsonBuilder_AppendIntProperty(jsonBuilder, "TrimOffsetY", imageDatas[i].trimOffsetY);
JsonBuilder_AppendIntProperty(jsonBuilder, "UntrimmedWidth", imageDatas[i].untrimmedWidth);
JsonBuilder_AppendIntProperty(jsonBuilder, "UntrimmedHeight", imageDatas[i].untrimmedHeight);
JsonBuilder_EndObject(jsonBuilder);