From 64875e9fda7bf0c78a535e9ea354c37d8f113019 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 14 Jul 2010 20:31:11 +0000 Subject: - change blend thumbnail loading function not to use goto's - fix for some warnings --- source/blender/imbuf/intern/thumbs_blend.c | 125 +++++++++++++++++------------ 1 file changed, 74 insertions(+), 51 deletions(-) (limited to 'source/blender/imbuf/intern/thumbs_blend.c') diff --git a/source/blender/imbuf/intern/thumbs_blend.c b/source/blender/imbuf/intern/thumbs_blend.c index c5c853f0c93..2b55e87546a 100644 --- a/source/blender/imbuf/intern/thumbs_blend.c +++ b/source/blender/imbuf/intern/thumbs_blend.c @@ -37,54 +37,60 @@ /* extracts the thumbnail from between the 'REND' and the 'GLOB' * chunks of the header, dont use typical blend loader because its too slow */ -ImBuf *IMB_loadblend_thumb(const char *path) + +static ImBuf *loadblend_thumb(gzFile gzfile) { char buf[8]; int code= 0; char endian, pointer_size; char endian_switch; int len, im_len, x, y; - int *rect= NULL; + ImBuf *img= NULL; - gzFile gzfile; - - ImBuf *img; - - /* not necessarily a gzip */ - gzfile = gzopen(path, "rb"); - if (NULL == gzfile ) { - return NULL; - } - /* read the blend file header */ - if(gzread(gzfile, buf, 8) != 8) goto thumb_error; - if(strncmp(buf, "BLENDER", 7)) goto thumb_error; - - if(buf[7]=='-') pointer_size= 8; - else if(buf[7]=='_') pointer_size= 4; - else goto thumb_error; - + if(gzread(gzfile, buf, 8) != 8) + return NULL; + if(strncmp(buf, "BLENDER", 7)) + return NULL; + + if(buf[7]=='-') + pointer_size= 8; + else if(buf[7]=='_') + pointer_size= 4; + else + return NULL; + /* read the next 4 bytes, only need the first char, ignore the version */ /* endian and vertsion (ignored) */ - if(gzread(gzfile, buf, 4) != 4) goto thumb_error; - - if(buf[0]=='V') endian= B_ENDIAN; /* big: PPC */ - else if(buf[0]=='v') endian= L_ENDIAN; /* little: x86 */ - else goto thumb_error; + if(gzread(gzfile, buf, 4) != 4) + return NULL; + + if(buf[0]=='V') + endian= B_ENDIAN; /* big: PPC */ + else if(buf[0]=='v') + endian= L_ENDIAN; /* little: x86 */ + else + return NULL; while(gzread(gzfile, &code, sizeof(int)) == sizeof(int)) { endian_switch = ((ENDIAN_ORDER != endian)) ? 1 : 0; - - if(gzread(gzfile, buf, sizeof(int)) != sizeof(int)) goto thumb_error; + + if(gzread(gzfile, buf, sizeof(int)) != sizeof(int)) + return NULL; + len = *( (int *)((void *)buf) ); - if(endian_switch) SWITCH_INT(len); - + + if(endian_switch) + SWITCH_INT(len); + /* finally read the rest of the bhead struct, pointer and 2 ints */ - if(gzread(gzfile, buf, pointer_size) != pointer_size) goto thumb_error; - if(gzread(gzfile, buf, sizeof(int) * 2) != sizeof(int) * 2) goto thumb_error; + if(gzread(gzfile, buf, pointer_size) != pointer_size) + return NULL; + if(gzread(gzfile, buf, sizeof(int) * 2) != sizeof(int) * 2) + return NULL; + /* we dont actually care whats in the bhead */ - if (code==REND) { gzseek(gzfile, len, SEEK_CUR); /* skip to the next */ } @@ -92,40 +98,57 @@ ImBuf *IMB_loadblend_thumb(const char *path) break; } } - + /* using 'TEST' since new names segfault when loading in old blenders */ - if(code != TEST) goto thumb_error; - - if(gzread(gzfile, &x, sizeof(int)) != sizeof(int)) goto thumb_error; - if(gzread(gzfile, &y, sizeof(int)) != sizeof(int)) goto thumb_error; + if(code != TEST) + return NULL; + + if(gzread(gzfile, &x, sizeof(int)) != sizeof(int)) + return NULL; + if(gzread(gzfile, &y, sizeof(int)) != sizeof(int)) + return NULL; + len -= sizeof(int) * 2; - if(endian_switch) { SWITCH_INT(x); SWITCH_INT(y); } + if(endian_switch) { + SWITCH_INT(x); + SWITCH_INT(y); + } /* inconsistant image size, quit early */ im_len = x * y * sizeof(int); - if(im_len != len) goto thumb_error; + if(im_len != len) + return NULL; /* finally malloc and read the data */ - rect= MEM_mallocN(len, "imb_loadblend_thumb"); + img= IMB_allocImBuf(x, y, 32, IB_rect, 0); + + if(gzread(gzfile, img->rect, len) != len) { + IMB_freeImBuf(img); + img= NULL; + } - if(gzread(gzfile, rect, len) != len) goto thumb_error; + return img; +} - /* read ok! */ - gzclose(gzfile); +ImBuf *IMB_loadblend_thumb(const char *path) +{ + gzFile gzfile; - img = IMB_allocImBuf(x, y, 32, IB_rect | IB_metadata, 0); + /* not necessarily a gzip */ + gzfile = gzopen(path, "rb"); - memcpy(img->rect, rect, im_len); + if (NULL == gzfile ) { + return NULL; + } + else { + ImBuf *img= loadblend_thumb(gzfile); - MEM_freeN(rect); - - return img; + /* read ok! */ + gzclose(gzfile); -thumb_error: - gzclose(gzfile); - if(rect) MEM_freeN(rect); - return NULL; + return img; + } } /* add a fake passepartout overlay to a byte buffer, use for blend file thumbnails */ -- cgit v1.2.3