-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.h
72 lines (65 loc) · 1.38 KB
/
encode.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
#include <stdio.h>
#include <string.h>
#include "turbojpeg.h"
#include "bytepool.h"
#ifndef H_GO_LIBJPEG_TURBO_ENCODE
#define H_GO_LIBJPEG_TURBO_ENCODE
typedef struct jpeg_encode_result_t {
unsigned char *data;
int data_size;
} jpeg_encode_result_t;
void free_jpeg_encode_result(void *ctx, jpeg_encode_result_t *result) {
if (NULL != result) {
if(0 < result->data_size) {
turbojpeg_bytepool_put(ctx, result->data, result->data_size);
}
}
free(result);
}
jpeg_encode_result_t *encode_jpeg(
void *ctx,
tjhandle handle,
unsigned char *data,
int width,
int height,
int stride,
int src_pixel_format,
int subsampling,
int quality
) {
unsigned char *out = NULL;
unsigned long out_size = 0;
int flags = 0;
int ret = tjCompress2(
handle,
data,
width,
stride,
height,
src_pixel_format,
&out,
&out_size,
subsampling,
quality,
flags
);
if (ret != 0) {
return NULL;
}
jpeg_encode_result_t *result = (jpeg_encode_result_t*) malloc(sizeof(jpeg_encode_result_t));
if(NULL == result) {
tjFree(out);
return NULL;
}
result->data = (unsigned char*) turbojpeg_bytepool_get(ctx, out_size);
if(NULL == result->data) {
free_jpeg_encode_result(ctx, result);
tjFree(out);
return NULL;
}
memcpy(result->data, out, out_size);
tjFree(out);
result->data_size = out_size;
return result;
}
#endif