ruby-epeg 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.travis.yml +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/Rakefile +11 -0
- data/epeg.gemspec +26 -0
- data/ext/epeg/EPEG_LICENSE +7 -0
- data/ext/epeg/Epeg.h +76 -0
- data/ext/epeg/epeg_image.c +414 -0
- data/ext/epeg/epeg_image.h +24 -0
- data/ext/epeg/epeg_main.c +1418 -0
- data/ext/epeg/epeg_private.h +86 -0
- data/ext/epeg/extconf.rb +12 -0
- data/lib/epeg/epeg.rb +7 -0
- data/lib/epeg/version.rb +3 -0
- data/spec/epeg_image_spec.rb +126 -0
- data/spec/fixtures/test.jpg +0 -0
- data/spec/spec_helper.rb +6 -0
- metadata +113 -0
@@ -0,0 +1,24 @@
|
|
1
|
+
#include <math.h>
|
2
|
+
#include <Epeg.h>
|
3
|
+
#include <ruby.h>
|
4
|
+
|
5
|
+
static VALUE rb_epeg_image_open(VALUE, VALUE);
|
6
|
+
static VALUE rb_epeg_image_from_blob(VALUE, VALUE);
|
7
|
+
static VALUE rb_epeg_image_get_default_quality(VALUE);
|
8
|
+
static VALUE rb_epeg_image_set_default_quality(VALUE, VALUE);
|
9
|
+
|
10
|
+
static VALUE rb_epeg_image_initialize(VALUE);
|
11
|
+
|
12
|
+
static void rb_epeg_image_encode_or_trim(VALUE, Epeg_Image *);
|
13
|
+
static VALUE rb_epeg_image_resize(VALUE, VALUE, VALUE);
|
14
|
+
static VALUE rb_epeg_image_resize_to_fit(VALUE, VALUE, VALUE);
|
15
|
+
static VALUE rb_epeg_image_resize_to_fill(VALUE, VALUE, VALUE);
|
16
|
+
static VALUE rb_epeg_image_crop(int, VALUE *, VALUE);
|
17
|
+
static VALUE rb_epeg_image_set_quality(VALUE, VALUE);
|
18
|
+
|
19
|
+
static VALUE rb_epeg_image_write(VALUE, VALUE);
|
20
|
+
static VALUE rb_epeg_image_to_blob(VALUE);
|
21
|
+
static VALUE rb_epeg_image_close(VALUE);
|
22
|
+
static VALUE rb_epeg_image_is_closed(VALUE);
|
23
|
+
|
24
|
+
static void rb_epeg_image_destroy(void *);
|
@@ -0,0 +1,1418 @@
|
|
1
|
+
#include "Epeg.h"
|
2
|
+
#include "epeg_private.h"
|
3
|
+
#include <jerror.h>
|
4
|
+
|
5
|
+
static Epeg_Image *_epeg_open_header (Epeg_Image *im);
|
6
|
+
static int _epeg_decode (Epeg_Image *im);
|
7
|
+
static int _epeg_scale (Epeg_Image *im);
|
8
|
+
static int _epeg_decode_for_trim (Epeg_Image *im);
|
9
|
+
static int _epeg_trim (Epeg_Image *im);
|
10
|
+
static int _epeg_encode (Epeg_Image *im);
|
11
|
+
|
12
|
+
static void _epeg_fatal_error_handler (j_common_ptr cinfo);
|
13
|
+
|
14
|
+
static const JOCTET fake_EOI[2] = { 0xFF, JPEG_EOI };
|
15
|
+
|
16
|
+
static ExifByteOrder exif_byte_order = EXIF_BYTE_ORDER_INTEL;
|
17
|
+
|
18
|
+
/**
|
19
|
+
* Open a JPEG image by filename.
|
20
|
+
* @param file The file path to open.
|
21
|
+
* @return A handle to the opened JPEG file, with the header decoded.
|
22
|
+
*
|
23
|
+
* This function opens the file indicated by the @p file parameter, and
|
24
|
+
* attempts to decode it as a jpeg file. If this failes, NULL is returned.
|
25
|
+
* Otherwise a valid handle to an open JPEG file is returned that can be used
|
26
|
+
* by other Epeg calls.
|
27
|
+
*
|
28
|
+
* The @p file must be a pointer to a valid C string, NUL (0 byte) terminated
|
29
|
+
* thats is a relative or absolute file path. If not results are not
|
30
|
+
* determined.
|
31
|
+
*
|
32
|
+
* See also: epeg_memory_open(), epeg_close()
|
33
|
+
*/
|
34
|
+
EAPI Epeg_Image *
|
35
|
+
epeg_file_open(const char *file)
|
36
|
+
{
|
37
|
+
Epeg_Image *im;
|
38
|
+
|
39
|
+
im = calloc(1, sizeof(Epeg_Image));
|
40
|
+
if (!im) return NULL;
|
41
|
+
|
42
|
+
im->in.file = strdup(file);
|
43
|
+
if (!im->in.file)
|
44
|
+
{
|
45
|
+
free(im);
|
46
|
+
return NULL;
|
47
|
+
}
|
48
|
+
|
49
|
+
im->in.f = fopen(im->in.file, "rb");
|
50
|
+
if (!im->in.f)
|
51
|
+
{
|
52
|
+
epeg_close(im);
|
53
|
+
return NULL;
|
54
|
+
}
|
55
|
+
fstat(fileno(im->in.f), &(im->stat_info));
|
56
|
+
im->out.quality = 75;
|
57
|
+
return _epeg_open_header(im);
|
58
|
+
}
|
59
|
+
|
60
|
+
/**
|
61
|
+
* Open a JPEG image stored in memory.
|
62
|
+
* @param data A pointer to the memory containing the JPEG data.
|
63
|
+
* @param size The size of the memory segment containing the JPEG.
|
64
|
+
* @return A handle to the opened JPEG, with the header decoded.
|
65
|
+
*
|
66
|
+
* This function opens a JPEG file that is stored in memory pointed to by
|
67
|
+
* @p data, and that is @p size bytes in size. If successful a valid handle
|
68
|
+
* is returned, or on failure NULL is returned.
|
69
|
+
*
|
70
|
+
* See also: epeg_file_open(), epeg_close()
|
71
|
+
*/
|
72
|
+
EAPI EAPI Epeg_Image *
|
73
|
+
epeg_memory_open(unsigned char *data, int size)
|
74
|
+
{
|
75
|
+
Epeg_Image *im;
|
76
|
+
|
77
|
+
im = calloc(1, sizeof(Epeg_Image));
|
78
|
+
if (!im) return NULL;
|
79
|
+
|
80
|
+
im->out.quality = 75;
|
81
|
+
im->in.mem.data = (unsigned char **)data;
|
82
|
+
im->in.mem.size = size;
|
83
|
+
im->in.f = NULL;
|
84
|
+
im->in.w = 0;
|
85
|
+
im->in.h = 0;
|
86
|
+
return _epeg_open_header(im);
|
87
|
+
}
|
88
|
+
|
89
|
+
/**
|
90
|
+
* Return the original JPEG pixel size.
|
91
|
+
* @param im A handle to an opened Epeg image.
|
92
|
+
* @param w A pointer to the width value in pixels to be filled in.
|
93
|
+
* @param h A pointer to the height value in pixels to be filled in.
|
94
|
+
*
|
95
|
+
* Returns the image size in pixels.
|
96
|
+
*
|
97
|
+
*/
|
98
|
+
EAPI void
|
99
|
+
epeg_size_get(Epeg_Image *im, int *w, int *h)
|
100
|
+
{
|
101
|
+
if (w) *w = im->in.w;
|
102
|
+
if (h) *h = im->in.h;
|
103
|
+
}
|
104
|
+
|
105
|
+
/**
|
106
|
+
* Return the original JPEG pixel color space.
|
107
|
+
* @param im A handle to an opened Epeg image.
|
108
|
+
* @param space A pointer to the color space value to be filled in.
|
109
|
+
*
|
110
|
+
* Returns the image color space.
|
111
|
+
*
|
112
|
+
*/
|
113
|
+
EAPI void
|
114
|
+
epeg_colorspace_get(Epeg_Image *im, int *space)
|
115
|
+
{
|
116
|
+
if (space) *space = im->color_space;
|
117
|
+
}
|
118
|
+
|
119
|
+
/**
|
120
|
+
* Set the size of the image to decode in pixels.
|
121
|
+
* @param im A handle to an opened Epeg image.
|
122
|
+
* @param w The width of the image to decode at, in pixels.
|
123
|
+
* @param h The height of the image to decode at, in pixels.
|
124
|
+
*
|
125
|
+
* Sets the size at which to deocode the JPEG image, giving an optimised load
|
126
|
+
* that only decodes the pixels needed.
|
127
|
+
*
|
128
|
+
*/
|
129
|
+
EAPI void
|
130
|
+
epeg_decode_size_set(Epeg_Image *im, int w, int h)
|
131
|
+
{
|
132
|
+
if (im->pixels) return;
|
133
|
+
if (w < 1) w = 1;
|
134
|
+
else if (w > im->in.w) w = im->in.w;
|
135
|
+
if (h < 1) h = 1;
|
136
|
+
else if (h > im->in.h) h = im->in.h;
|
137
|
+
im->out.w = w;
|
138
|
+
im->out.h = h;
|
139
|
+
im->out.x = 0;
|
140
|
+
im->out.y = 0;
|
141
|
+
}
|
142
|
+
|
143
|
+
EAPI void
|
144
|
+
epeg_decode_bounds_set(Epeg_Image *im, int x, int y, int w, int h)
|
145
|
+
{
|
146
|
+
if (im->pixels) return;
|
147
|
+
if (w < 1) w = 1;
|
148
|
+
else if (w > im->in.w) w = im->in.w;
|
149
|
+
if (h < 1) h = 1;
|
150
|
+
else if (h > im->in.h) h = im->in.h;
|
151
|
+
im->out.w = w;
|
152
|
+
im->out.h = h;
|
153
|
+
if (x < 0) x = 0;
|
154
|
+
if (y < 0) y = 0;
|
155
|
+
im->out.x = x;
|
156
|
+
im->out.y = y;
|
157
|
+
}
|
158
|
+
|
159
|
+
/**
|
160
|
+
* Set the colorspace in which to decode the image.
|
161
|
+
* @param im A handle to an opened Epeg image.
|
162
|
+
* @param colorspace The colorspace to decode the image in.
|
163
|
+
*
|
164
|
+
* This sets the colorspace to decode the image in. The default is EPEG_YUV8,
|
165
|
+
* as this is normally the native colorspace of a JPEG file, avoiding any
|
166
|
+
* colorspace conversions for a faster load and/or save.
|
167
|
+
*/
|
168
|
+
EAPI void
|
169
|
+
epeg_decode_colorspace_set(Epeg_Image *im, Epeg_Colorspace colorspace)
|
170
|
+
{
|
171
|
+
if (im->pixels) return;
|
172
|
+
if ((colorspace < EPEG_GRAY8) || (colorspace > EPEG_CMYK)) return;
|
173
|
+
im->color_space = colorspace;
|
174
|
+
}
|
175
|
+
|
176
|
+
/**
|
177
|
+
* Get a segment of decoded pixels from an image.
|
178
|
+
* @param im A handle to an opened Epeg image.
|
179
|
+
* @param x Rectangle X.
|
180
|
+
* @param y Rectangle Y.
|
181
|
+
* @param w Rectangle width.
|
182
|
+
* @param h Rectangle height.
|
183
|
+
* @return Pointer to the top left of the requested pixel block.
|
184
|
+
*
|
185
|
+
* Return image pixels in the decoded format from the specified location
|
186
|
+
* rectangle bounded with the box @p x, @p y @p w X @p y. The pixel block is
|
187
|
+
* packed with no row padding, and it organsied from top-left to bottom right,
|
188
|
+
* row by row. You must free the pixel block using epeg_pixels_free() before
|
189
|
+
* you close the image handle, and assume the pixels to be read-only memory.
|
190
|
+
*
|
191
|
+
* On success the pointer is returned, on failure, NULL is returned. Failure
|
192
|
+
* may be because the rectangle is out of the bounds of the image, memory
|
193
|
+
* allocations failed or the image data cannot be decoded.
|
194
|
+
*
|
195
|
+
*/
|
196
|
+
EAPI const void *
|
197
|
+
epeg_pixels_get(Epeg_Image *im, int x, int y, int w, int h)
|
198
|
+
{
|
199
|
+
int xx, yy, ww, hh, bpp, ox, oy, ow, oh, iw, ih;
|
200
|
+
|
201
|
+
if (!im->pixels)
|
202
|
+
{
|
203
|
+
if (_epeg_decode(im) != 0) return NULL;
|
204
|
+
}
|
205
|
+
|
206
|
+
if (!im->pixels) return NULL;
|
207
|
+
if ((im->out.w < 1) || (im->out.h < 1)) return NULL;
|
208
|
+
|
209
|
+
if (_epeg_scale(im) != 0) return NULL;
|
210
|
+
|
211
|
+
bpp = im->in.jinfo.output_components;
|
212
|
+
iw = im->out.w;
|
213
|
+
ih = im->out.h;
|
214
|
+
ow = w;
|
215
|
+
oh = h;
|
216
|
+
ox = 0;
|
217
|
+
oy = 0;
|
218
|
+
if ((x + ow) > iw) ow = iw - x;
|
219
|
+
if ((y + oh) > ih) oh = ih - y;
|
220
|
+
if (ow < 1) return NULL;
|
221
|
+
if (oh < 1) return NULL;
|
222
|
+
if (x < 0)
|
223
|
+
{
|
224
|
+
ow += x;
|
225
|
+
ox = -x;
|
226
|
+
}
|
227
|
+
if (y < 0)
|
228
|
+
{
|
229
|
+
oh += y;
|
230
|
+
oy = -y;
|
231
|
+
}
|
232
|
+
if (ow < 1) return NULL;
|
233
|
+
if (oh < 1) return NULL;
|
234
|
+
|
235
|
+
ww = x + ox + ow;
|
236
|
+
hh = y + oy + oh;
|
237
|
+
|
238
|
+
if (im->color_space == EPEG_GRAY8)
|
239
|
+
{
|
240
|
+
unsigned char *pix, *p;
|
241
|
+
|
242
|
+
pix = malloc(w * h * 1);
|
243
|
+
if (!pix) return NULL;
|
244
|
+
for (yy = y + oy; yy < hh; yy++)
|
245
|
+
{
|
246
|
+
unsigned char *s;
|
247
|
+
|
248
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
249
|
+
p = pix + ((((yy - y) * w) + ox));
|
250
|
+
for (xx = x + ox; xx < ww; xx++)
|
251
|
+
{
|
252
|
+
p[0] = s[0];
|
253
|
+
p++;
|
254
|
+
s += bpp;
|
255
|
+
}
|
256
|
+
}
|
257
|
+
return pix;
|
258
|
+
}
|
259
|
+
else if (im->color_space == EPEG_YUV8)
|
260
|
+
{
|
261
|
+
unsigned char *pix, *p;
|
262
|
+
|
263
|
+
pix = malloc(w * h * 3);
|
264
|
+
if (!pix) return NULL;
|
265
|
+
for (yy = y + oy; yy < hh; yy++)
|
266
|
+
{
|
267
|
+
unsigned char *s;
|
268
|
+
|
269
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
270
|
+
p = pix + ((((yy - y) * w) + ox) * 3);
|
271
|
+
for (xx = x + ox; xx < ww; xx++)
|
272
|
+
{
|
273
|
+
p[0] = s[0];
|
274
|
+
p[1] = s[1];
|
275
|
+
p[2] = s[2];
|
276
|
+
p += 3;
|
277
|
+
s += bpp;
|
278
|
+
}
|
279
|
+
}
|
280
|
+
return pix;
|
281
|
+
}
|
282
|
+
else if (im->color_space == EPEG_RGB8)
|
283
|
+
{
|
284
|
+
unsigned char *pix, *p;
|
285
|
+
|
286
|
+
pix = malloc(w * h * 3);
|
287
|
+
if (!pix) return NULL;
|
288
|
+
for (yy = y + oy; yy < hh; yy++)
|
289
|
+
{
|
290
|
+
unsigned char *s;
|
291
|
+
|
292
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
293
|
+
p = pix + ((((yy - y) * w) + ox) * 3);
|
294
|
+
for (xx = x + ox; xx < ww; xx++)
|
295
|
+
{
|
296
|
+
p[0] = s[0];
|
297
|
+
p[1] = s[1];
|
298
|
+
p[2] = s[2];
|
299
|
+
p += 3;
|
300
|
+
s += bpp;
|
301
|
+
}
|
302
|
+
}
|
303
|
+
return pix;
|
304
|
+
}
|
305
|
+
else if (im->color_space == EPEG_BGR8)
|
306
|
+
{
|
307
|
+
unsigned char *pix, *p;
|
308
|
+
|
309
|
+
pix = malloc(w * h * 3);
|
310
|
+
if (!pix) return NULL;
|
311
|
+
for (yy = y + oy; yy < hh; yy++)
|
312
|
+
{
|
313
|
+
unsigned char *s;
|
314
|
+
|
315
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
316
|
+
p = pix + ((((yy - y) * w) + ox) * 3);
|
317
|
+
for (xx = x + ox; xx < ww; xx++)
|
318
|
+
{
|
319
|
+
p[0] = s[2];
|
320
|
+
p[1] = s[1];
|
321
|
+
p[2] = s[0];
|
322
|
+
p += 3;
|
323
|
+
s += bpp;
|
324
|
+
}
|
325
|
+
}
|
326
|
+
return pix;
|
327
|
+
}
|
328
|
+
else if (im->color_space == EPEG_RGBA8)
|
329
|
+
{
|
330
|
+
unsigned char *pix, *p;
|
331
|
+
|
332
|
+
pix = malloc(w * h * 4);
|
333
|
+
if (!pix) return NULL;
|
334
|
+
for (yy = y + oy; yy < hh; yy++)
|
335
|
+
{
|
336
|
+
unsigned char *s;
|
337
|
+
|
338
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
339
|
+
p = pix + ((((yy - y) * w) + ox) * 4);
|
340
|
+
for (xx = x + ox; xx < ww; xx++)
|
341
|
+
{
|
342
|
+
p[0] = s[0];
|
343
|
+
p[1] = s[1];
|
344
|
+
p[2] = s[2];
|
345
|
+
p[3] = 0xff;
|
346
|
+
p += 4;
|
347
|
+
s += bpp;
|
348
|
+
}
|
349
|
+
}
|
350
|
+
return pix;
|
351
|
+
}
|
352
|
+
else if (im->color_space == EPEG_BGRA8)
|
353
|
+
{
|
354
|
+
unsigned char *pix, *p;
|
355
|
+
|
356
|
+
pix = malloc(w * h * 4);
|
357
|
+
if (!pix) return NULL;
|
358
|
+
for (yy = y + oy; yy < hh; yy++)
|
359
|
+
{
|
360
|
+
unsigned char *s;
|
361
|
+
|
362
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
363
|
+
p = pix + ((((yy - y) * w) + ox) * 4);
|
364
|
+
for (xx = x + ox; xx < ww; xx++)
|
365
|
+
{
|
366
|
+
p[0] = 0xff;
|
367
|
+
p[1] = s[2];
|
368
|
+
p[2] = s[1];
|
369
|
+
p[3] = s[0];
|
370
|
+
p += 4;
|
371
|
+
s += bpp;
|
372
|
+
}
|
373
|
+
}
|
374
|
+
return pix;
|
375
|
+
}
|
376
|
+
else if (im->color_space == EPEG_ARGB32)
|
377
|
+
{
|
378
|
+
unsigned int *pix, *p;
|
379
|
+
|
380
|
+
pix = malloc(w * h * 4);
|
381
|
+
if (!pix) return NULL;
|
382
|
+
for (yy = y + oy; yy < hh; yy++)
|
383
|
+
{
|
384
|
+
unsigned char *s;
|
385
|
+
|
386
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
387
|
+
p = pix + ((((yy - y) * w) + ox));
|
388
|
+
for (xx = x + ox; xx < ww; xx++)
|
389
|
+
{
|
390
|
+
p[0] = 0xff000000 | (s[0] << 16) | (s[1] << 8) | (s[2]);
|
391
|
+
p++;
|
392
|
+
s += bpp;
|
393
|
+
}
|
394
|
+
}
|
395
|
+
return pix;
|
396
|
+
}
|
397
|
+
else if (im->color_space == EPEG_CMYK)
|
398
|
+
{
|
399
|
+
unsigned char *pix, *p;
|
400
|
+
|
401
|
+
pix = malloc(w * h * 4);
|
402
|
+
if (!pix) return NULL;
|
403
|
+
for (yy = y + oy; yy < hh; yy++)
|
404
|
+
{
|
405
|
+
unsigned char *s;
|
406
|
+
|
407
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
408
|
+
p = pix + ((((yy - y) * w) + ox) * 4);
|
409
|
+
for (xx = x + ox; xx < ww; xx++)
|
410
|
+
{
|
411
|
+
p[0] = s[0];
|
412
|
+
p[1] = s[1];
|
413
|
+
p[2] = s[2];
|
414
|
+
p[3] = 0xff;
|
415
|
+
p += 4;
|
416
|
+
s += bpp;
|
417
|
+
}
|
418
|
+
}
|
419
|
+
return pix;
|
420
|
+
}
|
421
|
+
return NULL;
|
422
|
+
}
|
423
|
+
|
424
|
+
/**
|
425
|
+
* Get a segment of decoded pixels from an image.
|
426
|
+
* @param im A handle to an opened Epeg image.
|
427
|
+
* @param x Rectangle X.
|
428
|
+
* @param y Rectangle Y.
|
429
|
+
* @param w Rectangle width.
|
430
|
+
* @param h Rectangle height.
|
431
|
+
* @return Pointer to the top left of the requested pixel block.
|
432
|
+
*
|
433
|
+
* Return image pixels in the decoded format from the specified location
|
434
|
+
* rectangle bounded with the box @p x, @p y @p w X @p y. The pixel block is
|
435
|
+
* packed with no row padding, and it organsied from top-left to bottom right,
|
436
|
+
* row by row. You must free the pixel block using epeg_pixels_free() before
|
437
|
+
* you close the image handle, and assume the pixels to be read-only memory.
|
438
|
+
*
|
439
|
+
* On success the pointer is returned, on failure, NULL is returned. Failure
|
440
|
+
* may be because the rectangle is out of the bounds of the image, memory
|
441
|
+
* allocations failed or the image data cannot be decoded.
|
442
|
+
*
|
443
|
+
*/
|
444
|
+
EAPI const void *
|
445
|
+
epeg_pixels_get_as_RGB8(Epeg_Image *im, int x, int y, int w, int h)
|
446
|
+
{
|
447
|
+
int xx, yy, ww, hh, bpp, ox, oy, ow, oh, iw, ih;
|
448
|
+
|
449
|
+
if (!im->pixels)
|
450
|
+
{
|
451
|
+
if (_epeg_decode(im) != 0) return NULL;
|
452
|
+
}
|
453
|
+
|
454
|
+
if (!im->pixels) return NULL;
|
455
|
+
if ((im->out.w < 1) || (im->out.h < 1)) return NULL;
|
456
|
+
|
457
|
+
bpp = im->in.jinfo.output_components;
|
458
|
+
iw = im->out.w;
|
459
|
+
ih = im->out.h;
|
460
|
+
ow = w;
|
461
|
+
oh = h;
|
462
|
+
ox = 0;
|
463
|
+
oy = 0;
|
464
|
+
if ((x + ow) > iw) ow = iw - x;
|
465
|
+
if ((y + oh) > ih) oh = ih - y;
|
466
|
+
if (ow < 1) return NULL;
|
467
|
+
if (oh < 1) return NULL;
|
468
|
+
if (x < 0)
|
469
|
+
{
|
470
|
+
ow += x;
|
471
|
+
ox = -x;
|
472
|
+
}
|
473
|
+
if (y < 0)
|
474
|
+
{
|
475
|
+
oh += y;
|
476
|
+
oy = -y;
|
477
|
+
}
|
478
|
+
if (ow < 1) return NULL;
|
479
|
+
if (oh < 1) return NULL;
|
480
|
+
|
481
|
+
ww = x + ox + ow;
|
482
|
+
hh = y + oy + oh;
|
483
|
+
|
484
|
+
if (im->color_space == EPEG_GRAY8)
|
485
|
+
{
|
486
|
+
unsigned char *pix, *p;
|
487
|
+
|
488
|
+
pix = malloc(w * h * 3);
|
489
|
+
if (!pix) return NULL;
|
490
|
+
for (yy = y + oy; yy < hh; yy++)
|
491
|
+
{
|
492
|
+
unsigned char *s;
|
493
|
+
|
494
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
495
|
+
p = pix + ((((yy - y) * w) + ox) * 3);
|
496
|
+
for (xx = x + ox; xx < ww; xx++)
|
497
|
+
{
|
498
|
+
p[0] = s[0];
|
499
|
+
p[1] = s[0];
|
500
|
+
p[2] = s[0];
|
501
|
+
p += 3;
|
502
|
+
s += bpp;
|
503
|
+
}
|
504
|
+
}
|
505
|
+
return pix;
|
506
|
+
}
|
507
|
+
if (im->color_space == EPEG_RGB8)
|
508
|
+
{
|
509
|
+
unsigned char *pix, *p;
|
510
|
+
|
511
|
+
pix = malloc(w * h * 3);
|
512
|
+
if (!pix) return NULL;
|
513
|
+
for (yy = y + oy; yy < hh; yy++)
|
514
|
+
{
|
515
|
+
unsigned char *s;
|
516
|
+
|
517
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
518
|
+
p = pix + ((((yy - y) * w) + ox) * 3);
|
519
|
+
for (xx = x + ox; xx < ww; xx++)
|
520
|
+
{
|
521
|
+
p[0] = s[0];
|
522
|
+
p[1] = s[1];
|
523
|
+
p[2] = s[2];
|
524
|
+
p += 3;
|
525
|
+
s += bpp;
|
526
|
+
}
|
527
|
+
}
|
528
|
+
return pix;
|
529
|
+
}
|
530
|
+
if (im->color_space == EPEG_CMYK)
|
531
|
+
{
|
532
|
+
unsigned char *pix, *p;
|
533
|
+
|
534
|
+
pix = malloc(w * h * 3);
|
535
|
+
if (!pix) return NULL;
|
536
|
+
for (yy = y + oy; yy < hh; yy++)
|
537
|
+
{
|
538
|
+
unsigned char *s;
|
539
|
+
|
540
|
+
s = im->lines[yy] + ((x + ox) * bpp);
|
541
|
+
p = pix + ((((yy - y) * w) + ox) * 3);
|
542
|
+
for (xx = x + ox; xx < ww; xx++)
|
543
|
+
{
|
544
|
+
p[0] = (unsigned char)(MIN(255, (s[0] * s[3]) / 255));
|
545
|
+
p[1] = (unsigned char)(MIN(255, (s[1] * s[3]) / 255));
|
546
|
+
p[2] = (unsigned char)(MIN(255, (s[2] * s[3]) / 255));
|
547
|
+
p += 3;
|
548
|
+
s += bpp;
|
549
|
+
}
|
550
|
+
}
|
551
|
+
return pix;
|
552
|
+
}
|
553
|
+
return NULL;
|
554
|
+
}
|
555
|
+
|
556
|
+
/**
|
557
|
+
* Free requested pixel block from an image.
|
558
|
+
* @param im A handle to an opened Epeg image.
|
559
|
+
* @param data The pointer to the image pixels.
|
560
|
+
*
|
561
|
+
* This frees the data for a block of pixels requested from image @p im.
|
562
|
+
* @p data must be a valid (non NULL) pointer to a pixel block taken from the
|
563
|
+
* image @p im by epeg_pixels_get() and mustbe called before the image is
|
564
|
+
* closed by epeg_close().
|
565
|
+
*/
|
566
|
+
EAPI void
|
567
|
+
epeg_pixels_free(Epeg_Image *im, const void *data)
|
568
|
+
{
|
569
|
+
free((void *)data);
|
570
|
+
}
|
571
|
+
|
572
|
+
/**
|
573
|
+
* Get the image comment field as a string.
|
574
|
+
* @param im A handle to an opened Epeg image.
|
575
|
+
* @return A pointer to the loaded image comments.
|
576
|
+
*
|
577
|
+
* This function returns the comment field as a string (NUL byte terminated)
|
578
|
+
* of the loaded image @p im, if there is a comment, or NULL if no comment is
|
579
|
+
* saved with the image. Consider the string returned to be read-only.
|
580
|
+
*
|
581
|
+
*/
|
582
|
+
EAPI const char *
|
583
|
+
epeg_comment_get(Epeg_Image *im)
|
584
|
+
{
|
585
|
+
return im->in.comment;
|
586
|
+
}
|
587
|
+
|
588
|
+
/**
|
589
|
+
* Get thumbnail comments of loaded image.
|
590
|
+
* @param im A handle to an opened Epeg image.
|
591
|
+
* @param info Pointer to a thumbnail info struct to be filled in.
|
592
|
+
*
|
593
|
+
* This function retrieves thumbnail comments written by Epeg to any saved
|
594
|
+
* JPEG files. If no thumbnail comments were saved, the fields will be 0 in
|
595
|
+
* the @p info struct on return.
|
596
|
+
*
|
597
|
+
*/
|
598
|
+
EAPI void
|
599
|
+
epeg_thumbnail_comments_get(Epeg_Image *im, Epeg_Thumbnail_Info *info)
|
600
|
+
{
|
601
|
+
if (!info) return;
|
602
|
+
info->uri = im->in.thumb_info.uri;
|
603
|
+
info->mtime = im->in.thumb_info.mtime;
|
604
|
+
info->w = im->in.thumb_info.w;
|
605
|
+
info->h = im->in.thumb_info.h;
|
606
|
+
info->mimetype = im->in.thumb_info.mime;
|
607
|
+
}
|
608
|
+
|
609
|
+
/**
|
610
|
+
* Set the comment field of the image for saving.
|
611
|
+
* @param im A handle to an opened Epeg image.
|
612
|
+
* @param comment The comment to set.
|
613
|
+
*
|
614
|
+
* Set the comment for the image file for when it gets saved. This is a NUL
|
615
|
+
* byte terminated C string. If @p comment is NULL the output file will have
|
616
|
+
* no comment field.
|
617
|
+
*
|
618
|
+
* The default comment will be any comment loaded from the input file.
|
619
|
+
*
|
620
|
+
*/
|
621
|
+
EAPI void
|
622
|
+
epeg_comment_set(Epeg_Image *im, const char *comment)
|
623
|
+
{
|
624
|
+
if (im->out.comment) free(im->out.comment);
|
625
|
+
if (!comment) im->out.comment = NULL;
|
626
|
+
else im->out.comment = strdup(comment);
|
627
|
+
}
|
628
|
+
|
629
|
+
/**
|
630
|
+
* Set the encoding quality of the saved image.
|
631
|
+
* @param im A handle to an opened Epeg image.
|
632
|
+
* @param quality The quality of encoding from 0 to 100.
|
633
|
+
*
|
634
|
+
* Set the quality of the output encoded image. Values from 0 to 100
|
635
|
+
* inclusive are valid, with 100 being the maximum quality, and 0 being the
|
636
|
+
* minimum. If the quality is set equal to or above 90%, the output U and V
|
637
|
+
* color planes are encoded at 1:1 with the Y plane.
|
638
|
+
*
|
639
|
+
* The default quality is 75.
|
640
|
+
*
|
641
|
+
*/
|
642
|
+
EAPI void
|
643
|
+
epeg_quality_set(Epeg_Image *im, int quality)
|
644
|
+
{
|
645
|
+
if (quality < 0) quality = 0;
|
646
|
+
else if (quality > 100) quality = 100;
|
647
|
+
im->out.quality = quality;
|
648
|
+
}
|
649
|
+
|
650
|
+
/**
|
651
|
+
* Enable thumbnail comments in saved image.
|
652
|
+
* @param im A handle to an opened Epeg image.
|
653
|
+
* @param onoff A boolean on and off enabling flag.
|
654
|
+
*
|
655
|
+
* if @p onoff is 1, the output file will have thumbnail comments added to
|
656
|
+
* it, and if it is 0, it will not. The default is 0.
|
657
|
+
*
|
658
|
+
*/
|
659
|
+
EAPI void
|
660
|
+
epeg_thumbnail_comments_enable(Epeg_Image *im, int onoff)
|
661
|
+
{
|
662
|
+
im->out.thumbnail_info = onoff;
|
663
|
+
}
|
664
|
+
|
665
|
+
/**
|
666
|
+
* Set the output file path for the image when saved.
|
667
|
+
* @param im A handle to an opened Epeg image.
|
668
|
+
* @param file The path to the output file.
|
669
|
+
*
|
670
|
+
* This sets the output file path name (either a full or relative path name)
|
671
|
+
* to where the file will be written when saved. @p file must be a NUL
|
672
|
+
* terminated C string conatining the path to the file to be saved to. If it is
|
673
|
+
* NULL, the image will not be saved to a file when calling epeg_encode().
|
674
|
+
*/
|
675
|
+
EAPI void
|
676
|
+
epeg_file_output_set(Epeg_Image *im, const char *file)
|
677
|
+
{
|
678
|
+
if (im->out.file) free(im->out.file);
|
679
|
+
if (!file) im->out.file = NULL;
|
680
|
+
else im->out.file = strdup(file);
|
681
|
+
}
|
682
|
+
|
683
|
+
/**
|
684
|
+
* Set the output file to be a block of allocated memory.
|
685
|
+
* @param im A handle to an opened Epeg image.
|
686
|
+
* @param data A pointer to a pointer to a memory block.
|
687
|
+
* @param size A pointer to a counter of the size of the memory block.
|
688
|
+
*
|
689
|
+
* This sets the output encoding of the image when saved to be allocated
|
690
|
+
* memory. After epeg_close() is called the pointer pointed to by @p data
|
691
|
+
* and the integer pointed to by @p size will contain the pointer to the
|
692
|
+
* memory block and its size in bytes, respecitvely. The memory block can be
|
693
|
+
* freed with the free() function call. If the save fails the pointer to the
|
694
|
+
* memory block will be unaffected, as will the size.
|
695
|
+
*
|
696
|
+
*/
|
697
|
+
EAPI void
|
698
|
+
epeg_memory_output_set(Epeg_Image *im, unsigned char **data, int *size)
|
699
|
+
{
|
700
|
+
im->out.mem.data = data;
|
701
|
+
im->out.mem.size = size;
|
702
|
+
im->out.file = NULL;
|
703
|
+
}
|
704
|
+
|
705
|
+
/**
|
706
|
+
* This saves the image to its specified destination.
|
707
|
+
* @param im A handle to an opened Epeg image.
|
708
|
+
*
|
709
|
+
* This saves the image @p im to its destination specified by
|
710
|
+
* epeg_file_output_set() or epeg_memory_output_set(). The image will be
|
711
|
+
* encoded at the deoded pixel size, using the quality, comment and thumbnail
|
712
|
+
* comment settings set on the image.
|
713
|
+
*
|
714
|
+
* retval 1 - error scale
|
715
|
+
* 2 - error encode
|
716
|
+
* 3 - error decode
|
717
|
+
* 4 - error decode ( setjmp )
|
718
|
+
*/
|
719
|
+
EAPI int
|
720
|
+
epeg_encode(Epeg_Image *im)
|
721
|
+
{
|
722
|
+
int ret;
|
723
|
+
if ((ret = _epeg_decode(im)) != 0)
|
724
|
+
return (ret == 2 ? 4 : 3);
|
725
|
+
if (_epeg_scale(im) != 0)
|
726
|
+
return 1;
|
727
|
+
if (_epeg_encode(im) != 0)
|
728
|
+
return 2;
|
729
|
+
return 0;
|
730
|
+
}
|
731
|
+
|
732
|
+
/**
|
733
|
+
* FIXME: Document this
|
734
|
+
* @param im A handle to an opened Epeg image.
|
735
|
+
*
|
736
|
+
* FIXME: Document this.
|
737
|
+
*/
|
738
|
+
EAPI int
|
739
|
+
epeg_trim(Epeg_Image *im)
|
740
|
+
{
|
741
|
+
if (_epeg_decode_for_trim(im) != 0)
|
742
|
+
return 1;
|
743
|
+
if (_epeg_trim(im) != 0)
|
744
|
+
return 1;
|
745
|
+
if (_epeg_encode(im) != 0)
|
746
|
+
return 1;
|
747
|
+
return 0;
|
748
|
+
}
|
749
|
+
|
750
|
+
/**
|
751
|
+
* Close an image handle.
|
752
|
+
* @param im A handle to an opened Epeg image.
|
753
|
+
*
|
754
|
+
* This closes an opened image handle and frees all memory associated with it.
|
755
|
+
* It does not free encoded data generated by epeg_memory_output_set() followed
|
756
|
+
* by epeg_encode() nor does it guarantee to free any data recieved by
|
757
|
+
* epeg_pixels_get(). Once an image handle is closed consider it invalid.
|
758
|
+
*/
|
759
|
+
EAPI void
|
760
|
+
epeg_close(Epeg_Image *im)
|
761
|
+
{
|
762
|
+
if (!im) return;
|
763
|
+
if (im->pixels) free(im->pixels);
|
764
|
+
if (im->lines) free(im->lines);
|
765
|
+
if (im->in.file) free(im->in.file);
|
766
|
+
if (!im->in.file) free(im->in.jinfo.src);
|
767
|
+
if (im->in.f || im->in.mem.data) jpeg_destroy_decompress(&(im->in.jinfo));
|
768
|
+
if (im->in.f) fclose(im->in.f);
|
769
|
+
if (im->in.comment) free(im->in.comment);
|
770
|
+
if (im->in.thumb_info.uri) free(im->in.thumb_info.uri);
|
771
|
+
if (im->in.thumb_info.mime) free(im->in.thumb_info.mime);
|
772
|
+
if (im->out.file) free(im->out.file);
|
773
|
+
if (!im->out.file) free(im->out.jinfo.dest);
|
774
|
+
if (im->out.f || im->in.mem.data) jpeg_destroy_compress(&(im->out.jinfo));
|
775
|
+
if (im->out.f) fclose(im->out.f);
|
776
|
+
if (im->out.comment) free(im->out.comment);
|
777
|
+
free(im);
|
778
|
+
}
|
779
|
+
|
780
|
+
static Epeg_Image *
|
781
|
+
_epeg_open_header(Epeg_Image *im)
|
782
|
+
{
|
783
|
+
struct jpeg_marker_struct *m;
|
784
|
+
struct jpeg_source_mgr *src_mgr = NULL;
|
785
|
+
|
786
|
+
im->in.jinfo.err = jpeg_std_error(&(im->jerr.pub));
|
787
|
+
im->jerr.pub.error_exit = _epeg_fatal_error_handler;
|
788
|
+
#ifdef NOWARNINGS
|
789
|
+
im->jerr.pub.emit_message = _emit_message;
|
790
|
+
im->jerr.pub.output_message = _output_message;
|
791
|
+
im->jerr.pub.format_message = _format_message;
|
792
|
+
#endif
|
793
|
+
|
794
|
+
if (setjmp(im->jerr.setjmp_buffer))
|
795
|
+
{
|
796
|
+
error:
|
797
|
+
epeg_close(im);
|
798
|
+
im = NULL;
|
799
|
+
return NULL;
|
800
|
+
}
|
801
|
+
|
802
|
+
jpeg_create_decompress(&(im->in.jinfo));
|
803
|
+
jpeg_save_markers(&(im->in.jinfo), JPEG_APP0 + 7, 1024);
|
804
|
+
/* Save Exif markers */
|
805
|
+
jpeg_save_markers(&(im->in.jinfo), JPEG_APP0 + 1, 65535);
|
806
|
+
jpeg_save_markers(&(im->in.jinfo), JPEG_COM, 65535);
|
807
|
+
if (im->in.f != NULL)
|
808
|
+
{
|
809
|
+
jpeg_stdio_src(&(im->in.jinfo), im->in.f);
|
810
|
+
}
|
811
|
+
else
|
812
|
+
{
|
813
|
+
/* Setup RAM source manager. */
|
814
|
+
src_mgr = calloc(1, sizeof(struct jpeg_source_mgr));
|
815
|
+
if (!src_mgr) goto error;
|
816
|
+
src_mgr->init_source = _jpeg_init_source;
|
817
|
+
src_mgr->fill_input_buffer = _jpeg_fill_input_buffer;
|
818
|
+
src_mgr->skip_input_data = _jpeg_skip_input_data;
|
819
|
+
src_mgr->resync_to_restart = jpeg_resync_to_restart;
|
820
|
+
src_mgr->term_source = _jpeg_term_source;
|
821
|
+
src_mgr->bytes_in_buffer = im->in.mem.size;
|
822
|
+
src_mgr->next_input_byte = (JOCTET *) im->in.mem.data;
|
823
|
+
im->in.jinfo.src = (struct jpeg_source_mgr *) src_mgr;
|
824
|
+
}
|
825
|
+
|
826
|
+
jpeg_read_header(&(im->in.jinfo), TRUE);
|
827
|
+
im->in.w = im->in.jinfo.image_width;
|
828
|
+
im->in.h = im->in.jinfo.image_height;
|
829
|
+
if (im->in.w < 1) goto error;
|
830
|
+
if (im->in.h < 1) goto error;
|
831
|
+
|
832
|
+
im->out.w = im->in.w;
|
833
|
+
im->out.h = im->in.h;
|
834
|
+
|
835
|
+
im->color_space = ((im->in.color_space = im->in.jinfo.out_color_space) == JCS_GRAYSCALE) ? EPEG_GRAY8 : EPEG_RGB8;
|
836
|
+
if (im->in.color_space == JCS_CMYK) im->color_space = EPEG_CMYK;
|
837
|
+
|
838
|
+
for (m = im->in.jinfo.marker_list; m; m = m->next)
|
839
|
+
{
|
840
|
+
if (m->marker == JPEG_COM)
|
841
|
+
{
|
842
|
+
if (im->in.comment) free(im->in.comment);
|
843
|
+
im->in.comment = malloc(m->data_length + 1);
|
844
|
+
if (im->in.comment)
|
845
|
+
{
|
846
|
+
memcpy(im->in.comment, m->data, m->data_length);
|
847
|
+
im->in.comment[m->data_length] = 0;
|
848
|
+
}
|
849
|
+
}
|
850
|
+
else if (m->marker == (JPEG_APP0 + 7))
|
851
|
+
{
|
852
|
+
if ((m->data_length > 7) &&
|
853
|
+
(!strncmp((char *)m->data, "Thumb::", 7)))
|
854
|
+
{
|
855
|
+
char *p, *p2;
|
856
|
+
|
857
|
+
p = malloc(m->data_length + 1);
|
858
|
+
if (p)
|
859
|
+
{
|
860
|
+
memcpy(p, m->data, m->data_length);
|
861
|
+
p[m->data_length] = 0;
|
862
|
+
p2 = strchr(p, '\n');
|
863
|
+
if (p2)
|
864
|
+
{
|
865
|
+
p2[0] = 0;
|
866
|
+
if (!strcmp(p, "Thumb::URI"))
|
867
|
+
|
868
|
+
im->in.thumb_info.uri = strdup(p2 + 1);
|
869
|
+
else if (!strcmp(p, "Thumb::MTime"))
|
870
|
+
sscanf(p2 + 1, "%llu", &(im->in.thumb_info.mtime));
|
871
|
+
else if (!strcmp(p, "Thumb::Image::Width"))
|
872
|
+
im->in.thumb_info.w = atoi(p2 + 1);
|
873
|
+
else if (!strcmp(p, "Thumb::Image::Height"))
|
874
|
+
im->in.thumb_info.h = atoi(p2 + 1);
|
875
|
+
else if (!strcmp(p, "Thumb::Mimetype"))
|
876
|
+
im->in.thumb_info.mime = strdup(p2 + 1);
|
877
|
+
}
|
878
|
+
free(p);
|
879
|
+
}
|
880
|
+
}
|
881
|
+
}
|
882
|
+
else if (m->marker == (JPEG_APP0 + 1))
|
883
|
+
{
|
884
|
+
/*
|
885
|
+
* Look for an Exif Orientation tag. If found,
|
886
|
+
* store it in im->in.orientation. Later, this will
|
887
|
+
* be written to the output jpeg Exif data.
|
888
|
+
*/
|
889
|
+
im->in.orientation = 0;
|
890
|
+
ExifData *ed = exif_data_new_from_data(m->data, m->data_length);
|
891
|
+
if (ed) {
|
892
|
+
exif_byte_order = exif_data_get_byte_order(ed);
|
893
|
+
ExifEntry *entry = exif_content_get_entry(ed->ifd[EXIF_IFD_0],EXIF_TAG_ORIENTATION);
|
894
|
+
if (entry) {
|
895
|
+
im->in.orientation = exif_get_short(entry->data, exif_byte_order);
|
896
|
+
exif_entry_unref(entry);
|
897
|
+
}
|
898
|
+
}
|
899
|
+
// Should be able to release ed using unref, but this causes segfault. libexif bug?
|
900
|
+
// exif_data_unref(ed);
|
901
|
+
}
|
902
|
+
return im;
|
903
|
+
}
|
904
|
+
}
|
905
|
+
|
906
|
+
/**
|
907
|
+
retval 1 - malloc or other
|
908
|
+
2 - setjmp error
|
909
|
+
*/
|
910
|
+
static int
|
911
|
+
_epeg_decode(Epeg_Image *im)
|
912
|
+
{
|
913
|
+
int scale, scalew, scaleh, y;
|
914
|
+
JDIMENSION old_output_scanline = 1;
|
915
|
+
|
916
|
+
if (im->pixels) return 1;
|
917
|
+
if ((im->out.w < 1) || (im->out.h < 1)) return 1;
|
918
|
+
|
919
|
+
scalew = im->in.w / im->out.w;
|
920
|
+
scaleh = im->in.h / im->out.h;
|
921
|
+
|
922
|
+
scale = scalew;
|
923
|
+
if (scaleh < scalew) scale = scaleh;
|
924
|
+
|
925
|
+
if (scale > 8) scale = 8;
|
926
|
+
else if (scale < 1) scale = 1;
|
927
|
+
|
928
|
+
im->in.jinfo.scale_num = 1;
|
929
|
+
im->in.jinfo.scale_denom = scale;
|
930
|
+
im->in.jinfo.do_fancy_upsampling = FALSE;
|
931
|
+
im->in.jinfo.do_block_smoothing = FALSE;
|
932
|
+
im->in.jinfo.dct_method = JDCT_IFAST;
|
933
|
+
|
934
|
+
switch (im->color_space)
|
935
|
+
{
|
936
|
+
case EPEG_GRAY8:
|
937
|
+
im->in.jinfo.out_color_space = JCS_GRAYSCALE;
|
938
|
+
im->in.jinfo.output_components = 1;
|
939
|
+
break;
|
940
|
+
|
941
|
+
case EPEG_YUV8:
|
942
|
+
im->in.jinfo.out_color_space = JCS_YCbCr;
|
943
|
+
break;
|
944
|
+
|
945
|
+
case EPEG_RGB8:
|
946
|
+
case EPEG_BGR8:
|
947
|
+
case EPEG_RGBA8:
|
948
|
+
case EPEG_BGRA8:
|
949
|
+
case EPEG_ARGB32:
|
950
|
+
im->in.jinfo.out_color_space = JCS_RGB;
|
951
|
+
break;
|
952
|
+
|
953
|
+
case EPEG_CMYK:
|
954
|
+
im->in.jinfo.out_color_space = JCS_CMYK;
|
955
|
+
im->in.jinfo.output_components = 4;
|
956
|
+
break;
|
957
|
+
|
958
|
+
default:
|
959
|
+
break;
|
960
|
+
}
|
961
|
+
|
962
|
+
im->out.jinfo.err = jpeg_std_error(&(im->jerr.pub));
|
963
|
+
im->jerr.pub.error_exit = _epeg_fatal_error_handler;
|
964
|
+
#ifdef NOWARNINGS
|
965
|
+
im->jerr.pub.emit_message = _emit_message;
|
966
|
+
im->jerr.pub.output_message = _output_message;
|
967
|
+
im->jerr.pub.format_message = _format_message;
|
968
|
+
#endif
|
969
|
+
|
970
|
+
if (setjmp(im->jerr.setjmp_buffer))
|
971
|
+
return 2;
|
972
|
+
|
973
|
+
jpeg_calc_output_dimensions(&(im->in.jinfo));
|
974
|
+
|
975
|
+
im->pixels = malloc(im->in.jinfo.output_width * im->in.jinfo.output_height * im->in.jinfo.output_components);
|
976
|
+
if (!im->pixels) return 1;
|
977
|
+
|
978
|
+
im->lines = malloc(im->in.jinfo.output_height * sizeof(char *));
|
979
|
+
if (!im->lines)
|
980
|
+
{
|
981
|
+
free(im->pixels);
|
982
|
+
im->pixels = NULL;
|
983
|
+
return 1;
|
984
|
+
}
|
985
|
+
|
986
|
+
jpeg_start_decompress(&(im->in.jinfo));
|
987
|
+
|
988
|
+
for (y = 0; y < im->in.jinfo.output_height; y++)
|
989
|
+
im->lines[y] = im->pixels + (y * im->in.jinfo.output_components * im->in.jinfo.output_width);
|
990
|
+
|
991
|
+
while (im->in.jinfo.output_scanline < im->in.jinfo.output_height)
|
992
|
+
{
|
993
|
+
if (old_output_scanline == im->in.jinfo.output_scanline)
|
994
|
+
{
|
995
|
+
jpeg_abort_decompress(&(im->in.jinfo));
|
996
|
+
return 1;
|
997
|
+
}
|
998
|
+
old_output_scanline = im->in.jinfo.output_scanline;
|
999
|
+
jpeg_read_scanlines(&(im->in.jinfo),
|
1000
|
+
&(im->lines[im->in.jinfo.output_scanline]),
|
1001
|
+
im->in.jinfo.rec_outbuf_height);
|
1002
|
+
}
|
1003
|
+
|
1004
|
+
jpeg_finish_decompress(&(im->in.jinfo));
|
1005
|
+
|
1006
|
+
return 0;
|
1007
|
+
}
|
1008
|
+
|
1009
|
+
static int
|
1010
|
+
_epeg_scale(Epeg_Image *im)
|
1011
|
+
{
|
1012
|
+
unsigned char *dst, *row, *src;
|
1013
|
+
int x, y, w, h, i;
|
1014
|
+
|
1015
|
+
if ((im->in.w == im->out.w) && (im->in.h == im->out.h)) return 0;
|
1016
|
+
if (im->scaled) return 0;
|
1017
|
+
|
1018
|
+
if ((im->out.w < 1) || (im->out.h < 1)) return 0;
|
1019
|
+
|
1020
|
+
im->scaled = 1;
|
1021
|
+
w = im->out.w;
|
1022
|
+
h = im->out.h;
|
1023
|
+
for (y = 0; y < h; y++)
|
1024
|
+
{
|
1025
|
+
row = im->pixels + (((y * im->in.jinfo.output_height) / h) * im->in.jinfo.output_components * im->in.jinfo.output_width);
|
1026
|
+
dst = im->pixels + (y * im->in.jinfo.output_components * im->in.jinfo.output_width);
|
1027
|
+
|
1028
|
+
for (x = 0; x < im->out.w; x++)
|
1029
|
+
{
|
1030
|
+
src = row + (((x * im->in.jinfo.output_width) / w) * im->in.jinfo.output_components);
|
1031
|
+
for (i = 0; i < im->in.jinfo.output_components; i++)
|
1032
|
+
dst[i] = src[i];
|
1033
|
+
dst += im->in.jinfo.output_components;
|
1034
|
+
}
|
1035
|
+
}
|
1036
|
+
return 0;
|
1037
|
+
}
|
1038
|
+
|
1039
|
+
static int
|
1040
|
+
_epeg_decode_for_trim(Epeg_Image *im)
|
1041
|
+
{
|
1042
|
+
int y;
|
1043
|
+
|
1044
|
+
if (im->pixels) return 1;
|
1045
|
+
|
1046
|
+
im->in.jinfo.scale_num = 1;
|
1047
|
+
im->in.jinfo.scale_denom = 1;
|
1048
|
+
im->in.jinfo.do_fancy_upsampling = FALSE;
|
1049
|
+
im->in.jinfo.do_block_smoothing = FALSE;
|
1050
|
+
im->in.jinfo.dct_method = JDCT_ISLOW;
|
1051
|
+
|
1052
|
+
switch (im->color_space)
|
1053
|
+
{
|
1054
|
+
case EPEG_GRAY8:
|
1055
|
+
im->in.jinfo.out_color_space = JCS_GRAYSCALE;
|
1056
|
+
im->in.jinfo.output_components = 1;
|
1057
|
+
break;
|
1058
|
+
|
1059
|
+
case EPEG_YUV8:
|
1060
|
+
im->in.jinfo.out_color_space = JCS_YCbCr;
|
1061
|
+
break;
|
1062
|
+
|
1063
|
+
case EPEG_RGB8:
|
1064
|
+
case EPEG_BGR8:
|
1065
|
+
case EPEG_RGBA8:
|
1066
|
+
case EPEG_BGRA8:
|
1067
|
+
case EPEG_ARGB32:
|
1068
|
+
im->in.jinfo.out_color_space = JCS_RGB;
|
1069
|
+
break;
|
1070
|
+
|
1071
|
+
case EPEG_CMYK:
|
1072
|
+
im->in.jinfo.out_color_space = JCS_CMYK;
|
1073
|
+
im->in.jinfo.output_components = 4;
|
1074
|
+
break;
|
1075
|
+
|
1076
|
+
default:
|
1077
|
+
break;
|
1078
|
+
}
|
1079
|
+
|
1080
|
+
im->out.jinfo.err = jpeg_std_error(&(im->jerr.pub));
|
1081
|
+
im->jerr.pub.error_exit = _epeg_fatal_error_handler;
|
1082
|
+
#ifdef NOWARNINGS
|
1083
|
+
im->jerr.pub.emit_message = _emit_message;
|
1084
|
+
im->jerr.pub.output_message = _output_message;
|
1085
|
+
im->jerr.pub.format_message = _format_message;
|
1086
|
+
#endif
|
1087
|
+
|
1088
|
+
if (setjmp(im->jerr.setjmp_buffer))
|
1089
|
+
return 1;
|
1090
|
+
|
1091
|
+
jpeg_calc_output_dimensions(&(im->in.jinfo));
|
1092
|
+
|
1093
|
+
im->pixels = malloc(im->in.jinfo.output_width * im->in.jinfo.output_height * im->in.jinfo.output_components);
|
1094
|
+
if (!im->pixels) return 1;
|
1095
|
+
|
1096
|
+
im->lines = malloc(im->in.jinfo.output_height * sizeof(char *));
|
1097
|
+
if (!im->lines)
|
1098
|
+
{
|
1099
|
+
free(im->pixels);
|
1100
|
+
im->pixels = NULL;
|
1101
|
+
return 1;
|
1102
|
+
}
|
1103
|
+
|
1104
|
+
jpeg_start_decompress(&(im->in.jinfo));
|
1105
|
+
|
1106
|
+
for (y = 0; y < im->in.jinfo.output_height; y++)
|
1107
|
+
im->lines[y] = im->pixels + (y * im->in.jinfo.output_components * im->in.jinfo.output_width);
|
1108
|
+
|
1109
|
+
while (im->in.jinfo.output_scanline < im->in.jinfo.output_height)
|
1110
|
+
jpeg_read_scanlines(&(im->in.jinfo),
|
1111
|
+
&(im->lines[im->in.jinfo.output_scanline]),
|
1112
|
+
im->in.jinfo.rec_outbuf_height);
|
1113
|
+
|
1114
|
+
jpeg_finish_decompress(&(im->in.jinfo));
|
1115
|
+
|
1116
|
+
return 0;
|
1117
|
+
}
|
1118
|
+
|
1119
|
+
static int
|
1120
|
+
_epeg_trim(Epeg_Image *im)
|
1121
|
+
{
|
1122
|
+
int y, a, b, w, h;
|
1123
|
+
|
1124
|
+
if ((im->in.w == im->out.w) && (im->in.h == im->out.h)) return 1;
|
1125
|
+
if (im->scaled) return 1;
|
1126
|
+
|
1127
|
+
im->scaled = 1;
|
1128
|
+
w = im->out.w;
|
1129
|
+
h = im->out.h;
|
1130
|
+
a = im->out.x;
|
1131
|
+
b = im->out.y;
|
1132
|
+
|
1133
|
+
for (y = 0; y < h; y++)
|
1134
|
+
im->lines[y] = im->pixels + ((y+b) * im->in.jinfo.output_components * im->in.jinfo.output_width) + (a * im->in.jinfo.output_components);
|
1135
|
+
|
1136
|
+
return 0;
|
1137
|
+
}
|
1138
|
+
|
1139
|
+
struct epeg_destination_mgr
|
1140
|
+
{
|
1141
|
+
struct jpeg_destination_mgr dst_mgr;
|
1142
|
+
Epeg_Image *im;
|
1143
|
+
unsigned char *buf;
|
1144
|
+
};
|
1145
|
+
|
1146
|
+
/* Get an existing tag, or create one if it doesn't exist */
|
1147
|
+
static ExifEntry *init_tag(ExifData *exif, ExifIfd ifd, ExifTag tag)
|
1148
|
+
{
|
1149
|
+
ExifEntry *entry = exif_content_get_entry (exif->ifd[ifd], tag);
|
1150
|
+
if (entry)
|
1151
|
+
return entry;
|
1152
|
+
entry = exif_entry_new ();
|
1153
|
+
if (entry) {
|
1154
|
+
exif_content_add_entry (exif->ifd[ifd], entry);
|
1155
|
+
exif_entry_initialize (entry, tag);
|
1156
|
+
}
|
1157
|
+
return entry;
|
1158
|
+
}
|
1159
|
+
|
1160
|
+
static int
|
1161
|
+
_epeg_encode(Epeg_Image *im)
|
1162
|
+
{
|
1163
|
+
struct epeg_destination_mgr *dst_mgr = NULL;
|
1164
|
+
int ok = 0;
|
1165
|
+
|
1166
|
+
if ((im->out.w < 1) || (im->out.h < 1)) return 1;
|
1167
|
+
if (im->out.f) return 1;
|
1168
|
+
|
1169
|
+
if (im->out.file)
|
1170
|
+
{
|
1171
|
+
im->out.f = fopen(im->out.file, "wb");
|
1172
|
+
if (!im->out.f)
|
1173
|
+
{
|
1174
|
+
im->error = 1;
|
1175
|
+
return 1;
|
1176
|
+
}
|
1177
|
+
}
|
1178
|
+
else
|
1179
|
+
im->out.f = NULL;
|
1180
|
+
|
1181
|
+
im->out.jinfo.err = jpeg_std_error(&(im->jerr.pub));
|
1182
|
+
im->jerr.pub.error_exit = _epeg_fatal_error_handler;
|
1183
|
+
#ifdef NOWARNINGS
|
1184
|
+
im->jerr.pub.emit_message = _emit_message;
|
1185
|
+
im->jerr.pub.output_message = _output_message;
|
1186
|
+
im->jerr.pub.format_message = _format_message;
|
1187
|
+
#endif
|
1188
|
+
|
1189
|
+
if (setjmp(im->jerr.setjmp_buffer))
|
1190
|
+
{
|
1191
|
+
ok = 1;
|
1192
|
+
im->error = 1;
|
1193
|
+
goto done;
|
1194
|
+
}
|
1195
|
+
|
1196
|
+
jpeg_create_compress(&(im->out.jinfo));
|
1197
|
+
if (im->out.f)
|
1198
|
+
jpeg_stdio_dest(&(im->out.jinfo), im->out.f);
|
1199
|
+
else
|
1200
|
+
{
|
1201
|
+
*(im->out.mem.data) = NULL;
|
1202
|
+
*(im->out.mem.size) = 0;
|
1203
|
+
/* Setup RAM destination manager */
|
1204
|
+
dst_mgr = calloc(1, sizeof(struct epeg_destination_mgr));
|
1205
|
+
if (!dst_mgr) return 1;
|
1206
|
+
dst_mgr->dst_mgr.init_destination = _jpeg_init_destination;
|
1207
|
+
dst_mgr->dst_mgr.empty_output_buffer = _jpeg_empty_output_buffer;
|
1208
|
+
dst_mgr->dst_mgr.term_destination = _jpeg_term_destination;
|
1209
|
+
dst_mgr->im = im;
|
1210
|
+
dst_mgr->buf = malloc(65536);
|
1211
|
+
if (!dst_mgr->buf)
|
1212
|
+
{
|
1213
|
+
ok = 1;
|
1214
|
+
im->error = 1;
|
1215
|
+
goto done;
|
1216
|
+
}
|
1217
|
+
im->out.jinfo.dest = (struct jpeg_destination_mgr *)dst_mgr;
|
1218
|
+
}
|
1219
|
+
im->out.jinfo.image_width = im->out.w;
|
1220
|
+
im->out.jinfo.image_height = im->out.h;
|
1221
|
+
im->out.jinfo.input_components = im->in.jinfo.output_components;
|
1222
|
+
im->out.jinfo.in_color_space = im->in.jinfo.out_color_space;
|
1223
|
+
im->out.jinfo.dct_method = im->in.jinfo.dct_method;
|
1224
|
+
jpeg_set_defaults(&(im->out.jinfo));
|
1225
|
+
jpeg_set_quality(&(im->out.jinfo), im->out.quality, TRUE);
|
1226
|
+
|
1227
|
+
if (im->out.quality >= 90)
|
1228
|
+
{
|
1229
|
+
im->out.jinfo.comp_info[0].h_samp_factor = 1;
|
1230
|
+
im->out.jinfo.comp_info[0].v_samp_factor = 1;
|
1231
|
+
im->out.jinfo.comp_info[1].h_samp_factor = 1;
|
1232
|
+
im->out.jinfo.comp_info[1].v_samp_factor = 1;
|
1233
|
+
im->out.jinfo.comp_info[2].h_samp_factor = 1;
|
1234
|
+
im->out.jinfo.comp_info[2].v_samp_factor = 1;
|
1235
|
+
}
|
1236
|
+
jpeg_start_compress(&(im->out.jinfo), TRUE);
|
1237
|
+
|
1238
|
+
/* Set the image options for Exif */
|
1239
|
+
ExifData *exif = exif_data_new();
|
1240
|
+
exif_data_set_option(exif, EXIF_DATA_OPTION_FOLLOW_SPECIFICATION);
|
1241
|
+
exif_data_set_byte_order(exif, exif_byte_order);
|
1242
|
+
exif_data_fix(exif);
|
1243
|
+
/* Add Exif Orientation tag */
|
1244
|
+
if (im->in.orientation != 0) {
|
1245
|
+
ExifEntry *entry = init_tag(exif, EXIF_IFD_0, EXIF_TAG_ORIENTATION);
|
1246
|
+
exif_set_short(entry->data, exif_byte_order, im->in.orientation);
|
1247
|
+
exif_entry_unref(entry);
|
1248
|
+
}
|
1249
|
+
/* Write Exif data to output jpeg file */
|
1250
|
+
unsigned char *exif_data;
|
1251
|
+
unsigned int exif_data_len;
|
1252
|
+
exif_data_save_data(exif, &exif_data, &exif_data_len);
|
1253
|
+
jpeg_write_marker(&(im->out.jinfo), JPEG_APP0 + 1, exif_data, exif_data_len);
|
1254
|
+
exif_data_unref(exif);
|
1255
|
+
|
1256
|
+
/* Output comment if there is one */
|
1257
|
+
if (im->out.comment && *im->out.comment)
|
1258
|
+
jpeg_write_marker(&(im->out.jinfo), JPEG_COM, im->out.comment, strlen(im->out.comment));
|
1259
|
+
|
1260
|
+
/* Output thumbnail info in APP7 */
|
1261
|
+
if (im->out.thumbnail_info)
|
1262
|
+
{
|
1263
|
+
char buf[8192];
|
1264
|
+
|
1265
|
+
if (im->in.file)
|
1266
|
+
{
|
1267
|
+
snprintf(buf, sizeof(buf), "Thumb::URI\nfile://%s", im->in.file);
|
1268
|
+
jpeg_write_marker(&(im->out.jinfo), JPEG_APP0 + 7, buf, strlen(buf));
|
1269
|
+
snprintf(buf, sizeof(buf), "Thumb::MTime\n%llu", (unsigned long long int)im->stat_info.st_mtime);
|
1270
|
+
}
|
1271
|
+
jpeg_write_marker(&(im->out.jinfo), JPEG_APP0 + 7, buf, strlen(buf));
|
1272
|
+
snprintf(buf, sizeof(buf), "Thumb::Image::Width\n%i", im->in.w);
|
1273
|
+
jpeg_write_marker(&(im->out.jinfo), JPEG_APP0 + 7, buf, strlen(buf));
|
1274
|
+
snprintf(buf, sizeof(buf), "Thumb::Image::Height\n%i", im->in.h);
|
1275
|
+
jpeg_write_marker(&(im->out.jinfo), JPEG_APP0 + 7, buf, strlen(buf));
|
1276
|
+
snprintf(buf, sizeof(buf), "Thumb::Mimetype\nimage/jpeg");
|
1277
|
+
jpeg_write_marker(&(im->out.jinfo), JPEG_APP0 + 7, buf, strlen(buf));
|
1278
|
+
}
|
1279
|
+
|
1280
|
+
while (im->out.jinfo.next_scanline < im->out.h)
|
1281
|
+
jpeg_write_scanlines(&(im->out.jinfo), &(im->lines[im->out.jinfo.next_scanline]), 1);
|
1282
|
+
jpeg_finish_compress(&(im->out.jinfo));
|
1283
|
+
|
1284
|
+
done:
|
1285
|
+
if ((im->in.f) || (im->in.mem.data != NULL)) jpeg_destroy_decompress(&(im->in.jinfo));
|
1286
|
+
if ((im->in.f) && (im->in.file)) fclose(im->in.f);
|
1287
|
+
if (dst_mgr)
|
1288
|
+
{
|
1289
|
+
if (dst_mgr->buf) free(dst_mgr->buf);
|
1290
|
+
free(dst_mgr);
|
1291
|
+
im->out.jinfo.dest = NULL;
|
1292
|
+
}
|
1293
|
+
jpeg_destroy_compress(&(im->out.jinfo));
|
1294
|
+
if ((im->out.f) && (im->out.file)) fclose(im->out.f);
|
1295
|
+
im->in.f = NULL;
|
1296
|
+
im->out.f = NULL;
|
1297
|
+
|
1298
|
+
return ok;
|
1299
|
+
}
|
1300
|
+
|
1301
|
+
static void
|
1302
|
+
_epeg_fatal_error_handler(j_common_ptr cinfo)
|
1303
|
+
{
|
1304
|
+
emptr errmgr;
|
1305
|
+
|
1306
|
+
errmgr = (emptr)cinfo->err;
|
1307
|
+
longjmp(errmgr->setjmp_buffer, 1);
|
1308
|
+
return;
|
1309
|
+
}
|
1310
|
+
|
1311
|
+
/* Source manager methods */
|
1312
|
+
METHODDEF(void)
|
1313
|
+
_jpeg_decompress_error_exit(j_common_ptr cinfo)
|
1314
|
+
{
|
1315
|
+
}
|
1316
|
+
|
1317
|
+
|
1318
|
+
METHODDEF(void)
|
1319
|
+
_jpeg_init_source(j_decompress_ptr cinfo)
|
1320
|
+
{
|
1321
|
+
}
|
1322
|
+
|
1323
|
+
METHODDEF(boolean)
|
1324
|
+
_jpeg_fill_input_buffer(j_decompress_ptr cinfo)
|
1325
|
+
{
|
1326
|
+
WARNMS(cinfo, JWRN_JPEG_EOF);
|
1327
|
+
|
1328
|
+
/* Insert a fake EOI marker */
|
1329
|
+
cinfo->src->next_input_byte = fake_EOI;
|
1330
|
+
cinfo->src->bytes_in_buffer = sizeof(fake_EOI);
|
1331
|
+
return TRUE;
|
1332
|
+
}
|
1333
|
+
|
1334
|
+
|
1335
|
+
METHODDEF(void)
|
1336
|
+
_jpeg_skip_input_data(j_decompress_ptr cinfo, long num_bytes)
|
1337
|
+
{
|
1338
|
+
if (num_bytes > (long)(cinfo)->src->bytes_in_buffer)
|
1339
|
+
ERREXIT(cinfo, 0);
|
1340
|
+
|
1341
|
+
(cinfo)->src->next_input_byte += num_bytes;
|
1342
|
+
(cinfo)->src->bytes_in_buffer -= num_bytes;
|
1343
|
+
}
|
1344
|
+
|
1345
|
+
METHODDEF(void)
|
1346
|
+
_jpeg_term_source(j_decompress_ptr cinfo)
|
1347
|
+
{
|
1348
|
+
}
|
1349
|
+
|
1350
|
+
|
1351
|
+
/* destination manager methods */
|
1352
|
+
METHODDEF(void)
|
1353
|
+
_jpeg_init_destination(j_compress_ptr cinfo)
|
1354
|
+
{
|
1355
|
+
struct epeg_destination_mgr *dst_mgr;
|
1356
|
+
|
1357
|
+
dst_mgr = (struct epeg_destination_mgr *)cinfo->dest;
|
1358
|
+
dst_mgr->dst_mgr.free_in_buffer = 65536;
|
1359
|
+
dst_mgr->dst_mgr.next_output_byte = (JOCTET *)dst_mgr->buf;
|
1360
|
+
}
|
1361
|
+
|
1362
|
+
METHODDEF(boolean)
|
1363
|
+
_jpeg_empty_output_buffer(j_compress_ptr cinfo)
|
1364
|
+
{
|
1365
|
+
struct epeg_destination_mgr *dst_mgr;
|
1366
|
+
unsigned char *p;
|
1367
|
+
int psize;
|
1368
|
+
|
1369
|
+
dst_mgr = (struct epeg_destination_mgr *)cinfo->dest;
|
1370
|
+
psize = *(dst_mgr->im->out.mem.size);
|
1371
|
+
*(dst_mgr->im->out.mem.size) += 65536;
|
1372
|
+
p = realloc(*(dst_mgr->im->out.mem.data), *(dst_mgr->im->out.mem.size));
|
1373
|
+
if (p)
|
1374
|
+
{
|
1375
|
+
*(dst_mgr->im->out.mem.data) = p;
|
1376
|
+
memcpy(p + psize, dst_mgr->buf, 65536);
|
1377
|
+
dst_mgr->dst_mgr.free_in_buffer = 65536;
|
1378
|
+
dst_mgr->dst_mgr.next_output_byte = (JOCTET *)dst_mgr->buf;
|
1379
|
+
}
|
1380
|
+
else
|
1381
|
+
return FALSE;
|
1382
|
+
return TRUE;
|
1383
|
+
}
|
1384
|
+
|
1385
|
+
METHODDEF(void)
|
1386
|
+
_jpeg_term_destination(j_compress_ptr cinfo)
|
1387
|
+
{
|
1388
|
+
struct epeg_destination_mgr *dst_mgr;
|
1389
|
+
unsigned char *p;
|
1390
|
+
int psize;
|
1391
|
+
|
1392
|
+
dst_mgr = (struct epeg_destination_mgr *)cinfo->dest;
|
1393
|
+
psize = *(dst_mgr->im->out.mem.size);
|
1394
|
+
*(dst_mgr->im->out.mem.size) += 65536 - dst_mgr->dst_mgr.free_in_buffer;
|
1395
|
+
p = realloc(*(dst_mgr->im->out.mem.data), *(dst_mgr->im->out.mem.size));
|
1396
|
+
if (p)
|
1397
|
+
{
|
1398
|
+
*(dst_mgr->im->out.mem.data) = p;
|
1399
|
+
memcpy(p + psize, dst_mgr->buf, 65536 - dst_mgr->dst_mgr.free_in_buffer);
|
1400
|
+
}
|
1401
|
+
}
|
1402
|
+
|
1403
|
+
/* be noisy - not */
|
1404
|
+
METHODDEF(void)
|
1405
|
+
_emit_message(j_common_ptr cinfo, int msg_level)
|
1406
|
+
{
|
1407
|
+
}
|
1408
|
+
|
1409
|
+
METHODDEF(void)
|
1410
|
+
_output_message(j_common_ptr cinfo)
|
1411
|
+
{
|
1412
|
+
}
|
1413
|
+
|
1414
|
+
METHODDEF(void)
|
1415
|
+
_format_message(j_common_ptr cinfo, char * buffer)
|
1416
|
+
{
|
1417
|
+
}
|
1418
|
+
|