1+ /**
2+ * @file decode_to_file.c
3+ *
4+ * This demonstrates decoding input file to output file.
5+ *
6+ * Changelog:
7+ * - 2020-08-11 - initial implelemtation
8+ * - 2026-02-18 - rename, support for abitrary output format
9+ */
110#include <libgpujpeg/gpujpeg_common.h>
211#include <libgpujpeg/gpujpeg_decoder.h>
312#include <stdbool.h>
1120
1221static void usage (const char * progname ) {
1322 printf ("Usage:\n" );
14- printf ("\t%s file.jpg\n" , progname );
23+ printf ("\t%s input.jpg [output.ext]\n" , progname );
24+ printf (
25+ "\noptional output file should can use one of recognized extensions (eg. png); defaults to <input>.rgb\n" );
1526}
1627
1728static bool check_params (int argc , char * argv []) {
18- if (argc != 2 || strrchr (argv [1 ], '.' ) == NULL ) {
29+ if ( ( argc < 2 || argc > 3 ) || strrchr (argv [1 ], '.' ) == NULL ) {
1930 return false;
2031 }
2132 const char * ext = strrchr (argv [1 ], '.' ) + 1 ;
@@ -29,7 +40,8 @@ struct decode_data {
2940 uint8_t * input_image ;
3041};
3142
32- static int decode (const char * input_filename , struct decode_data * d )
43+ static int
44+ decode (const char * input_filename , const char * output_filename , struct decode_data * d )
3345{
3446 // create decoder
3547 if ((d -> decoder = gpujpeg_decoder_create (0 )) == NULL ) {
@@ -52,15 +64,20 @@ static int decode(const char *input_filename, struct decode_data *d)
5264 return 1 ;
5365 }
5466
55- // create output file name
56- d -> out_filename = malloc (strlen (input_filename ) + 1 );
57- strcpy (d -> out_filename , input_filename );
58- strcpy (strrchr (d -> out_filename , '.' ) + 1 , "rgb" );
67+ if (output_filename != nullptr ) {
68+ d -> out_filename = strdup (output_filename );
69+ } else { // create output file name with .rgb extension
70+ d -> out_filename = malloc (strlen (input_filename ) + 1 );
71+ strcpy (d -> out_filename , input_filename );
72+ strcpy (strrchr (d -> out_filename , '.' ) + 1 , "rgb" );
73+ }
5974
6075 // write the decoded image
61- if (gpujpeg_image_save_to_file (d -> out_filename , decoder_output .data , decoder_output .data_size , NULL ) != 0 ) {
62- return 1 ;
76+ if ( gpujpeg_image_save_to_file (d -> out_filename , decoder_output .data , decoder_output .data_size ,
77+ & decoder_output .param_image ) != 0 ) {
78+ return 1 ;
6379 }
80+ printf ("File %s successfully written.\n" , d -> out_filename );
6481
6582 return 0 ;
6683}
@@ -72,7 +89,7 @@ int main(int argc, char *argv[]) {
7289 }
7390
7491 struct decode_data d = { 0 };
75- int rc = decode (argv [1 ], & d );
92+ int rc = decode (argv [1 ], argv [ 2 ], & d );
7693
7794 free (d .out_filename );
7895 gpujpeg_image_destroy (d .input_image );
0 commit comments