Skip to content

Commit dc2f3e7

Browse files
committed
decoder: add dec_opt_flipped
symmeetric to enc_opt_flipped
1 parent 34c78cc commit dc2f3e7

File tree

8 files changed

+43
-18
lines changed

8 files changed

+43
-18
lines changed

libgpujpeg/gpujpeg_decoder.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ gpujpeg_decoder_get_image_info(uint8_t *image, size_t image_size, struct gpujpeg
271271
/// global so it affects all decoder instances
272272
#define GPUJPEG_DEC_OPT_TGA_RLE_BOOL "dec_opt_tga_rle" ///< GPUJPEG_VAL_TRUE or GPUJPEG_VAL_FALSE
273273

274+
/// output image should be vertically flipped (bottom-up): values @ref GPUJPEG_VAL_TRUE or @ref GPUJPEG_VAL_FALSE
275+
#define GPUJPEG_DEC_OPT_FLIPPED_BOOL "dec_opt_flipped"
276+
274277
/// @copydoc GPUJPEG_ENC_OPT_CHANNEL_REMAP
275278
#define GPUJPEG_DEC_OPT_CHANNEL_REMAP "dec_opt_channel_remap"
276279

src/gpujpeg_common.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2318,4 +2318,19 @@ gpujpeg_init_term_colors()
23182318
init = true;
23192319
}
23202320

2321+
int
2322+
gpujpeg_parse_bool_opt(bool* out_var, const char* val, const char* opt)
2323+
{
2324+
if ( strcasecmp(val, GPUJPEG_VAL_TRUE) == 0 ) {
2325+
*out_var = true;
2326+
return GPUJPEG_NOERR;
2327+
}
2328+
else if ( strcasecmp(val, GPUJPEG_VAL_FALSE) == 0 ) {
2329+
*out_var = false;
2330+
return GPUJPEG_NOERR;
2331+
}
2332+
ERROR_MSG("Unknown option %s for %s\n", val, opt);
2333+
return GPUJPEG_ERROR;
2334+
}
2335+
23212336
/* vi: set expandtab sw=4 : */

src/gpujpeg_common_internal.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,9 @@ gpujpeg_init_term_colors();
542542
int
543543
gpujpeg_opt_set_channel_remap(struct gpujpeg_coder* coder, const char* val, const char* optname);
544544

545+
int
546+
gpujpeg_parse_bool_opt(bool* out_var, const char* val, const char* opt);
547+
545548
#ifdef __cplusplus
546549
} // extern "C"
547550
#endif // __cplusplus

src/gpujpeg_decoder.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,9 @@ gpujpeg_decoder_set_option(struct gpujpeg_decoder* decoder, const char *opt, con
493493
image_delegate_stbi_tga_set_rle(strcmp(val, GPUJPEG_VAL_TRUE) == 0);
494494
return GPUJPEG_NOERR;
495495
}
496+
if ( strcmp(opt, GPUJPEG_DEC_OPT_FLIPPED_BOOL) == 0 ) {
497+
return gpujpeg_parse_bool_opt(&decoder->coder.preprocessor.flipped, val, GPUJPEG_DEC_OPT_FLIPPED_BOOL);
498+
}
496499
if ( strcmp(opt, GPUJPEG_DEC_OPT_CHANNEL_REMAP) == 0 ) {
497500
return gpujpeg_opt_set_channel_remap(&decoder->coder, val, GPUJPEG_DEC_OPT_CHANNEL_REMAP);
498501
}
@@ -505,6 +508,8 @@ gpujpeg_decoder_print_options()
505508
{
506509
printf("\t" GPUJPEG_DEC_OPT_TGA_RLE_BOOL "=[" GPUJPEG_VAL_FALSE "|" GPUJPEG_VAL_TRUE
507510
"] - set decoder option (not) to output RLE TGA\n");
511+
printf("\t" GPUJPEG_DEC_OPT_FLIPPED_BOOL "=[" GPUJPEG_VAL_FALSE "|" GPUJPEG_VAL_TRUE
512+
"] - vertically flip the output image\n");
508513
printf("\t" GPUJPEG_DEC_OPT_CHANNEL_REMAP "=XYZ[W] - input channel mapping, eg. '210F' for GBRX,\n"
509514
"\t\t'210' for GBR; special placeholders 'F' and 'Z' to set a channel to all-ones or all-zeros\n");
510515
}

src/gpujpeg_encoder.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -729,17 +729,7 @@ gpujpeg_encoder_set_option(struct gpujpeg_encoder* encoder, const char *opt, con
729729
return GPUJPEG_NOERR;
730730
}
731731
if ( strcmp(opt, GPUJPEG_ENC_OPT_FLIPPED_BOOL) == 0 ) {
732-
if ( strcasecmp(val, GPUJPEG_VAL_TRUE) == 0 ) {
733-
encoder->coder.preprocessor.input_flipped = true;
734-
}
735-
else if ( strcasecmp(val, GPUJPEG_VAL_FALSE) == 0 ) {
736-
encoder->coder.preprocessor.input_flipped = false;
737-
}
738-
else {
739-
ERROR_MSG("Unknown option %s for " GPUJPEG_ENC_OPT_FLIPPED_BOOL "\n", val);
740-
return GPUJPEG_ERROR;
741-
}
742-
return GPUJPEG_NOERR;
732+
return gpujpeg_parse_bool_opt(&encoder->coder.preprocessor.flipped, val, GPUJPEG_ENC_OPT_FLIPPED_BOOL);
743733
}
744734
if ( strcmp(opt, GPUJPEG_ENC_OPT_CHANNEL_REMAP) == 0 ) {
745735
return gpujpeg_opt_set_channel_remap(&encoder->coder, val, GPUJPEG_ENC_OPT_CHANNEL_REMAP);

src/gpujpeg_postprocessor.cu

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,13 @@ gpujpeg_preprocessor_decoder_copy_planar_data(struct gpujpeg_coder * coder, cuda
436436
int
437437
gpujpeg_postprocessor_decode(struct gpujpeg_coder* coder, cudaStream_t stream)
438438
{
439+
if ( coder->preprocessor.flipped ) {
440+
int ret = gpujpeg_preprocessor_flip_lines(coder, stream);
441+
if ( ret != 0 ) {
442+
return ret;
443+
}
444+
}
445+
439446
if ( coder->preprocessor.kernel == nullptr ) {
440447
return gpujpeg_preprocessor_decoder_copy_planar_data(coder, stream);
441448
}

src/gpujpeg_preprocessor.cu

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,16 +470,15 @@ vertical_flip_kernel(uint32_t* data,
470470
}
471471
}
472472

473-
static int
474-
flip_lines(struct gpujpeg_encoder* encoder)
473+
int
474+
gpujpeg_preprocessor_flip_lines(struct gpujpeg_coder* coder, cudaStream_t stream)
475475
{
476-
struct gpujpeg_coder* coder = &encoder->coder;
477476
for ( int i = 0; i < coder->param.comp_count; ++i ) {
478477
dim3 block(RGB_8BIT_THREADS, 1);
479478
int width = coder->component[i].data_width / 4;
480479
int height = coder->component[i].data_height;
481480
dim3 grid((width + block.x - 1) / block.x, height / 2); // only half of height
482-
vertical_flip_kernel<<<grid, block, 0, encoder->stream>>>((uint32_t*)coder->component[i].d_data, width, height);
481+
vertical_flip_kernel<<<grid, block, 0, stream>>>((uint32_t*)coder->component[i].d_data, width, height);
483482
}
484483
gpujpeg_cuda_check_error("Preprocessor flip failed", return -1);
485484
return 0;
@@ -579,8 +578,8 @@ gpujpeg_preprocessor_encode(struct gpujpeg_encoder * encoder)
579578
if (ret != 0) {
580579
return ret;
581580
}
582-
if (coder->preprocessor.input_flipped) {
583-
return flip_lines(encoder);
581+
if ( coder->preprocessor.flipped ) {
582+
return gpujpeg_preprocessor_flip_lines(coder, encoder->stream);
584583
}
585584
return ret;
586585
}

src/gpujpeg_preprocessor.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ struct gpujpeg_preprocessor_data
6969
struct gpujpeg_preprocessor
7070
{
7171
void* kernel; // function poitner
72-
bool input_flipped; ///< [preprocess only] input buf is flipped
72+
bool flipped; ///< flip image before encode or after decode
7373
unsigned int channel_remap; ///< remap channels if != 0
7474
///< format: count_8b | 00000000 | idx0_4b | idx1_4b | idx2_4b | idx3_4b
7575
struct gpujpeg_preprocessor_data data;
@@ -100,6 +100,9 @@ gpujpeg_preprocessor_encode(struct gpujpeg_encoder * encoder);
100100
// shared with postprocess
101101
int
102102
gpujpeg_preprocessor_channel_remap(struct gpujpeg_coder* coder, cudaStream_t stream);
103+
104+
int
105+
gpujpeg_preprocessor_flip_lines(struct gpujpeg_coder* coder, cudaStream_t stream);
103106

104107
#ifdef __cplusplus
105108
}

0 commit comments

Comments
 (0)