Skip to content

Commit

Permalink
avifenc: Set nclx/range values in avifImage earlier so proper YUV coe…
Browse files Browse the repository at this point in the history
…fficients are used when converting JPEG/PNG
  • Loading branch information
Joe Drago committed Apr 16, 2020
1 parent c74f70c commit 70f9af8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Changed
- avifenc: Set nclx/range values in avifImage earlier so proper YUV coefficients are used when converting JPEG/PNG

## [0.7.0] - 2020-04-16
### Added
Expand Down
23 changes: 16 additions & 7 deletions apps/avifenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ int main(int argc, char * argv[])
avifImage * avif = avifImageCreateEmpty();
avifRWData raw = AVIF_DATA_EMPTY;

// Set range and nclx in advance so any upcoming RGB -> YUV use the proper coefficients
if (requestedRangeSet) {
avif->yuvRange = requestedRange;
}
if (nclxSet) {
nclx.range = avif->yuvRange;
avifImageSetProfileNCLX(avif, &nclx);
}

const char * fileExt = strrchr(inputFilename, '.');
if (!fileExt) {
fprintf(stderr, "Cannot determine input file extension: %s\n", inputFilename);
Expand All @@ -332,13 +341,18 @@ int main(int argc, char * argv[])
returnCode = 1;
goto cleanup;
}
if (nclxSet && (nclx.range != avif->yuvRange)) {
// Update the NCLX profile based on the new range from the y4m file
nclx.range = avif->yuvRange;
avifImageSetProfileNCLX(avif, &nclx);
}
} else if (!strcmp(fileExt, ".jpg") || !strcmp(fileExt, ".jpeg")) {
if (!avifJPEGRead(avif, inputFilename, requestedFormat, requestedDepth, requestedRange)) {
if (!avifJPEGRead(avif, inputFilename, requestedFormat, requestedDepth)) {
returnCode = 1;
goto cleanup;
}
} else if (!strcmp(fileExt, ".png")) {
if (!avifPNGRead(avif, inputFilename, requestedFormat, requestedDepth, requestedRange)) {
if (!avifPNGRead(avif, inputFilename, requestedFormat, requestedDepth)) {
returnCode = 1;
goto cleanup;
}
Expand All @@ -348,11 +362,6 @@ int main(int argc, char * argv[])
}
printf("Successfully loaded: %s\n", inputFilename);

if (nclxSet) {
nclx.range = avif->yuvRange;
avifImageSetProfileNCLX(avif, &nclx);
}

if (paspCount == 2) {
avif->transformFlags |= AVIF_TRANSFORM_PASP;
avif->pasp.hSpacing = paspValues[0];
Expand Down
3 changes: 1 addition & 2 deletions apps/shared/avifjpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void setup_read_icc_profile(j_decompress_ptr cinfo);
static boolean read_icc_profile(j_decompress_ptr cinfo, JOCTET ** icc_data_ptr, unsigned int * icc_data_len);
static void write_icc_profile(j_compress_ptr cinfo, const JOCTET * icc_data_ptr, unsigned int icc_data_len);

avifBool avifJPEGRead(avifImage * avif, const char * inputFilename, avifPixelFormat requestedFormat, int requestedDepth, avifRange requestedRange)
avifBool avifJPEGRead(avifImage * avif, const char * inputFilename, avifPixelFormat requestedFormat, int requestedDepth)
{
avifBool ret = AVIF_FALSE;
FILE * f = NULL;
Expand Down Expand Up @@ -69,7 +69,6 @@ avifBool avifJPEGRead(avifImage * avif, const char * inputFilename, avifPixelFor
avif->height = cinfo.output_height;
avif->yuvFormat = requestedFormat;
avif->depth = requestedDepth ? requestedDepth : 8;
avif->yuvRange = requestedRange;
avifRGBImageSetDefaults(&rgb, avif);
rgb.format = AVIF_RGB_FORMAT_RGB;
rgb.depth = 8;
Expand Down
2 changes: 1 addition & 1 deletion apps/shared/avifjpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "avif/avif.h"

avifBool avifJPEGRead(avifImage * avif, const char * inputFilename, avifPixelFormat requestedFormat, int requestedDepth, avifRange requestedRange);
avifBool avifJPEGRead(avifImage * avif, const char * inputFilename, avifPixelFormat requestedFormat, int requestedDepth);
avifBool avifJPEGWrite(avifImage * avif, const char * outputFilename, int jpegQuality);

#endif // ifndef LIBAVIF_APPS_SHARED_AVIFJPEG_H
3 changes: 1 addition & 2 deletions apps/shared/avifpng.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <stdlib.h>
#include <string.h>

avifBool avifPNGRead(avifImage * avif, const char * inputFilename, avifPixelFormat requestedFormat, int requestedDepth, avifRange requestedRange)
avifBool avifPNGRead(avifImage * avif, const char * inputFilename, avifPixelFormat requestedFormat, int requestedDepth)
{
avifBool readResult = AVIF_FALSE;
png_structp png = NULL;
Expand Down Expand Up @@ -101,7 +101,6 @@ avifBool avifPNGRead(avifImage * avif, const char * inputFilename, avifPixelForm
avif->height = rawHeight;
avif->yuvFormat = requestedFormat;
avif->depth = requestedDepth;
avif->yuvRange = requestedRange;
if (avif->depth == 0) {
if (imgBitDepth == 8) {
avif->depth = 8;
Expand Down
2 changes: 1 addition & 1 deletion apps/shared/avifpng.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "avif/avif.h"

// if (requestedDepth == 0), do best-fit
avifBool avifPNGRead(avifImage * avif, const char * inputFilename, avifPixelFormat requestedFormat, int requestedDepth, avifRange requestedRange);
avifBool avifPNGRead(avifImage * avif, const char * inputFilename, avifPixelFormat requestedFormat, int requestedDepth);
avifBool avifPNGWrite(avifImage * avif, const char * outputFilename, int requestedDepth);

#endif // ifndef LIBAVIF_APPS_SHARED_AVIFPNG_H

0 comments on commit 70f9af8

Please sign in to comment.