passepartout overlay wasnt written into thumbnails
[blender.git] / source / blender / windowmanager / intern / wm_files.c
index 30a37301266fadbad55baf0704c38930a6d86a36..44768116c7ce6c4acb2784afbb84a9a131f917cd 100644 (file)
 
 #include "RNA_access.h"
 
+#include "IMB_imbuf.h"
+#include "IMB_imbuf_types.h"
+#include "IMB_thumbs.h"
+
 #include "ED_datafiles.h"
 #include "ED_object.h"
 #include "ED_screen.h"
 #include "ED_sculpt.h"
+#include "ED_view3d.h"
 #include "ED_util.h"
 
 #include "GHOST_C-api.h"
@@ -286,7 +291,8 @@ void WM_read_file(bContext *C, char *name, ReportList *reports)
                
                if (retval!=0) {
                        G.relbase_valid = 1;
-                       writeBlog();
+                       if(!G.background) /* assume automated tasks with background, dont write recent file list */
+                               writeBlog();
                }
 
 // XXX         undo_editmode_clear();
@@ -485,12 +491,57 @@ static void do_history(char *name, ReportList *reports)
                BKE_report(reports, RPT_ERROR, "Unable to make version backup");
 }
 
+static ImBuf *blend_file_thumb(const char *path, Scene *scene, int **thumb_pt)
+{
+       /* will be scaled down, but gives some nice oversampling */
+       ImBuf *ibuf;
+       int *thumb;
+
+       *thumb_pt= NULL;
+       
+       if(G.background || scene->camera==NULL)
+               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));
+       }
+       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)
 {
        Library *li;
        int len;
        char di[FILE_MAX];
-       
+
+       int *thumb= NULL;
+       ImBuf *ibuf_thumb= NULL;
+
        len = strlen(target);
        
        if (len == 0) {
@@ -531,7 +582,10 @@ int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
 
        do_history(di, reports);
        
-       if (BLO_write_file(CTX_data_main(C), di, fileflags, reports)) {
+       /* blend file thumbnail */
+       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);
                G.relbase_valid = 1;
                strcpy(G.main->name, di);       /* is guaranteed current file */
@@ -545,7 +599,18 @@ int WM_write_file(bContext *C, char *target, int fileflags, ReportList *reports)
                else G.fileflags &= ~G_FILE_AUTOPLAY;
 
                writeBlog();
-       } else {
+
+               /* 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;
        }
 
@@ -570,7 +635,7 @@ int WM_write_homefile(bContext *C, wmOperator *op)
        /*  force save as regular blend file */
        fileflags = G.fileflags & ~(G_FILE_COMPRESS | G_FILE_AUTOPLAY | G_FILE_LOCK | G_FILE_SIGN);
 
-       BLO_write_file(CTX_data_main(C), tstr, fileflags, op->reports);
+       BLO_write_file(CTX_data_main(C), tstr, fileflags, op->reports, NULL);
        
        G.save_over= 0;
        
@@ -639,7 +704,7 @@ void wm_autosave_timer(const bContext *C, wmWindowManager *wm, wmTimer *wt)
        fileflags = G.fileflags & ~(G_FILE_COMPRESS|G_FILE_AUTOPLAY |G_FILE_LOCK|G_FILE_SIGN);
 
        /* no error reporting to console */
-       BLO_write_file(CTX_data_main(C), filename, fileflags, NULL);
+       BLO_write_file(CTX_data_main(C), filename, fileflags, NULL, NULL);
 
        /* do timer after file write, just in case file write takes a long time */
        wm->autosavetimer= WM_event_add_timer(wm, NULL, TIMERAUTOSAVE, U.savetime*60.0);