18
#include "emile_private.h"
24
#define IMG_MAX_SIZE 65000
26
#define IMG_TOO_BIG(w, h) \
27
((((unsigned long long)w) * ((unsigned long long)h)) >= \
28
((1ULL << (29 * (sizeof(void *) / 4))) - 2048))
30
#define SPANS_COMMON(x1, w1, x2, w2) \
31
(!(((int)((x2) + (int)(w2)) <= (int)(x1)) || (int)((x2) >= (int)((x1) + (int)(w1)))))
33
#define RECTS_INTERSECT(x, y, w, h, xx, yy, ww, hh) \
34
((SPANS_COMMON((x), (w), (xx), (ww))) && (SPANS_COMMON((y), (h), (yy), (hh))))
36
#define RECTS_CLIP_TO_RECT(_x, _y, _w, _h, _cx, _cy, _cw, _ch) \
38
if (RECTS_INTERSECT(_x, _y, _w, _h, _cx, _cy, _cw, _ch)) \
40
if ((int)_x < (int)(_cx)) \
42
if ((int)_w + ((int)_x - (int)(_cx)) < 0) _w = 0; \
43
else _w += ((int)_x - (int)(_cx)); \
46
if ((int)(_x + _w) > (int)((_cx) + (_cw))) \
47
_w = (_cx) + (_cw) - _x; \
48
if ((int)_y < (int)(_cy)) \
50
if ((int)_h + ((int)_y - (int)(_cy)) < 0) _h = 0; \
51
else _h += ((int)_y - (int)(_cy)); \
54
if ((int)(_y + _h) > (int)((_cy) + (_ch))) \
55
_h = (_cy) + (_ch) - _y; \
63
#ifndef WORDS_BIGENDIAN
65
#define A_VAL(p) (((uint8_t *)(p))[3])
66
#define R_VAL(p) (((uint8_t *)(p))[2])
67
#define G_VAL(p) (((uint8_t *)(p))[1])
68
#define B_VAL(p) (((uint8_t *)(p))[0])
71
#define A_VAL(p) (((uint8_t *)(p))[0])
72
#define R_VAL(p) (((uint8_t *)(p))[1])
73
#define G_VAL(p) (((uint8_t *)(p))[2])
74
#define B_VAL(p) (((uint8_t *)(p))[3])
77
#define ARGB_JOIN(a, r, g, b) \
78
(((a) << 24) + ((r) << 16) + ((g) << 8) + (b))
80
#define OFFSET_BLOCK_SIZE 4
81
#define OFFSET_ALGORITHM 5
82
#define OFFSET_OPTIONS 6
84
#define OFFSET_HEIGHT 12
85
#define OFFSET_BLOCKS 16
89
Emile_Image_Load_Opts opts;
97
Eina_Rectangle region;
105
const unsigned char *source_data;
107
Eina_Bool (*bind)(Emile_Image *image, Emile_Image_Load_Opts *opts, Emile_Image_Animated *animated, Emile_Image_Load_Error *error);
108
Eina_Bool (*head)(Emile_Image *image, Emile_Image_Property *prop, unsigned int property_size, Emile_Image_Load_Error *error);
109
Eina_Bool (*data)(Emile_Image *image, Emile_Image_Property *prop, unsigned int property_size, void *pixels, Emile_Image_Load_Error *error);
110
void (*close)(Emile_Image *image);
112
Emile_Action_Cb cancelled;
113
const void *cancelled_data;
115
Emile_Colorspace cspace;
117
Eina_Bool bin_source : 1;
120
Eina_Bool unpremul : 1;
121
Eina_Bool compress : 1;
122
Eina_Bool blockless : 1;
123
Eina_Bool load_opts : 1;
126
static inline Eina_Bool
127
_emile_image_cancelled_is(Emile_Image *image)
129
if (!image->cancelled) return EINA_FALSE;
130
return image->cancelled((void*) image->cancelled_data, image, EMILE_ACTION_CANCELLED);
133
#define EMILE_IMAGE_TASK_CHECK(Image, Count, Mask, Error, Error_Handler) \
136
if ((Count & Mask) == Mask) \
139
if (_emile_image_cancelled_is(Image)) \
141
*Error = EMILE_IMAGE_LOAD_ERROR_CANCELLED; \
142
goto Error_Handler; \
147
static const unsigned char *
148
_emile_image_file_source_map(Emile_Image *image, unsigned int *length)
153
if (image->bin_source)
156
*length = eina_binbuf_length_get(image->source.bin);
157
return eina_binbuf_string_get(image->source.bin);
161
*length = eina_file_size_get(image->source.f);
162
if (!image->source_data)
164
image->source_data = eina_file_map_all(image->source.f, EINA_FILE_SEQUENTIAL);
166
return image->source_data;
170
_emile_image_file_source_unmap(Emile_Image *image)
172
if (!(image && image->source_data))
174
eina_file_map_free(image->source.f, (void *)image->source_data);
175
image->source_data = NULL;
179
_roundup(int val, int rup)
181
if (val >= 0 && rup > 0)
182
return (val + rup - 1) - ((val + rup - 1) % rup);
188
static const Emile_Colorspace cspaces_etc1[] = {
189
EMILE_COLORSPACE_ETC1,
190
EMILE_COLORSPACE_RGB8_ETC2,
191
EMILE_COLORSPACE_ARGB8888
194
static const Emile_Colorspace cspaces_rgb8_etc2[] = {
195
EMILE_COLORSPACE_RGB8_ETC2,
196
EMILE_COLORSPACE_ARGB8888
199
static const Emile_Colorspace cspaces_rgba8_etc2_eac[] = {
200
EMILE_COLORSPACE_RGBA8_ETC2_EAC,
201
EMILE_COLORSPACE_ARGB8888
204
static const Emile_Colorspace cspaces_etc1_alpha[] = {
205
EMILE_COLORSPACE_ETC1_ALPHA,
206
EMILE_COLORSPACE_ARGB8888
209
static const Emile_Colorspace cspaces_gry[] = {
210
EMILE_COLORSPACE_GRY8,
211
EMILE_COLORSPACE_AGRY88,
212
EMILE_COLORSPACE_ARGB8888
215
/**************************************************************
216
* The TGV file format is oriented around compression mecanism
217
* that hardware are good at decompressing. We do still provide
218
* a fully software implementation in case your hardware doesn't
219
* handle it. As OpenGL is pretty bad at handling border of
220
* texture, we do duplicate the first pixels of every border.
222
* This file format is designed to compress/decompress things
223
* in block area. Giving opportunity to store really huge file
224
* and only decompress/compress them as we need. Note that region
225
* only work with software decompression as we don't have a sane
226
* way to duplicate border to avoid artifact when scaling texture.
228
* The file format is as follow :
229
* - char magic[4]: "TGV1"
230
* - uint8_t block_size (real block size = (4 << bits[0-3], 4 << bits[4-7])
231
* - uint8_t algorithm (0 -> ETC1, 1 -> ETC2 RGB, 2 -> ETC2 RGBA, 3 -> ETC1+Alpha)
232
* - uint8_t options[2] (bitmask: 1 -> lz4, 2 for block-less, 4 -> unpremultiplied)
236
* - 0 length encoded compress size (if length == 64 * block_size => no compression)
237
* - lzma encoded etc1 block
239
* If the format is ETC1+Alpha (algo = 3), then a second image is encoded
240
* in ETC1 right after the first one, and it contains grey-scale alpha
242
**************************************************************/
244
// FIXME: wondering if we should support mipmap
245
// TODO: support ETC1+ETC2 images (RGB only)
248
_emile_tgv_bind(Emile_Image *image,
249
Emile_Image_Load_Opts *opts EINA_UNUSED,
250
Emile_Image_Animated *animated EINA_UNUSED,
251
Emile_Image_Load_Error *error)
253
const unsigned char *m;
256
m = _emile_image_file_source_map(image, &length);
260
/* Fast check for file characteristic (useful when trying to guess
261
a file format without extention). */
262
if (length < 16 || strncmp((const char *)m, "TGV1", 4) != 0)
265
if ((m[OFFSET_ALGORITHM] & 0xFF) > 3)
271
*error = EMILE_IMAGE_LOAD_ERROR_UNKNOWN_FORMAT;
272
_emile_image_file_source_unmap(image);
277
_emile_tgv_head(Emile_Image *image,
278
Emile_Image_Property *prop,
279
unsigned int property_size,
280
Emile_Image_Load_Error *error)
282
const unsigned char *m;
285
m = _emile_image_file_source_map(image, &length);
289
/* This can be used for later ABI change of the structure. */
290
if (sizeof(Emile_Image_Property) != property_size)
293
switch (m[OFFSET_ALGORITHM] & 0xFF)
296
prop->cspaces = cspaces_etc1;
297
prop->alpha = EINA_FALSE;
298
image->cspace = EMILE_COLORSPACE_ETC1;
302
prop->cspaces = cspaces_rgb8_etc2;
303
prop->alpha = EINA_FALSE;
304
image->cspace = EMILE_COLORSPACE_RGB8_ETC2;
308
prop->cspaces = cspaces_rgba8_etc2_eac;
309
prop->alpha = EINA_TRUE;
310
image->cspace = EMILE_COLORSPACE_RGBA8_ETC2_EAC;
314
prop->cspaces = cspaces_etc1_alpha;
315
prop->alpha = EINA_TRUE;
316
prop->premul = !!(m[OFFSET_OPTIONS] & 0x4);
317
image->unpremul = prop->premul;
318
image->cspace = EMILE_COLORSPACE_ETC1_ALPHA;
322
*error = EMILE_IMAGE_LOAD_ERROR_CORRUPT_FILE;
326
image->compress = m[OFFSET_OPTIONS] & 0x1;
327
image->blockless = (m[OFFSET_OPTIONS] & 0x2) != 0;
329
image->size.width = eina_ntohl(*((unsigned int *)&(m[OFFSET_WIDTH])));
330
image->size.height = eina_ntohl(*((unsigned int *)&(m[OFFSET_HEIGHT])));
332
if (image->blockless)
334
image->block.width = _roundup(image->size.width + 2, 4);
335
image->block.height = _roundup(image->size.height + 2, 4);
339
image->block.width = 4 << (m[OFFSET_BLOCK_SIZE] & 0x0f);
340
image->block.height = 4 << ((m[OFFSET_BLOCK_SIZE] & 0xf0) >> 4);
343
EINA_RECTANGLE_SET(&image->region,
345
image->size.width, image->size.height);
346
if (image->load_opts &&
347
((image->opts.region.w > 0) && (image->opts.region.h > 0) &&
348
(image->opts.region.w != (int) image->size.width) &&
349
(image->opts.region.h != (int) image->size.height)))
351
/* ETC colorspace doesn't work with region for now */
352
prop->cspaces = NULL;
354
if (!eina_rectangle_intersection(&image->region, &image->opts.region))
356
*error = EMILE_IMAGE_LOAD_ERROR_GENERIC;
361
prop->w = image->size.width;
362
prop->h = image->size.height;
365
prop->borders.r = _roundup(prop->w + 2, 4) - prop->w - 1;
366
prop->borders.b = _roundup(prop->h + 2, 4) - prop->h - 1;
371
static inline unsigned int
372
_tgv_length_get(const unsigned char *m,
374
unsigned int *offset)
377
unsigned int shift = 0;
379
while (*offset < length && ((*m) & 0x80))
381
r = r | (((*m) & 0x7F) << shift);
386
if (*offset < length)
388
r = r | (((*m) & 0x7F) << shift);
396
_emile_tgv_data(Emile_Image *image,
397
Emile_Image_Property *prop,
398
unsigned int property_size,
400
Emile_Image_Load_Error *error)
402
const unsigned char *m;
403
unsigned int *p = pixels;
404
unsigned char *p_etc = pixels;
405
Eina_Binbuf *buffer = NULL;
406
Eina_Rectangle master;
407
unsigned int block_length;
408
unsigned int length, offset;
410
unsigned int block_count;
411
unsigned int etc_width = 0;
412
unsigned int etc_block_size;
413
int num_planes = 1, plane, alpha_offset = 0;
414
Eina_Bool r = EINA_FALSE;
416
m = _emile_image_file_source_map(image, &length);
419
*error = EMILE_IMAGE_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
423
if (sizeof(Emile_Image_Property) != property_size)
426
offset = OFFSET_BLOCKS;
428
*error = EMILE_IMAGE_LOAD_ERROR_CORRUPT_FILE;
430
/* By definition, prop{.w, .h} == region{.w, .h} */
431
EINA_RECTANGLE_SET(&master,
432
image->region.x, image->region.y,
435
switch (image->cspace)
437
case EMILE_COLORSPACE_ETC1:
438
case EMILE_COLORSPACE_RGB8_ETC2:
442
case EMILE_COLORSPACE_RGBA8_ETC2_EAC:
446
case EMILE_COLORSPACE_ETC1_ALPHA:
449
alpha_offset = ((prop->w + 2 + 3) / 4) * ((prop->h + 2 + 3) / 4) * 8 / sizeof(*p_etc);
455
etc_width = ((prop->w + 2 + 3) / 4) * etc_block_size;
457
switch (prop->cspace)
459
case EMILE_COLORSPACE_ETC1:
460
case EMILE_COLORSPACE_RGB8_ETC2:
461
case EMILE_COLORSPACE_RGBA8_ETC2_EAC:
462
case EMILE_COLORSPACE_ETC1_ALPHA:
463
if (master.x % 4 || master.y % 4)
464
// FIXME: Should we really abort here ? Seems like a late check for me
468
case EMILE_COLORSPACE_ARGB8888:
469
/* Offset to take duplicated pixels into account */
478
if (prop->cspace != EMILE_COLORSPACE_ARGB8888 &&
479
prop->cspace != image->cspace)
481
if (!((prop->cspace == EMILE_COLORSPACE_RGB8_ETC2) &&
482
(image->cspace == EMILE_COLORSPACE_ETC1)))
484
*error = EMILE_IMAGE_LOAD_ERROR_GENERIC;
487
/* else: ETC2 is compatible with ETC1 and is preferred */
490
/* Allocate space for each ETC block (8 or 16 bytes per 4 * 4 pixels group) */
491
block_count = image->block.width * image->block.height / (4 * 4);
493
buffer = eina_binbuf_manage_new(alloca(etc_block_size * block_count),
494
etc_block_size * block_count,
497
for (plane = 0; plane < num_planes; plane++)
498
for (y = 0; y < image->size.height + 2; y += image->block.height)
499
for (x = 0; x < image->size.width + 2; x += image->block.width)
501
Eina_Rectangle current;
502
Eina_Binbuf *data_start;
503
const unsigned char *it;
506
block_length = _tgv_length_get(m + offset, length, &offset);
508
if (block_length == 0)
510
*error = EMILE_IMAGE_LOAD_ERROR_CORRUPT_FILE;
514
data_start = eina_binbuf_manage_new(m + offset,
517
offset += block_length;
519
EINA_RECTANGLE_SET(¤t,
521
image->block.width, image->block.height);
523
if (!eina_rectangle_intersection(¤t, &master))
525
eina_binbuf_free(data_start);
531
if (!emile_expand(data_start, buffer, EMILE_LZ4HC))
533
eina_binbuf_free(data_start);
540
if (block_count * etc_block_size != block_length)
542
eina_binbuf_free(data_start);
546
it = eina_binbuf_string_get(buffer);
548
for (i = 0; i < image->block.height; i += 4)
549
for (j = 0; j < image->block.width; j += 4, it += etc_block_size)
551
Eina_Rectangle current_etc;
552
unsigned int temporary[4 * 4];
553
unsigned int offset_x, offset_y;
556
EINA_RECTANGLE_SET(¤t_etc, x + j, y + i, 4, 4);
558
if (!eina_rectangle_intersection(¤t_etc, ¤t))
561
switch (prop->cspace)
563
case EMILE_COLORSPACE_ARGB8888:
564
switch (image->cspace)
566
case EMILE_COLORSPACE_ETC1:
567
case EMILE_COLORSPACE_ETC1_ALPHA:
568
if (!rg_etc1_unpack_block(it, temporary, 0))
570
// TODO: Should we decode as RGB8_ETC2?
571
fprintf(stderr, "ETC1: Block starting at {%i, %i} is corrupted!\n", x + j, y + i);
576
case EMILE_COLORSPACE_RGB8_ETC2:
577
rg_etc2_rgb8_decode_block((uint8_t *)it, temporary);
580
case EMILE_COLORSPACE_RGBA8_ETC2_EAC:
581
rg_etc2_rgba8_decode_block((uint8_t *)it, temporary);
588
offset_x = current_etc.x - x - j;
589
offset_y = current_etc.y - y - i;
594
if (eina_cpu_features_get() & EINA_CPU_NEON)
596
uint32_t *dst = &p[current_etc.x - 1 + (current_etc.y - 1) * master.w];
597
uint32_t *src = &temporary[offset_x + offset_y * 4];
598
for (k = 0; k < current_etc.h; k++)
600
if (current_etc.w == 4)
601
vst1q_u32(dst, vld1q_u32(src));
602
else if (current_etc.w == 3)
604
vst1_u32(dst, vld1_u32(src));
605
*(dst + 2) = *(src + 2);
607
else if (current_etc.w == 2)
608
vst1_u32(dst, vld1_u32(src));
617
for (k = 0; k < current_etc.h; k++)
619
memcpy(&p[current_etc.x - 1 + (current_etc.y - 1 + k) * master.w],
620
&temporary[offset_x + (offset_y + k) * 4],
621
current_etc.w * sizeof(unsigned int));
626
for (k = 0; k < current_etc.h; k++)
627
for (l = 0; l < current_etc.w; l++)
629
unsigned int *rgbdata = &p[current_etc.x - 1 + (current_etc.y - 1 + k) * master.w + l];
630
unsigned int *adata = &temporary[offset_x + (offset_y + k) * 4 + l];
631
A_VAL(rgbdata) = G_VAL(adata);
636
case EMILE_COLORSPACE_ETC1:
637
case EMILE_COLORSPACE_RGB8_ETC2:
638
case EMILE_COLORSPACE_RGBA8_ETC2_EAC:
639
memcpy(&p_etc[(current_etc.x / 4) * etc_block_size + (current_etc.y / 4) * etc_width],
644
case EMILE_COLORSPACE_ETC1_ALPHA:
645
memcpy(&p_etc[(current_etc.x / 4) * etc_block_size + (current_etc.y / 4) * etc_width + plane * alpha_offset],
653
} /* bx,by inside blocks */
655
eina_binbuf_free(data_start);
656
} /* x,y macroblocks */
658
// TODO: Add support for more unpremultiplied modes (ETC2)
659
if (prop->cspace == EMILE_COLORSPACE_ARGB8888)
660
prop->premul = image->unpremul; /* call premul if unpremul data */
662
*error = EMILE_IMAGE_LOAD_ERROR_NONE;
666
if (image->compress) eina_binbuf_free(buffer);
667
_emile_image_file_source_unmap(image);
672
_emile_tgv_close(Emile_Image *image EINA_UNUSED)
674
/* TGV file loader doesn't keep any data allocated around */
679
typedef struct _JPEG_error_mgr *emptr;
680
struct _JPEG_error_mgr
682
struct jpeg_error_mgr pub;
683
jmp_buf setjmp_buffer;
686
struct jpeg_membuf_src
688
struct jpeg_source_mgr pub;
690
const unsigned char *buf;
692
struct jpeg_membuf_src *self;
696
_emile_image_jpeg_error_exit_cb(j_common_ptr cinfo)
698
char buffer[JMSG_LENGTH_MAX];
701
// Avoid message "Not a JPEG file: starts with 0x%02x 0x%02x"
702
if (cinfo->client_data || (cinfo->err->msg_code != JERR_NO_SOI))
704
(*cinfo->err->format_message)(cinfo, buffer);
707
errmgr = (emptr)cinfo->err;
708
longjmp(errmgr->setjmp_buffer, 1);
712
_emile_image_jpeg_emit_message_cb(j_common_ptr cinfo, int msg_level)
714
char buffer[JMSG_LENGTH_MAX];
715
struct jpeg_error_mgr *err;
720
if ((err->num_warnings == 0) || (err->trace_level >= 3))
722
(*cinfo->err->format_message)(cinfo, buffer);
729
if (err->trace_level >= msg_level)
731
(*cinfo->err->format_message)(cinfo, buffer);
738
_emile_image_jpeg_output_message_cb(j_common_ptr cinfo)
740
char buffer[JMSG_LENGTH_MAX];
742
(*cinfo->err->format_message)(cinfo, buffer);
747
_emile_jpeg_membuf_src_init(j_decompress_ptr cinfo EINA_UNUSED)
752
_emile_jpeg_membuf_src_fill(j_decompress_ptr cinfo)
754
static const JOCTET jpeg_eoi[2] = { 0xFF, JPEG_EOI };
755
struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
757
src->pub.bytes_in_buffer = sizeof(jpeg_eoi);
758
src->pub.next_input_byte = jpeg_eoi;
764
_emile_jpeg_membuf_src_skip(j_decompress_ptr cinfo, long num_bytes)
766
struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
768
if ((((long)src->pub.bytes_in_buffer - (long)src->len) > num_bytes) ||
769
((long)src->pub.bytes_in_buffer < num_bytes))
771
(*(cinfo)->err->error_exit)((j_common_ptr)(cinfo));
774
src->pub.bytes_in_buffer -= num_bytes;
775
src->pub.next_input_byte += num_bytes;
779
_emile_jpeg_membuf_src_term(j_decompress_ptr cinfo)
781
struct jpeg_membuf_src *src = (struct jpeg_membuf_src *)cinfo->src;
789
_emile_jpeg_membuf_src(j_decompress_ptr cinfo, const void *map, size_t length)
791
struct jpeg_membuf_src *src;
793
src = calloc(1, sizeof(*src));
799
cinfo->src = &src->pub;
802
src->pub.init_source = _emile_jpeg_membuf_src_init;
803
src->pub.fill_input_buffer = _emile_jpeg_membuf_src_fill;
804
src->pub.skip_input_data = _emile_jpeg_membuf_src_skip;
805
src->pub.resync_to_restart = jpeg_resync_to_restart;
806
src->pub.term_source = _emile_jpeg_membuf_src_term;
807
src->pub.bytes_in_buffer = src->len;
808
src->pub.next_input_byte = src->buf;
813
/*! Magic number for EXIF header, App0, App1*/
814
static const unsigned char ExifHeader[] = { 0x45, 0x78, 0x69, 0x66, 0x00, 0x00 };
815
static const unsigned char JfifHeader[] = { 0x4A, 0x46, 0x49, 0x46, 0x00 };
816
static const unsigned char JfxxHeader[] = { 0x4A, 0x46, 0x58, 0x58, 0x00 };
817
static const unsigned char App0[] = { 0xff, 0xe0 };
818
static const unsigned char App1[] = { 0xff, 0xe1 };
819
static const unsigned char II[] = { 0x49, 0x49 };
820
static const unsigned char MM[] = { 0x4d, 0x4d };
829
_get_next_app0(const unsigned char *map, size_t fsize, size_t *position)
831
unsigned short length = 0;
832
unsigned int w = 0, h = 0;
833
unsigned int format = 0;
834
unsigned int data_size = 0;
835
const unsigned char *app0_head, *p;
837
/* header_mark:2, length:2, identifier:5 version:2, unit:1, den=4 thum=2 */
838
if ((*position + 16) >= fsize)
840
app0_head = map + *position;
842
/* p is appn's start pointer excluding app0 marker */
845
length = ((*p << 8) + *(p + 1));
847
/* JFIF segment format */
848
if (!memcmp(p + 2, JfifHeader, sizeof(JfifHeader)))
854
else if (!memcmp(p + 2, JfxxHeader, sizeof(JfxxHeader)))
856
if (*(p + 7) == 0x11)
864
data_size = format * w * h;
866
if ((*position + 2 + length + data_size) > fsize)
869
*position = *position + 2 + length + data_size;
874
/* If app1 data is abnormal, returns EINA_FALSE.
875
If app1 data is normal, returns EINA_TRUE.
876
If app1 data is normal but not orientation data, orientation value is -1.
880
_get_orientation_app1(const unsigned char *map,
883
int *orientation_res,
886
const unsigned char *app1_head, *buf;
887
unsigned char orientation[2]; //orientation tag
888
ExifByteAlign byte_align;
889
unsigned int num_directory = 0;
890
unsigned int ifd_offset = 10; //IFD offset start at 10th byte (mark:2 + data_size:2 + exif:6)
893
unsigned int data_size = 0;
895
/* app1 mark:2, data_size:2, exif:6 tiff:8 */
896
if ((*position + 18) >= fsize)
898
app1_head = map + *position;
901
data_size = ((*(buf + 2) << 8) + *(buf + 3));
902
if ((*position + 2 + data_size) > fsize)
905
if (memcmp(buf + 4, ExifHeader, sizeof(ExifHeader)))
907
*position = *position + 2 + data_size;
908
*orientation_res = -1;
912
/* 2. get 10&11 byte get info of "II(0x4949)" or "MM(0x4d4d)" */
913
/* 3. get 14th - 17th byte get info for IFD offset */
914
/* 4. get directory entry IFD */
917
if (!memcmp(buf + 10, MM, sizeof(MM)))
919
// get 4byte by little endian
920
ifd_offset += (*(buf + 14) << 24) + (*(buf + 15) << 16) + (*(buf + 16) << 8) + (*(buf + 17));
922
if (ifd_offset > fsize)
925
byte_align = EXIF_BYTE_ALIGN_MM;
926
num_directory = ((*(buf + ifd_offset) << 8) + *(buf + ifd_offset + 1));
927
orientation[0] = 0x01;
928
orientation[1] = 0x12;
930
else if (!memcmp(buf + 10, II, sizeof(II)))
932
// get 4byte by big endian
933
ifd_offset += (*(buf + 14)) + (*(buf + 15) << 8) + (*(buf + 16) << 16) + (*(buf + 17) << 24);
935
if (ifd_offset > fsize)
938
byte_align = EXIF_BYTE_ALIGN_II;
939
num_directory = ((*(buf + ifd_offset + 1) << 8) + *(buf + ifd_offset));
940
orientation[0] = 0x12;
941
orientation[1] = 0x01;
946
/* check num_directory data */
947
if ((*position + (12 * num_directory + 20)) > fsize)
950
buf = app1_head + ifd_offset + 2; //next to 0th ifd (1st tag)
954
for (i = 0; i < num_directory; i++)
956
if (!memcmp(buf + j, orientation, 2))
958
/*get orientation tag */
959
if (byte_align == EXIF_BYTE_ALIGN_MM)
960
direction = *(buf + j + 9);
962
direction = *(buf + j + 8);
966
*orientation_res = 180;
967
*flipped = EINA_FALSE;
971
*orientation_res = 180;
972
*flipped = EINA_TRUE;
976
*orientation_res = 90;
977
*flipped = EINA_FALSE;
981
*orientation_res = 90;
982
*flipped = EINA_TRUE;
986
*orientation_res = 270;
987
*flipped = EINA_TRUE;
991
*orientation_res = 270;
992
*flipped = EINA_FALSE;
996
*orientation_res = 0;
997
*flipped = EINA_TRUE;
1001
*orientation_res = 0;
1002
*flipped = EINA_FALSE;
1013
_get_orientation(const void *map, size_t length, Eina_Bool *flipped)
1016
size_t position = 0;
1017
int orientation = -1;
1018
Eina_Bool res = EINA_FALSE;
1020
*flipped = EINA_FALSE;
1022
/* open file and get 22 byte frome file */
1025
/* 1. read 22byte */
1028
buf = (unsigned char *)map;
1031
/* 2. check 2,3 bypte with APP0(0xFFE0) or APP1(0xFFE1) */
1032
while ((length - position) > 0)
1034
if (!memcmp(buf + position, App0, sizeof(App0)))
1036
res = _get_next_app0(map, length, &position);
1040
else if (!memcmp(buf + position, App1, sizeof(App1)))
1042
res = _get_orientation_app1(map,
1049
if (orientation != -1)
1059
_rotate_region(unsigned int *r_x, unsigned int *r_y,
1060
unsigned int *r_w, unsigned int *r_h,
1061
unsigned int x, unsigned int y,
1062
unsigned int w, unsigned int h,
1063
unsigned int output_w, unsigned int output_h,
1064
int degree, Eina_Bool flipped)
1071
*r_x = output_w - (y + h);
1072
*r_y = output_h - (x + w);
1079
*r_y = output_h - (x + w);
1088
*r_y = output_h - (y + h);
1092
*r_x = output_w - (x + w);
1093
*r_y = output_h - (y + h);
1107
*r_x = output_w - (y + h);
1116
*r_x = output_w - (x + w);
1122
_rotate_180(uint32_t *data, int w, int h)
1129
p2 = data + (h * w) - 1;
1130
for (x = (w * h) / 2; --x >= 0; )
1141
_flip_horizontal(uint32_t *data, int w, int h)
1147
for (y = 0; y < h; y++)
1149
p1 = data + (y * w);
1150
p2 = data + ((y + 1) * w) - 1;
1151
for (x = 0; x < (w >> 1); x++)
1163
_flip_vertical(uint32_t *data, int w, int h)
1169
for (y = 0; y < (h >> 1); y++)
1171
p1 = data + (y * w);
1172
p2 = data + ((h - 1 - y) * w);
1173
for (x = 0; x < w; x++)
1185
_rotate_change_wh(uint32_t *to, uint32_t *from, int w, int h, int dx, int dy)
1189
for (x = h; --x >= 0; )
1191
for (y = w; --y >= 0; )
1202
_rotate8_180(uint8_t *data, int w, int h)
1209
p2 = data + (h * w) - 1;
1210
for (x = (w * h) / 2; --x >= 0; )
1221
_flip_horizontal8(uint8_t *data, int w, int h)
1227
for (y = 0; y < h; y++)
1229
p1 = data + (y * w);
1230
p2 = data + ((y + 1) * w) - 1;
1231
for (x = 0; x < (w >> 1); x++)
1243
_flip_vertical8(uint8_t *data, int w, int h)
1249
for (y = 0; y < (h >> 1); y++)
1251
p1 = data + (y * w);
1252
p2 = data + ((h - 1 - y) * w);
1253
for (x = 0; x < w; x++)
1265
_rotate_change_wh8(uint8_t *to, uint8_t *from, int w, int h, int dx, int dy)
1269
for (x = h; --x >= 0; )
1271
for (y = w; --y >= 0; )
1282
_rotate16_180(uint16_t *data, int w, int h)
1289
p2 = data + (h * w) - 1;
1290
for (x = (w * h) / 2; --x >= 0; )
1301
_flip_horizontal16(uint16_t *data, int w, int h)
1307
for (y = 0; y < h; y++)
1309
p1 = data + (y * w);
1310
p2 = data + ((y + 1) * w) - 1;
1311
for (x = 0; x < (w >> 1); x++)
1323
_flip_vertical16(uint16_t *data, int w, int h)
1329
for (y = 0; y < (h >> 1); y++)
1331
p1 = data + (y * w);
1332
p2 = data + ((h - 1 - y) * w);
1333
for (x = 0; x < w; x++)
1345
_rotate_change_wh16(uint16_t *to, uint16_t *from, int w, int h, int dx, int dy)
1349
for (x = h; --x >= 0; )
1351
for (y = w; --y >= 0; )
1362
_emile_jpeg_bind(Emile_Image *image EINA_UNUSED,
1363
Emile_Image_Load_Opts *opts EINA_UNUSED,
1364
Emile_Image_Animated *animated EINA_UNUSED,
1365
Emile_Image_Load_Error *error EINA_UNUSED)
1371
_emile_jpeg_head(Emile_Image *image,
1372
Emile_Image_Property *prop,
1373
unsigned int property_size,
1374
Emile_Image_Load_Error *error)
1376
volatile Emile_Image_Load_Opts *opts = (image->load_opts) ? &image->opts : NULL;
1377
const unsigned char *m;
1378
unsigned int scalew, scaleh;
1379
struct jpeg_decompress_struct cinfo;
1380
struct _JPEG_error_mgr jerr;
1381
unsigned int length;
1383
/* for rotation decoding */
1384
volatile int degree = 0;
1385
volatile Eina_Bool change_wh = EINA_FALSE;
1386
volatile unsigned int load_opts_w = 0, load_opts_h = 0;
1388
if (sizeof(Emile_Image_Property) != property_size)
1391
m = _emile_image_file_source_map(image, &length);
1395
memset(&cinfo, 0, sizeof(cinfo));
1396
cinfo.err = jpeg_std_error(&(jerr.pub));
1397
cinfo.client_data = NULL;
1398
jerr.pub.error_exit = _emile_image_jpeg_error_exit_cb;
1399
jerr.pub.emit_message = _emile_image_jpeg_emit_message_cb;
1400
jerr.pub.output_message = _emile_image_jpeg_output_message_cb;
1401
if (setjmp(jerr.setjmp_buffer))
1403
jpeg_destroy_decompress(&cinfo);
1404
_emile_jpeg_membuf_src_term(&cinfo);
1405
if (cinfo.saw_JFIF_marker)
1406
*error = EMILE_IMAGE_LOAD_ERROR_CORRUPT_FILE;
1408
*error = EMILE_IMAGE_LOAD_ERROR_UNKNOWN_FORMAT;
1411
jpeg_create_decompress(&cinfo);
1413
if (_emile_jpeg_membuf_src(&cinfo, m, length))
1415
jpeg_destroy_decompress(&cinfo);
1416
_emile_jpeg_membuf_src_term(&cinfo);
1417
*error = EMILE_IMAGE_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
1421
jpeg_read_header(&cinfo, TRUE);
1422
cinfo.do_fancy_upsampling = FALSE;
1423
cinfo.do_block_smoothing = FALSE;
1424
cinfo.dct_method = JDCT_ISLOW; /* JDCT_FLOAT JDCT_IFAST(quality loss) */
1425
cinfo.dither_mode = JDITHER_ORDERED;
1426
cinfo.buffered_image = TRUE; /* buffered mode in case jpg is progressive */
1427
jpeg_start_decompress(&cinfo);
1429
if (cinfo.jpeg_color_space == JCS_GRAYSCALE)
1431
/* We do handle GRY8 and AGRY88 (with FF for alpha) colorspace as an output for JPEG */
1432
prop->cspaces = cspaces_gry;
1435
/* rotation decoding */
1436
if (opts && opts->orientation)
1438
degree = _get_orientation(m, length, &prop->flipped);
1439
if (degree != 0 || prop->flipped)
1441
opts->degree = degree;
1442
prop->rotated = EINA_TRUE;
1444
if (degree == 90 || degree == 270)
1445
change_wh = EINA_TRUE;
1450
prop->w = cinfo.output_width;
1451
prop->h = cinfo.output_height;
1452
if ((prop->w < 1) ||
1454
(prop->w > IMG_MAX_SIZE) ||
1455
(prop->h > IMG_MAX_SIZE) ||
1456
(IMG_TOO_BIG(prop->w, prop->h)))
1458
jpeg_destroy_decompress(&cinfo);
1459
_emile_jpeg_membuf_src_term(&cinfo);
1460
if (IMG_TOO_BIG(prop->w, prop->h))
1461
*error = EMILE_IMAGE_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
1463
*error = EMILE_IMAGE_LOAD_ERROR_GENERIC;
1466
if (opts && opts->scale_down_by > 1)
1468
prop->w /= opts->scale_down_by;
1469
prop->h /= opts->scale_down_by;
1471
else if (opts && opts->dpi > 0.0)
1473
prop->w = (prop->w * opts->dpi) / 90.0;
1474
prop->h = (prop->h * opts->dpi) / 90.0;
1476
else if (opts && ((opts->w > 0) && (opts->h > 0)))
1478
unsigned int w2 = prop->w, h2 = prop->h;
1479
/* user set load_opts' w,h on the assumption
1480
that image already rotated according to it's orientation info */
1483
load_opts_w = opts->w;
1484
load_opts_h = opts->h;
1485
opts->w = load_opts_h;
1486
opts->h = load_opts_w;
1490
h2 = (opts->w * prop->h) / prop->w;
1495
w3 = (opts->h * prop->w) / prop->h;
1503
opts->w = load_opts_w;
1504
opts->h = load_opts_h;
1512
if ((prop->w != cinfo.output_width) || (prop->h != cinfo.output_height))
1514
scalew = cinfo.output_width / prop->w;
1515
scaleh = cinfo.output_height / prop->h;
1517
prop->scale = scalew;
1518
if (scaleh < scalew)
1519
prop->scale = scaleh;
1521
if (prop->scale > 8)
1523
else if (prop->scale < 1)
1526
if (prop->scale == 3)
1528
else if (prop->scale == 5)
1530
else if (prop->scale == 6)
1532
else if (prop->scale == 7)
1536
if (prop->scale > 1)
1538
jpeg_destroy_decompress(&cinfo);
1539
_emile_jpeg_membuf_src_term(&cinfo);
1540
jpeg_create_decompress(&cinfo);
1542
if (_emile_jpeg_membuf_src(&cinfo, m, length))
1544
jpeg_destroy_decompress(&cinfo);
1545
_emile_jpeg_membuf_src_term(&cinfo);
1546
*error = EMILE_IMAGE_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
1550
jpeg_read_header(&cinfo, TRUE);
1551
cinfo.do_fancy_upsampling = FALSE;
1552
cinfo.do_block_smoothing = FALSE;
1553
cinfo.scale_num = 1;
1554
cinfo.scale_denom = prop->scale;
1555
cinfo.buffered_image = TRUE; /* buffered mode in case jpg is progressive */
1556
jpeg_calc_output_dimensions(&(cinfo));
1557
jpeg_start_decompress(&cinfo);
1560
prop->w = cinfo.output_width;
1561
prop->h = cinfo.output_height;
1563
/* be nice and clip region to image. if its totally outside, fail load */
1564
if (opts && ((opts->region.w > 0) && (opts->region.h > 0)))
1566
unsigned int load_region_x = opts->region.x;
1567
unsigned int load_region_y = opts->region.y;
1568
unsigned int load_region_w = opts->region.w;
1569
unsigned int load_region_h = opts->region.h;
1572
_rotate_region(&load_region_x, &load_region_y,
1573
&load_region_w, &load_region_h,
1574
opts->region.x, opts->region.y,
1575
opts->region.w, opts->region.h,
1577
degree, prop->flipped);
1579
if (prop->scale > 1)
1581
load_region_x /= prop->scale;
1582
load_region_y /= prop->scale;
1583
load_region_w /= prop->scale;
1584
load_region_h /= prop->scale;
1586
RECTS_CLIP_TO_RECT(load_region_x, load_region_y,
1587
load_region_w, load_region_h,
1588
0, 0, prop->w, prop->h);
1589
if ((load_region_w == 0) || (load_region_h == 0))
1591
jpeg_destroy_decompress(&cinfo);
1592
_emile_jpeg_membuf_src_term(&cinfo);
1593
*error = EMILE_IMAGE_LOAD_ERROR_GENERIC;
1596
prop->w = load_region_w;
1597
prop->h = load_region_h;
1599
/* end head decoding */
1608
jpeg_destroy_decompress(&cinfo);
1609
_emile_jpeg_membuf_src_term(&cinfo);
1610
*error = EMILE_IMAGE_LOAD_ERROR_NONE;
1615
_jpeg_convert_copy(volatile uint32_t **dst, uint8_t **src, unsigned int w, Eina_Bool adobe_marker)
1617
uint32_t *ptr2 = (uint32_t*) *dst;
1618
uint8_t *ptr = *src;
1623
for (x = 0; x < w; x++)
1625
/* According to libjpeg doc, Photoshop inverse the values of C, M, Y and K, */
1626
/* that is C is replaces by 255 - C, etc... */
1627
/* See the comment below for the computation of RGB values from CMYK ones. */
1628
*ptr2 = (0xff000000) |
1629
((ptr[0] * ptr[3] / 255) << 16) |
1630
((ptr[1] * ptr[3] / 255) << 8) |
1631
((ptr[2] * ptr[3] / 255));
1638
for (x = 0; x < w; x++)
1640
/* Conversion from CMYK to RGB is done in 2 steps: */
1641
/* CMYK => CMY => RGB (see http://www.easyrgb.com/index.php?X=MATH) */
1642
/* after computation, if C, M, Y and K are between 0 and 1, we have: */
1643
/* R = (1 - C) * (1 - K) * 255 */
1644
/* G = (1 - M) * (1 - K) * 255 */
1645
/* B = (1 - Y) * (1 - K) * 255 */
1646
/* libjpeg stores CMYK values between 0 and 255, */
1647
/* so we replace C by C * 255 / 255, etc... and we obtain: */
1648
/* R = (255 - C) * (255 - K) / 255 */
1649
/* G = (255 - M) * (255 - K) / 255 */
1650
/* B = (255 - Y) * (255 - K) / 255 */
1651
/* with C, M, Y and K between 0 and 255. */
1652
*ptr2 = (0xff000000) |
1653
(((255 - ptr[0]) * (255 - ptr[3]) / 255) << 16) |
1654
(((255 - ptr[1]) * (255 - ptr[3]) / 255) << 8) |
1655
(((255 - ptr[2]) * (255 - ptr[3]) / 255));
1666
_jpeg_gry8_convert_copy(uint8_t **dst, uint8_t **src, unsigned int w)
1668
uint8_t *ptrg = (uint8_t*) *dst;
1669
uint8_t *ptr = *src;
1672
for (x = 0; x < w; x++)
1684
_jpeg_agry88_convert_copy(uint16_t **dst, uint8_t **src, unsigned int w)
1686
uint16_t *ptrag = (uint16_t*) *dst;
1687
uint8_t *ptr = *src;
1690
for (x = 0; x < w; x++)
1692
*ptrag = 0xFF00 | ptr[0];
1702
_jpeg_argb8888_convert_copy(volatile uint32_t **dst, uint8_t **src, unsigned int w)
1704
uint32_t *ptr2 = (uint32_t*) *dst;
1705
uint8_t *ptr = *src;
1708
for (x = 0; x < w; x++)
1710
*ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[0], ptr[0]);
1720
_jpeg_copy(volatile uint32_t **dst, uint8_t **src, unsigned int w)
1722
uint32_t *ptr2 = (uint32_t*) *dst;
1723
uint8_t *ptr = *src;
1726
for (x = 0; x < w; x++)
1728
*ptr2 = ARGB_JOIN(0xff, ptr[0], ptr[1], ptr[2]);
1738
_emile_jpeg_data(Emile_Image *image,
1739
Emile_Image_Property *prop,
1740
unsigned int property_size,
1742
Emile_Image_Load_Error *error)
1744
/* Handle RGB, ARGB, GRY and AGRY */
1745
volatile Emile_Image_Load_Opts *opts = (image->load_opts) ? &image->opts : NULL;
1747
struct jpeg_decompress_struct cinfo;
1748
struct _JPEG_error_mgr jerr;
1749
const unsigned char *m = NULL;
1750
uint8_t *ptr, *line[16], *data;
1751
volatile uint32_t *ptr2 = NULL;
1752
uint32_t *ptr_rotate = NULL;
1753
uint16_t *ptrag = NULL, *ptrag2 = NULL, *ptrag_rotate = NULL;
1754
uint8_t *ptrg = NULL, *ptrg2 = NULL, *ptrg_rotate = NULL;
1755
unsigned int y, l, i, scans;
1756
volatile int region = 0;
1757
/* rotation setting */
1758
unsigned int ie_w = 0, ie_h = 0;
1761
unsigned int x, y, w, h;
1762
} opts_region = {0, 0, 0, 0};
1763
volatile int degree = 0;
1764
volatile Eina_Bool change_wh = EINA_FALSE;
1765
volatile Eina_Bool line_done = EINA_FALSE;
1766
volatile Eina_Bool ptrg_free = EINA_FALSE;
1767
volatile Eina_Bool ptrag_free = EINA_FALSE;
1768
volatile Eina_Bool r = EINA_FALSE;
1769
unsigned int length;
1770
volatile unsigned short count = 0;
1772
if (sizeof(Emile_Image_Property) != property_size)
1775
m = _emile_image_file_source_map(image, &length);
1779
memset(&cinfo, 0, sizeof(cinfo));
1782
degree = opts ? opts->degree : 0;
1783
if (degree == 90 || degree == 270)
1784
change_wh = EINA_TRUE;
1787
cinfo.err = jpeg_std_error(&(jerr.pub));
1788
cinfo.client_data = (void *)(intptr_t) 0x1;
1789
jerr.pub.error_exit = _emile_image_jpeg_error_exit_cb;
1790
jerr.pub.emit_message = _emile_image_jpeg_emit_message_cb;
1791
jerr.pub.output_message = _emile_image_jpeg_output_message_cb;
1792
if (setjmp(jerr.setjmp_buffer))
1794
*error = EMILE_IMAGE_LOAD_ERROR_CORRUPT_FILE;
1797
jpeg_create_decompress(&cinfo);
1799
if (_emile_jpeg_membuf_src(&cinfo, m, length))
1801
*error = EMILE_IMAGE_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
1805
jpeg_read_header(&cinfo, TRUE);
1806
cinfo.do_fancy_upsampling = FALSE;
1807
cinfo.do_block_smoothing = FALSE;
1808
cinfo.dct_method = JDCT_ISLOW; /* JDCT_FLOAT JDCT_IFAST(quality loss) */
1809
cinfo.dither_mode = JDITHER_ORDERED;
1811
if (prop->scale > 1)
1813
cinfo.scale_num = 1;
1814
cinfo.scale_denom = prop->scale;
1817
/* Colorspace conversion options */
1818
/* libjpeg can do the following conversions: */
1819
/* GRAYSCLAE => RGB YCbCr => RGB and YCCK => CMYK */
1820
switch (cinfo.jpeg_color_space)
1826
if (prop->cspace == EMILE_COLORSPACE_GRY8 ||
1827
prop->cspace == EMILE_COLORSPACE_AGRY88)
1829
cinfo.out_color_space = JCS_GRAYSCALE;
1832
/* The caller doesn't handle GRAYSCALE,
1839
cinfo.out_color_space = JCS_RGB;
1844
cinfo.out_color_space = JCS_CMYK;
1848
cinfo.out_color_space = JCS_RGB;
1853
jpeg_calc_output_dimensions(&(cinfo));
1854
jpeg_start_decompress(&cinfo);
1856
w = cinfo.output_width;
1857
h = cinfo.output_height;
1870
if (opts && ((opts->region.w > 0) && (opts->region.h > 0)))
1874
/* scale value already applied when decompress.
1875
When access to decoded image, have to apply scale value to region value */
1876
if (prop->scale > 1)
1878
opts_region.x = opts->region.x / prop->scale;
1879
opts_region.y = opts->region.y / prop->scale;
1880
opts_region.w = opts->region.w / prop->scale;
1881
opts_region.h = opts->region.h / prop->scale;
1886
opts_region.x = opts->region.x;
1887
opts_region.y = opts->region.y;
1888
opts_region.w = opts->region.w;
1889
opts_region.h = opts->region.h;
1895
unsigned int load_region_x = 0, load_region_y = 0;
1896
unsigned int load_region_w = 0, load_region_h = 0;
1898
load_region_x = opts_region.x;
1899
load_region_y = opts_region.y;
1900
load_region_w = opts_region.w;
1901
load_region_h = opts_region.h;
1902
_rotate_region(&opts_region.x, &opts_region.y,
1903
&opts_region.w, &opts_region.h,
1904
load_region_x, load_region_y,
1905
load_region_w, load_region_h,
1906
w, h, degree, prop->flipped);
1910
if ((!region) && ((w != ie_w) || (h != ie_h)))
1912
*error = EMILE_IMAGE_LOAD_ERROR_GENERIC;
1915
if ((region) && ((ie_w != opts_region.w) || (ie_h != opts_region.h)))
1917
*error = EMILE_IMAGE_LOAD_ERROR_GENERIC;
1921
switch (prop->cspace)
1923
case EMILE_COLORSPACE_GRY8:
1924
case EMILE_COLORSPACE_AGRY88:
1925
if (!(cinfo.out_color_space == JCS_GRAYSCALE &&
1926
cinfo.output_components == 1))
1928
*error = EMILE_IMAGE_LOAD_ERROR_UNKNOWN_FORMAT;
1933
case EMILE_COLORSPACE_ARGB8888:
1934
if (!((cinfo.out_color_space == JCS_RGB && cinfo.output_components == 3) ||
1935
(cinfo.out_color_space == JCS_CMYK && cinfo.output_components == 4)))
1937
*error = EMILE_IMAGE_LOAD_ERROR_UNKNOWN_FORMAT;
1943
*error = EMILE_IMAGE_LOAD_ERROR_GENERIC;
1947
/* end head decoding */
1949
if (cinfo.rec_outbuf_height > 16)
1951
*error = EMILE_IMAGE_LOAD_ERROR_UNKNOWN_FORMAT;
1954
data = alloca(w * 16 * cinfo.output_components);
1955
if ((prop->rotated) && change_wh)
1957
if (prop->cspace == EMILE_COLORSPACE_GRY8)
1959
ptrg = malloc(w * h * sizeof(uint8_t));
1961
ptrg_free = EINA_TRUE;
1963
else if (prop->cspace == EMILE_COLORSPACE_AGRY88)
1965
ptrag = malloc(w * h * sizeof(uint16_t));
1966
ptrag_rotate = ptrag;
1967
ptrag_free = EINA_TRUE;
1971
ptr2 = malloc(w * h * sizeof(uint32_t));
1972
ptr_rotate = (uint32_t*) ptr2;
1982
if (!ptr2 && !ptrag && !ptrg)
1984
*error = EMILE_IMAGE_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED;
1988
/* We handle first CMYK (4 components) */
1989
if (cinfo.output_components == 4)
1991
// FIXME: handle region
1992
for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
1993
line[i] = data + (i * w * 4);
1994
for (l = 0; l < h; l += cinfo.rec_outbuf_height)
1996
// Check for continuing every 16 scanlines fetch
1997
EMILE_IMAGE_TASK_CHECK(image, count, 0xF, error, on_error);
1999
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
2000
scans = cinfo.rec_outbuf_height;
2001
if ((h - l) < scans)
2006
for (y = 0; y < scans; y++)
2008
_jpeg_convert_copy(&ptr2, &ptr, w, cinfo.saw_Adobe_marker);
2013
/* if line # > region last line, break */
2014
if (l >= (opts_region.y + opts_region.h))
2016
line_done = EINA_TRUE;
2017
/* if rotation flag is set , we have to rotate image */
2019
/*jpeg_destroy_decompress(&cinfo);
2020
_emile_jpeg_membuf_src_term(&cinfo);
2021
* error = EMILE_IMAGE_LOAD_ERROR_NONE;
2022
return EINA_FALSE; */
2024
/* else if scan block intersects region start or later */
2025
else if ((l + scans) > (opts_region.y))
2027
for (y = 0; y < scans; y++)
2029
if (((y + l) >= opts_region.y) && ((y + l) < (opts_region.y + opts_region.h)))
2031
ptr += opts_region.x;
2032
_jpeg_convert_copy(&ptr2, &ptr, opts_region.w, cinfo.saw_Adobe_marker);
2033
ptr += (4 * (w - (opts_region.x + opts_region.w)));
2042
/* We handle then RGB with 3 components */
2043
else if (cinfo.output_components == 3)
2050
printf("R| %p %5ix%5i %s: %5i %5i %5ix%5i - ",
2061
for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
2062
line[i] = data + (i * w * 3);
2063
for (l = 0; l < h; l += cinfo.rec_outbuf_height)
2065
// Check for continuing every 16 scanlines fetch
2066
EMILE_IMAGE_TASK_CHECK(image, count, 0xF, error, on_error);
2068
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
2069
scans = cinfo.rec_outbuf_height;
2070
if ((h - l) < scans)
2075
for (y = 0; y < scans; y++)
2077
_jpeg_copy(&ptr2, &ptr, w);
2082
/* if line # > region last line, break
2083
but not return immediately for rotation job */
2084
if (l >= (opts_region.y + opts_region.h))
2086
line_done = EINA_TRUE;
2087
/* if rotation flag is set , we have to rotate image */
2090
/* else if scan block intersects region start or later */
2091
else if ((l + scans) > (opts_region.y))
2093
for (y = 0; y < scans; y++)
2095
if (((y + l) >= opts_region.y) &&
2096
((y + l) < (opts_region.y + opts_region.h)))
2098
ptr += (3 * opts_region.x);
2099
_jpeg_copy(&ptr2, &ptr, opts_region.w);
2100
ptr += (3 * (w - (opts_region.x + opts_region.w)));
2110
printf("%3.3f\n", t);
2113
/* We finally handle RGB with 1 component */
2114
else if (cinfo.output_components == 1)
2118
for (i = 0; (int)i < cinfo.rec_outbuf_height; i++)
2119
line[i] = data + (i * w);
2120
for (l = 0; l < h; l += cinfo.rec_outbuf_height)
2122
// Check for continuing every 16 scanlines fetch
2123
EMILE_IMAGE_TASK_CHECK(image, count, 0xF, error, on_error);
2125
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
2126
scans = cinfo.rec_outbuf_height;
2127
if ((h - l) < scans)
2132
for (y = 0; y < scans; y++)
2134
switch (prop->cspace)
2136
case EMILE_COLORSPACE_GRY8:
2137
_jpeg_gry8_convert_copy(&ptrg2, &ptr, w);
2139
case EMILE_COLORSPACE_AGRY88:
2140
_jpeg_agry88_convert_copy(&ptrag2, &ptr, w);
2143
_jpeg_argb8888_convert_copy(&ptr2, &ptr, w);
2150
/* if line # > region last line, break */
2151
if (l >= (opts_region.y + opts_region.h))
2153
line_done = EINA_TRUE;
2154
/* if rotation flag is set , we have to rotate image */
2156
/*jpeg_destroy_decompress(&cinfo);
2157
_emile_jpeg_membuf_src_term(&cinfo);
2158
* error = EMILE_IMAGE_LOAD_ERROR_NONE;
2159
return EINA_TRUE; */
2161
/* else if scan block intersects region start or later */
2162
else if ((l + scans) > (opts_region.y))
2164
for (y = 0; y < scans; y++)
2166
if (((y + l) >= opts_region.y) &&
2167
((y + l) < (opts_region.y + opts_region.h)))
2169
ptr += opts_region.x;
2170
switch (prop->cspace)
2172
case EMILE_COLORSPACE_GRY8:
2173
_jpeg_gry8_convert_copy(&ptrg2, &ptr, opts_region.w);
2175
case EMILE_COLORSPACE_AGRY88:
2176
_jpeg_agry88_convert_copy(&ptrag2, &ptr, opts_region.w);
2179
_jpeg_argb8888_convert_copy(&ptr2, &ptr, opts_region.w);
2182
ptr += w - (opts_region.x + opts_region.w);
2191
/* if rotation operation need, rotate it */
2209
if (prop->cspace == EMILE_COLORSPACE_GRY8)
2212
_rotate_change_wh8(to8 + hw - 1, ptrg_rotate, ie_w, ie_h, hw - 1, -ie_h);
2214
_rotate_change_wh8(to8 + ie_h - 1, ptrg_rotate, ie_w, ie_h, -hw - 1, ie_h);
2216
else if (prop->cspace == EMILE_COLORSPACE_AGRY88)
2219
_rotate_change_wh16(to16 + hw - 1, ptrag_rotate, ie_w, ie_h, hw - 1, -ie_h);
2221
_rotate_change_wh16(to16 + ie_h - 1, ptrag_rotate, ie_w, ie_h, -hw - 1, ie_h);
2226
_rotate_change_wh(to + hw - 1, ptr_rotate, ie_w, ie_h, hw - 1, -ie_h);
2228
_rotate_change_wh(to + ie_h - 1, ptr_rotate, ie_w, ie_h, -hw - 1, ie_h);
2233
if (prop->cspace == EMILE_COLORSPACE_GRY8)
2236
_flip_vertical8(to8, ie_w, ie_h);
2238
_rotate8_180(to8, ie_w, ie_h);
2240
else if (prop->cspace == EMILE_COLORSPACE_AGRY88)
2243
_flip_vertical16(to16, ie_w, ie_h);
2245
_rotate16_180(to16, ie_w, ie_h);
2250
_flip_vertical(to, ie_w, ie_h);
2252
_rotate_180(to, ie_w, ie_h);
2257
if (prop->cspace == EMILE_COLORSPACE_GRY8)
2260
_rotate_change_wh8(to8, ptrg_rotate, ie_w, ie_h, -hw + 1, ie_h);
2262
_rotate_change_wh8(to8 + hw - ie_h, ptrg_rotate, ie_w, ie_h, hw + 1, -ie_h);
2264
else if (prop->cspace == EMILE_COLORSPACE_AGRY88)
2267
_rotate_change_wh16(to16, ptrag_rotate, w, h, -hw + 1, h);
2269
_rotate_change_wh16(to16 + hw - ie_h, ptrag_rotate, ie_w, ie_h, hw + 1, -ie_h);
2274
_rotate_change_wh(to, ptr_rotate, ie_w, ie_h, -hw + 1, ie_h);
2276
_rotate_change_wh(to + hw - ie_h, ptr_rotate, ie_w, ie_h, hw + 1, -ie_h);
2283
if (prop->cspace == EMILE_COLORSPACE_GRY8)
2284
_flip_horizontal8(to8, ie_w, ie_h);
2285
else if (prop->cspace == EMILE_COLORSPACE_AGRY88)
2286
_flip_horizontal16(to16, ie_w, ie_h);
2288
_flip_horizontal(to, ie_w, ie_h);
2301
*error = EMILE_IMAGE_LOAD_ERROR_NONE;
2304
/* end data decoding */
2305
jpeg_finish_decompress(&cinfo);
2306
*error = EMILE_IMAGE_LOAD_ERROR_NONE;
2310
if (ptrg_free) free(ptrg);
2311
if (ptrag_free) free(ptrag);
2313
jpeg_destroy_decompress(&cinfo);
2314
_emile_jpeg_membuf_src_term(&cinfo);
2315
_emile_image_file_source_unmap(image);
2321
_emile_jpeg_close(Emile_Image *image EINA_UNUSED)
2323
/* JPEG file loader doesn't keep any data allocated around (for now) */
2326
/* Generic helper to instantiate a new Emile_Image */
2329
_emile_image_new(Eina_Bool (*bind)(Emile_Image *image, Emile_Image_Load_Opts *opts, Emile_Image_Animated *animated, Emile_Image_Load_Error *error),
2330
Eina_Bool (*head)(Emile_Image *image, Emile_Image_Property *prop, unsigned int property_size, Emile_Image_Load_Error *error),
2331
Eina_Bool (*data)(Emile_Image *image, Emile_Image_Property *prop, unsigned int property_size, void *pixels, Emile_Image_Load_Error *error),
2332
void (*close)(Emile_Image *image))
2336
ei = calloc(1, sizeof(Emile_Image));
2349
_emile_image_binbuf_set(Emile_Image *ei, Eina_Binbuf *source)
2351
ei->source.bin = source;
2352
ei->bin_source = EINA_TRUE;
2356
_emile_image_file_set(Emile_Image *ei, Eina_File *source)
2358
ei->source.f = eina_file_dup(source);
2359
ei->bin_source = EINA_FALSE;
2363
_emile_image_bind(Emile_Image *ei,
2364
Emile_Image_Load_Opts *opts,
2365
Emile_Image_Animated *animated,
2366
Emile_Image_Load_Error *error)
2371
ei->opts.orientation = !!ei->opts.orientation;
2375
*error = EMILE_IMAGE_LOAD_ERROR_NONE;
2376
if (ei->bind(ei, opts, animated, error))
2379
/* File is not of that format */
2380
if (!ei->bin_source)
2381
eina_file_close(ei->source.f);
2386
/* Public API to manipulate Emile_Image */
2389
emile_image_tgv_memory_open(Eina_Binbuf *source,
2390
Emile_Image_Load_Opts *opts,
2391
Emile_Image_Animated *animated,
2392
Emile_Image_Load_Error *error)
2396
ei = _emile_image_new(_emile_tgv_bind,
2403
_emile_image_binbuf_set(ei, source);
2404
return _emile_image_bind(ei, opts, animated, error);
2408
emile_image_tgv_file_open(Eina_File *source,
2409
Emile_Image_Load_Opts *opts,
2410
Emile_Image_Animated *animated,
2411
Emile_Image_Load_Error *error)
2415
ei = _emile_image_new(_emile_tgv_bind,
2422
_emile_image_file_set(ei, source);
2423
return _emile_image_bind(ei, opts, animated, error);
2427
emile_image_jpeg_memory_open(Eina_Binbuf *source,
2428
Emile_Image_Load_Opts *opts,
2429
Emile_Image_Animated *animated,
2430
Emile_Image_Load_Error *error)
2434
ei = _emile_image_new(_emile_jpeg_bind,
2441
_emile_image_binbuf_set(ei, source);
2442
return _emile_image_bind(ei, opts, animated, error);
2446
emile_image_jpeg_file_open(Eina_File *source,
2447
Emile_Image_Load_Opts *opts,
2448
Emile_Image_Animated *animated,
2449
Emile_Image_Load_Error *error)
2453
ei = _emile_image_new(_emile_jpeg_bind,
2460
_emile_image_file_set(ei, source);
2461
return _emile_image_bind(ei, opts, animated, error);
2465
emile_image_callback_set(Emile_Image *image, Emile_Action_Cb callback, Emile_Action action, const void *data)
2467
if (!image) return ;
2468
// We only handle one type of callback for now
2469
if (action != EMILE_ACTION_CANCELLED) return ;
2471
image->cancelled_data = data;
2472
image->cancelled = callback;
2476
emile_image_close(Emile_Image *image)
2481
_emile_image_file_source_unmap(image);
2482
if (!image->bin_source)
2483
eina_file_close(image->source.f);
2484
image->close(image);
2489
emile_image_head(Emile_Image *image,
2490
Emile_Image_Property *prop,
2491
unsigned int property_size,
2492
Emile_Image_Load_Error *error)
2497
*error = EMILE_IMAGE_LOAD_ERROR_NONE;
2498
return image->head(image, prop, property_size, error);
2502
emile_image_data(Emile_Image *image,
2503
Emile_Image_Property *prop,
2504
unsigned int property_size,
2506
Emile_Image_Load_Error *error)
2511
*error = EMILE_IMAGE_LOAD_ERROR_NONE;
2512
return image->data(image, prop, property_size, pixels, error);
2516
emile_load_error_str(Emile_Image *source EINA_UNUSED,
2517
Emile_Image_Load_Error error)
2521
case EMILE_IMAGE_LOAD_ERROR_NONE:
2524
case EMILE_IMAGE_LOAD_ERROR_GENERIC:
2525
return "Generic error encountered while loading image.";
2527
case EMILE_IMAGE_LOAD_ERROR_DOES_NOT_EXIST:
2528
return "File does not exist.";
2530
case EMILE_IMAGE_LOAD_ERROR_PERMISSION_DENIED:
2531
return "Permission to open file denied.";
2533
case EMILE_IMAGE_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED:
2534
return "Not enough memory to open file.";
2536
case EMILE_IMAGE_LOAD_ERROR_CORRUPT_FILE:
2537
return "File is corrupted.";
2539
case EMILE_IMAGE_LOAD_ERROR_UNKNOWN_FORMAT:
2540
return "Unexpected file format.";
2542
case EMILE_IMAGE_LOAD_ERROR_CANCELLED:
2543
return "Loading was stopped by an external request.";