* Additional changes for PR #477 * clang-format all files * Make it clear in avifenc syntax that -p signals prem in the resultant AVIF * Fix tref prem parsing to match tref auxl parsing (same syntax/box) * parenthesize binops in assignments (style) * Continue new convertResult pattern in avifImageYUVToRGB instead of early-out * Replace pColorIrefType/pColorIrefToID with new avifEncoderDataFindItemByID() call, as avifEncoderItem* mem storage may shift during new calls to avifEncoderDataCreateItem() so deep pointers into them during this is bad, plus this happens to remove accidental Hungarian notation, which is good
diff --git a/src/alpha.c b/src/alpha.c index fa1d551..e3301ae 100644 --- a/src/alpha.c +++ b/src/alpha.c
@@ -3,8 +3,8 @@ #include "avif/internal.h" -#include <string.h> #include <assert.h> +#include <string.h> static int calcMaxChannel(uint32_t depth, avifRange range) { @@ -382,7 +382,8 @@ return AVIF_TRUE; } -avifResult avifRGBImagePremultiplyAlpha(avifRGBImage * rgb) { +avifResult avifRGBImagePremultiplyAlpha(avifRGBImage * rgb) +{ // no data if (!rgb->pixels || !rgb->rowBytes) { return AVIF_RESULT_REFORMAT_FAILED;
diff --git a/src/read.c b/src/read.c index 941b20d..db18c9a 100644 --- a/src/read.c +++ b/src/read.c
@@ -2033,12 +2033,13 @@ if (!memcmp(header.type, "auxl", 4)) { uint32_t toID; - CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[] + CHECK(avifROStreamReadU32(&s, &toID)); // unsigned int(32) track_IDs[]; CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one track->auxForID = toID; } else if (!memcmp(header.type, "prem", 4)) { uint32_t byID; - CHECK(avifROStreamReadU32(&s, &byID)); // unsigned int(32) to_item_ID + CHECK(avifROStreamReadU32(&s, &byID)); // unsigned int(32) track_IDs[]; + CHECK(avifROStreamSkip(&s, header.size - sizeof(uint32_t))); // just take the first one track->premByID = byID; } else { CHECK(avifROStreamSkip(&s, header.size)); @@ -2613,7 +2614,7 @@ decoder->image->width = colorTrack->width; decoder->image->height = colorTrack->height; decoder->alphaPresent = (alphaTrack != NULL); - decoder->image->alphaPremultiplied = decoder->alphaPresent && colorTrack->premByID == alphaTrack->id; + decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorTrack->premByID == alphaTrack->id); } else { // Create from items @@ -2770,7 +2771,7 @@ decoder->image->height = 0; } decoder->alphaPresent = (alphaItem != NULL); - decoder->image->alphaPremultiplied = decoder->alphaPresent && colorItem->premByID == alphaItem->id; + decoder->image->alphaPremultiplied = decoder->alphaPresent && (colorItem->premByID == alphaItem->id); } // Sanity check tiles
diff --git a/src/reformat.c b/src/reformat.c index 047b8c1..54a3058 100644 --- a/src/reformat.c +++ b/src/reformat.c
@@ -1138,9 +1138,9 @@ if (avifRGBFormatHasAlpha(rgb->format) && !rgb->ignoreAlpha) { if (image->alphaPremultiplied && !rgb->alphaPremultiplied) { - return avifRGBImageUnpremultiplyAlpha(rgb); + convertResult = avifRGBImageUnpremultiplyAlpha(rgb); } else if (!image->alphaPremultiplied && rgb->alphaPremultiplied) { - return avifRGBImagePremultiplyAlpha(rgb); + convertResult = avifRGBImagePremultiplyAlpha(rgb); } }
diff --git a/src/write.c b/src/write.c index e8ae8ac..b65f993 100644 --- a/src/write.c +++ b/src/write.c
@@ -3,6 +3,7 @@ #include "avif/internal.h" +#include <assert.h> #include <string.h> #include <time.h> @@ -145,6 +146,17 @@ return item; } +static avifEncoderItem * avifEncoderDataFindItemByID(avifEncoderData * data, uint16_t id) +{ + for (uint32_t itemIndex = 0; itemIndex < data->items.count; ++itemIndex) { + avifEncoderItem * item = &data->items.item[itemIndex]; + if (item->id == id) { + return item; + } + } + return NULL; +} + static void avifEncoderDataDestroy(avifEncoderData * data) { for (uint32_t i = 0; i < data->items.count; ++i) { @@ -461,9 +473,6 @@ // Prepare all AV1 items - const char ** pColorIrefType = NULL; - uint16_t * pColorIrefToID = NULL; - uint16_t gridColorID = 0; if (cellCount > 1) { avifEncoderItem * gridColorItem = avifEncoderDataCreateItem(encoder->data, "grid", "Color", 6, 0); @@ -473,8 +482,6 @@ gridColorID = gridColorItem->id; encoder->data->primaryItemID = gridColorID; - pColorIrefType = &gridColorItem->irefType; - pColorIrefToID = &gridColorItem->irefToID; } for (uint32_t cellIndex = 0; cellIndex < cellCount; ++cellIndex) { @@ -490,8 +497,6 @@ item->dimgFromID = gridColorID; } else { encoder->data->primaryItemID = item->id; - pColorIrefType = &item->irefType; - pColorIrefToID = &item->irefToID; } } @@ -529,8 +534,10 @@ gridAlphaID = gridAlphaItem->id; if (encoder->data->imageMetadata->alphaPremultiplied) { - *pColorIrefType = "prem"; - *pColorIrefToID = gridAlphaID; + avifEncoderItem * primaryItem = avifEncoderDataFindItemByID(encoder->data, encoder->data->primaryItemID); + assert(primaryItem); + primaryItem->irefType = "prem"; + primaryItem->irefToID = gridAlphaID; } } @@ -550,8 +557,10 @@ item->irefType = "auxl"; if (encoder->data->imageMetadata->alphaPremultiplied) { - *pColorIrefType = "prem"; - *pColorIrefToID = item->id; + avifEncoderItem * primaryItem = avifEncoderDataFindItemByID(encoder->data, encoder->data->primaryItemID); + assert(primaryItem); + primaryItem->irefType = "prem"; + primaryItem->irefToID = item->id; } } }