Skip to content

Commit 7a41d59

Browse files
committed
rename decode_to_raw_rgb->encode_file + updates
Allow now decoding to arbitrary of supported formats (.png, .pnm etc.) accordinh to the provided extension (gpujpeg_image_save_to_file supports that if metadata struct provided).
1 parent d393faf commit 7a41d59

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

examples/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ target_link_libraries(decode_minimal gpujpeg)
1818
add_executable(decode_to_pnm decode_to_pnm.c)
1919
target_link_libraries(decode_to_pnm gpujpeg)
2020

21-
add_executable(decode_to_raw_rgb decode_to_raw_rgb.c)
22-
target_link_libraries(decode_to_raw_rgb gpujpeg)
21+
add_executable(decode_to_file decode_to_file.c)
22+
target_link_libraries(decode_to_file gpujpeg)
2323

2424
include(CheckLanguage)
2525
check_language(CUDA)
Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
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>
@@ -11,11 +20,13 @@
1120

1221
static 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

1728
static 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);

libgpujpeg/gpujpeg_common.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @file
3-
* Copyright (c) 2011-2025, CESNET
3+
* Copyright (c) 2011-2026, CESNET
44
* Copyright (c) 2011, Silicon Genome, LLC.
55
*
66
* All rights reserved.
@@ -430,7 +430,8 @@ gpujpeg_image_load_from_file(const char* filename, uint8_t** image, size_t* imag
430430
* @param filaname Image filename
431431
* @param image Image data buffer
432432
* @param image_size Image data buffer size
433-
* @param param_image Image properties (may be NULL)
433+
* @param param_image Image properties (may be NULL); must be set if outputting image
434+
* in a container (PNG, PNM et all.), otherwise just data outputted
434435
* @return 0 if succeeds, otherwise nonzero
435436
*/
436437
GPUJPEG_API int

0 commit comments

Comments
 (0)