-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.h
122 lines (110 loc) · 2.44 KB
/
decode.h
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include <stdio.h>
#include <string.h>
#include "turbojpeg.h"
#include "bytepool.h"
#ifndef H_GO_LIBJPEG_TURBO_DECODE
#define H_GO_LIBJPEG_TURBO_DECODE
typedef struct jpeg_header_t {
int width;
int height;
int subsampling;
int colorspace;
} jpeg_header_t;
typedef struct jpeg_decode_result_t {
int width;
int height;
int subsampling;
int colorspace;
int pixel_format;
unsigned char *data;
int data_size;
} jpeg_decode_result_t;
void free_jpeg_header(jpeg_header_t *header) {
free(header);
}
void free_jpeg_decode_result(void *ctx, jpeg_decode_result_t *result) {
if(NULL != result) {
if(0 < result->data_size) {
turbojpeg_bytepool_put(ctx, result->data, result->data_size);
}
}
free(result);
}
jpeg_header_t *decode_jpeg_header(
tjhandle handle,
unsigned char *data,
unsigned long data_size
) {
jpeg_header_t *header = malloc(sizeof(jpeg_header_t));
if(NULL == header) {
return NULL;
}
memset(header, 0, sizeof(jpeg_header_t));
int ret = tjDecompressHeader3(
handle,
data,
data_size,
&header->width,
&header->height,
&header->subsampling,
&header->colorspace
);
if(0 != ret) {
free_jpeg_header(header);
return NULL;
}
return header;
}
jpeg_decode_result_t *decode_jpeg(
void *ctx,
tjhandle handle,
unsigned char *data,
unsigned long data_size,
int dst_pixel_format
) {
jpeg_decode_result_t *result = (jpeg_decode_result_t*) malloc(sizeof(jpeg_decode_result_t));
if(NULL == result) {
return NULL;
}
memset(result, 0, sizeof(jpeg_decode_result_t));
int ret = tjDecompressHeader3(
handle,
data,
data_size,
&result->width,
&result->height,
&result->subsampling,
&result->colorspace
);
if (0 != ret) {
free_jpeg_decode_result(ctx, result);
return NULL;
}
int pitch = tjPixelSize[dst_pixel_format] * result->width;
int dst_size = pitch * result->height;
result->data = (unsigned char*) turbojpeg_bytepool_get(ctx, dst_size);
if (NULL == result->data) {
free_jpeg_decode_result(ctx, result);
return NULL;
}
result->data_size = dst_size;
result->pixel_format = dst_pixel_format;
int flags = 0;
int ret_decompress = tjDecompress2(
handle,
data,
data_size,
result->data,
result->width,
pitch,
result->height,
dst_pixel_format,
flags
);
if (0 != ret_decompress) {
free_jpeg_decode_result(ctx, result);
return NULL;
}
return result;
}
#endif