Skip to content

Commit

Permalink
Apply comments
Browse files Browse the repository at this point in the history
  • Loading branch information
y-guyon committed Apr 10, 2024
1 parent 66d7b4e commit 23e1316
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
1 change: 1 addition & 0 deletions include/avif/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ typedef struct avifSampleTransformToken
{
uint8_t type; // avifSampleTransformTokenType
int32_t constant; // If value is AVIF_SAMPLE_TRANSFORM_CONSTANT.
// Only 32-bit (bit_depth=2) constants are supported.
uint8_t inputImageItemIndex; // If value is AVIF_SAMPLE_TRANSFORM_INPUT_IMAGE_ITEM_INDEX. 1-based.
} avifSampleTransformToken;

Expand Down
16 changes: 10 additions & 6 deletions src/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,7 @@ static avifBool avifParseToneMappedImageBox(avifGainMapMetadata * metadata, cons
#endif // AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP

#if defined(AVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM)
// bit-depth is assumed to be 2 (32-bit).
static avifResult avifParseSampleTransformTokens(avifROStream * s, avifSampleTransformExpression * expression)
{
uint8_t tokenCount;
Expand All @@ -2036,7 +2037,7 @@ static avifResult avifParseSampleTransformTokens(avifROStream * s, avifSampleTra

AVIF_CHECK(avifROStreamRead(s, &token->type, /*size=*/1)); // unsigned int(8) token;
if (token->type == AVIF_SAMPLE_TRANSFORM_CONSTANT) {
// TODO(yguyon): Verify two's complement representation is guaranteed here.
// Two's complement representation is assumed here.
uint32_t constant;
AVIF_CHECK(avifROStreamReadU32(s, &constant)); // signed int(1<<(bit_depth+3)) constant;
token->constant = *(int32_t *)&constant; // maybe =(int32_t)constant; is enough
Expand Down Expand Up @@ -2166,11 +2167,11 @@ static avifResult avifDecoderItemReadAndParse(const avifDecoder * decoder,
*codecType = avifGetCodecType(item->type);
AVIF_ASSERT_OR_RETURN(*codecType != AVIF_CODEC_TYPE_UNKNOWN);
}
// Note: If AVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM is defined, backward-incompatible files
// with a primary 'sato' Sample Transform derived image item could be handled here
// (compared to backward-compatible files with a 'sato' item in the same 'altr' group
// as the primary regular color item which are handled in
// avifDecoderDataFindSampleTransformImageItem() below).
// TODO(yguyon): If AVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM is defined, backward-incompatible
// files with a primary 'sato' Sample Transform derived image item could be
// handled here (compared to backward-compatible files with a 'sato' item in the
// same 'altr' group as the primary regular color item which are handled in
// avifDecoderDataFindSampleTransformImageItem() below).
return AVIF_RESULT_OK;
}

Expand Down Expand Up @@ -5155,6 +5156,8 @@ avifResult avifDecoderReset(avifDecoder * decoder)
// Input image item order is important because input image items are indexed according to this order.
AVIF_CHECKERR(inputImageItem->dimgIdx == data->sampleTransformNumInputImageItems, AVIF_RESULT_NOT_IMPLEMENTED);

AVIF_CHECKERR(data->sampleTransformNumInputImageItems < AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS,
AVIF_RESULT_NOT_IMPLEMENTED);
avifItemCategory * category = &data->sampleTransformInputImageItems[data->sampleTransformNumInputImageItems];
avifBool foundItem = AVIF_FALSE;
uint32_t alphaCategory = AVIF_ITEM_CATEGORY_COUNT;
Expand Down Expand Up @@ -5646,6 +5649,7 @@ avifResult avifDecoderApplySampleTransform(const avifDecoder * decoder, avifImag
}

for (avifBool alpha = AVIF_FALSE; alpha <= decoder->alphaPresent ? AVIF_TRUE : AVIF_FALSE; ++alpha) {
AVIF_ASSERT_OR_RETURN(decoder->data->sampleTransformNumInputImageItems <= AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS);
const avifImage * inputImages[AVIF_SAMPLE_TRANSFORM_MAX_NUM_INPUT_IMAGE_ITEMS];
for (uint32_t i = 0; i < decoder->data->sampleTransformNumInputImageItems; ++i) {
avifItemCategory category = decoder->data->sampleTransformInputImageItems[i];
Expand Down
33 changes: 19 additions & 14 deletions src/write.c
Original file line number Diff line number Diff line change
Expand Up @@ -1161,6 +1161,24 @@ static const char infeNameGainMap[] = "GMap";
static const char infeNameSampleTransform[] = "SampleTransform";
#endif

static const char * getInfeName(avifItemCategory itemCategory)
{
if (avifIsAlpha(itemCategory)) {
return infeNameAlpha;
}
#if defined(AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP)
if (itemCategory == AVIF_ITEM_GAIN_MAP) {
return infeNameGainMap;
}
#endif
#if defined(AVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM)
if (itemCategory >= AVIF_SAMPLE_TRANSFORM_MIN_CATEGORY && itemCategory <= AVIF_SAMPLE_TRANSFORM_MAX_CATEGORY) {
return infeNameSampleTransform;
}
#endif
return infeNameColor;
}

// Adds the items for a single cell or a grid of cells. Outputs the topLevelItemID which is
// the only item if there is exactly one cell, or the grid item for multiple cells.
// Note: The topLevelItemID output argument has the type uint16_t* instead of avifEncoderItem** because
Expand All @@ -1174,20 +1192,7 @@ static avifResult avifEncoderAddImageItems(avifEncoder * encoder,
uint16_t * topLevelItemID)
{
const uint32_t cellCount = gridCols * gridRows;
const char * infeName;
if (avifIsAlpha(itemCategory)) {
infeName = infeNameAlpha;
#if defined(AVIF_ENABLE_EXPERIMENTAL_GAIN_MAP)
} else if (itemCategory == AVIF_ITEM_GAIN_MAP) {
infeName = infeNameGainMap;
#endif
#if defined(AVIF_ENABLE_EXPERIMENTAL_SAMPLE_TRANSFORM)
} else if (itemCategory >= AVIF_SAMPLE_TRANSFORM_MIN_CATEGORY && itemCategory <= AVIF_SAMPLE_TRANSFORM_MAX_CATEGORY) {
infeName = infeNameSampleTransform;
#endif
} else {
infeName = infeNameColor;
}
const char * infeName = getInfeName(itemCategory);
const size_t infeNameSize = strlen(infeName) + 1;

if (cellCount > 1) {
Expand Down

0 comments on commit 23e1316

Please sign in to comment.