@@ -467,7 +467,9 @@ static const uint64_t MAGIC_NUMBER = 8248453963162350458; // "zstd-exr"
467467#define ZSTD_EXR_FLAG_DELTA_AFTER_SORT 1u
468468#define ZSTD_EXR_V1_HEADER 24u
469469
470- /** Deep pixel ZSTD wire: 1 = sort+shuffle (header v1); 2 = sort+delta+shuffle (v2). */
470+ /** ZSTD wire: 1 = sort+shuffle (header v1); 2 = sort+delta+shuffle (v2).
471+ * This is the default wire version (overridable at build time); flat chunks
472+ * are bumped to v2 at runtime, see exr_zstd_build_encode_pipeline_sorted. */
471473#ifndef EXR_ZSTD_SORTED_WIRE_VERSION
472474# define EXR_ZSTD_SORTED_WIRE_VERSION 1
473475#endif
@@ -494,17 +496,18 @@ typedef enum
494496} exr_zstd_channel_grid_scenario_t ;
495497
496498static void
497- exr_zstd_build_encode_pipeline_sorted (exr_zstd_pack_pipeline * out )
499+ exr_zstd_build_encode_pipeline_sorted (
500+ exr_zstd_channel_grid_scenario_t scenario , exr_zstd_pack_pipeline * out )
498501{
499- #if EXR_ZSTD_SORTED_WIRE_VERSION >= 2
500- out -> hdr_format = ZSTD_EXR_FORMAT_V2 ;
501- out -> hdr_flags = ZSTD_EXR_FLAG_DELTA_AFTER_SORT ;
502- out -> apply_delta = true;
503- #else
504- out -> hdr_format = ZSTD_EXR_FORMAT_V1 ;
505- out -> hdr_flags = 0 ;
506- out -> apply_delta = false ;
507- #endif
502+ /* Delta along the sample axis helps flat scanline/tiled data but tends to
503+ * hurt deep data (e.g. uncorrelated float Z, where it scrambles the
504+ * exponent byte plane), so flat chunks use v2 (delta); everything else
505+ * uses the default wire version. */
506+ out -> hdr_format = ( scenario == EXR_ZSTD_GRID_FLAT_SCAN_OR_TILE )
507+ ? ZSTD_EXR_FORMAT_V2
508+ : ( uint32_t ) EXR_ZSTD_SORTED_WIRE_VERSION ;
509+ out -> apply_delta = ( out -> hdr_format == ZSTD_EXR_FORMAT_V2 ) ;
510+ out -> hdr_flags = out -> apply_delta ? ZSTD_EXR_FLAG_DELTA_AFTER_SORT : 0 ;
508511}
509512
510513static void
@@ -1082,7 +1085,7 @@ internal_exr_apply_zstd (exr_encode_pipeline_t* encode)
10821085 int const pipeline_height = encode -> chunk .height ;
10831086
10841087 exr_zstd_pack_pipeline pipe ;
1085- exr_zstd_build_encode_pipeline_sorted (& pipe );
1088+ exr_zstd_build_encode_pipeline_sorted (grid_scenario , & pipe );
10861089 exr_result_t arv = internal_encode_alloc_buffer (
10871090 encode ,
10881091 EXR_TRANSCODE_BUFFER_SCRATCH1 ,
0 commit comments