static inline void _alpha_to_greyscale_convert(uint32_t *data, int len) { for (int k = 0; k < len; k++) { int alpha = A_VAL(data); *data++ = ARGB_JOIN(alpha, alpha, alpha, alpha); } }
static int read_uint(const unsigned char *map, size_t length, size_t *position, unsigned int *ret) { if (*position + 4 > length) return 0; // FIXME: need to check order *ret = ARGB_JOIN(map[(*position) + 0], map[(*position) + 1], map[(*position) + 2], map[(*position) + 3]); *position += 4; return 1; }
static Eina_Bool read_uint(unsigned char *map, size_t length, size_t *position, unsigned int *ret) { unsigned char b[4]; unsigned int i; if (*position + 4 > length) return EINA_FALSE; for (i = 0; i < 4; i++) b[i] = map[(*position)++]; *ret = ARGB_JOIN(b[3], b[2], b[1], b[0]); return EINA_TRUE; }
static void raster(TIFFRGBAImage_Extra * img, uint32 * rast, uint32 x, uint32 y, uint32 w, uint32 h) { int image_width, image_height; uint32 *pixel, pixel_value; int i, j, dy, rast_offset; DATA32 *buffer_pixel, *buffer = evas_cache_image_pixels(img->image); int alpha_premult = 0; image_width = img->image->w; image_height = img->image->h; dy = h > y ? -1 : y - h; /* rast seems to point to the beginning of the last strip processed */ /* so you need use negative offsets. Bizzare. Someone please check this */ /* I don't understand why, but that seems to be what's going on. */ /* libtiff needs better docs! */ if (img->rgba.alpha == EXTRASAMPLE_UNASSALPHA) alpha_premult = 1; for (i = y, rast_offset = 0; i > dy; i--, rast_offset--) { pixel = rast + (rast_offset * image_width); buffer_pixel = buffer + ((((image_height - 1) - i) * image_width) + x); for (j = 0; j < w; j++) { int a, r, g, b; pixel_value = (*(pixel++)); a = TIFFGetA(pixel_value); r = TIFFGetR(pixel_value); g = TIFFGetG(pixel_value); b = TIFFGetB(pixel_value); if (!alpha_premult && (a < 255)) { r = (r * (a + 1)) >> 8; g = (g * (a + 1)) >> 8; b = (b * (a + 1)) >> 8; } (*(buffer_pixel++)) = ARGB_JOIN(a, r, g, b); } }
Eina_Bool evas_image_load_file_data_eet(Evas_Img_Load_Params *ilp, const char *file, const char *key, int *error) { unsigned int w, h; int alpha, compression, quality, lossy, ok; Eet_File *ef; DATA32 *body, *p, *end, *data; DATA32 nas = 0; Eina_Bool res = EINA_FALSE; if (!key) { *error = CSERVE2_DOES_NOT_EXIST; return EINA_FALSE; } ef = eet_open(file, EET_FILE_MODE_READ); if (!ef) { *error = CSERVE2_DOES_NOT_EXIST; return EINA_FALSE; } ok = eet_data_image_header_read(ef, key, &w, &h, &alpha, &compression, &quality, &lossy); if (IMG_TOO_BIG(w, h)) { *error = CSERVE2_RESOURCE_ALLOCATION_FAILED; goto on_error; } if (!ok) { *error = CSERVE2_DOES_NOT_EXIST; goto on_error; } data = ilp->buffer; if (!data) { *error = CSERVE2_RESOURCE_ALLOCATION_FAILED; goto on_error; } ok = eet_data_image_read_to_surface(ef, key, 0, 0, data, w, h, w * 4, &alpha, &compression, &quality, &lossy); if (!ok) { *error = CSERVE2_GENERIC; goto on_error; } if (alpha) { ilp->alpha = 1; body = ilp->buffer; end = body + (w * h); for (p = body; p < end; p++) { DATA32 r, g, b, a; a = A_VAL(p); r = R_VAL(p); g = G_VAL(p); b = B_VAL(p); if ((a == 0) || (a == 255)) nas++; if (r > a) r = a; if (g > a) g = a; if (b > a) b = a; *p = ARGB_JOIN(a, r, g, b); } if ((ALPHA_SPARSE_INV_FRACTION * nas) >= (w * h)) ilp->alpha_sparse = 1; } *error = CSERVE2_NONE; res = EINA_TRUE; on_error: eet_close(ef); return res; }
Eina_Bool evas_image_load_file_data_eet(Image_Entry *ie, const char *file, const char *key, int *error) { unsigned int w, h; int alpha, compression, quality, lossy, ok; Eet_File *ef; DATA32 *body, *p, *end; DATA32 nas = 0; Eina_Bool res = EINA_FALSE; if (!key) { *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; return EINA_FALSE; } if (ie->flags.loaded) { *error = EVAS_LOAD_ERROR_NONE; return EINA_TRUE; } ef = eet_open(file, EET_FILE_MODE_READ); if (!ef) { *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; return EINA_FALSE; } ok = eet_data_image_header_read(ef, key, &w, &h, &alpha, &compression, &quality, &lossy); if (IMG_TOO_BIG(w, h)) { *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; goto on_error; } if (!ok) { *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; goto on_error; } evas_cache_image_surface_alloc(ie, w, h); ok = eet_data_image_read_to_surface(ef, key, 0, 0, evas_cache_image_pixels(ie), w, h, w * 4, &alpha, &compression, &quality, &lossy); if (!ok) { *error = EVAS_LOAD_ERROR_GENERIC; goto on_error; } if (alpha) { ie->flags.alpha = 1; body = evas_cache_image_pixels(ie); end = body +(w * h); for (p = body; p < end; p++) { DATA32 r, g, b, a; a = A_VAL(p); r = R_VAL(p); g = G_VAL(p); b = B_VAL(p); if ((a == 0) || (a == 255)) nas++; if (r > a) r = a; if (g > a) g = a; if (b > a) b = a; *p = ARGB_JOIN(a, r, g, b); } if ((ALPHA_SPARSE_INV_FRACTION * nas) >= (ie->w * ie->h)) ie->flags.alpha_sparse = 1; } // result is already premultiplied now if u compile with edje // evas_common_image_premul(im); *error = EVAS_LOAD_ERROR_NONE; res = EINA_TRUE; on_error: eet_close(ef); return res; }
static Eina_Bool evas_image_load_file_data_ico(Evas_Img_Load_Params *ilp, const char *file, const char *key, int *error) { Eina_File *f; void *map = NULL; size_t position = 0; unsigned short word; unsigned char byte; unsigned int dword; int wanted_w = 0, wanted_h = 0, w, h, cols, i, planes = 0, bpp = 0, pdelta, search = -1, have_choice = 0, stride, pstride, j, right_way_up = 0, diff_size = 0, cols2; unsigned int bmoffset, bmsize, bitcount, fsize, *pal, *surface, *pix, none_zero_alpha = 0; unsigned short reserved, type, count; unsigned char *maskbuf, *pixbuf, *p; struct { int pdelta; int w, h; int cols; int bpp, planes; int hot_x, hot_y; unsigned int bmoffset, bmsize; } chosen = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; f = eina_file_open(file, EINA_FALSE); if (!f) { *error = EVAS_LOAD_ERROR_DOES_NOT_EXIST; return EINA_FALSE; } *error = EVAS_LOAD_ERROR_UNKNOWN_FORMAT; fsize = eina_file_size_get(f); if (fsize < (6 + 16 + 40)) goto close_file; map = eina_file_map_all(f, EINA_FILE_SEQUENTIAL); if (!map) goto close_file; // key: // NULL == highest res // biggest == highest res // smallest == lowest res // // smaller == next size SMALLER than load opts WxH (if possible) // bigger == next size BIGGER than load opts WxH (if possible) // more ? search = BIGGEST; if ((ilp->opts.w > 0) && (ilp->opts.h > 0)) { wanted_w = ilp->opts.w; wanted_h = ilp->opts.h; search = SMALLER; } if (!read_ushort(map, fsize, &position, &reserved)) goto close_file; if (!read_ushort(map, fsize, &position, &type)) goto close_file; if (!read_ushort(map, fsize, &position, &count)) goto close_file; if (!((reserved == 0) && ((type == ICON) || (type == CURSOR)) && (count > 0))) goto close_file; *error = EVAS_LOAD_ERROR_CORRUPT_FILE; if (key) { if (!strcmp(key, "biggest")) { wanted_w = 0; wanted_h = 0; search = BIGGEST; chosen.pdelta = 0; } else if (!strcmp(key, "smallest")) { wanted_w = 1; wanted_h = 1; search = SMALLEST; chosen.pdelta = 0x7fffffff; } else if (!strcmp(key, "smaller")) { chosen.pdelta = 0x7fffffff; search = SMALLER; } else if (!strcmp(key, "bigger")) { chosen.pdelta = 0x7fffffff; search = BIGGER; } } for (i = 0; i < count; i++) { unsigned char tw = 0, th = 0, tcols = 0; if (!read_uchar(map, fsize, &position, &tw)) goto close_file; w = tw; if (w <= 0) w = 256; if (!read_uchar(map, fsize, &position, &th)) goto close_file; h = th; if (h <= 0) h = 256; if (!read_uchar(map, fsize, &position, &tcols)) goto close_file; cols = tcols; if (cols <= 0) cols = 256; if (!read_uchar(map, fsize, &position, &byte)) goto close_file; if (!read_ushort(map, fsize, &position, &word)) goto close_file; if (type == 1) planes = word; //else hot_x = word; if (!read_ushort(map, fsize, &position, &word)) goto close_file; if (type == 1) bpp = word; //else hot_y = word; if (!read_uint(map, fsize, &position, &bmsize)) goto close_file; if (!read_uint(map, fsize, &position, &bmoffset)) goto close_file; if ((bmsize <= 0) || (bmoffset <= 0) || (bmoffset >= fsize)) goto close_file; if (search == BIGGEST) { pdelta = w * h; if ((!have_choice) || ((pdelta >= chosen.pdelta) && (((bpp >= 3) && (bpp >= chosen.bpp)) || ((bpp < 3) && (cols >= chosen.cols))))) { have_choice = 1; chosen.pdelta = pdelta; chosen.w = w; chosen.h = h; chosen.cols = cols; chosen.bpp = bpp; chosen.planes = planes; chosen.bmsize = bmsize; chosen.bmoffset = bmoffset; } } else { if (search == SMALLEST) { pdelta = w * h; if ((!have_choice) || ((pdelta <= chosen.pdelta) && (((bpp >= 3) && (bpp >= chosen.bpp)) || ((bpp < 3) && (cols >= chosen.cols))))) { have_choice = 1; chosen.pdelta = pdelta; chosen.w = w; chosen.h = h; chosen.cols = cols; chosen.bpp = bpp; chosen.planes = planes; chosen.bmsize = bmsize; chosen.bmoffset = bmoffset; } } else if (search == SMALLER) { pdelta = (wanted_w * wanted_h) - (w * h); if ((!have_choice) || ((w <= wanted_w) && (h <= wanted_h) && (pdelta <= chosen.pdelta) && (((bpp >= 3) && (bpp >= chosen.bpp)) || ((bpp < 3) && (cols >= chosen.cols))))) { have_choice = 1; if (pdelta < 0) pdelta = 0x7fffffff; chosen.pdelta = pdelta; chosen.w = w; chosen.h = h; chosen.cols = cols; chosen.bpp = bpp; chosen.planes = planes; chosen.bmsize = bmsize; chosen.bmoffset = bmoffset; } } else if (search == BIGGER) { pdelta = (w * h) - (wanted_w * wanted_h); if ((!have_choice) || ((w >= wanted_w) && (h >= wanted_h) && (pdelta <= chosen.pdelta) && (((bpp >= 3) && (bpp >= chosen.bpp)) || ((bpp < 3) && (cols >= chosen.cols))))) { have_choice = 1; if (pdelta < 0) pdelta = 0x7fffffff; chosen.pdelta = pdelta; chosen.w = w; chosen.h = h; chosen.cols = cols; chosen.bpp = bpp; chosen.planes = planes; chosen.bmsize = bmsize; chosen.bmoffset = bmoffset; } } } } if (chosen.bmoffset == 0) goto close_file; position = chosen.bmoffset; w = chosen.w; h = chosen.h; cols = chosen.cols; bpp = chosen.bpp; // changed since we loaded header? if (((int)ilp->w != w) || ((int)ilp->h != h)) goto close_file; // read bmp header time... let's do some checking if (!read_uint(map, fsize, &position, &dword)) goto close_file; // headersize - dont care if (!read_uint(map, fsize, &position, &dword)) goto close_file; // width if (dword > 0) { if ((int)dword != w) { w = dword; diff_size = 1; } } if (!read_uint(map, fsize, &position, &dword)) goto close_file; // height if (dword > 0) { if ((int)dword != (h * 2)) { h = dword / 2; diff_size = 1; } } if (diff_size) { ERR("Broken ICO file: %s - " " Reporting size of %ix%i in index, but bitmap is %ix%i. " " May be expanded or cropped.", file, ilp->w, ilp->h, w, h); } if (!read_ushort(map, fsize, &position, &word)) goto close_file; // planes //planes2 = word; if (!read_ushort(map, fsize, &position, &word)) goto close_file; // bitcount bitcount = word; if (!read_uint(map, fsize, &position, &dword)) goto close_file; // compression //compression = dword; if (!read_uint(map, fsize, &position, &dword)) goto close_file; // imagesize //imagesize = dword; if (!read_uint(map, fsize, &position, &dword)) goto close_file; // z pixels per m if (!read_uint(map, fsize, &position, &dword)) goto close_file; // y pizels per m if (!read_uint(map, fsize, &position, &dword)) goto close_file; // colors used //colorsused = dword; if (!read_uint(map, fsize, &position, &dword)) goto close_file; // colors important //colorsimportant = dword; surface = ilp->buffer; if (!surface) { *error = EVAS_LOAD_ERROR_RESOURCE_ALLOCATION_FAILED; goto close_file; } memset(surface, 0, ilp->w * ilp->h * 4); if (!((bitcount == 1) || (bitcount == 4) || (bitcount == 8) || (bitcount == 24) || (bitcount == 32))) goto close_file; if (bitcount <= 8) { cols2 = 1 << bitcount; if (cols == 0) cols = cols2; if (cols > cols2) cols = cols2; if (cols > 256) cols = 256; } else cols = 0; if (bitcount > 8) cols = 0; pal = alloca(256 * 4); for (i = 0; i < cols; i++) { unsigned char a, r, g, b; if (!read_uchar(map, fsize, &position, &b)) goto close_file; if (!read_uchar(map, fsize, &position, &g)) goto close_file; if (!read_uchar(map, fsize, &position, &r)) goto close_file; if (!read_uchar(map, fsize, &position, &a)) goto close_file; a = 0xff; pal[i] = ARGB_JOIN(a, r, g, b); } stride = ((w + 31) / 32); maskbuf = alloca(stride * h); pixbuf = alloca(stride * 32 * 4); // more than enough if (bitcount == 1) { pstride = stride * 4; for (i = 0; i < h; i++) { pix = surface + (i * ilp->w); if (!right_way_up) pix = surface + ((ilp->h - 1 - i) * ilp->w); if (!read_mem(map, fsize, &position, pixbuf, pstride)) goto close_file; p = pixbuf; if (i >= (int)ilp->h) continue; for (j = 0; j < w; j++) { if (j >= (int)ilp->w) break; if ((j & 0x7) == 0x0) { *pix = pal[*p >> 7]; } else if ((j & 0x7) == 0x1) { *pix = pal[(*p >> 6) & 0x1]; }