passepartout overlay wasnt written into thumbnails
[blender.git] / source / blender / windowmanager / intern / wm_files.c
index 91ebb5d6fb4fdda0d1e14aa37a310b69fed18389..44768116c7ce6c4acb2784afbb84a9a131f917cd 100644 (file)
@@ -491,8 +491,7 @@ static void do_history(char *name, ReportList *reports)
                BKE_report(reports, RPT_ERROR, "Unable to make version backup");
 }
 
-/* writes a thumbnail for a blendfile */
-static void writeThumb(const char *path, Scene *scene, int **thumb_pt)
+static ImBuf *blend_file_thumb(const char *path, Scene *scene, int **thumb_pt)
 {
        /* will be scaled down, but gives some nice oversampling */
        ImBuf *ibuf;
@@ -501,33 +500,37 @@ static void writeThumb(const char *path, Scene *scene, int **thumb_pt)
        *thumb_pt= NULL;
        
        if(G.background || scene->camera==NULL)
-               return;
-       
-       thumb = MEM_mallocN(((2 + (BLEN_THUMB_SIZE * BLEN_THUMB_SIZE))) * sizeof(int), "write_file thumb");
-       
+               return NULL;
+
        /* gets scaled to BLEN_THUMB_SIZE */
        ibuf= ED_view3d_draw_offscreen_imbuf_simple(scene, BLEN_THUMB_SIZE * 2, BLEN_THUMB_SIZE * 2, OB_SOLID);
        
        if(ibuf) {              
-               
+               float aspect= (scene->r.xsch*scene->r.xasp) / (scene->r.ysch*scene->r.yasp);
+
                /* dirty oversampling */
                IMB_scaleImBuf(ibuf, BLEN_THUMB_SIZE, BLEN_THUMB_SIZE);
+
+               /* add pretty overlay */
+               IMB_overlayblend_thumb(ibuf->rect, ibuf->x, ibuf->y, aspect);
                
                /* first write into thumb buffer */
+               thumb= MEM_mallocN(((2 + (BLEN_THUMB_SIZE * BLEN_THUMB_SIZE))) * sizeof(int), "write_file thumb");
+
                thumb[0] = BLEN_THUMB_SIZE;
                thumb[1] = BLEN_THUMB_SIZE;
+
                memcpy(thumb + 2, ibuf->rect, BLEN_THUMB_SIZE * BLEN_THUMB_SIZE * sizeof(int));
-               
-               /* the image is scaled here */
-               ibuf= IMB_thumb_create(path, THB_NORMAL, THB_SOURCE_BLEND, ibuf);
        }
-
-       if (ibuf) {             
-               IMB_freeImBuf(ibuf);
+       else {
+               /* '*thumb_pt' needs to stay NULL to prevent a bad thumbnail from being handled */
+               thumb= NULL;
        }
        
        /* must be freed by caller */
        *thumb_pt= thumb;
+       
+       return ibuf;
 }
 
 int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
@@ -537,6 +540,7 @@ int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
        char di[FILE_MAX];
 
        int *thumb= NULL;
+       ImBuf *ibuf_thumb= NULL;
 
        len = strlen(target);
        
@@ -579,7 +583,7 @@ int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
        do_history(di, reports);
        
        /* blend file thumbnail */
-       writeThumb(di, CTX_data_scene(C), &thumb);
+       ibuf_thumb= blend_file_thumb(di, CTX_data_scene(C), &thumb);
 
        if (BLO_write_file(CTX_data_main(C), di, fileflags, reports, thumb)) {
                strcpy(G.sce, di);
@@ -596,9 +600,16 @@ int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
 
                writeBlog();
 
+               /* run this function after because the file cant be written before the blend is */
+               if (ibuf_thumb) {
+                       ibuf_thumb= IMB_thumb_create(di, THB_NORMAL, THB_SOURCE_BLEND, ibuf_thumb);
+                       IMB_freeImBuf(ibuf_thumb);
+               }
+
                if(thumb) MEM_freeN(thumb);
        }
        else {
+               if(ibuf_thumb) IMB_freeImBuf(ibuf_thumb);
                if(thumb) MEM_freeN(thumb);
                return -1;
        }