|
Blender V2.27
|
00001 00030 #include <stdio.h> 00031 00032 #include "BKE_utildefines.h" 00033 #include "BLI_blenlib.h" 00034 #include "MEM_guardedalloc.h" 00035 00036 #include "IMB_imbuf_types.h" 00037 #include "IMB_imbuf.h" 00038 #include "IMB_thumbs.h" 00039 #include "IMB_metadata.h" 00040 00041 #include "md5.h" 00042 00043 #include <ctype.h> 00044 #include <stdlib.h> 00045 #include <string.h> 00046 #include <time.h> 00047 #include <sys/types.h> 00048 #include <sys/stat.h> 00049 #include <stdio.h> 00050 00051 #ifdef WIN32 00052 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined */ 00053 #ifndef _WIN32_IE 00054 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */ 00055 #endif 00056 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */ 00057 #include <process.h> /* getpid */ 00058 #include <direct.h> /* chdir */ 00059 #include "BLI_winstuff.h" 00060 #else 00061 #include <unistd.h> 00062 #endif 00063 00064 #define URI_MAX FILE_MAX*3 + 8 00065 00066 static int get_thumb_dir( char* dir , ThumbSize size) 00067 { 00068 #ifdef WIN32 00069 /* yes, applications shouldn't store data there, but so does GIMP :)*/ 00070 SHGetSpecialFolderPath(0, dir, CSIDL_PROFILE, 0); 00071 #else 00072 char* home = getenv("HOME"); 00073 if (!home) return 0; 00074 BLI_strncpy(dir, home, FILE_MAX); 00075 #endif 00076 switch(size) { 00077 case THB_NORMAL: 00078 strcat(dir, "/.thumbnails/normal/"); 00079 break; 00080 case THB_LARGE: 00081 strcat(dir, "/.thumbnails/large/"); 00082 break; 00083 case THB_FAIL: 00084 strcat(dir, "/.thumbnails/fail/blender/"); 00085 break; 00086 default: 00087 return 0; /* unknown size */ 00088 } 00089 return 1; 00090 } 00091 00097 typedef enum { 00098 UNSAFE_ALL = 0x1, /* Escape all unsafe characters */ 00099 UNSAFE_ALLOW_PLUS = 0x2, /* Allows '+' */ 00100 UNSAFE_PATH = 0x8, /* Allows '/', '&', '=', ':', '@', '+', '$' and ',' */ 00101 UNSAFE_HOST = 0x10, /* Allows '/' and ':' and '@' */ 00102 UNSAFE_SLASHES = 0x20 /* Allows all characters except for '/' and '%' */ 00103 } UnsafeCharacterSet; 00104 00105 static const unsigned char acceptable[96] = { 00106 /* A table of the ASCII chars from space (32) to DEL (127) */ 00107 /* ! " # $ % & ' ( ) * + , - . / */ 00108 0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C, 00109 /* 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */ 00110 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20, 00111 /* @ A B C D E F G H I J K L M N O */ 00112 0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 00113 /* P Q R S T U V W X Y Z [ \ ] ^ _ */ 00114 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F, 00115 /* ` a b c d e f g h i j k l m n o */ 00116 0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F, 00117 /* p q r s t u v w x y z { | } ~ DEL */ 00118 0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20 00119 }; 00120 00121 static const char hex[17] = "0123456789abcdef"; 00122 00123 /* Note: This escape function works on file: URIs, but if you want to 00124 * escape something else, please read RFC-2396 */ 00125 void escape_uri_string (const char *string, char* escaped_string, int len,UnsafeCharacterSet mask) 00126 { 00127 #define ACCEPTABLE(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask)) 00128 00129 const char *p; 00130 char *q; 00131 int c; 00132 UnsafeCharacterSet use_mask; 00133 use_mask = mask; 00134 00135 for (q = escaped_string, p = string; (*p != '\0') && len; p++) { 00136 c = (unsigned char) *p; 00137 len--; 00138 00139 if (!ACCEPTABLE (c)) { 00140 *q++ = '%'; /* means hex coming */ 00141 *q++ = hex[c >> 4]; 00142 *q++ = hex[c & 15]; 00143 } else { 00144 *q++ = *p; 00145 } 00146 } 00147 00148 *q = '\0'; 00149 } 00150 00151 void to_hex_char(char* hexbytes, const unsigned char* bytes, int len) 00152 { 00153 const unsigned char *p; 00154 char *q; 00155 00156 for (q = hexbytes, p = bytes; len; p++) { 00157 const unsigned char c = (unsigned char) *p; 00158 len--; 00159 *q++ = hex[c >> 4]; 00160 *q++ = hex[c & 15]; 00161 } 00162 } 00163 00166 static int uri_from_filename( const char *path, char *uri ) 00167 { 00168 char orig_uri[URI_MAX]; 00169 const char* dirstart = path; 00170 00171 #ifdef WIN32 00172 { 00173 char vol[3]; 00174 00175 BLI_strncpy(orig_uri, "file:///", FILE_MAX); 00176 if (strlen(path) < 2 && path[1] != ':') { 00177 /* not a correct absolute path */ 00178 return 0; 00179 } 00180 /* on windows, using always uppercase drive/volume letter in uri */ 00181 vol[0] = (unsigned char)toupper(path[0]); 00182 vol[1] = ':'; 00183 vol[2] = '\0'; 00184 strcat(orig_uri, vol); 00185 dirstart += 2; 00186 } 00187 #else 00188 BLI_strncpy(orig_uri, "file://", FILE_MAX); 00189 #endif 00190 strcat(orig_uri, dirstart); 00191 BLI_char_switch(orig_uri, '\\', '/'); 00192 00193 #ifdef WITH_ICONV 00194 { 00195 char uri_utf8[FILE_MAX*3+8]; 00196 escape_uri_string(orig_uri, uri_utf8, FILE_MAX*3+8, UNSAFE_PATH); 00197 BLI_string_to_utf8(uri_utf8, uri, NULL); 00198 } 00199 #else 00200 escape_uri_string(orig_uri, uri, FILE_MAX*3+8, UNSAFE_PATH); 00201 #endif 00202 return 1; 00203 } 00204 00205 static void thumbname_from_uri(const char* uri, char* thumb) 00206 { 00207 char hexdigest[33]; 00208 unsigned char digest[16]; 00209 00210 md5_buffer( uri, strlen(uri), digest); 00211 hexdigest[0] = '\0'; 00212 to_hex_char(hexdigest, digest, 16); 00213 hexdigest[32] = '\0'; 00214 sprintf(thumb, "%s.png", hexdigest); 00215 } 00216 00217 static int thumbpath_from_uri(const char* uri, char* path, ThumbSize size) 00218 { 00219 char tmppath[FILE_MAX]; 00220 int rv = 0; 00221 00222 if (get_thumb_dir(tmppath, size)) { 00223 char thumb[40]; 00224 thumbname_from_uri(uri, thumb); 00225 BLI_snprintf(path, FILE_MAX, "%s%s", tmppath, thumb); 00226 rv = 1; 00227 } 00228 return rv; 00229 } 00230 00231 void IMB_thumb_makedirs() 00232 { 00233 char tpath[FILE_MAX]; 00234 if (get_thumb_dir(tpath, THB_NORMAL)) { 00235 BLI_recurdir_fileops(tpath); 00236 } 00237 if (get_thumb_dir(tpath, THB_FAIL)) { 00238 BLI_recurdir_fileops(tpath); 00239 } 00240 } 00241 00242 /* create thumbnail for file and returns new imbuf for thumbnail */ 00243 ImBuf* IMB_thumb_create(const char* path, ThumbSize size, ThumbSource source, ImBuf *img) 00244 { 00245 char uri[URI_MAX]= ""; 00246 char desc[URI_MAX+22]; 00247 char tpath[FILE_MAX]; 00248 char tdir[FILE_MAX]; 00249 char temp[FILE_MAX]; 00250 char mtime[40]= "0"; /* incase we can't stat the file */ 00251 char cwidth[40]= "0"; /* incase images have no data */ 00252 char cheight[40]= "0"; 00253 char thumb[40]; 00254 short tsize = 128; 00255 short ex, ey; 00256 float scaledx, scaledy; 00257 struct stat info; 00258 00259 switch(size) { 00260 case THB_NORMAL: 00261 tsize = 128; 00262 break; 00263 case THB_LARGE: 00264 tsize = 256; 00265 break; 00266 case THB_FAIL: 00267 tsize = 1; 00268 break; 00269 default: 00270 return 0; /* unknown size */ 00271 } 00272 00273 uri_from_filename(path, uri); 00274 thumbname_from_uri(uri, thumb); 00275 if (get_thumb_dir(tdir, size)) { 00276 BLI_snprintf(tpath, FILE_MAX, "%s%s", tdir, thumb); 00277 thumb[8] = '\0'; /* shorten for tempname, not needed anymore */ 00278 BLI_snprintf(temp, FILE_MAX, "%sblender_%d_%s.png", tdir, abs(getpid()), thumb); 00279 if (strncmp(path, tdir, strlen(tdir)) == 0) { 00280 return NULL; 00281 } 00282 if (size == THB_FAIL) { 00283 img = IMB_allocImBuf(1,1,32, IB_rect | IB_metadata); 00284 if (!img) return 0; 00285 } else { 00286 if (THB_SOURCE_IMAGE == source || THB_SOURCE_BLEND == source) { 00287 00288 /* only load if we didnt give an image */ 00289 if(img==NULL) { 00290 if(THB_SOURCE_BLEND == source) { 00291 img = IMB_loadblend_thumb(path); 00292 } 00293 else { 00294 img = IMB_loadiffname(path, IB_rect | IB_metadata); 00295 } 00296 } 00297 00298 if (img != NULL) { 00299 stat(path, &info); 00300 sprintf(mtime, "%ld", info.st_mtime); 00301 sprintf(cwidth, "%d", img->x); 00302 sprintf(cheight, "%d", img->y); 00303 } 00304 } else if (THB_SOURCE_MOVIE == source) { 00305 struct anim * anim = NULL; 00306 anim = IMB_open_anim(path, IB_rect | IB_metadata); 00307 if (anim != NULL) { 00308 img = IMB_anim_absolute(anim, 0); 00309 if (img == NULL) { 00310 printf("not an anim; %s\n", path); 00311 } else { 00312 IMB_freeImBuf(img); 00313 img = IMB_anim_previewframe(anim); 00314 } 00315 IMB_free_anim(anim); 00316 } 00317 stat(path, &info); 00318 sprintf(mtime, "%ld", info.st_mtime); 00319 } 00320 if (!img) return 0; 00321 00322 if (img->x > img->y) { 00323 scaledx = (float)tsize; 00324 scaledy = ( (float)img->y/(float)img->x )*tsize; 00325 } 00326 else { 00327 scaledy = (float)tsize; 00328 scaledx = ( (float)img->x/(float)img->y )*tsize; 00329 } 00330 ex = (short)scaledx; 00331 ey = (short)scaledy; 00332 00333 IMB_scaleImBuf(img, ex, ey); 00334 } 00335 sprintf(desc, "Thumbnail for %s", uri); 00336 IMB_metadata_change_field(img, "Description", desc); 00337 IMB_metadata_change_field(img, "Software", "Blender"); 00338 IMB_metadata_change_field(img, "Thumb::URI", uri); 00339 IMB_metadata_change_field(img, "Thumb::MTime", mtime); 00340 if (THB_SOURCE_IMAGE == source) { 00341 IMB_metadata_change_field(img, "Thumb::Image::Width", cwidth); 00342 IMB_metadata_change_field(img, "Thumb::Image::Height", cheight); 00343 } 00344 img->ftype = PNG; 00345 img->depth = 32; 00346 if (IMB_saveiff(img, temp, IB_rect | IB_metadata)) { 00347 #ifndef WIN32 00348 chmod(temp, S_IRUSR | S_IWUSR); 00349 #endif 00350 BLI_rename(temp, tpath); 00351 } 00352 00353 return img; 00354 } 00355 return img; 00356 } 00357 00358 /* read thumbnail for file and returns new imbuf for thumbnail */ 00359 ImBuf* IMB_thumb_read(const char* path, ThumbSize size) 00360 { 00361 char thumb[FILE_MAX]; 00362 char uri[FILE_MAX*3+8]; 00363 ImBuf *img = 0; 00364 00365 if (!uri_from_filename(path,uri)) { 00366 return NULL; 00367 } 00368 if (thumbpath_from_uri(uri, thumb, size)) { 00369 img = IMB_loadiffname(thumb, IB_rect | IB_metadata); 00370 } 00371 00372 return img; 00373 } 00374 00375 /* delete all thumbs for the file */ 00376 void IMB_thumb_delete(const char* path, ThumbSize size) 00377 { 00378 char thumb[FILE_MAX]; 00379 char uri[FILE_MAX*3+8]; 00380 00381 if (!uri_from_filename(path ,uri)) { 00382 return; 00383 } 00384 if (thumbpath_from_uri(uri, thumb, size)) { 00385 if (strncmp(path, thumb, strlen(thumb)) == 0) { 00386 return; 00387 } 00388 if (BLI_exists(thumb)) { 00389 BLI_delete(thumb, 0, 0); 00390 } 00391 } 00392 } 00393 00394 00395 /* create the thumb if necessary and manage failed and old thumbs */ 00396 ImBuf* IMB_thumb_manage(const char* path, ThumbSize size, ThumbSource source) 00397 { 00398 char thumb[FILE_MAX]; 00399 char uri[FILE_MAX*3+8]; 00400 struct stat st; 00401 ImBuf* img = NULL; 00402 00403 if (stat(path, &st)) { 00404 return NULL; 00405 } 00406 if (!uri_from_filename(path,uri)) { 00407 return NULL; 00408 } 00409 if (thumbpath_from_uri(uri, thumb, THB_FAIL)) { 00410 /* failure thumb exists, don't try recreating */ 00411 if (BLI_exists(thumb)) { 00412 return NULL; 00413 } 00414 } 00415 00416 if (thumbpath_from_uri(uri, thumb, size)) { 00417 if (strncmp(path, thumb, strlen(thumb)) == 0) { 00418 img = IMB_loadiffname(path, IB_rect); 00419 } else { 00420 img = IMB_loadiffname(thumb, IB_rect | IB_metadata); 00421 if (img) { 00422 char mtime[40]; 00423 if (!IMB_metadata_get_field(img, "Thumb::MTime", mtime, 40)) { 00424 /* illegal thumb, forget it! */ 00425 IMB_freeImBuf(img); 00426 img = 0; 00427 } else { 00428 time_t t = atol(mtime); 00429 if (st.st_mtime != t) { 00430 /* recreate all thumbs */ 00431 IMB_freeImBuf(img); 00432 img = 0; 00433 IMB_thumb_delete(path, THB_NORMAL); 00434 IMB_thumb_delete(path, THB_LARGE); 00435 IMB_thumb_delete(path, THB_FAIL); 00436 img = IMB_thumb_create(path, size, source, NULL); 00437 if(!img){ 00438 /* thumb creation failed, write fail thumb */ 00439 img = IMB_thumb_create(path, THB_FAIL, source, NULL); 00440 if (img) { 00441 /* we don't need failed thumb anymore */ 00442 IMB_freeImBuf(img); 00443 img = 0; 00444 } 00445 } 00446 } 00447 } 00448 } else { 00449 img = IMB_thumb_create(path, size, source, NULL); 00450 if(!img){ 00451 /* thumb creation failed, write fail thumb */ 00452 img = IMB_thumb_create(path, THB_FAIL, source, NULL); 00453 if (img) { 00454 /* we don't need failed thumb anymore */ 00455 IMB_freeImBuf(img); 00456 img = 0; 00457 } 00458 } 00459 } 00460 } 00461 } 00462 00463 return img; 00464 } 00465 00466
1.7.3