@@ -1114,14 +1114,19 @@ int GrkDecompress::preProcess(grk_plugin_decompress_callback_info* info)
11141114 goto cleanup;
11151115 }
11161116 }
1117- // 3a. initialize writer before decompress so it's ready for incremental output
1118- if (!writeInit (info))
1119- goto cleanup;
1120- // enable incremental band writing when postProcess is a no-op
1117+ // 3a. initialize writer before decompress so it's ready for incremental output.
1118+ // Only call writeInit early when postProcess is a no-op, so we can write header
1119+ // and enable incremental band writes. When post-processing is needed, defer
1120+ // writeInit until after decompress (postProcess) so that the library's
1121+ // io_buffer_callback (strip write) doesn't write raw/unprocessed strips
1122+ // during decompress.
11211123 incrementalWriteActive_ = false ;
11221124 if (storeToDisk && !parameters->single_tile_decompress && !info->init_decompressors_func &&
1123- grk_image_is_post_process_no_op (info->image ))
1125+ grk_image_is_post_process_no_op (info->image ) &&
1126+ parameters->dw_y1 == 0 )
11241127 {
1128+ if (!writeInit (info))
1129+ goto cleanup;
11251130 // write header early — metadata is already final since postProcess is a no-op
11261131 if (!writeHeader (info))
11271132 goto cleanup;
@@ -1267,37 +1272,38 @@ int GrkDecompress::postProcess(grk_plugin_decompress_callback_info* info)
12671272 }
12681273 if (storeToDisk)
12691274 {
1270- // GPU decode path: format was not initialized during preProcess
1271- // (writeInit was skipped because grk_decompress was not called).
1272- // Initialize the format now with the bridge image that has actual data.
1273- if (info->decompress_flags & GRK_DECODE_POST_T1 &&
1274- fmt->getWriteState () == IMAGE_FORMAT_UNWRITTEN)
1275+ // Update the format's image pointer in case it changed (e.g. single tile decompress
1276+ // replaces info->image after writeInit was called in preProcess)
1277+ fmt->setImage (image);
1278+ // Initialize the format if not already done. writeInit is deferred to postProcess
1279+ // when post-processing is needed, so the library's io_buffer_callback (strip write)
1280+ // doesn't write raw/unprocessed strips during decompress.
1281+ if (fmt->getWriteState () == IMAGE_FORMAT_UNWRITTEN)
12751282 {
1276- // Set decompress output fields on the bridge image
1277- if (image->numcomps > 0 )
1283+ if (info->decompress_flags & GRK_DECODE_POST_T1)
12781284 {
1279- if (image->decompress_prec == 0 )
1280- {
1281- image->decompress_prec = image->comps [0 ].prec ;
1282- image->decompress_width = image->comps [0 ].w ;
1283- image->decompress_height = image->comps [0 ].h ;
1284- image->decompress_num_comps = image->numcomps ;
1285- image->decompress_colour_space = image->color_space ;
1286- }
1287- if (image->packed_row_bytes == 0 )
1285+ // GPU decode path: set decompress output fields on the bridge image
1286+ if (image->numcomps > 0 )
12881287 {
1289- uint16_t ncmp = image->numcomps ;
1290- uint8_t prec = image->comps [0 ].prec ;
1291- image->packed_row_bytes = ((uint64_t )image->comps [0 ].w * ncmp * prec + 7U ) / 8U ;
1288+ if (image->decompress_prec == 0 )
1289+ {
1290+ image->decompress_prec = image->comps [0 ].prec ;
1291+ image->decompress_width = image->comps [0 ].w ;
1292+ image->decompress_height = image->comps [0 ].h ;
1293+ image->decompress_num_comps = image->numcomps ;
1294+ image->decompress_colour_space = image->color_space ;
1295+ }
1296+ if (image->packed_row_bytes == 0 )
1297+ {
1298+ uint16_t ncmp = image->numcomps ;
1299+ uint8_t prec = image->comps [0 ].prec ;
1300+ image->packed_row_bytes = ((uint64_t )image->comps [0 ].w * ncmp * prec + 7U ) / 8U ;
1301+ }
1302+ if (image->rows_per_strip == 0 )
1303+ image->rows_per_strip = std::min ((uint32_t )32 , image->comps [0 ].h );
12921304 }
1293- if (image->rows_per_strip == 0 )
1294- image->rows_per_strip = std::min ((uint32_t )32 , image->comps [0 ].h );
12951305 }
1296- if (!fmt->writeInit (
1297- image, outfile ? std::string (outfile) : " " ,
1298- info->cod_format == GRK_FMT_TIF ? info->decompressor_parameters ->compression : 0 ,
1299- info->decompressor_parameters ->num_threads ? info->decompressor_parameters ->num_threads
1300- : std::thread::hardware_concurrency ()))
1306+ if (!writeInit (info))
13011307 {
13021308 spdlog::error (" Outfile {} not generated" , outfile ? std::string (outfile) : " " );
13031309 goto cleanup;
0 commit comments