Blender V2.27

thumbs_blend.c

00001 
00025 #include <string.h>
00026 
00027 #include "zlib.h"
00028 
00029 #include "BLI_utildefines.h"
00030 
00031 #include "BKE_utildefines.h"
00032 #include "BKE_global.h"
00033 
00034 #include "IMB_imbuf_types.h"
00035 #include "IMB_imbuf.h"
00036 #include "IMB_thumbs.h"
00037 
00038 #include "MEM_guardedalloc.h"
00039 
00040 /* extracts the thumbnail from between the 'REND' and the 'GLOB'
00041  * chunks of the header, dont use typical blend loader because its too slow */
00042 
00043 static ImBuf *loadblend_thumb(gzFile gzfile)
00044 {
00045         char buf[12];
00046         int bhead[24/sizeof(int)]; /* max size on 64bit */
00047         char endian, pointer_size;
00048         char endian_switch;
00049         int sizeof_bhead ;
00050 
00051         /* read the blend file header */
00052         if(gzread(gzfile, buf, 12) != 12)
00053                 return NULL;
00054         if(strncmp(buf, "BLENDER", 7))
00055                 return NULL;
00056 
00057         if(buf[7]=='-')
00058                 pointer_size= 8;
00059         else if(buf[7]=='_')
00060                 pointer_size= 4;
00061         else
00062                 return NULL;
00063 
00064          sizeof_bhead = 16 + pointer_size;
00065 
00066         if(buf[8]=='V')
00067                 endian= B_ENDIAN; /* big: PPC */
00068         else if(buf[8]=='v')
00069                 endian= L_ENDIAN; /* little: x86 */
00070         else
00071                 return NULL;
00072 
00073         endian_switch = ((ENDIAN_ORDER != endian)) ? 1 : 0;
00074 
00075         while(gzread(gzfile, bhead, sizeof_bhead) == sizeof_bhead) {
00076                 if(endian_switch)
00077                         SWITCH_INT(bhead[1]); /* length */
00078 
00079                 if (bhead[0]==REND) {
00080                         gzseek(gzfile, bhead[1], SEEK_CUR); /* skip to the next */
00081                 }
00082                 else {
00083                         break;
00084                 }
00085         }
00086 
00087         /* using 'TEST' since new names segfault when loading in old blenders */
00088         if(bhead[0] == TEST) {
00089                 ImBuf *img= NULL;
00090                 int size[2];
00091 
00092                 if(gzread(gzfile, size, sizeof(size)) != sizeof(size))
00093                         return NULL;
00094 
00095                 if(endian_switch) {
00096                         SWITCH_INT(size[0]);
00097                         SWITCH_INT(size[1]);
00098                 }
00099                 /* length */
00100                 bhead[1] -= sizeof(int) * 2;
00101 
00102                 /* inconsistent image size, quit early */
00103                 if(bhead[1] != size[0] * size[1] * sizeof(int))
00104                         return NULL;
00105         
00106                 /* finally malloc and read the data */
00107                 img= IMB_allocImBuf(size[0], size[1], 32, IB_rect | IB_metadata);
00108         
00109                 if(gzread(gzfile, img->rect, bhead[1]) != bhead[1]) {
00110                         IMB_freeImBuf(img);
00111                         img= NULL;
00112                 }
00113         
00114                 return img;
00115         }
00116         
00117         return NULL;
00118 }
00119 
00120 ImBuf *IMB_loadblend_thumb(const char *path)
00121 {
00122         gzFile gzfile;
00123 
00124         /* not necessarily a gzip */
00125         gzfile = gzopen(path, "rb");
00126 
00127         if (NULL == gzfile ) {
00128                 return NULL;
00129         }
00130         else {
00131                 ImBuf *img= loadblend_thumb(gzfile);
00132 
00133                 /* read ok! */
00134                 gzclose(gzfile);
00135 
00136                 return img;
00137         }
00138 }
00139 
00140 /* add a fake passepartout overlay to a byte buffer, use for blend file thumbnails */
00141 #define MARGIN 2
00142 
00143 void IMB_overlayblend_thumb(unsigned int *thumb, int width, int height, float aspect)
00144 {
00145         unsigned char *px= (unsigned char *)thumb;
00146         int margin_l = MARGIN;
00147         int margin_b = MARGIN;
00148         int margin_r = width - MARGIN;
00149         int margin_t = height - MARGIN;
00150 
00151         if(aspect < 1.0f) {
00152                 margin_l= (int)((width - ((float)width * aspect)) / 2.0f);
00153                 margin_l += MARGIN;
00154                 CLAMP(margin_l, MARGIN, (width/2));
00155                 margin_r = width - margin_l;
00156         }
00157         else if (aspect > 1.0f) {
00158                 margin_b= (int)((height - ((float)height / aspect)) / 2.0f);
00159                 margin_b += MARGIN;
00160                 CLAMP(margin_b, MARGIN, (height/2));
00161                 margin_t = height - margin_b;
00162         }
00163 
00164         {       
00165                 int x, y;
00166                 int stride_x= (margin_r - margin_l) - 2;
00167                 
00168                 for(y=0; y < height; y++) {
00169                         for(x=0; x < width; x++, px+=4) {
00170                                 int hline= 0, vline= 0;
00171                                 if((x > margin_l && x < margin_r) && (y > margin_b && y < margin_t)) {
00172                                         /* interior. skip */
00173                                         x  += stride_x;
00174                                         px += stride_x * 4;
00175                                 } else if(      (hline=(((x == margin_l || x == margin_r)) && y >= margin_b && y <= margin_t)) ||
00176                                                         (vline=(((y == margin_b || y == margin_t)) && x >= margin_l && x <= margin_r))
00177                                 ) {
00178                                         /* dashed line */
00179                                         if((hline && y % 2) || (vline && x % 2)) {
00180                                                 px[0]= px[1]= px[2]= 0;
00181                                                 px[3] = 255;
00182                                         }
00183                                 }
00184                                 else {
00185                                         /* outside, fill in alpha, like passepartout */
00186                                         px[0] *= 0.5f;
00187                                         px[1] *= 0.5f;
00188                                         px[2] *= 0.5f;
00189                                         px[3] = (px[3] * 0.5f) + 96;
00190                                 }
00191                         }
00192                 }
00193         }
00194 }