kwo pushed a commit to branch master. http://git.enlightenment.org/legacy/imlib2.git/commit/?id=bd3aa88242bc146562f5e110c53f6bbfadcf0679
commit bd3aa88242bc146562f5e110c53f6bbfadcf0679 Author: Kim Woelders <[email protected]> Date: Sat Nov 30 08:58:46 2019 +0100 Loader cleanups - Do exit cleanups at exit. - Merge data load conditions. - Use LOAD_FAIL/SUCCESS/BREAK for load() exit code. - Cosmetics. --- src/lib/image.c | 1 + src/lib/image.h | 6 +- src/modules/loaders/loader_argb.c | 107 ++++---- src/modules/loaders/loader_bmp.c | 109 ++++---- src/modules/loaders/loader_bz2.c | 4 +- src/modules/loaders/loader_common.h | 2 +- src/modules/loaders/loader_ff.c | 106 ++++---- src/modules/loaders/loader_gif.c | 139 +++++----- src/modules/loaders/loader_ico.c | 7 +- src/modules/loaders/loader_id3.c | 6 +- src/modules/loaders/loader_jpeg.c | 177 +++++++------ src/modules/loaders/loader_lbm.c | 196 +++++++------- src/modules/loaders/loader_png.c | 228 ++++++++-------- src/modules/loaders/loader_pnm.c | 508 +++++++++++++++++------------------- src/modules/loaders/loader_tga.c | 19 +- src/modules/loaders/loader_tiff.c | 138 +++++----- src/modules/loaders/loader_webp.c | 80 +++--- src/modules/loaders/loader_xpm.c | 48 ++-- src/modules/loaders/loader_zlib.c | 4 +- 19 files changed, 920 insertions(+), 965 deletions(-) diff --git a/src/lib/image.c b/src/lib/image.c index 922b0f7..d46871f 100644 --- a/src/lib/image.c +++ b/src/lib/image.c @@ -868,6 +868,7 @@ __imlib_LoadImageWrapper(const ImlibLoader * l, ImlibImage * im, { int rc; + immediate_load = immediate_load || progress || im->loader; im->data_memory_func = imlib_context_get_image_data_memory_function(); rc = l->load(im, progress, progress_granularity, immediate_load); if (rc == 0) diff --git a/src/lib/image.h b/src/lib/image.h index 991f428..5d6804d 100644 --- a/src/lib/image.h +++ b/src/lib/image.h @@ -97,7 +97,7 @@ struct _imlibloader { void *handle; char (*load)(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load); + char progress_granularity, char load_data); char (*save)(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity); @@ -185,6 +185,10 @@ void __imlib_SaveImage(ImlibImage * im, const char *file, #define SET_FLAG(flags, f) ((flags) |= (f)) #define UNSET_FLAG(flags, f) ((flags) &= (~f)) +#define LOAD_FAIL 0 +#define LOAD_SUCCESS 1 +#define LOAD_BREAK 2 + /* 32767 is the maximum pixmap dimension and ensures that * (w * h * sizeof(DATA32)) won't exceed ULONG_MAX */ #define X_MAX_DIM 32767 diff --git a/src/modules/loaders/loader_argb.c b/src/modules/loaders/loader_argb.c index 337b784..041f56f 100644 --- a/src/modules/loaders/loader_argb.c +++ b/src/modules/loaders/loader_argb.c @@ -2,14 +2,20 @@ char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { - int w = 0, h = 0, alpha = 0; + int rc; FILE *f; + int w = 0, h = 0, alpha = 0; + DATA32 *ptr; + int y, l; + int pper = 0, pl = 0; f = fopen(im->real_file, "rb"); if (!f) - return 0; + return LOAD_FAIL; + + rc = LOAD_FAIL; /* header */ { @@ -17,22 +23,16 @@ load(ImlibImage * im, ImlibProgressFunction progress, buf[0] = '\0'; if (!fgets(buf, 255, f)) - { - fclose(f); - return 0; - } + goto quit; + buf2[0] = '\0'; sscanf(buf, "%s %i %i %i", buf2, &w, &h, &alpha); if (strcmp(buf2, "ARGB")) - { - fclose(f); - return 0; - } + goto quit; + if (!IMAGE_DIMENSIONS_OK(w, h)) - { - fclose(f); - return 0; - } + goto quit; + im->w = w; im->h = h; if (alpha) @@ -41,55 +41,56 @@ load(ImlibImage * im, ImlibProgressFunction progress, UNSET_FLAG(im->flags, F_HAS_ALPHA); } - if (im->loader || immediate_load || progress) + if (!load_data) { - DATA32 *ptr; - int y, l, pl = 0; - char pper = 0; + rc = LOAD_SUCCESS; + goto quit; + } + + /* Load data */ + + ptr = __imlib_AllocateData(im); + if (!ptr) + goto quit; + + for (y = 0; y < h; y++) + { + if (fread(ptr, im->w, 4, f) != 4) + goto quit; - /* must set the im->data member before callign progress function */ - ptr = __imlib_AllocateData(im); - if (!ptr) - { - im->w = 0; - fclose(f); - return 0; - } - for (y = 0; y < h; y++) - { - if (fread(ptr, im->w, 4, f) != 4) - { - __imlib_FreeData(im); - fclose(f); - return 0; - } #ifdef WORDS_BIGENDIAN - for (l = 0; l < im->w; l++) - SWAP_LE_32_INPLACE(ptr[l]); + for (l = 0; l < im->w; l++) + SWAP_LE_32_INPLACE(ptr[l]); #endif - ptr += im->w; - if (progress) - { - char per; + ptr += im->w; - per = (char)((100 * y) / im->h); - if (((per - pper) >= progress_granularity) || - (y == (im->h - 1))) + if (progress) + { + char per; + + per = (char)((100 * y) / im->h); + if (((per - pper) >= progress_granularity) || (y == (im->h - 1))) + { + l = y - pl; + if (!progress(im, per, 0, (y - l), im->w, l)) { - l = y - pl; - if (!progress(im, per, 0, (y - l), im->w, l)) - { - fclose(f); - return 2; - } - pper = per; - pl = y; + rc = LOAD_BREAK; + goto quit; } + pper = per; + pl = y; } } } + + rc = LOAD_SUCCESS; + + quit: + if (rc <= 0) + __imlib_FreeData(im); fclose(f); - return 1; + + return rc; } char diff --git a/src/modules/loaders/loader_bmp.c b/src/modules/loaders/loader_bmp.c index 2fa8616..b84833d 100644 --- a/src/modules/loaders/loader_bmp.c +++ b/src/modules/loaders/loader_bmp.c @@ -135,8 +135,9 @@ WriteleLong(FILE * file, unsigned long val) char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { + int rc; FILE *f; char pper = 0; int pl = 0; @@ -158,8 +159,9 @@ load(ImlibImage * im, ImlibProgressFunction progress, f = fopen(im->real_file, "rb"); if (!f) - return 0; + return LOAD_FAIL; + rc = LOAD_FAIL; buffer = NULL; /* Load header */ @@ -168,27 +170,27 @@ load(ImlibImage * im, ImlibProgressFunction progress, bfh_t bfh; if (fstat(fileno(f), &statbuf) < 0) - goto quit_err; + goto quit; size = statbuf.st_size; if (size != statbuf.st_size) - goto quit_err; + goto quit; if (fread(&bfh, sizeof(bfh), 1, f) != 1) - goto quit_err; + goto quit; if (bfh.header[0] != 'B' || bfh.header[1] != 'M') - goto quit_err; + goto quit; #define WORD_LE_32(p8) ((p8[3] << 24) | (p8[2] << 16) | (p8[1] << 8) | p8[0]) offset = WORD_LE_32(bfh.offs); if (offset >= size) - goto quit_err; + goto quit; memset(&bih, 0, sizeof(bih)); if (fread(&bih, 4, 1, f) != 1) - goto quit_err; + goto quit; SWAP_LE_32_INPLACE(bih.header_size); @@ -196,10 +198,10 @@ load(ImlibImage * im, ImlibProgressFunction progress, size, bih.header_size, WORD_LE_32(bfh.size), offset); if (bih.header_size < 12 || bih.header_size > sizeof(bih)) - goto quit_err; + goto quit; if (fread(&bih.header_size + 1, bih.header_size - 4, 1, f) != 1) - goto quit_err; + goto quit; w = h = 0; bitcount = 0; @@ -233,7 +235,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, { ncols = (comp == BI_ALPHABITFIELDS) ? 4 : 3; if (fread(&bih.bih.mask_r, 4, ncols, f) != ncols) - goto quit_err; + goto quit; } rmask = SWAP_LE_32(bih.bih.mask_r); gmask = SWAP_LE_32(bih.bih.mask_g); @@ -245,7 +247,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, } else { - goto quit_err; + goto quit; } imgsize = size - offset; @@ -256,12 +258,12 @@ load(ImlibImage * im, ImlibProgressFunction progress, h = abs(h); if (!IMAGE_DIMENSIONS_OK(w, h)) - goto quit_err; + goto quit; switch (bitcount) { default: - goto quit_err; + goto quit; case 1: case 4: @@ -274,7 +276,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, ncols = 256; for (i = 0; i < ncols; i++) if (fread(&rgbQuads[i], 3, 1, f) != 1) - goto quit_err; + goto quit; } else { @@ -282,7 +284,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, if (ncols > 256) ncols = 256; if (fread(rgbQuads, 4, ncols, f) != ncols) - goto quit_err; + goto quit; } for (i = 0; i < ncols; i++) argbCmap[i] = @@ -311,7 +313,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, bmask &= 0xffffU; } if (rmask == 0 && gmask == 0 && bmask == 0) - goto quit_err; + goto quit; for (bit = 0; bit < bitcount; bit++) { /* Find LSB bit positions */ @@ -390,29 +392,25 @@ load(ImlibImage * im, ImlibProgressFunction progress, im->h = h; } - if (!(im->loader || immediate_load || progress)) + if (!load_data) { - fclose(f); - return 1; + rc = LOAD_SUCCESS; + goto quit; } /* Load data */ fseek(f, offset, SEEK_SET); + if (!__imlib_AllocateData(im)) + goto quit; + buffer = malloc(imgsize); if (!buffer) - goto quit_err; - - if (!__imlib_AllocateData(im)) - goto quit_err; + goto quit; if (fread(buffer, imgsize, 1, f) != 1) - { - __imlib_FreeData(im); - goto quit_err; - } - fclose(f); + goto quit; buffer_ptr = buffer; buffer_end = buffer + imgsize; @@ -422,13 +420,13 @@ load(ImlibImage * im, ImlibProgressFunction progress, switch (bitcount) { default: /* It should not be possible to go here */ - goto quit_err2; + goto quit; case 1: switch (comp) { default: - goto quit_err2; + goto quit; case BI_RGB: skip = ((((w + 31) / 32) * 32) - w) / 8; @@ -458,8 +456,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, (im, per, 0, im->h - y - 1, im->w, im->h - y + ll)) { - free(buffer); - return 2; + rc = LOAD_BREAK; + goto quit; } pper = per; pl = y; @@ -474,7 +472,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, switch (comp) { default: - goto quit_err2; + goto quit; case BI_RLE4: buffer_end_safe = buffer_end - 1; @@ -573,8 +571,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, (im, per, 0, im->h - y - 1, im->w, im->h - y + ll)) { - free(buffer); - return 2; + rc = LOAD_BREAK; + goto quit; } pper = per; pl = y; @@ -611,8 +609,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, (im, per, 0, im->h - y - 1, im->w, im->h - y + ll)) { - free(buffer); - return 2; + rc = LOAD_BREAK; + goto quit; } pper = per; pl = y; @@ -627,7 +625,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, switch (comp) { default: - goto quit_err2; + goto quit; case BI_RLE8: buffer_end_safe = buffer_end - 1; @@ -713,8 +711,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, if (!progress (im, per, 0, im->h - y - 1, im->w, im->h - y + ll)) { - free(buffer); - return 2; + rc = LOAD_BREAK; + goto quit; } pper = per; pl = y; @@ -748,8 +746,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, (im, per, 0, im->h - y - 1, im->w, im->h - y + ll)) { - free(buffer); - return 2; + rc = LOAD_BREAK; + goto quit; } pper = per; pl = y; @@ -796,8 +794,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, if (!progress (im, per, 0, im->h - y - 1, im->w, im->h - y + ll)) { - free(buffer); - return 2; + rc = LOAD_BREAK; + goto quit; } pper = per; pl = y; @@ -835,8 +833,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, if (!progress (im, per, 0, im->h - y - 1, im->w, im->h - y + ll)) { - free(buffer); - return 2; + rc = LOAD_BREAK; + goto quit; } pper = per; pl = y; @@ -881,8 +879,8 @@ load(ImlibImage * im, ImlibProgressFunction progress, if (!progress (im, per, 0, im->h - y - 1, im->w, im->h - y + ll)) { - free(buffer); - return 2; + rc = LOAD_BREAK; + goto quit; } pper = per; pl = y; @@ -892,14 +890,15 @@ load(ImlibImage * im, ImlibProgressFunction progress, break; } - free(buffer); - return 1; + rc = LOAD_SUCCESS; - quit_err: - fclose(f); - quit_err2: + quit: + if (rc <= 0) + __imlib_FreeData(im); free(buffer); - return 0; + fclose(f); + + return rc; } char diff --git a/src/modules/loaders/loader_bz2.c b/src/modules/loaders/loader_bz2.c index 3b6ceba..50608ed 100644 --- a/src/modules/loaders/loader_bz2.c +++ b/src/modules/loaders/loader_bz2.c @@ -46,7 +46,7 @@ uncompress_file(FILE * fp, int dest) char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { ImlibLoader *loader; FILE *fp; @@ -98,7 +98,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, file = im->real_file; im->real_file = strdup(tmp); - loader->load(im, progress, progress_granularity, immediate_load); + loader->load(im, progress, progress_granularity, load_data); free(im->real_file); im->real_file = file; diff --git a/src/modules/loaders/loader_common.h b/src/modules/loaders/loader_common.h index ccbacef..5adce56 100644 --- a/src/modules/loaders/loader_common.h +++ b/src/modules/loaders/loader_common.h @@ -6,7 +6,7 @@ #include "image.h" __EXPORT__ char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load); + char progress_granularity, char load_data); __EXPORT__ char save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity); __EXPORT__ void formats(ImlibLoader * l); diff --git a/src/modules/loaders/loader_ff.c b/src/modules/loaders/loader_ff.c index e52b99e..601a439 100644 --- a/src/modules/loaders/loader_ff.c +++ b/src/modules/loaders/loader_ff.c @@ -8,86 +8,84 @@ char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { + int rc; FILE *f; size_t rowlen, i, j; uint32_t hdr[2 + 1 + 1], w, h; uint16_t *row; uint8_t *dat; - /* open the file for reading */ - if (!(f = fopen(im->real_file, "rb"))) - { - return 0; - } + f = fopen(im->real_file, "rb"); + if (!f) + return LOAD_FAIL; + + rc = LOAD_FAIL; + row = NULL; /* read and check the header */ if (fread(hdr, sizeof(uint32_t), LEN(hdr), f) != LEN(hdr) || memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) - { - fclose(f); - return 0; - } + goto quit; + im->w = ntohl(hdr[2]); im->h = ntohl(hdr[3]); if (!IMAGE_DIMENSIONS_OK(im->w, im->h)) + goto quit; + + SET_FLAG(im->flags, F_HAS_ALPHA); + + if (!load_data) { - im->w = 0; - fclose(f); - return 0; + rc = LOAD_SUCCESS; + goto quit; } - SET_FLAG(im->flags, F_HAS_ALPHA); + /* Load data */ + + w = im->w; + h = im->h; + rowlen = w * (sizeof("RGBA") - 1); + + if (!__imlib_AllocateData(im)) + goto quit; + + row = malloc(rowlen * sizeof(uint16_t)); + if (!row) + goto quit; - /* load the data */ - if (im->loader || immediate_load || progress) + dat = (uint8_t *) im->data; + for (i = 0; i < h; i++, dat += rowlen) { - w = im->w; - h = im->h; - rowlen = w * (sizeof("RGBA") - 1); + if (fread(row, sizeof(uint16_t), rowlen, f) != rowlen) + goto quit; - if (!(__imlib_AllocateData(im)) || - !(row = malloc(rowlen * sizeof(uint16_t)))) + for (j = 0; j < rowlen; j += 4) { - __imlib_FreeData(im); - free(row); - fclose(f); - return 0; + /* + * 16-Bit to 8-Bit (RGBA -> BGRA) + * 255 * 257 = 65535 = 2^16-1 = UINT16_MAX + */ + dat[j + 2] = ntohs(row[j + 0]) / 257; + dat[j + 1] = ntohs(row[j + 1]) / 257; + dat[j + 0] = ntohs(row[j + 2]) / 257; + dat[j + 3] = ntohs(row[j + 3]) / 257; } + } - dat = (uint8_t *) im->data; - for (i = 0; i < h; i++, dat += rowlen) - { - if (fread(row, sizeof(uint16_t), rowlen, f) != rowlen) - { - __imlib_FreeData(im); - free(row); - fclose(f); - return 0; - } - for (j = 0; j < rowlen; j += 4) - { - /* - * 16-Bit to 8-Bit (RGBA -> BGRA) - * 255 * 257 = 65535 = 2^16-1 = UINT16_MAX - */ - dat[j + 2] = ntohs(row[j + 0]) / 257; - dat[j + 1] = ntohs(row[j + 1]) / 257; - dat[j + 0] = ntohs(row[j + 2]) / 257; - dat[j + 3] = ntohs(row[j + 3]) / 257; - } - } - if (progress) - { - progress(im, 100, 0, 0, im->w, im->h); - } + if (progress) + progress(im, 100, 0, 0, im->w, im->h); - free(row); - } + rc = LOAD_SUCCESS; + quit: + free(row); + if (rc <= 0) + __imlib_FreeData(im); fclose(f); - return 1; + + return rc; } char diff --git a/src/modules/loaders/loader_gif.c b/src/modules/loaders/loader_gif.c index baa17c5..1332d8c 100644 --- a/src/modules/loaders/loader_gif.c +++ b/src/modules/loaders/loader_gif.c @@ -5,8 +5,8 @@ #include <gif_lib.h> char -load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, - char immediate_load) +load(ImlibImage * im, ImlibProgressFunction progress, + char progress_granularity, char load_data) { static const int intoffset[] = { 0, 4, 2, 1 }; static const int intjump[] = { 8, 8, 4, 2 }; @@ -21,27 +21,25 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, int last_per = 0, last_y = 0; int transp; int fd; - - done = 0; - rows = NULL; - transp = -1; + DATA32 colormap[256]; fd = open(im->real_file, O_RDONLY); if (fd < 0) - return 0; + return LOAD_FAIL; #if GIFLIB_MAJOR >= 5 gif = DGifOpenFileHandle(fd, NULL); #else gif = DGifOpenFileHandle(fd); #endif + close(fd); if (!gif) - { - close(fd); - return 0; - } + return LOAD_FAIL; - rc = 0; /* Failure */ + rc = LOAD_FAIL; + done = 0; + rows = NULL; + transp = -1; do { @@ -50,6 +48,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, /* PrintGifError(); */ rec = TERMINATE_RECORD_TYPE; } + if ((rec == IMAGE_DESC_RECORD_TYPE) && (!done)) { if (DGifGetImageDesc(gif) == GIF_ERROR) @@ -58,14 +57,15 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, rec = TERMINATE_RECORD_TYPE; break; } - w = gif->Image.Width; - h = gif->Image.Height; + + im->w = w = gif->Image.Width; + im->h = h = gif->Image.Height; if (!IMAGE_DIMENSIONS_OK(w, h)) - goto quit2; + goto quit; rows = calloc(h, sizeof(GifRowType *)); if (!rows) - goto quit2; + goto quit; for (i = 0; i < h; i++) { @@ -114,87 +114,84 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, while (rec != TERMINATE_RECORD_TYPE); if (transp >= 0) - { - SET_FLAG(im->flags, F_HAS_ALPHA); - } + SET_FLAG(im->flags, F_HAS_ALPHA); else - { - UNSET_FLAG(im->flags, F_HAS_ALPHA); - } + UNSET_FLAG(im->flags, F_HAS_ALPHA); + if (!rows) + goto quit; + + if (!load_data) { - goto quit2; + rc = LOAD_SUCCESS; + goto quit; } - im->w = w; - im->h = h; + /* Load data */ - if (im->loader || immediate_load || progress) + bg = gif->SBackGroundColor; + cmap = (gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap); + memset(colormap, 0, sizeof(colormap)); + if (cmap != NULL) { - DATA32 colormap[256]; - - bg = gif->SBackGroundColor; - cmap = (gif->Image.ColorMap ? gif->Image.ColorMap : gif->SColorMap); - memset(colormap, 0, sizeof(colormap)); - if (cmap != NULL) + for (i = cmap->ColorCount > 256 ? 256 : cmap->ColorCount; i-- > 0;) { - for (i = cmap->ColorCount > 256 ? 256 : cmap->ColorCount; i-- > 0;) - { - r = cmap->Colors[i].Red; - g = cmap->Colors[i].Green; - b = cmap->Colors[i].Blue; - colormap[i] = PIXEL_ARGB(0xff, r, g, b); - } - /* if bg > cmap->ColorCount, it is transparent black already */ - if (transp >= 0 && transp < 256) - colormap[transp] = bg >= 0 && bg < 256 ? - colormap[bg] & 0x00ffffff : 0x00000000; + r = cmap->Colors[i].Red; + g = cmap->Colors[i].Green; + b = cmap->Colors[i].Blue; + colormap[i] = PIXEL_ARGB(0xff, r, g, b); } + /* if bg > cmap->ColorCount, it is transparent black already */ + if (transp >= 0 && transp < 256) + colormap[transp] = bg >= 0 && bg < 256 ? + colormap[bg] & 0x00ffffff : 0x00000000; + } - ptr = __imlib_AllocateData(im); - if (!ptr) - goto quit; + ptr = __imlib_AllocateData(im); + if (!ptr) + goto quit; - per_inc = 100.0 / (float)h; - for (i = 0; i < h; i++) + per_inc = 100.0 / (float)h; + for (i = 0; i < h; i++) + { + for (j = 0; j < w; j++) { - for (j = 0; j < w; j++) - { - *ptr++ = colormap[rows[i][j]]; - } - per += per_inc; - if (progress && (((int)per) != last_per) - && (((int)per) % progress_granularity == 0)) + *ptr++ = colormap[rows[i][j]]; + } + per += per_inc; + if (progress && (((int)per) != last_per) + && (((int)per) % progress_granularity == 0)) + { + last_per = (int)per; + if (!(progress(im, per, 0, last_y, w, i))) { - last_per = (int)per; - if (!(progress(im, (int)per, 0, last_y, w, i))) - { - rc = 2; - goto quit; - } - last_y = i; + rc = LOAD_BREAK; + goto quit; } + last_y = i; } - - if (progress) - progress(im, 100, 0, last_y, w, h); } - rc = 1; /* Success */ + if (progress) + progress(im, 100, 0, last_y, im->w, im->h); + + rc = LOAD_SUCCESS; quit: - for (i = 0; i < h; i++) - free(rows[i]); - free(rows); + if (rows) + { + for (i = 0; i < h; i++) + free(rows[i]); + free(rows); + } - quit2: #if GIFLIB_MAJOR > 5 || (GIFLIB_MAJOR == 5 && GIFLIB_MINOR >= 1) DGifCloseFile(gif, NULL); #else DGifCloseFile(gif); #endif - if (rc == 0) + if (rc <= 0) __imlib_FreeData(im); return rc; diff --git a/src/modules/loaders/loader_ico.c b/src/modules/loaders/loader_ico.c index e973394..f2215e4 100644 --- a/src/modules/loaders/loader_ico.c +++ b/src/modules/loaders/loader_ico.c @@ -406,17 +406,16 @@ ico_load(ico_t * ico, ImlibImage * im, int load_data) } char -load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, - char immediate_load) +load(ImlibImage * im, ImlibProgressFunction progress, + char progress_granularity, char load_data) { ico_t *ico; - int ok, load_data; + int ok; ico = ico_read(im->real_file); if (!ico) return 0; - load_data = im->loader || immediate_load || progress; ok = ico_load(ico, im, load_data); if (ok) { diff --git a/src/modules/loaders/loader_id3.c b/src/modules/loaders/loader_id3.c index fafdda6..29b77aa 100644 --- a/src/modules/loaders/loader_id3.c +++ b/src/modules/loaders/loader_id3.c @@ -488,7 +488,7 @@ write_tags(ImlibImage * im, lopt * opt) char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { ImlibLoader *loader; lopt opt; @@ -524,7 +524,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, ofile = im->real_file; im->real_file = strdup(tmp); - res = loader->load(im, progress, progress_granularity, immediate_load); + res = loader->load(im, progress, progress_granularity, load_data); free(im->real_file); im->real_file = ofile; @@ -561,7 +561,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, } ofile = im->real_file; im->real_file = file; - res = loader->load(im, progress, progress_granularity, immediate_load); + res = loader->load(im, progress, progress_granularity, load_data); if (!im->loader) __imlib_AttachTag(im, "id3-link-url", 0, url, destructor_data); else diff --git a/src/modules/loaders/loader_jpeg.c b/src/modules/loaders/loader_jpeg.c index 0d22f5b..c691799 100644 --- a/src/modules/loaders/loader_jpeg.c +++ b/src/modules/loaders/loader_jpeg.c @@ -59,141 +59,144 @@ _jdata_init(ImLib_JPEG_data * jd) char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { int w, h, rc; struct jpeg_decompress_struct cinfo; ImLib_JPEG_data jdata; FILE *f; + DATA8 *ptr, *line[16]; + DATA32 *ptr2; + int x, y, l, i, scans, count, prevy; f = fopen(im->real_file, "rb"); if (!f) - return 0; + return LOAD_FAIL; + + rc = LOAD_FAIL; /* set up error handling */ cinfo.err = _jdata_init(&jdata); if (sigsetjmp(jdata.setjmp_buffer, 1)) - goto quit_error; + goto quit; jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, f); jpeg_read_header(&cinfo, TRUE); + im->w = w = cinfo.image_width; im->h = h = cinfo.image_height; + if (!IMAGE_DIMENSIONS_OK(w, h)) + goto quit; - rc = 1; /* Ok */ + UNSET_FLAG(im->flags, F_HAS_ALPHA); - if ((!im->loader) && (!im->data)) + if (!load_data) { - if (!IMAGE_DIMENSIONS_OK(w, h)) - goto quit_error; - UNSET_FLAG(im->flags, F_HAS_ALPHA); + rc = LOAD_SUCCESS; + goto quit; } - if (im->loader || immediate_load || progress) - { - DATA8 *ptr, *line[16]; - DATA32 *ptr2; - int x, y, l, i, scans, count, prevy; + /* Load data */ + + cinfo.do_fancy_upsampling = FALSE; + cinfo.do_block_smoothing = FALSE; + jpeg_start_decompress(&cinfo); - cinfo.do_fancy_upsampling = FALSE; - cinfo.do_block_smoothing = FALSE; - jpeg_start_decompress(&cinfo); + if ((cinfo.rec_outbuf_height > 16) || (cinfo.output_components <= 0)) + goto quit; - if ((cinfo.rec_outbuf_height > 16) || (cinfo.output_components <= 0) || - !IMAGE_DIMENSIONS_OK(w, h)) - goto quit_error; + jdata.data = malloc(w * 16 * cinfo.output_components); + if (!jdata.data) + goto quit; - jdata.data = malloc(w * 16 * cinfo.output_components); - if (!jdata.data) - goto quit_error; + /* must set the im->data member before callign progress function */ + ptr2 = __imlib_AllocateData(im); + if (!ptr2) + goto quit; - /* must set the im->data member before callign progress function */ - ptr2 = __imlib_AllocateData(im); - if (!ptr2) - goto quit_error; + count = 0; + prevy = 0; - count = 0; - prevy = 0; + for (i = 0; i < cinfo.rec_outbuf_height; i++) + line[i] = jdata.data + (i * w * cinfo.output_components); - for (i = 0; i < cinfo.rec_outbuf_height; i++) - line[i] = jdata.data + (i * w * cinfo.output_components); + for (l = 0; l < h; l += cinfo.rec_outbuf_height) + { + jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height); - for (l = 0; l < h; l += cinfo.rec_outbuf_height) + scans = cinfo.rec_outbuf_height; + if ((h - l) < scans) + scans = h - l; + ptr = jdata.data; + + for (y = 0; y < scans; y++) { - jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height); - scans = cinfo.rec_outbuf_height; - if ((h - l) < scans) - scans = h - l; - ptr = jdata.data; - for (y = 0; y < scans; y++) + switch (cinfo.out_color_space) { - switch (cinfo.out_color_space) + default: + goto quit; + case JCS_GRAYSCALE: + for (x = 0; x < w; x++) + { + *ptr2 = PIXEL_ARGB(0xff, ptr[0], ptr[0], ptr[0]); + ptr++; + ptr2++; + } + break; + case JCS_RGB: + for (x = 0; x < w; x++) + { + *ptr2 = PIXEL_ARGB(0xff, ptr[0], ptr[1], ptr[2]); + ptr += cinfo.output_components; + ptr2++; + } + break; + case JCS_CMYK: + for (x = 0; x < w; x++) { - default: - goto quit_error; - case JCS_GRAYSCALE: - for (x = 0; x < w; x++) - { - *ptr2 = PIXEL_ARGB(0xff, ptr[0], ptr[0], ptr[0]); - ptr++; - ptr2++; - } - break; - case JCS_RGB: - for (x = 0; x < w; x++) - { - *ptr2 = PIXEL_ARGB(0xff, ptr[0], ptr[1], ptr[2]); - ptr += cinfo.output_components; - ptr2++; - } - break; - case JCS_CMYK: - for (x = 0; x < w; x++) - { - *ptr2 = PIXEL_ARGB(0xff, ptr[0] * ptr[3] / 255, - ptr[1] * ptr[3] / 255, - ptr[2] * ptr[3] / 255); - ptr += cinfo.output_components; - ptr2++; - } - break; + *ptr2 = PIXEL_ARGB(0xff, ptr[0] * ptr[3] / 255, + ptr[1] * ptr[3] / 255, + ptr[2] * ptr[3] / 255); + ptr += cinfo.output_components; + ptr2++; } + break; } + } - if (progress) - { - int per; + if (progress) + { + int per; - per = (l * 100) / h; - if (((per - count) >= progress_granularity) - || ((h - l) <= cinfo.rec_outbuf_height)) + per = (l * 100) / h; + if (((per - count) >= progress_granularity) + || ((h - l) <= cinfo.rec_outbuf_height)) + { + count = per; + if (!progress(im, per, 0, prevy, w, scans + l - prevy)) { - count = per; - if (!progress(im, per, 0, prevy, w, scans + l - prevy)) - { - rc = 2; - goto done; - } - prevy = l + scans; + rc = LOAD_BREAK; + goto done; } + prevy = l + scans; } } - - done: - jpeg_finish_decompress(&cinfo); } + done: + jpeg_finish_decompress(&cinfo); + + rc = LOAD_SUCCESS; + quit: jpeg_destroy_decompress(&cinfo); free(jdata.data); + if (rc <= 0) + __imlib_FreeData(im); fclose(f); - return rc; - quit_error: - rc = 0; /* Error */ - __imlib_FreeData(im); - goto quit; + return rc; } char diff --git a/src/modules/loaders/loader_lbm.c b/src/modules/loaders/loader_lbm.c index e8ded9c..10b4808 100644 --- a/src/modules/loaders/loader_lbm.c +++ b/src/modules/loaders/loader_lbm.c @@ -442,92 +442,90 @@ deplane(DATA32 * row, int w, ILBM * ilbm, unsigned char *plane[]) } /*------------------------------------------------------------------------------ - * Loads an image. If im->loader is non-zero, or immediate_load is non-zero, or - * progress is non-zero, then the file is fully loaded, otherwise only the width - * and height are read. + * Loads an image. If load_data is non-zero then the file is fully loaded, + * otherwise only the width and height are read. * * Imlib2 doesn't support reading comment chunks like ANNO. *------------------------------------------------------------------------------*/ char -load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, - char immediate_load) +load(ImlibImage * im, ImlibProgressFunction progress, + char progress_granularity, char load_data) { + int rc; char *env; - int cancel, full, i, n, ok, y, z, gran, nexty, prevy; + int i, n, y, z, gran, nexty, prevy; unsigned char *plane[40]; ILBM ilbm; /*---------- - * Load the chunk(s) we're interested in. If full is not true, then we only + * Load the chunk(s) we're interested in. If load_data is not true, then we only * want the image size and format. *----------*/ - full = (im->loader || immediate_load || progress); - ok = loadchunks(im->real_file, &ilbm, full); - if (!ok) - return 0; + rc = loadchunks(im->real_file, &ilbm, load_data); + if (rc == 0) + return LOAD_FAIL; /*---------- * Use and check header. *----------*/ - ok = 0; - if (ilbm.bmhd.size >= 20) - { - ok = 1; + rc = LOAD_FAIL; + plane[0] = NULL; - im->w = L2RWORD(ilbm.bmhd.data); - im->h = L2RWORD(ilbm.bmhd.data + 2); - if (!IMAGE_DIMENSIONS_OK(im->w, im->h)) - { - ok = 0; - } + if (ilbm.bmhd.size < 20) + goto quit; - ilbm.depth = ilbm.bmhd.data[8]; - if (ilbm.depth < 1 - || (ilbm.depth > 8 && ilbm.depth != 24 && ilbm.depth != 32)) - ok = 0; /* Only 1 to 8, 24, or 32 planes. */ + im->w = L2RWORD(ilbm.bmhd.data); + im->h = L2RWORD(ilbm.bmhd.data + 2); + if (!IMAGE_DIMENSIONS_OK(im->w, im->h)) + goto quit; - ilbm.rle = ilbm.bmhd.data[10]; - if (ilbm.rle < 0 || ilbm.rle > 1) - ok = 0; /* Only NONE or RLE compression. */ + ilbm.depth = ilbm.bmhd.data[8]; + if (ilbm.depth < 1 + || (ilbm.depth > 8 && ilbm.depth != 24 && ilbm.depth != 32)) + goto quit; /* Only 1 to 8, 24, or 32 planes. */ - ilbm.mask = ilbm.bmhd.data[9]; + ilbm.rle = ilbm.bmhd.data[10]; + if (ilbm.rle < 0 || ilbm.rle > 1) + goto quit; /* Only NONE or RLE compression. */ - if (ilbm.mask || ilbm.depth == 32) - SET_FLAG(im->flags, F_HAS_ALPHA); - else - UNSET_FLAG(im->flags, F_HAS_ALPHA); + ilbm.mask = ilbm.bmhd.data[9]; - env = getenv("IMLIB2_LBM_NOMASK"); - if (env - && (!strcmp(env, "true") || !strcmp(env, "1") || !strcmp(env, "yes") - || !strcmp(env, "on"))) - UNSET_FLAG(im->flags, F_HAS_ALPHA); + if (ilbm.mask || ilbm.depth == 32) + SET_FLAG(im->flags, F_HAS_ALPHA); + else + UNSET_FLAG(im->flags, F_HAS_ALPHA); - ilbm.ham = 0; - ilbm.hbrite = 0; - if (ilbm.depth <= 8) - { - if (ilbm.camg.size == 4) - { - if (ilbm.camg.data[2] & 0x08) - ilbm.ham = 1; - if (ilbm.camg.data[3] & 0x80) - ilbm.hbrite = 1; - } - else - { /* Only guess at ham and hbrite if CMAP is present. */ - if (ilbm.depth == 6 && full && ilbm.cmap.size >= 3 * 16) - ilbm.ham = 1; - if (full && !ilbm.ham && ilbm.depth > 1 - && ilbm.cmap.size == 3 * (1 << (ilbm.depth - 1))) - ilbm.hbrite = 1; - } - } + env = getenv("IMLIB2_LBM_NOMASK"); + if (env + && (!strcmp(env, "true") || !strcmp(env, "1") || !strcmp(env, "yes") + || !strcmp(env, "on"))) + UNSET_FLAG(im->flags, F_HAS_ALPHA); + + if (!load_data) + { + rc = LOAD_SUCCESS; + goto quit; } - if (!full || !ok) + + ilbm.ham = 0; + ilbm.hbrite = 0; + if (ilbm.depth <= 8) { - freeilbm(&ilbm); - return ok; + if (ilbm.camg.size == 4) + { + if (ilbm.camg.data[2] & 0x08) + ilbm.ham = 1; + if (ilbm.camg.data[3] & 0x80) + ilbm.hbrite = 1; + } + else + { /* Only guess at ham and hbrite if CMAP is present. */ + if (ilbm.depth == 6 && ilbm.cmap.size >= 3 * 16) + ilbm.ham = 1; + if (!ilbm.ham && ilbm.depth > 1 + && ilbm.cmap.size == 3 * (1 << (ilbm.depth - 1))) + ilbm.hbrite = 1; + } } /*---------- @@ -536,72 +534,72 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, * from each plane are interleaved, from top to bottom. The first plane is the * 0 bit. *----------*/ - ok = 0; - cancel = 0; plane[0] = NULL; gran = nexty = 0; __imlib_AllocateData(im); + if (!im->data) + goto quit; + n = ilbm.depth; if (ilbm.mask == 1) n++; plane[0] = malloc(((im->w + 15) / 16) * 2 * n); - if (im->data && plane[0]) - { - for (i = 1; i < n; i++) - plane[i] = plane[i - 1] + ((im->w + 15) / 16) * 2; + if (!plane[0]) + goto quit; - z = ((im->w + 15) / 16) * 2 * n; + for (i = 1; i < n; i++) + plane[i] = plane[i - 1] + ((im->w + 15) / 16) * 2; - if (progress) - { - prevy = 0; - if (progress_granularity <= 0) - progress_granularity = 1; - gran = progress_granularity; - nexty = ((im->h * gran) / 100); - } + z = ((im->w + 15) / 16) * 2 * n; + + if (progress) + { + prevy = 0; + if (progress_granularity <= 0) + progress_granularity = 1; + gran = progress_granularity; + nexty = ((im->h * gran) / 100); + } - scalecmap(&ilbm); + scalecmap(&ilbm); - for (y = 0; y < im->h; y++) - { - bodyrow(plane[0], z, &ilbm); + for (y = 0; y < im->h; y++) + { + bodyrow(plane[0], z, &ilbm); - deplane(im->data + im->w * y, im->w, &ilbm, plane); - ilbm.row++; + deplane(im->data + im->w * y, im->w, &ilbm, plane); + ilbm.row++; - if (progress && (y >= nexty || y == im->h - 1)) + if (progress && (y >= nexty || y == im->h - 1)) + { + if (!progress + (im, (char)((100 * (y + 1)) / im->h), 0, prevy, im->w, y + 1)) { - if (!progress - (im, (char)((100 * (y + 1)) / im->h), 0, prevy, im->w, - y + 1)) - { - cancel = 1; - break; - } - prevy = y; - gran += progress_granularity; - nexty = ((im->h * gran) / 100); + rc = LOAD_BREAK; + goto quit; } + prevy = y; + gran += progress_granularity; + nexty = ((im->h * gran) / 100); } - - ok = !cancel; } + rc = LOAD_SUCCESS; + + quit: /*---------- * We either had a successful decode, the user cancelled, or we couldn't get * the memory for im->data or plane[0]. *----------*/ - if (!ok) + if (rc <= 0) __imlib_FreeData(im); - if (plane[0]) - free(plane[0]); + free(plane[0]); freeilbm(&ilbm); - return (cancel) ? 2 : ok; + return rc; } /*------------------------------------------------------------------------------ diff --git a/src/modules/loaders/loader_png.c b/src/modules/loaders/loader_png.c index e775c06..7509f7c 100644 --- a/src/modules/loaders/loader_png.c +++ b/src/modules/loaders/loader_png.c @@ -14,8 +14,9 @@ comment_free(ImlibImage * im, void *data) char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { + int rc; png_uint_32 w32, h32; int w, h; char hasa; @@ -24,59 +25,47 @@ load(ImlibImage * im, ImlibProgressFunction progress, png_infop info_ptr = NULL; int bit_depth, color_type, interlace_type; unsigned char buf[PNG_BYTES_TO_CHECK]; + unsigned char **lines; + int i; f = fopen(im->real_file, "rb"); if (!f) - return 0; + return LOAD_FAIL; /* read header */ + rc = LOAD_FAIL; hasa = 0; + lines = NULL; if (fread(buf, 1, PNG_BYTES_TO_CHECK, f) != PNG_BYTES_TO_CHECK) - { - fclose(f); - return 0; - } + goto quit; + if (png_sig_cmp(buf, 0, PNG_BYTES_TO_CHECK)) - { - fclose(f); - return 0; - } + goto quit; + rewind(f); png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (!png_ptr) - { - fclose(f); - return 0; - } + goto quit; + info_ptr = png_create_info_struct(png_ptr); if (!info_ptr) - { - png_destroy_read_struct(&png_ptr, NULL, NULL); - fclose(f); - return 0; - } + goto quit; + if (setjmp(png_jmpbuf(png_ptr))) - { - im->w = 0; - png_destroy_read_struct(&png_ptr, &info_ptr, NULL); - fclose(f); - return 0; - } + goto quit; + png_init_io(png_ptr, f); png_read_info(png_ptr, info_ptr); png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *) (&w32), (png_uint_32 *) (&h32), &bit_depth, &color_type, &interlace_type, NULL, NULL); if (!IMAGE_DIMENSIONS_OK(w32, h32)) - { - im->w = 0; - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - fclose(f); - return 0; - } + goto quit; + im->w = (int)w32; im->h = (int)h32; + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) hasa = 1; if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) @@ -88,34 +77,37 @@ load(ImlibImage * im, ImlibProgressFunction progress, else UNSET_FLAG(im->flags, F_HAS_ALPHA); - if (im->loader || immediate_load || progress) + if (!load_data) { - unsigned char **lines; - int i; - - w = im->w; - h = im->h; - - /* Prep for transformations... ultimately we want ARGB */ - /* expand palette -> RGB if necessary */ - if (color_type == PNG_COLOR_TYPE_PALETTE) - png_set_palette_to_rgb(png_ptr); - /* expand gray (w/reduced bits) -> 8-bit RGB if necessary */ - if ((color_type == PNG_COLOR_TYPE_GRAY) || - (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) - { - png_set_gray_to_rgb(png_ptr); - if (bit_depth < 8) - png_set_expand_gray_1_2_4_to_8(png_ptr); - } - /* expand transparency entry -> alpha channel if present */ - if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) - png_set_tRNS_to_alpha(png_ptr); - /* reduce 16bit color -> 8bit color if necessary */ - if (bit_depth > 8) - png_set_strip_16(png_ptr); - /* pack all pixels to byte boundaries */ - png_set_packing(png_ptr); + rc = LOAD_SUCCESS; + goto quit; + } + + /* Load data */ + + w = im->w; + h = im->h; + + /* Prep for transformations... ultimately we want ARGB */ + /* expand palette -> RGB if necessary */ + if (color_type == PNG_COLOR_TYPE_PALETTE) + png_set_palette_to_rgb(png_ptr); + /* expand gray (w/reduced bits) -> 8-bit RGB if necessary */ + if ((color_type == PNG_COLOR_TYPE_GRAY) || + (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) + { + png_set_gray_to_rgb(png_ptr); + if (bit_depth < 8) + png_set_expand_gray_1_2_4_to_8(png_ptr); + } + /* expand transparency entry -> alpha channel if present */ + if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) + png_set_tRNS_to_alpha(png_ptr); + /* reduce 16bit color -> 8bit color if necessary */ + if (bit_depth > 8) + png_set_strip_16(png_ptr); + /* pack all pixels to byte boundaries */ + png_set_packing(png_ptr); /* note from raster: */ /* thanks to mustapha for helping debug this on PPC Linux remotely by */ @@ -123,84 +115,70 @@ load(ImlibImage * im, ImlibProgressFunction progress, /* what the hell was up with the colors */ /* now png loading should work on big-endian machines nicely */ #ifdef WORDS_BIGENDIAN - png_set_swap_alpha(png_ptr); - if (!hasa) - png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE); + png_set_swap_alpha(png_ptr); + if (!hasa) + png_set_filler(png_ptr, 0xff, PNG_FILLER_BEFORE); #else - png_set_bgr(png_ptr); - if (!hasa) - png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); + png_set_bgr(png_ptr); + if (!hasa) + png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); #endif - if (!__imlib_AllocateData(im)) - { - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - fclose(f); - return 0; - } - lines = (unsigned char **)malloc(h * sizeof(unsigned char *)); + if (!__imlib_AllocateData(im)) + goto quit; - if (!lines) - { - __imlib_FreeData(im); - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); - fclose(f); - return 0; - } - for (i = 0; i < h; i++) - lines[i] = ((unsigned char *)(im->data)) + (i * w * sizeof(DATA32)); - if (progress) - { - int y, count, prevy, pass, number_passes, per, - nrows = 1; + lines = (unsigned char **)malloc(h * sizeof(unsigned char *)); + if (!lines) + goto quit; + + for (i = 0; i < h; i++) + lines[i] = ((unsigned char *)(im->data)) + (i * w * sizeof(DATA32)); - count = 0; - number_passes = png_set_interlace_handling(png_ptr); - for (pass = 0; pass < number_passes; pass++) + if (progress) + { + int y, count, prevy, pass, number_passes, per, + nrows = 1; + + count = 0; + number_passes = png_set_interlace_handling(png_ptr); + for (pass = 0; pass < number_passes; pass++) + { + prevy = 0; + per = 0; + for (y = 0; y < h; y += nrows) { - prevy = 0; - per = 0; - for (y = 0; y < h; y += nrows) - { - png_read_rows(png_ptr, &lines[y], NULL, nrows); + png_read_rows(png_ptr, &lines[y], NULL, nrows); - per = (((pass * h) + y) * 100) / (h * number_passes); - if ((per - count) >= progress_granularity) + per = (((pass * h) + y) * 100) / (h * number_passes); + if ((per - count) >= progress_granularity) + { + count = per; + if (!progress(im, per, 0, prevy, w, y - prevy + 1)) { - count = per; - if (!progress(im, per, 0, prevy, w, y - prevy + 1)) - { - free(lines); - png_read_end(png_ptr, info_ptr); - png_destroy_read_struct(&png_ptr, &info_ptr, - (png_infopp) NULL); - fclose(f); - return 2; - } - prevy = y + 1; + rc = LOAD_BREAK; + goto quit1; } + prevy = y + 1; } - if (!progress(im, per, 0, prevy, w, y - prevy + 1)) - { - free(lines); - png_read_end(png_ptr, info_ptr); - png_destroy_read_struct(&png_ptr, &info_ptr, - (png_infopp) NULL); - fclose(f); - return 2; - } + } + if (!progress(im, per, 0, prevy, w, y - prevy + 1)) + { + rc = LOAD_BREAK; + goto quit1; } } - else - png_read_image(png_ptr, lines); - free(lines); - png_read_end(png_ptr, info_ptr); } + else + { + png_read_image(png_ptr, lines); + } + + rc = LOAD_SUCCESS; + #ifdef PNG_TEXT_SUPPORTED { png_textp text; int num; - int i; num = 0; png_get_text(png_ptr, info_ptr, &text, &num); @@ -212,9 +190,17 @@ load(ImlibImage * im, ImlibProgressFunction progress, } } #endif - png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); + + quit1: + png_read_end(png_ptr, info_ptr); + quit: + free(lines); + png_destroy_read_struct(&png_ptr, &info_ptr, NULL); + if (rc <= 0) + __imlib_FreeData(im); fclose(f); - return 1; + + return rc; } char diff --git a/src/modules/loaders/loader_pnm.c b/src/modules/loaders/loader_pnm.c index 9c57f42..a7fd178 100644 --- a/src/modules/loaders/loader_pnm.c +++ b/src/modules/loaders/loader_pnm.c @@ -28,28 +28,27 @@ do_progress(ImlibImage * im, ImlibProgressFunction progress, char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { int rc; + FILE *f; char p = ' ', numbers = 3, count = 0; int w = 0, h = 0, v = 255, c = 0; char buf[256]; - FILE *f = NULL; + DATA8 *data = NULL; /* for the binary versions */ + DATA8 *ptr = NULL; + int *idata = NULL; /* for the ASCII versions */ + DATA32 *ptr2, rval, gval, bval; + int i, j, x, y, pl = 0; + char pper = 0; f = fopen(im->real_file, "rb"); if (!f) - return 0; - - /* can't use fgets(), because there might be - * binary data after the header and there - * needn't be a newline before the data, so - * no chance to distinguish between end of buffer - * and a binary 0. - */ + return LOAD_FAIL; /* read the header info */ - rc = 0; /* Error */ + rc = LOAD_FAIL; c = fgetc(f); if (c != 'P') @@ -83,7 +82,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, /* no comment -> proceed */ else { - int i = 0; + i = 0; /* read numbers */ while (c != EOF && !isspace(c) && (i < 255)) @@ -126,307 +125,288 @@ load(ImlibImage * im, ImlibProgressFunction progress, else UNSET_FLAG(im->flags, F_HAS_ALPHA); - rc = 1; /* Ok */ + if (!load_data) + { + rc = LOAD_SUCCESS; + goto quit; + } - if (im->loader || immediate_load || progress) + /* Load data */ + + ptr2 = __imlib_AllocateData(im); + if (!ptr2) + goto quit; + + /* start reading the data */ + switch (p) { - DATA8 *data = NULL; /* for the binary versions */ - DATA8 *ptr = NULL; - int *idata = NULL; /* for the ASCII versions */ - DATA32 *ptr2, rval, gval, bval; - int i, j, x, y, pl = 0; - char pper = 0; - - ptr2 = __imlib_AllocateData(im); - if (!ptr2) - goto quit_error; - - /* start reading the data */ - switch (p) + case '1': /* ASCII monochrome */ + for (y = 0; y < h; y++) { - case '1': /* ASCII monochrome */ - for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { - for (x = 0; x < w; x++) + j = fscanf(f, "%u", &gval); + if (j <= 0) + goto quit; + + if (gval == 1) + *ptr2++ = 0xff000000; + else if (gval == 0) + *ptr2++ = 0xffffffff; + else + goto quit; + } + if (progress && + do_progress(im, progress, progress_granularity, &pper, &pl, y)) + goto quit_progress; + } + break; + case '2': /* ASCII greyscale */ + for (y = 0; y < h; y++) + { + for (x = 0; x < w; x++) + { + j = fscanf(f, "%u", &gval); + if (j <= 0) + goto quit; + + if (v == 0 || v == 255) { - j = fscanf(f, "%u", &gval); - if (j <= 0) - goto quit_error; - - if (gval == 1) - *ptr2++ = 0xff000000; - else if (gval == 0) - *ptr2++ = 0xffffffff; - else - goto quit_error; + *ptr2++ = 0xff000000 | (gval << 16) | (gval << 8) | gval; + } + else + { + *ptr2++ = + 0xff000000 | (((gval * 255) / v) << 16) | + (((gval * 255) / v) << 8) | ((gval * 255) / v); } - if (progress && - do_progress(im, progress, progress_granularity, - &pper, &pl, y)) - goto quit_progress; } - break; - case '2': /* ASCII greyscale */ - for (y = 0; y < h; y++) + + if (progress && + do_progress(im, progress, progress_granularity, &pper, &pl, y)) + goto quit_progress; + } + break; + case '3': /* ASCII RGB */ + for (y = 0; y < h; y++) + { + for (x = 0; x < w; x++) { - for (x = 0; x < w; x++) + j = fscanf(f, "%u %u %u", &rval, &gval, &bval); + if (j <= 2) + goto quit; + + if (v == 0 || v == 255) { - j = fscanf(f, "%u", &gval); - if (j <= 0) - goto quit_error; - - if (v == 0 || v == 255) - { - *ptr2++ = - 0xff000000 | (gval << 16) | (gval << 8) | gval; - } - else - { - *ptr2++ = - 0xff000000 | (((gval * 255) / v) << 16) | - (((gval * 255) / v) << 8) | ((gval * 255) / v); - } + *ptr2++ = 0xff000000 | (rval << 16) | (gval << 8) | bval; + } + else + { + *ptr2++ = + 0xff000000 | + (((rval * 255) / v) << 16) | + (((gval * 255) / v) << 8) | ((bval * 255) / v); } - - if (progress && - do_progress(im, progress, progress_granularity, - &pper, &pl, y)) - goto quit_progress; } - break; - case '3': /* ASCII RGB */ - for (y = 0; y < h; y++) + if (progress && + do_progress(im, progress, progress_granularity, &pper, &pl, y)) + goto quit_progress; + } + break; + case '4': /* binary 1bit monochrome */ + data = malloc((w + 7) / 8 * sizeof(DATA8)); + if (!data) + goto quit; + + ptr2 = im->data; + for (y = 0; y < h; y++) + { + if (!fread(data, (w + 7) / 8, 1, f)) + goto quit; + + ptr = data; + for (x = 0; x < w; x += 8) { - for (x = 0; x < w; x++) + j = (w - x >= 8) ? 8 : w - x; + for (i = 0; i < j; i++) { - j = fscanf(f, "%u %u %u", &rval, &gval, &bval); - if (j <= 2) - goto quit_error; - - if (v == 0 || v == 255) - { - *ptr2++ = - 0xff000000 | (rval << 16) | (gval << 8) | bval; - } + if (ptr[0] & (0x80 >> i)) + *ptr2 = 0xff000000; else - { - *ptr2++ = - 0xff000000 | - (((rval * 255) / v) << 16) | - (((gval * 255) / v) << 8) | ((bval * 255) / v); - } + *ptr2 = 0xffffffff; + ptr2++; } - if (progress && - do_progress(im, progress, progress_granularity, - &pper, &pl, y)) - goto quit_progress; + ptr++; } - break; - case '4': /* binary 1bit monochrome */ - data = malloc((w + 7) / 8 * sizeof(DATA8)); - if (!data) - goto quit_error; - - ptr2 = im->data; - for (y = 0; y < h; y++) - { - if (!fread(data, (w + 7) / 8, 1, f)) - goto quit_error; + if (progress && + do_progress(im, progress, progress_granularity, &pper, &pl, y)) + goto quit_progress; + } + break; + case '5': /* binary 8bit grayscale GGGGGGGG */ + data = malloc(1 * sizeof(DATA8) * w); + if (!data) + goto quit; + + ptr2 = im->data; + for (y = 0; y < h; y++) + { + if (!fread(data, w * 1, 1, f)) + goto quit; - ptr = data; - for (x = 0; x < w; x += 8) + ptr = data; + if (v == 0 || v == 255) + { + for (x = 0; x < w; x++) { - j = (w - x >= 8) ? 8 : w - x; - for (i = 0; i < j; i++) - { - if (ptr[0] & (0x80 >> i)) - *ptr2 = 0xff000000; - else - *ptr2 = 0xffffffff; - ptr2++; - } + *ptr2 = + 0xff000000 | (ptr[0] << 16) | (ptr[0] << 8) | ptr[0]; + ptr2++; ptr++; } - if (progress && - do_progress(im, progress, progress_granularity, - &pper, &pl, y)) - goto quit_progress; } - break; - case '5': /* binary 8bit grayscale GGGGGGGG */ - data = malloc(1 * sizeof(DATA8) * w); - if (!data) - goto quit_error; - - ptr2 = im->data; - for (y = 0; y < h; y++) + else { - if (!fread(data, w * 1, 1, f)) - goto quit_error; - - ptr = data; - if (v == 0 || v == 255) - { - for (x = 0; x < w; x++) - { - *ptr2 = - 0xff000000 | (ptr[0] << 16) | (ptr[0] << 8) | - ptr[0]; - ptr2++; - ptr++; - } - } - else + for (x = 0; x < w; x++) { - for (x = 0; x < w; x++) - { - *ptr2 = - 0xff000000 | - (((ptr[0] * 255) / v) << 16) | - (((ptr[0] * 255) / v) << 8) | - ((ptr[0] * 255) / v); - ptr2++; - ptr++; - } + *ptr2 = + 0xff000000 | + (((ptr[0] * 255) / v) << 16) | + (((ptr[0] * 255) / v) << 8) | ((ptr[0] * 255) / v); + ptr2++; + ptr++; } - if (progress && - do_progress(im, progress, progress_granularity, - &pper, &pl, y)) - goto quit_progress; } - break; - case '6': /* 24bit binary RGBRGBRGB */ - data = malloc(3 * sizeof(DATA8) * w); - if (!data) - goto quit_error; - - ptr2 = im->data; - for (y = 0; y < h; y++) - { - if (!fread(data, w * 3, 1, f)) - goto quit_error; + if (progress && + do_progress(im, progress, progress_granularity, &pper, &pl, y)) + goto quit_progress; + } + break; + case '6': /* 24bit binary RGBRGBRGB */ + data = malloc(3 * sizeof(DATA8) * w); + if (!data) + goto quit; - ptr = data; - if (v == 0 || v == 255) - { - for (x = 0; x < w; x++) - { - *ptr2 = - 0xff000000 | (ptr[0] << 16) | (ptr[1] << 8) | - ptr[2]; - ptr2++; - ptr += 3; - } - } - else + ptr2 = im->data; + for (y = 0; y < h; y++) + { + if (!fread(data, w * 3, 1, f)) + goto quit; + + ptr = data; + if (v == 0 || v == 255) + { + for (x = 0; x < w; x++) { - for (x = 0; x < w; x++) - { - *ptr2 = - 0xff000000 | - (((ptr[0] * 255) / v) << 16) | - (((ptr[1] * 255) / v) << 8) | - ((ptr[2] * 255) / v); - ptr2++; - ptr += 3; - } + *ptr2 = + 0xff000000 | (ptr[0] << 16) | (ptr[1] << 8) | ptr[2]; + ptr2++; + ptr += 3; } - if (progress && - do_progress(im, progress, progress_granularity, - &pper, &pl, y)) - goto quit_progress; } - break; - case '7': /* XV's 8bit 332 format */ - data = malloc(1 * sizeof(DATA8) * w); - if (!data) - goto quit_error; - - ptr2 = im->data; - for (y = 0; y < h; y++) + else { - if (!fread(data, w * 1, 1, f)) - goto quit_error; - - ptr = data; for (x = 0; x < w; x++) { - int r, g, b; - - r = (*ptr >> 5) & 0x7; - g = (*ptr >> 2) & 0x7; - b = (*ptr) & 0x3; *ptr2 = 0xff000000 | - (((r << 21) | (r << 18) | (r << 15)) & 0xff0000) | - (((g << 13) | (g << 10) | (g << 7)) & 0xff00) | - ((b << 6) | (b << 4) | (b << 2) | (b << 0)); + (((ptr[0] * 255) / v) << 16) | + (((ptr[1] * 255) / v) << 8) | ((ptr[2] * 255) / v); ptr2++; - ptr++; + ptr += 3; } - if (progress && - do_progress(im, progress, progress_granularity, - &pper, &pl, y)) - goto quit_progress; } - break; - case '8': /* 24bit binary RGBARGBARGBA */ - data = malloc(4 * sizeof(DATA8) * w); - if (!data) - goto quit_error; - - ptr2 = im->data; - for (y = 0; y < h; y++) + if (progress && + do_progress(im, progress, progress_granularity, &pper, &pl, y)) + goto quit_progress; + } + break; + case '7': /* XV's 8bit 332 format */ + data = malloc(1 * sizeof(DATA8) * w); + if (!data) + goto quit; + + ptr2 = im->data; + for (y = 0; y < h; y++) + { + if (!fread(data, w * 1, 1, f)) + goto quit; + + ptr = data; + for (x = 0; x < w; x++) { - if (!fread(data, w * 4, 1, f)) - goto quit_error; + int r, g, b; + + r = (*ptr >> 5) & 0x7; + g = (*ptr >> 2) & 0x7; + b = (*ptr) & 0x3; + *ptr2 = + 0xff000000 | + (((r << 21) | (r << 18) | (r << 15)) & 0xff0000) | + (((g << 13) | (g << 10) | (g << 7)) & 0xff00) | + ((b << 6) | (b << 4) | (b << 2) | (b << 0)); + ptr2++; + ptr++; + } + if (progress && + do_progress(im, progress, progress_granularity, &pper, &pl, y)) + goto quit_progress; + } + break; + case '8': /* 24bit binary RGBARGBARGBA */ + data = malloc(4 * sizeof(DATA8) * w); + if (!data) + goto quit; - ptr = data; - if (v == 0 || v == 255) + ptr2 = im->data; + for (y = 0; y < h; y++) + { + if (!fread(data, w * 4, 1, f)) + goto quit; + + ptr = data; + if (v == 0 || v == 255) + { + for (x = 0; x < w; x++) { - for (x = 0; x < w; x++) - { - *ptr2 = PIXEL_ARGB(ptr[3], ptr[0], ptr[1], ptr[2]); - ptr2++; - ptr += 4; - } + *ptr2 = PIXEL_ARGB(ptr[3], ptr[0], ptr[1], ptr[2]); + ptr2++; + ptr += 4; } - else + } + else + { + for (x = 0; x < w; x++) { - for (x = 0; x < w; x++) - { - *ptr2 = - PIXEL_ARGB((ptr[3] * 255) / v, - (ptr[0] * 255) / v, - (ptr[1] * 255) / v, - (ptr[2] * 255) / v); - ptr2++; - ptr += 4; - } + *ptr2 = + PIXEL_ARGB((ptr[3] * 255) / v, + (ptr[0] * 255) / v, + (ptr[1] * 255) / v, (ptr[2] * 255) / v); + ptr2++; + ptr += 4; } - if (progress && - do_progress(im, progress, progress_granularity, - &pper, &pl, y)) - goto quit_progress; } - break; - default: - quit_error: - rc = 0; - break; - quit_progress: - rc = 2; - break; + if (progress && + do_progress(im, progress, progress_granularity, &pper, &pl, y)) + goto quit_progress; } - if (idata) - free(idata); - if (data) - free(data); + break; + default: + goto quit; + quit_progress: + rc = LOAD_BREAK; + goto quit; } + + rc = LOAD_SUCCESS; + quit: - fclose(f); + free(idata); + free(data); + if (rc == 0) __imlib_FreeData(im); + fclose(f); return rc; } diff --git a/src/modules/loaders/loader_tga.c b/src/modules/loaders/loader_tga.c index d8cd55e..ffd8869 100644 --- a/src/modules/loaders/loader_tga.c +++ b/src/modules/loaders/loader_tga.c @@ -180,7 +180,7 @@ save(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity) char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { int fd, rc; void *seg, *filedata; @@ -198,9 +198,9 @@ load(ImlibImage * im, ImlibProgressFunction progress, fd = open(im->real_file, O_RDONLY); if (fd < 0) - return 0; + return LOAD_FAIL; - rc = 0; /* Error */ + rc = LOAD_FAIL; seg = MAP_FAILED; if (fstat(fd, &ss) < 0) @@ -301,9 +301,9 @@ load(ImlibImage * im, ImlibProgressFunction progress, else UNSET_FLAG(im->flags, F_HAS_ALPHA); - if (!(im->loader || immediate_load || progress)) + if (!load_data) { - rc = 1; + rc = LOAD_SUCCESS; goto quit; } @@ -572,18 +572,17 @@ load(ImlibImage * im, ImlibProgressFunction progress, } if (progress) - { - progress(im, 100, 0, 0, im->w, im->h); - } + progress(im, 100, 0, 0, im->w, im->h); - rc = 1; /* Success */ + rc = LOAD_SUCCESS; quit: - if (rc == 0) + if (rc <= 0) __imlib_FreeData(im); if (seg != MAP_FAILED) munmap(seg, ss.st_size); close(fd); + return rc; } diff --git a/src/modules/loaders/loader_tiff.c b/src/modules/loaders/loader_tiff.c index 626db83..585a873 100644 --- a/src/modules/loaders/loader_tiff.c +++ b/src/modules/loaders/loader_tiff.c @@ -258,61 +258,61 @@ raster(TIFFRGBAImage_Extra * img, uint32 * rast, char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { + int rc; + FILE *f; TIFF *tif = NULL; - FILE *file; - int fd, ok; + int fd; uint16 magic_number; TIFFRGBAImage_Extra rgba_image; uint32 *rast = NULL; uint32 num_pixels; char txt[1024]; - ok = 0; + f = fopen(im->real_file, "rb"); + if (!f) + return LOAD_FAIL; - file = fopen(im->real_file, "rb"); - if (!file) - return 0; + rc = LOAD_FAIL; + rgba_image.image = NULL; + + if (fread(&magic_number, sizeof(uint16), 1, f) != 1) + goto quit; - if (fread(&magic_number, sizeof(uint16), 1, file) != 1) - { - fclose(file); - return 0; - } /* Apparently rewind(f) isn't sufficient */ - fseek(file, (long)0, SEEK_SET); + fseek(f, 0, SEEK_SET); if ((magic_number != TIFF_BIGENDIAN) /* Checks if actually tiff file */ && (magic_number != TIFF_LITTLEENDIAN)) - { - fclose(file); - return 0; - } + goto quit; - fd = fileno(file); + fd = fileno(f); fd = dup(fd); - lseek(fd, (long)0, SEEK_SET); - fclose(file); + lseek(fd, 0, SEEK_SET); + fclose(f); + f = NULL; tif = TIFFFdOpen(fd, im->real_file, "r"); if (!tif) - return 0; + goto quit; strcpy(txt, "Cannot be processed by libtiff"); if (!TIFFRGBAImageOK(tif, txt)) - goto quit1; + goto quit; + strcpy(txt, "Cannot begin reading tiff"); if (!TIFFRGBAImageBegin((TIFFRGBAImage *) & rgba_image, tif, 1, txt)) - goto quit1; + goto quit; + + rgba_image.image = im; if (!rgba_image.rgba.put.any) { fprintf(stderr, "imlib2-tiffloader: No put function"); - goto quit2; + goto quit; } - rgba_image.image = im; switch (rgba_image.rgba.orientation) { default: @@ -332,62 +332,66 @@ load(ImlibImage * im, ImlibProgressFunction progress, break; } if (!IMAGE_DIMENSIONS_OK(im->w, im->h)) - { - im->w = 0; - goto quit2; - } + goto quit; + rgba_image.num_pixels = num_pixels = im->w * im->h; if (rgba_image.rgba.alpha != EXTRASAMPLE_UNSPECIFIED) SET_FLAG(im->flags, F_HAS_ALPHA); else UNSET_FLAG(im->flags, F_HAS_ALPHA); - if (im->loader || immediate_load || progress) + if (!load_data) { - rgba_image.progress = progress; - rgba_image.pper = rgba_image.py = 0; - rgba_image.progress_granularity = progress_granularity; - - if (!__imlib_AllocateData(im)) - goto quit2; + rc = LOAD_SUCCESS; + goto quit; + } - rast = (uint32 *) _TIFFmalloc(sizeof(uint32) * num_pixels); - if (!rast) - { - fprintf(stderr, "imlib2-tiffloader: Out of memory\n"); - __imlib_FreeData(im); - goto quit2; - } + /* Load data */ - if (rgba_image.rgba.isContig) - { - rgba_image.put_contig = rgba_image.rgba.put.contig; - rgba_image.rgba.put.contig = put_contig_and_raster; - } - else - { - rgba_image.put_separate = rgba_image.rgba.put.separate; - rgba_image.rgba.put.separate = put_separate_and_raster; - } + rgba_image.progress = progress; + rgba_image.pper = rgba_image.py = 0; + rgba_image.progress_granularity = progress_granularity; - if (!TIFFRGBAImageGet((TIFFRGBAImage *) & rgba_image, rast, - rgba_image.rgba.width, rgba_image.rgba.height)) - { - _TIFFfree(rast); - __imlib_FreeData(im); - goto quit2; - } + if (!__imlib_AllocateData(im)) + goto quit; - _TIFFfree(rast); + rast = (uint32 *) _TIFFmalloc(sizeof(uint32) * num_pixels); + if (!rast) + { + fprintf(stderr, "imlib2-tiffloader: Out of memory\n"); + goto quit; } - ok = 1; - quit2: - TIFFRGBAImageEnd((TIFFRGBAImage *) & rgba_image); - quit1: - TIFFClose(tif); + if (rgba_image.rgba.isContig) + { + rgba_image.put_contig = rgba_image.rgba.put.contig; + rgba_image.rgba.put.contig = put_contig_and_raster; + } + else + { + rgba_image.put_separate = rgba_image.rgba.put.separate; + rgba_image.rgba.put.separate = put_separate_and_raster; + } - return ok; + if (!TIFFRGBAImageGet((TIFFRGBAImage *) & rgba_image, rast, + rgba_image.rgba.width, rgba_image.rgba.height)) + goto quit; + + rc = LOAD_SUCCESS; + + quit: + if (rast) + _TIFFfree(rast); + if (rc <= 0) + __imlib_FreeData(im); + if (rgba_image.image) + TIFFRGBAImageEnd((TIFFRGBAImage *) & rgba_image); + if (tif) + TIFFClose(tif); + if (f) + fclose(f); + + return rc; } /* this seems to work, except the magic number isn't written. I'm guessing */ diff --git a/src/modules/loaders/loader_webp.c b/src/modules/loaders/loader_webp.c index 0f1b2dd..626ee58 100644 --- a/src/modules/loaders/loader_webp.c +++ b/src/modules/loaders/loader_webp.c @@ -33,8 +33,9 @@ webp_strerror(VP8StatusCode code) char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { + int rc; uint8_t *encoded_data; struct stat stats; int encoded_fd; @@ -42,51 +43,36 @@ load(ImlibImage * im, ImlibProgressFunction progress, VP8StatusCode vp8return; if (stat(im->real_file, &stats) < 0) - { - return 0; - } + return LOAD_FAIL; encoded_fd = open(im->real_file, O_RDONLY); if (encoded_fd < 0) - { - return 0; - } + return LOAD_FAIL; - encoded_data = malloc(stats.st_size); + rc = LOAD_FAIL; - if (encoded_data == NULL) - { - close(encoded_fd); - return 0; - } + encoded_data = malloc(stats.st_size); + if (!encoded_data) + goto quit; if (read(encoded_fd, encoded_data, stats.st_size) < stats.st_size) - { - free(encoded_data); - close(encoded_fd); - return 0; - } + goto quit; + close(encoded_fd); + encoded_fd = -1; if (WebPGetInfo(encoded_data, stats.st_size, &im->w, &im->h) == 0) - { - free(encoded_data); - return 0; - } + goto quit; if (!IMAGE_DIMENSIONS_OK(im->w, im->h)) - { - free(encoded_data); - return 0; - } + goto quit; vp8return = WebPGetFeatures(encoded_data, stats.st_size, &features); if (vp8return != VP8_STATUS_OK) { fprintf(stderr, "%s: Error reading file header: %s\n", im->real_file, webp_strerror(vp8return)); - free(encoded_data); - return 0; + goto quit; } if (features.has_alpha == 0) @@ -94,26 +80,34 @@ load(ImlibImage * im, ImlibProgressFunction progress, else SET_FLAG(im->flags, F_HAS_ALPHA); - if (im->loader || immediate_load || progress) + if (!load_data) { - size_t webp_buffer_size = sizeof(DATA32) * im->w * im->h; + rc = LOAD_SUCCESS; + goto quit; + } - __imlib_AllocateData(im); - if (WebPDecodeBGRAInto(encoded_data, stats.st_size, - (uint8_t *) im->data, webp_buffer_size, - im->w * 4) == NULL) - { - free(encoded_data); - __imlib_FreeData(im); - return 0; - } + /* Load data */ - if (progress) - progress(im, 100, 0, 0, im->w, im->h); - } + if (!__imlib_AllocateData(im)) + goto quit; + if (WebPDecodeBGRAInto(encoded_data, stats.st_size, (uint8_t *) im->data, + sizeof(DATA32) * im->w * im->h, im->w * 4) == NULL) + goto quit; + + if (progress) + progress(im, 100, 0, 0, im->w, im->h); + + rc = LOAD_SUCCESS; + + quit: + if (rc <= 0) + __imlib_FreeData(im); free(encoded_data); - return 1; /* Success */ + if (encoded_fd >= 0) + close(encoded_fd); + + return rc; } char diff --git a/src/modules/loaders/loader_xpm.c b/src/modules/loaders/loader_xpm.c index fc461bd..a3bbc3f 100644 --- a/src/modules/loaders/loader_xpm.c +++ b/src/modules/loaders/loader_xpm.c @@ -128,8 +128,8 @@ xpm_cmap_lookup(const cmap_t * cmap, int nc, int cpp, const char *s) } char -load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, - char immediate_load) +load(ImlibImage * im, ImlibProgressFunction progress, + char progress_granularity, char load_data) { int rc; DATA32 *ptr; @@ -144,16 +144,16 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, int last_per = 0, last_y = 0; int count, pixels; - rc = 0; + f = fopen(im->real_file, "rb"); + if (!f) + return LOAD_FAIL; + + rc = LOAD_FAIL; done = 0; transp = -1; line = NULL; cmap = NULL; - f = fopen(im->real_file, "rb"); - if (!f) - return 0; - len = fread(s, 1, sizeof(s) - 1, f); if (len < 9) goto quit; @@ -243,19 +243,17 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, per_inc = 100.0 / (((float)w) * h); - if (im->loader || immediate_load || progress) - { - ptr = __imlib_AllocateData(im); - if (!ptr) - goto quit; - pixels = w * h; - } - else + if (!load_data) { - rc = 1; + rc = LOAD_SUCCESS; goto quit; } + ptr = __imlib_AllocateData(im); + if (!ptr) + goto quit; + pixels = w * h; + j = 0; context++; } @@ -396,7 +394,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, last_per = (int)per; if (!(progress(im, (int)per, 0, last_y, w, i))) { - rc = 2; + rc = LOAD_BREAK; goto quit; } last_y = i; @@ -452,23 +450,17 @@ load(ImlibImage * im, ImlibProgressFunction progress, char progress_granularity, } if (transp >= 0) - { - SET_FLAG(im->flags, F_HAS_ALPHA); - } + SET_FLAG(im->flags, F_HAS_ALPHA); else - { - UNSET_FLAG(im->flags, F_HAS_ALPHA); - } + UNSET_FLAG(im->flags, F_HAS_ALPHA); if (progress) - { - progress(im, 100, 0, last_y, w, h); - } + progress(im, 100, 0, last_y, w, h); - rc = 1; + rc = LOAD_SUCCESS; quit: - if (rc == 0) + if (rc <= 0) __imlib_FreeData(im); fclose(f); diff --git a/src/modules/loaders/loader_zlib.c b/src/modules/loaders/loader_zlib.c index 3bd9389..6a4608e 100644 --- a/src/modules/loaders/loader_zlib.c +++ b/src/modules/loaders/loader_zlib.c @@ -39,7 +39,7 @@ uncompress_file(FILE * fp, int dest) char load(ImlibImage * im, ImlibProgressFunction progress, - char progress_granularity, char immediate_load) + char progress_granularity, char load_data) { ImlibLoader *loader; FILE *fp; @@ -91,7 +91,7 @@ load(ImlibImage * im, ImlibProgressFunction progress, file = im->real_file; im->real_file = strdup(tmp); - loader->load(im, progress, progress_granularity, immediate_load); + loader->load(im, progress, progress_granularity, load_data); free(im->real_file); im->real_file = file; --
