-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathdecode_minimal.c
More file actions
47 lines (45 loc) · 1.35 KB
/
decode_minimal.c
File metadata and controls
47 lines (45 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <assert.h>
#include <libgpujpeg/gpujpeg_decoder.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
static uint8_t*
load_file(const char* fname, size_t* jpeg_len)
{
FILE* in_file = fopen(fname, "rb");
assert(in_file != NULL);
fseek(in_file, 0, SEEK_END);
*jpeg_len = ftell(in_file);
uint8_t* image_data = malloc(*jpeg_len);
fseek(in_file, 0, SEEK_SET);
fread(image_data, *jpeg_len, 1, in_file);
fclose(in_file);
return image_data;
}
int
main(int argc, char* argv[])
{
if ( argc <= 1 ) {
printf("usage:\n%s <jpg>\n", argv[0]);
return 1;
}
int ret = EXIT_SUCCESS;
size_t jpeg_len = 0;
uint8_t* jpeg_data = load_file(argv[1], &jpeg_len);
struct gpujpeg_decoder* decoder = gpujpeg_decoder_create(0);
assert(decoder != NULL);
struct gpujpeg_decoder_output dec_output;
gpujpeg_decoder_output_set_default(&dec_output);
char fname[] = "out.XXX";
if ( gpujpeg_decoder_decode(decoder, jpeg_data, jpeg_len, &dec_output) != 0 ||
gpujpeg_image_save_to_file(fname, dec_output.data, dec_output.data_size, &dec_output.param_image) != 0 ) {
fprintf(stderr, "decode or write failed!\n");
ret = EXIT_FAILURE;
}
else {
printf("Output written to %s\n", fname);
}
free(jpeg_data);
gpujpeg_decoder_destroy(decoder);
return ret;
}