avifenc: Set nclx/range values in avifImage earlier so proper YUV coefficients are used when converting JPEG/PNG
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 915efc4..46cf419 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,6 +5,8 @@
 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
diff --git a/apps/avifenc.c b/apps/avifenc.c
index fec860a..7e2cd4c 100644
--- a/apps/avifenc.c
+++ b/apps/avifenc.c
@@ -319,6 +319,15 @@
     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);
@@ -332,13 +341,18 @@
             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;
         }
@@ -348,11 +362,6 @@
     }
     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];
diff --git a/apps/shared/avifjpeg.c b/apps/shared/avifjpeg.c
index ee857ce..676d4f9 100644
--- a/apps/shared/avifjpeg.c
+++ b/apps/shared/avifjpeg.c
@@ -27,7 +27,7 @@
 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;
@@ -69,7 +69,6 @@
     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;
diff --git a/apps/shared/avifjpeg.h b/apps/shared/avifjpeg.h
index 9b81f58..13c54dd 100644
--- a/apps/shared/avifjpeg.h
+++ b/apps/shared/avifjpeg.h
@@ -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
diff --git a/apps/shared/avifpng.c b/apps/shared/avifpng.c
index e1a38b8..933d0e4 100644
--- a/apps/shared/avifpng.c
+++ b/apps/shared/avifpng.c
@@ -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;
@@ -101,7 +101,6 @@
     avif->height = rawHeight;
     avif->yuvFormat = requestedFormat;
     avif->depth = requestedDepth;
-    avif->yuvRange = requestedRange;
     if (avif->depth == 0) {
         if (imgBitDepth == 8) {
             avif->depth = 8;
diff --git a/apps/shared/avifpng.h b/apps/shared/avifpng.h
index 6858a7b..a10220e 100644
--- a/apps/shared/avifpng.h
+++ b/apps/shared/avifpng.h
@@ -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