Skip to content

Commit 3d6cd5a

Browse files
committed
added examples/decode_minimal.c
As a complement to encode_minimal.c, this example presents usage of the decoder with less code than already is in other examples.
1 parent 085eb0b commit 3d6cd5a

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed

examples/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ target_link_libraries(encode_minimal gpujpeg)
1212
add_executable(encode_raw encode_raw.c)
1313
target_link_libraries(encode_raw gpujpeg)
1414

15+
add_executable(decode_minimal decode_minimal.c)
16+
target_link_libraries(decode_minimal gpujpeg)
17+
1518
add_executable(decode_to_pnm decode_to_pnm.c)
1619
target_link_libraries(decode_to_pnm gpujpeg)
1720

examples/decode_minimal.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <assert.h>
2+
#include <libgpujpeg/gpujpeg_decoder.h>
3+
#include <stdint.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
7+
static uint8_t*
8+
load_file(const char* fname, size_t* jpeg_len)
9+
{
10+
FILE* in_file = fopen(fname, "rb");
11+
assert(in_file != NULL);
12+
fseek(in_file, 0, SEEK_END);
13+
*jpeg_len = ftell(in_file);
14+
uint8_t* image_data = malloc(*jpeg_len);
15+
fseek(in_file, 0, SEEK_SET);
16+
fread(image_data, *jpeg_len, 1, in_file);
17+
fclose(in_file);
18+
return image_data;
19+
}
20+
21+
int
22+
main(int argc, char* argv[])
23+
{
24+
if ( argc <= 1 ) {
25+
printf("usage:\n%s <jpg>\n", argv[0]);
26+
return 1;
27+
}
28+
int ret = EXIT_SUCCESS;
29+
size_t jpeg_len = 0;
30+
uint8_t* jpeg_data = load_file(argv[1], &jpeg_len);
31+
struct gpujpeg_decoder* decoder = gpujpeg_decoder_create(0);
32+
assert(decoder != NULL);
33+
struct gpujpeg_decoder_output dec_output;
34+
gpujpeg_decoder_output_set_default(&dec_output);
35+
char fname[] = "out.XXX";
36+
if ( gpujpeg_decoder_decode(decoder, jpeg_data, jpeg_len, &dec_output) != 0 ||
37+
gpujpeg_image_save_to_file(fname, dec_output.data, dec_output.data_size, &dec_output.param_image) != 0 ) {
38+
fprintf(stderr, "decode or write failed!\n");
39+
ret = EXIT_FAILURE;
40+
}
41+
else {
42+
printf("Output written to %s\n", fname);
43+
}
44+
free(jpeg_data);
45+
gpujpeg_decoder_destroy(decoder);
46+
return ret;
47+
}

0 commit comments

Comments
 (0)