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