Skip to content

Commit df69dd0

Browse files
committed
encoder channel remap: improvements
- improved doxygen docu - store remapped channel count explicitly, so eg. "0000" will be allowed - as a consequence, refuse remapping if remapped channel count doesn't equal image pixel format channel count
1 parent 67b30a9 commit df69dd0

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

libgpujpeg/gpujpeg_encoder.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,15 @@ gpujpeg_encoder_suggest_restart_interval(const struct gpujpeg_image_parameters*
235235
#define GPUJPEG_ENC_OPT_FLIPPED_BOOL "enc_opt_flipped"
236236

237237
/**
238-
* remaps input channel order, format "XYZ" or "XYZW" where the letters stand for input channel
239-
* indices (0-indexed) mapped to output position; so eg. remapping from ARGB to RGBA will be
240-
* "1230". If number of channes doesn't equal the length of the string, the characters are invalid
241-
* or out-of bound, the behavior is undefined. set to "" (empty string) to revert this setting.
242-
* Placeholder'Z' or 'F' can be used instead of numbers to fill out channel with zeros or all-ones.
238+
* remap input channel order
239+
*
240+
* Format is "XYZ" or "XYZW" where the letters stand for input channel indices (0-indexed) mapped to output position;
241+
* so eg. for remapping from ARGB to RGBA the option value will be "1230".
242+
*
243+
* The number of image channes must equal the length of the string. Set to "" (empty string) to revert this setting.
244+
* Letters 'Z' or 'F' can be used instead of indices to fill given output channel with zeros or all-ones.
245+
*
246+
* Currently, the remapping is implemented for GPUJPEG_4444_U8_P0123 only.
243247
*/
244248
#define GPUJPEG_ENC_OPT_CHANNEL_REMAP "enc_opt_channel_remap"
245249

src/gpujpeg_encoder.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,8 @@ enc_opt_set_channel_remap(struct gpujpeg_encoder* encoder, const char* val)
664664
printf("\t\"1230\" or \"123F\" to map ARGB to RGBA\n");
665665
return GPUJPEG_ERROR;
666666
}
667-
if ( strlen(val) > GPUJPEG_MAX_COMPONENT_COUNT ) {
667+
const int mapped_count = strlen(val);
668+
if ( mapped_count > GPUJPEG_MAX_COMPONENT_COUNT ) {
668669
ERROR_MSG("Mapping for more than %d channels specified!\n", GPUJPEG_MAX_COMPONENT_COUNT);
669670
return GPUJPEG_ERROR;
670671
}
@@ -678,13 +679,15 @@ enc_opt_set_channel_remap(struct gpujpeg_encoder* encoder, const char* val)
678679
else if ( *val == 'Z' ) {
679680
src_chan = 5;
680681
}
681-
else if ( src_chan >= GPUJPEG_MAX_COMPONENT_COUNT ) {
682-
ERROR_MSG("Invalid channel %c for " GPUJPEG_ENC_OPT_CHANNEL_REMAP "!\n", *val);
682+
else if ( src_chan < 0 || src_chan >= mapped_count ) {
683+
ERROR_MSG("Invalid channel index %c for " GPUJPEG_ENC_OPT_CHANNEL_REMAP " (mapping %d channels)!\n", *val,
684+
mapped_count);
683685
return GPUJPEG_ERROR;
684686
}
685687
encoder->coder.preprocessor.channel_remap |= src_chan << 12;
686688
val++;
687689
}
690+
encoder->coder.preprocessor.channel_remap |= mapped_count << 24;
688691
return GPUJPEG_NOERR;
689692
}
690693

src/gpujpeg_preprocessor.cu

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,23 @@ static int
521521
channel_remap(struct gpujpeg_encoder* encoder)
522522
{
523523
struct gpujpeg_coder* coder = &encoder->coder;
524+
525+
const unsigned comp_count = gpujpeg_pixel_format_get_comp_count(coder->param_image.pixel_format);
526+
const unsigned mapped_count = coder->preprocessor.channel_remap >> 24;
527+
if (comp_count != mapped_count) {
528+
ERROR_MSG("Wrong channel remapping given, given %u channels but pixel format has %u!\n", mapped_count,
529+
comp_count);
530+
return -1;
531+
}
532+
const unsigned mapping = coder->preprocessor.channel_remap & 0xFFFF;
533+
524534
if ( coder->param_image.pixel_format == GPUJPEG_4444_U8_P0123 ) {
525535
dim3 block(16, 16);
526536
int width = coder->param_image.width;
527537
int height = coder->param_image.height;
528538
dim3 grid((width + block.x - 1) / block.x, (height + block.y - 1) / block.y);
529539
channel_remap_kernel<GPUJPEG_4444_U8_P0123><<<grid, block, 0, encoder->stream>>>(
530-
encoder->coder.d_data_raw, width, height, coder->preprocessor.channel_remap);
540+
encoder->coder.d_data_raw, width, height, mapping);
531541
}
532542
else {
533543
ERROR_MSG("Pixel format %s currently unsupported for channel remap!\n",

src/gpujpeg_preprocessor.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct gpujpeg_preprocessor
7171
void* kernel; // function poitner
7272
bool input_flipped; ///< [preprocess only] input buf is flipped
7373
unsigned int channel_remap; ///< remap input channels if != 0; currently preproecss only
74+
///< format: count_8b | 00000000 | idx0_4b | idx1_4b | idx2_4b | idx3_4b
7475
struct gpujpeg_preprocessor_data data;
7576
};
7677

0 commit comments

Comments
 (0)