-
Notifications
You must be signed in to change notification settings - Fork 7
/
jpg.c
218 lines (175 loc) · 5.42 KB
/
jpg.c
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "jpeglib.h"
#include "jerror.h"
#include "jpg.h"
typedef struct
{
struct jpeg_destination_mgr pub; /* public fields */
unsigned char ** outbuffer; /* target buffer */
unsigned long * outsize;
unsigned char * newbuffer; /* newly allocated buffer */
JOCTET * buffer; /* start of buffer */
size_t bufsize;
} mem_dest_mgr;
typedef mem_dest_mgr * mem_dest_mgr_ptr;
static void init_mem_destination (j_compress_ptr cinfo)
{
}
static boolean empty_mem_output_buffer(j_compress_ptr cinfo)
{
size_t nextsize;
JOCTET * nextbuffer;
mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest;
/* Try to allocate new buffer with double size */
nextsize = dest->bufsize * 2;
nextbuffer = malloc(nextsize);
if (nextbuffer == NULL)
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
memcpy(nextbuffer, dest->buffer, dest->bufsize);
if (dest->newbuffer != NULL)
free(dest->newbuffer);
dest->newbuffer = nextbuffer;
dest->pub.next_output_byte = nextbuffer + dest->bufsize;
dest->pub.free_in_buffer = dest->bufsize;
dest->buffer = nextbuffer;
dest->bufsize = nextsize;
return TRUE;
}
static void term_mem_destination(j_compress_ptr cinfo)
{
mem_dest_mgr_ptr dest = (mem_dest_mgr_ptr) cinfo->dest;
*dest->outbuffer = dest->buffer;
*dest->outsize = dest->bufsize - dest->pub.free_in_buffer;
}
static void my_jpeg_mem_dest(j_compress_ptr cinfo, unsigned char ** outbuffer, unsigned long * outsize)
{
mem_dest_mgr_ptr dest;
if (outbuffer == NULL || outsize == NULL) /* sanity check */
ERREXIT(cinfo, JERR_BUFFER_SIZE);
/* The destination object is made permanent so that multiple JPEG images
* can be written to the same buffer without re-executing jpeg_mem_dest.
*/
if (cinfo->dest == NULL) /* first time for this JPEG object? */
{
cinfo->dest = (struct jpeg_destination_mgr *)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
sizeof(mem_dest_mgr));
}
dest = (mem_dest_mgr_ptr) cinfo->dest;
dest->pub.init_destination = init_mem_destination;
dest->pub.empty_output_buffer = empty_mem_output_buffer;
dest->pub.term_destination = term_mem_destination;
dest->outbuffer = outbuffer;
dest->outsize = outsize;
dest->newbuffer = NULL;
if (*outbuffer == NULL || *outsize == 0)
{
/* Allocate initial buffer */
dest->newbuffer = *outbuffer = malloc(65536);
if (dest->newbuffer == NULL)
ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 10);
*outsize = 65536;
}
dest->pub.next_output_byte = dest->buffer = *outbuffer;
dest->pub.free_in_buffer = dest->bufsize = *outsize;
}
#ifdef HAVE_LIBJPEG_TURBO
jpg_buf_t build_jpg_from_fb(unsigned char *fb_mem, int fb_width, int fb_height, int bits_per_pixel, int mon_width, int mon_height)
{
int scanline;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpg_buf_t jpg_buf;
jpg_buf.size = 1 << 20;
jpg_buf.ptr = NULL;
my_jpeg_mem_dest(&cinfo, &jpg_buf.ptr, &jpg_buf.size);
cinfo.image_width = mon_width;
cinfo.image_height = mon_height;
cinfo.input_components = 4;
cinfo.in_color_space = JCS_EXT_BGRX;
jpeg_set_defaults(&cinfo);
cinfo.dct_method = JDCT_FASTEST;
jpeg_set_quality(&cinfo, 70, TRUE);
jpeg_start_compress(&cinfo, TRUE);
unsigned char buf[mon_width * 4];
JSAMPROW row_ptr[1] = {buf};
memset(buf, 0, mon_width*4);
if (fb_width >= mon_width)
while (cinfo.next_scanline < cinfo.image_height)
{
row_ptr[0] = &fb_mem[cinfo.next_scanline * fb_width * 4];
jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
else
{
for (scanline = 0; scanline < MIN(fb_height, mon_height); scanline++)
{
memcpy(buf, &fb_mem[scanline * fb_width * 4], fb_width * 4);
jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
}
memset(buf, 0, mon_width*4);
for (scanline = fb_height; scanline < mon_height; scanline++)
{
jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return jpg_buf;
}
#else
static void convert_rgba_to_rgb(unsigned char *buf, unsigned char *fb_mem, int width)
{
int i;
for (i = 0; i < width; i++)
{
// BGRX
unsigned int x = *(unsigned int *)fb_mem;
*buf++ = (x >> 16) & 0xff;
*buf++ = (x >> 8) & 0xff;
*buf++ = (x) & 0xff;
fb_mem += 4;
}
}
jpg_buf_t build_jpg_from_fb(unsigned char *fb_mem, int fb_width, int fb_height, int bits_per_pixel, int mon_width, int mon_height)
{
int scanline;
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpg_buf_t jpg_buf;
jpg_buf.size = 1 << 20;
jpg_buf.ptr = NULL;
my_jpeg_mem_dest(&cinfo, &jpg_buf.ptr, &jpg_buf.size);
cinfo.image_width = mon_width;
cinfo.image_height = mon_height;
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
jpeg_set_defaults(&cinfo);
cinfo.dct_method = JDCT_FASTEST;
jpeg_set_quality(&cinfo, 70, TRUE);
jpeg_start_compress(&cinfo, TRUE);
JSAMPLE buf[mon_width * 3];
JSAMPROW row_ptr[1] = {buf};
memset(buf, 0, mon_width*3);
for (scanline = 0; scanline < MIN(fb_height, mon_height); scanline++)
{
convert_rgba_to_rgb(buf, &fb_mem[scanline * fb_width * 4], mon_width);
jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
memset(buf, 0, mon_width*3);
for (scanline = fb_height; scanline < mon_height; scanline++)
{
jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return jpg_buf;
}
#endif