thumbs.c: warning fixes
[blender.git] / source / blender / imbuf / intern / thumbs.c
1 /**
2  * $Id$ 
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2007 Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): Andrea Weikert.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 #include "BKE_global.h"
31 #include "BKE_utildefines.h"
32 #include "BLI_blenlib.h"
33 #include "MEM_guardedalloc.h"
34
35 #include "IMB_imbuf_types.h"
36 #include "IMB_imbuf.h"
37 #include "IMB_thumbs.h"
38 #include "IMB_imginfo.h"
39
40
41 #include "md5.h"
42
43 #ifdef WIN32
44 #include <windows.h> /* need to include windows.h so _WIN32_IE is defined  */
45 #ifndef _WIN32_IE
46 #define _WIN32_IE 0x0400 /* minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already */
47 #endif
48 #include <shlobj.h> /* for SHGetSpecialFolderPath, has to be done before BLI_winstuff because 'near' is disabled through BLI_windstuff */
49 #include "BLI_winstuff.h"
50 #include <process.h> /* getpid */
51 #include <direct.h> /* chdir */
52 #else
53 #include <unistd.h>
54 #endif
55
56 #include <stdlib.h>
57 #include <string.h>
58 #include <stdio.h>
59 #include <time.h>
60 #include <sys/types.h>
61 #include <sys/stat.h>
62 #include <stdio.h>
63
64 #define URI_MAX FILE_MAX*3 + 8
65
66 static int get_thumb_dir( char* dir , ThumbSize size)
67 {
68 #ifdef WIN32
69         /* yes, applications shouldn't store data there, but so does GIMP :)*/
70         SHGetSpecialFolderPath(0, dir, CSIDL_PROFILE, 0);
71 #else
72         home = getenv("HOME");
73         if (!home) return 0;
74         BLI_strncpy(dir, home, FILE_MAX);
75 #endif
76         switch(size) {
77                 case THB_NORMAL:
78                         strcat(dir, "/.thumbnails/normal");
79                         break;
80                 case THB_LARGE:
81                         strcat(dir, "/.thumbnails/large");
82                         break;
83                 case THB_FAIL:
84                         strcat(dir, "/.thumbnails/fail/blender");
85                         break;
86                 default:
87                         return 0; /* unknown size */
88         }
89         BLI_cleanup_dir(G.sce, dir);
90         return 1;
91 }
92
93 /** ----- begin of adapted code from glib ---
94  * The following code is adapted from function g_escape_uri_string from the gnome glib
95  * Source: http://svn.gnome.org/viewcvs/glib/trunk/glib/gconvert.c?view=markup
96  * released under the Gnu General Public License.
97  */
98 typedef enum {
99   UNSAFE_ALL        = 0x1,  /* Escape all unsafe characters   */
100   UNSAFE_ALLOW_PLUS = 0x2,  /* Allows '+'  */
101   UNSAFE_PATH       = 0x8,  /* Allows '/', '&', '=', ':', '@', '+', '$' and ',' */
102   UNSAFE_HOST       = 0x10, /* Allows '/' and ':' and '@' */
103   UNSAFE_SLASHES    = 0x20  /* Allows all characters except for '/' and '%' */
104 } UnsafeCharacterSet;
105
106 static const unsigned char acceptable[96] = {
107   /* A table of the ASCII chars from space (32) to DEL (127) */
108   /*      !    "    #    $    %    &    '    (    )    *    +    ,    -    .    / */ 
109   0x00,0x3F,0x20,0x20,0x28,0x00,0x2C,0x3F,0x3F,0x3F,0x3F,0x2A,0x28,0x3F,0x3F,0x1C,
110   /* 0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? */
111   0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x38,0x20,0x20,0x2C,0x20,0x20,
112   /* @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O */
113   0x38,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
114   /* P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _ */
115   0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x20,0x3F,
116   /* `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o */
117   0x20,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,
118   /* p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~  DEL */
119   0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x3F,0x20,0x20,0x20,0x3F,0x20
120 };
121
122 static const char hex[17] = "0123456789abcdef";
123
124 /* Note: This escape function works on file: URIs, but if you want to
125  * escape something else, please read RFC-2396 */
126 void escape_uri_string (const char *string, char* escaped_string, int len,UnsafeCharacterSet mask)
127 {
128 #define ACCEPTABLE(a) ((a)>=32 && (a)<128 && (acceptable[(a)-32] & use_mask))
129
130         const char *p;
131         char *q;
132         int c;
133         UnsafeCharacterSet use_mask;
134         use_mask = mask;
135
136         for (q = escaped_string, p = string; (*p != '\0') && len; p++) {
137                 c = (unsigned char) *p;
138                 len--;
139
140                 if (!ACCEPTABLE (c)) {
141                         *q++ = '%'; /* means hex coming */
142                         *q++ = hex[c >> 4];
143                         *q++ = hex[c & 15];
144                 } else {
145                         *q++ = *p;
146                 }
147         }
148   
149         *q = '\0';
150 }
151
152 void to_hex_char(char* hexbytes, const unsigned char* bytes, int len)
153 {
154         const unsigned char *p;
155         char *q;
156
157         for (q = hexbytes, p = bytes; len; p++) {
158                 const unsigned char c = (unsigned char) *p;
159                 len--;
160                 *q++ = hex[c >> 4];
161                 *q++ = hex[c & 15];
162         }
163 }
164
165 /** ----- end of adapted code from glib --- */
166
167 static int uri_from_filename( const char *dir, const char *file, char *uri )
168 {
169         char orig_uri[URI_MAX]; 
170         const char* dirstart = dir;
171         
172 #ifdef WIN32
173         {
174                 char vol[3];
175
176                 BLI_strncpy(orig_uri, "file:///", FILE_MAX);
177                 if (strlen(dir) < 2 && dir[1] != ':') {
178                         /* not a correct absolute path */
179                         return 0;
180                 }
181                 /* on windows, using always uppercase drive/volume letter in uri */
182                 vol[0] = (unsigned char)toupper(dir[0]);
183                 vol[1] = ':';
184                 vol[2] = '\0';
185                 strcat(orig_uri, vol);
186                 dirstart += 2;
187         }
188 #else
189         BLI_strncpy(orig_uri, "file://", FILE_MAX);
190 #endif
191         strcat(orig_uri, dirstart);
192         strcat(orig_uri, file);
193         BLI_char_switch(orig_uri, '\\', '/');
194         
195 #ifdef WITH_ICONV
196         {
197                 escape_uri_string(orig_uri, uri_utf8, FILE_MAX*3+8, UNSAFE_PATH);
198                 BLI_string_to_utf8(uri_utf8, uri, NULL);
199                 char uri_utf8[FILE_MAX*3+8];
200         }
201 #else 
202         escape_uri_string(orig_uri, uri, FILE_MAX*3+8, UNSAFE_PATH);
203 #endif
204         return 1;
205 }
206
207 static void thumbname_from_uri(const char* uri, char* thumb)
208 {
209         char hexdigest[33];
210         unsigned char digest[16];
211
212         md5_buffer( uri, strlen(uri), digest);
213         hexdigest[0] = '\0';
214         to_hex_char(hexdigest, digest, 16);
215         hexdigest[32] = '\0';
216         sprintf(thumb, "%s.png", hexdigest);
217 }
218
219 static int thumbpath_from_uri(const char* uri, char* path, ThumbSize size)
220 {
221         char tmppath[FILE_MAX];
222         int rv = 0;
223
224         if (get_thumb_dir(tmppath, size)) {
225                 char thumb[40];
226                 thumbname_from_uri(uri, thumb);
227                 BLI_snprintf(path, FILE_MAX, "%s%s", tmppath, thumb);
228                 rv = 1;
229         }
230         return rv;
231 }
232
233
234 /* create thumbnail for file and returns new imbuf for thumbnail */
235 ImBuf* IMB_thumb_create(const char* dir, const char* file, ThumbSize size, ThumbSource source)
236 {
237         ImBuf *img = 0;
238         char uri[URI_MAX];
239         char desc[URI_MAX+22];
240         char tpath[FILE_MAX];
241         char tdir[FILE_MAX];
242         char wdir[FILE_MAX];
243         char temp[FILE_MAX];
244         char mtime[40];
245         char cwidth[40];
246         char cheight[40];
247         char thumb[40];
248         short tsize = 128;
249         short ex, ey;
250         float scaledx, scaledy; 
251         struct stat info;
252
253         switch(size) {
254                 case THB_NORMAL:
255                         tsize = 128;
256                         break;
257                 case THB_LARGE:
258                         tsize = 256;
259                         break;
260                 case THB_FAIL:
261                         tsize = 0;
262                         break;
263                 default:
264                         return 0; /* unknown size */
265         }
266
267         uri_from_filename(dir, file,uri);
268         thumbname_from_uri(uri, thumb);
269         if (get_thumb_dir(tdir, size)) {
270                 BLI_snprintf(tpath, FILE_MAX, "%s%s", tdir, thumb);
271                 thumb[8] = '\0'; /* shorten for tempname, not needed anymore */
272                 BLI_snprintf(temp, FILE_MAX, "%sblender_%d_%s.png", tdir, abs(getpid()), thumb);
273                 if (strncmp(thumb, dir, strlen(dir)) == 0) {
274                         return NULL;
275                 }
276                 if (size == THB_FAIL) {
277                         img = IMB_allocImBuf(0,0,32, IB_rect | IB_imginfo, 0);
278                         if (!img) return 0;
279                 } else {
280                         if (THB_SOURCE_IMAGE == source) {
281                                 BLI_getwdN(wdir);
282                                 chdir(dir);
283                                 img = IMB_loadiffname(file, IB_rect);
284                                 if (img != NULL) {
285                                         stat(file, &info);
286                                         sprintf(mtime, "%ld", info.st_mtime);
287                                         sprintf(cwidth, "%d", img->x);
288                                         sprintf(cheight, "%d", img->y);
289                                         chdir(wdir);
290                                 }
291                         } else if (THB_SOURCE_MOVIE == source) {
292                                 struct anim * anim = NULL;
293                                 BLI_getwdN(wdir);
294                                 chdir(dir);
295                                 anim = IMB_open_anim(file, IB_rect | IB_imginfo);
296                                 if (anim != NULL) {
297                                         img = IMB_anim_absolute(anim, 0);
298                                         if (img == NULL) {
299                                                 printf("not an anim; %s\n", file);
300                                         } else {
301                                                 IMB_freeImBuf(img);
302                                                 img = IMB_anim_previewframe(anim);                                              
303                                         }
304                                         IMB_free_anim(anim);
305                                 }
306                                 stat(file, &info);
307                                 sprintf(mtime, "%ld", info.st_mtime);
308                                 chdir(wdir);
309                         }
310                         if (!img) return 0;             
311
312                         if (img->x > img->y) {
313                                 scaledx = (float)tsize;
314                                 scaledy =  ( (float)img->y/(float)img->x )*tsize;
315                         }
316                         else {
317                                 scaledy = (float)tsize;
318                                 scaledx =  ( (float)img->x/(float)img->y )*tsize;
319                         }
320                         ex = (short)scaledx;
321                         ey = (short)scaledy;
322                         
323                         IMB_scaleImBuf(img, ex, ey);
324                 }
325                 sprintf(desc, "Thumbnail for %s", uri);
326                 IMB_imginfo_add_field(img, "Description", desc);
327                 IMB_imginfo_add_field(img, "Software", "Blender");
328                 IMB_imginfo_add_field(img, "Thumb::URI", uri);
329                 IMB_imginfo_add_field(img, "Thumb::MTime", mtime);
330                 if (THB_SOURCE_IMAGE == source) {
331                         IMB_imginfo_add_field(img, "Thumb::Image::Width", cwidth);
332                         IMB_imginfo_add_field(img, "Thumb::Image::Height", cheight);
333                 }
334                 img->ftype = PNG;
335                 img->depth = 32;                
336                 if (IMB_saveiff(img, temp, IB_rect | IB_imginfo)) {
337 #ifndef WIN32
338                         chmod(temp, S_IRUSR | S_IWUSR);
339 #endif
340                         BLI_rename(temp, tpath);
341                 }
342
343                 return img;
344         }
345         return img;
346 }
347
348 /* read thumbnail for file and returns new imbuf for thumbnail */
349 ImBuf* IMB_thumb_read(const char* dir, const char* file, ThumbSize size)
350 {
351         char thumb[FILE_MAX];
352         char uri[FILE_MAX*3+8];
353         ImBuf *img = 0;
354
355         if (!uri_from_filename(dir, file,uri)) {
356                 return NULL;
357         }
358         if (thumbpath_from_uri(uri, thumb, size)) {             
359                 img = IMB_loadiffname(thumb, IB_rect | IB_imginfo);
360         }
361
362         return img;
363 }
364
365 /* delete all thumbs for the file */
366 void IMB_thumb_delete(const char* dir, const char* file, ThumbSize size)
367 {
368         char thumb[FILE_MAX];
369         char uri[FILE_MAX*3+8];
370
371         if (!uri_from_filename(dir, file,uri)) {
372                 return;
373         }
374         if (thumbpath_from_uri(uri, thumb, size)) {
375                 if (strncmp(thumb, dir, strlen(dir)) == 0) {
376                         return;
377                 }
378                 if (BLI_exists(thumb)) {
379                         BLI_delete(thumb, 0, 0);
380                 }
381         }
382 }
383
384
385 /* create the thumb if necessary and manage failed and old thumbs */
386 ImBuf* IMB_thumb_manage(const char* dir, const char* file, ThumbSize size, ThumbSource source)
387 {
388         char path[FILE_MAX];
389         char thumb[FILE_MAX];
390         char uri[FILE_MAX*3+8];
391         struct stat st;
392         ImBuf* img = NULL;
393
394         BLI_join_dirfile(path, dir, file);
395         if (stat(path, &st)) {
396                 return NULL;
397         }       
398         if (!uri_from_filename(dir, file,uri)) {
399                 return NULL;
400         }
401         if (thumbpath_from_uri(uri, thumb, THB_FAIL)) {
402                 /* failure thumb exists, don't try recreating */
403                 if (BLI_exists(thumb)) {
404                         return NULL;
405                 }
406         }
407
408         if (thumbpath_from_uri(uri, thumb, size)) {
409                 if (strncmp(thumb, dir, strlen(dir)) == 0) {
410                         img = IMB_loadiffname(path, IB_rect);
411                 } else {
412                         img = IMB_loadiffname(thumb, IB_rect | IB_imginfo);
413                         if (img) {
414                                 char mtime[40];
415                                 if (!IMB_imginfo_get_field(img, "Thumb::MTime", mtime, 40)) {
416                                         /* illegal thumb, forget it! */
417                                         IMB_freeImBuf(img);
418                                         img = 0;
419                                 } else {
420                                         time_t t = atol(mtime);
421                                         if (st.st_mtime != t) {
422                                                 /* recreate all thumbs */
423                                                 IMB_freeImBuf(img);
424                                                 img = 0;
425                                                 IMB_thumb_delete(dir, file, THB_NORMAL);
426                                                 IMB_thumb_delete(dir, file, THB_LARGE);
427                                                 IMB_thumb_delete(dir, file, THB_FAIL);
428                                                 img = IMB_thumb_create(dir, file, size, source);
429                                                 if(!img){
430                                                         /* thumb creation failed, write fail thumb */
431                                                         img = IMB_thumb_create(dir, file, THB_FAIL, source);
432                                                         if (img) {
433                                                                 /* we don't need failed thumb anymore */
434                                                                 IMB_freeImBuf(img);
435                                                                 img = 0;
436                                                         }
437                                                 }
438                                         }
439                                 }
440                         } else {
441                                 img = IMB_thumb_create(dir, file, size, source);
442                                 if(!img){
443                                         /* thumb creation failed, write fail thumb */
444                                         img = IMB_thumb_create(dir, file, THB_FAIL, source);
445                                         if (img) {
446                                                 /* we don't need failed thumb anymore */
447                                                 IMB_freeImBuf(img);
448                                                 img = 0;
449                                         }
450                                 }
451                         }
452                 }
453         }
454
455         return img;
456 }
457
458