simplify thumbnail reading and remove some warnings
authorCampbell Barton <ideasman42@gmail.com>
Sat, 17 Jul 2010 00:38:34 +0000 (00:38 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sat, 17 Jul 2010 00:38:34 +0000 (00:38 +0000)
release/freedesktop/blender-thumbnailer.py
source/blender/editors/interface/interface_templates.c
source/blender/imbuf/intern/thumbs_blend.c
source/blender/makesrna/intern/rna_controller.c
source/blender/windowmanager/intern/wm_files.c
source/blender/windowmanager/intern/wm_operators.c

index 5274da1..27d6259 100755 (executable)
@@ -25,57 +25,54 @@ To run automatically with nautilus:
    gconftool --type string --set /desktop/gnome/thumbnailers/application@x-blender/command "blender-thumbnailer.py %i %o"
 """
 
-import os
 import struct
-import sys
 
 def blend_extract_thumb(path):
+    import os
+
     # def MAKE_ID(tag): ord(tag[0])<<24 | ord(tag[1])<<16 | ord(tag[2])<<8 | ord(tag[3])
     REND = 1145980242 # MAKE_ID(b'REND')
     TEST = 1414743380 # MAKE_ID(b'TEST')
 
     blendfile = open(path, 'rb')
 
-    head = blendfile.read(7)
+    head = blendfile.read(12)
 
     if head[0:2] == b'\x1f\x8b': # gzip magic
         import gzip
         blendfile.close()
         blendfile = gzip.open(path, 'rb')
-        head = blendfile.read(7)
+        head = blendfile.read(12)
 
-    if head != b'BLENDER':
+    if not head.startswith(b'BLENDER'):
         blendfile.close()
         return None, 0, 0
 
-    is_64_bit = (blendfile.read(1) == b'-')
+    is_64_bit = (head[7] == b'-')
 
     # true for PPC, false for X86
-    is_big_endian = (blendfile.read(1) == b'V')
-
-    # Now read the bhead chunk!!!
-    blendfile.read(3) # skip the version
+    is_big_endian = (head[8] == b'V')
 
-    sizeof_pointer = 8 if is_64_bit else 4
+    # blender pre 2.5 had no thumbs
+    if head[9:11] <= b'24':
+        return None, 0, 0
 
     sizeof_bhead = 24 if is_64_bit else 20
-    
-    int_endian = '>i' if is_big_endian else '<i'
     int_endian_pair = '>ii' if is_big_endian else '<ii'
-    
+
     while True:
-        try:
-            code, length = struct.unpack(int_endian_pair, blendfile.read(8)) # 8 == sizeof(int) * 2
-        except IOError:
+        bhead = blendfile.read(sizeof_bhead)
+
+        if len(bhead) < sizeof_bhead:
             return None, 0, 0
-        
-        # finally read the rest of the bhead struct, pointer and 2 ints
-        blendfile.seek(sizeof_bhead - 8, os.SEEK_CUR)
+
+        code, length = struct.unpack(int_endian_pair, bhead[0:8]) # 8 == sizeof(int) * 2
 
         if code == REND:
             blendfile.seek(length, os.SEEK_CUR)
         else:
             break
+            
     
     if code != TEST:
         return None, 0, 0
@@ -86,12 +83,12 @@ def blend_extract_thumb(path):
         return None, 0, 0
 
     length -= 8 # sizeof(int) * 2
-    
+
     if length != x * y * 4:
         return None, 0, 0
-    
+
     image_buffer = blendfile.read(length)
-    
+
     if len(image_buffer) != length:
         return None, 0, 0
 
@@ -104,7 +101,7 @@ def write_png(buf, width, height):
     # reverse the vertical line order and add null bytes at the start
     width_byte_4 = width * 4
     raw_data = b"".join([b'\x00' + buf[span:span + width_byte_4] for span in range((height - 1) * width * 4, -1, - width_byte_4)])
-    
+
     def png_pack(png_tag, data):
         chunk_head = png_tag + data
         return struct.pack("!I", len(data)) + chunk_head + struct.pack("!I", 0xFFFFFFFF & zlib.crc32(chunk_head))
@@ -117,6 +114,8 @@ def write_png(buf, width, height):
 
 
 if __name__ == '__main__':
+    import sys
+
     if len(sys.argv) < 2:
         print("Expected 2 arguments <input.blend> <output.png>")
     else:
index ecbdab9..53a9b85 100644 (file)
@@ -195,7 +195,7 @@ static void id_search_cb(const bContext *C, void *arg_template, char *str, uiSea
 
                                        RNA_parameter_set_lookup(&parms, "context", &C);
 
-                                       if (RNA_function_call(C, &reports, &ptr, func, &parms) == 0) {
+                                       if (RNA_function_call((bContext *)C, &reports, &ptr, func, &parms) == 0) {
                                                int* ret;
                                                RNA_parameter_get_lookup(&parms, "ret", (void **)&ret);
 
index 7f1c903..cb27b1a 100644 (file)
 
 static ImBuf *loadblend_thumb(gzFile gzfile)
 {
-       char buf[8];
-       int code= 0;
+       char buf[12];
+       int bhead[24/sizeof(int)]; /* max size on 64bit */
        char endian, pointer_size;
        char endian_switch;
-       int len, im_len, x, y;
-       ImBuf *img= NULL;
-
+       int sizeof_bhead ;
 
        /* read the blend file header */
-       if(gzread(gzfile, buf, 8) != 8)
+       if(gzread(gzfile, buf, 12) != 12)
                return NULL;
        if(strncmp(buf, "BLENDER", 7))
                return NULL;
@@ -61,38 +59,23 @@ static ImBuf *loadblend_thumb(gzFile gzfile)
        else
                return NULL;
 
-       /* read the next 4 bytes, only need the first char, ignore the version */
-       /* endian and vertsion (ignored) */
-       if(gzread(gzfile, buf, 4) != 4)
-               return NULL;
+        sizeof_bhead = 16 + pointer_size;
 
-       if(buf[0]=='V')
+       if(buf[8]=='V')
                endian= B_ENDIAN; /* big: PPC */
-       else if(buf[0]=='v')
+       else if(buf[8]=='v')
                endian= L_ENDIAN; /* little: x86 */
        else
                return NULL;
 
-       while(gzread(gzfile, &code, sizeof(int)) == sizeof(int)) {
-               endian_switch = ((ENDIAN_ORDER != endian)) ? 1 : 0;
-
-               if(gzread(gzfile, buf, sizeof(int)) != sizeof(int))
-                       return NULL;
-
-               len = *( (int *)((void *)buf) );
+       endian_switch = ((ENDIAN_ORDER != endian)) ? 1 : 0;
 
+       while(gzread(gzfile, bhead, sizeof_bhead) == sizeof_bhead) {
                if(endian_switch)
-                       SWITCH_INT(len);
+                       SWITCH_INT(bhead[1]); /* length */
 
-               /* finally read the rest of the bhead struct, pointer and 2 ints */
-               if(gzread(gzfile, buf, pointer_size) != pointer_size)
-                       return NULL;
-               if(gzread(gzfile, buf, sizeof(int) * 2) != sizeof(int) * 2)
-                       return NULL;
-
-               /* we dont actually care whats in the bhead */
-               if (code==REND) {
-                       gzseek(gzfile, len, SEEK_CUR); /* skip to the next */
+               if (bhead[0]==REND) {
+                       gzseek(gzfile, bhead[1], SEEK_CUR); /* skip to the next */
                }
                else {
                        break;
@@ -100,35 +83,36 @@ static ImBuf *loadblend_thumb(gzFile gzfile)
        }
 
        /* using 'TEST' since new names segfault when loading in old blenders */
-       if(code != TEST)
-               return NULL;
-
-       if(gzread(gzfile, &x, sizeof(int)) != sizeof(int))
-               return NULL;
-       if(gzread(gzfile, &y, sizeof(int)) != sizeof(int))
-               return NULL;
-
-       len -= sizeof(int) * 2;
-
-       if(endian_switch) {
-               SWITCH_INT(x);
-               SWITCH_INT(y);
-       }
+       if(bhead[0] == TEST) {
+               ImBuf *img= NULL;
+               int size[2];
 
-       /* inconsistant image size, quit early */
-       im_len = x * y * sizeof(int);
-       if(im_len != len)
-               return NULL;
+               if(gzread(gzfile, size, sizeof(size)) != sizeof(size))
+                       return NULL;
 
-       /* finally malloc and read the data */
-       img= IMB_allocImBuf(x, y, 32, IB_rect | IB_metadata, 0);
+               if(endian_switch) {
+                       SWITCH_INT(size[0]);
+                       SWITCH_INT(size[1]);
+               }
+               /* length */
+               bhead[1] -= sizeof(int) * 2;
 
-       if(gzread(gzfile, img->rect, len) != len) {
-               IMB_freeImBuf(img);
-               img= NULL;
+               /* inconsistant image size, quit early */
+               if(bhead[1] != size[0] * size[1] * sizeof(int))
+                       return NULL;
+       
+               /* finally malloc and read the data */
+               img= IMB_allocImBuf(size[0], size[1], 32, IB_rect | IB_metadata, 0);
+       
+               if(gzread(gzfile, img->rect, bhead[1]) != bhead[1]) {
+                       IMB_freeImBuf(img);
+                       img= NULL;
+               }
+       
+               return img;
        }
-
-       return img;
+       
+       return NULL;
 }
 
 ImBuf *IMB_loadblend_thumb(const char *path)
index c66ce53..43297f2 100644 (file)
@@ -103,6 +103,7 @@ static void rna_Controller_state_number_set(struct PointerRNA *ptr, const int va
        cont->state_mask = (1 << (value - 1));
 }
 
+#if 0 /* editable is set to false, comment for now. */
 static void rna_Controller_state_get(PointerRNA *ptr, int *values)
 {
        bController *cont= (bController *)ptr->data;
@@ -113,7 +114,6 @@ static void rna_Controller_state_get(PointerRNA *ptr, int *values)
                values[i] = (cont->state_mask & (1<<i));
 }
 
-#if 0 /* editable is set to false, comment for now. */
 static void rna_Controller_state_set(PointerRNA *ptr, const int *values)
 {
        bController *cont= (bController *)ptr->data;
index fc7bf9b..86e0811 100644 (file)
@@ -689,14 +689,14 @@ void wm_autosave_location(char *filename)
        sprintf(pidstr, "%d.blend", abs(getpid()));
        
 #ifdef WIN32
-       // XXX Need to investigate how to handle default location of '/tmp/'
-       // This is a relative directory on Windows, and it may be
-       // found. Example:
-       // Blender installed on D:\ drive, D:\ drive has D:\tmp\
-       // Now, BLI_exists() will find '/tmp/' exists, but
-       // BLI_make_file_string will create string that has it most likely on C:\
-       // through get_default_root().
-       // If there is no C:\tmp autosave fails.
+       /* XXX Need to investigate how to handle default location of '/tmp/'
+        * This is a relative directory on Windows, and it may be
+        * found. Example:
+        * Blender installed on D:\ drive, D:\ drive has D:\tmp\
+        * Now, BLI_exists() will find '/tmp/' exists, but
+        * BLI_make_file_string will create string that has it most likely on C:\
+        * through get_default_root().
+        * If there is no C:\tmp autosave fails. */
        if (!BLI_exists(U.tempdir)) {
                savedir = BLI_get_folder_create(BLENDER_USER_AUTOSAVE, NULL);
                BLI_make_file_string("/", filename, savedir, pidstr);
index 09c11bc..370dd11 100644 (file)
@@ -2702,7 +2702,7 @@ int WM_radial_control_modal(bContext *C, wmOperator *op, wmEvent *event)
        float dist;
        double new_value = RNA_float_get(op->ptr, "new_value");
        int ret = OPERATOR_RUNNING_MODAL;
-       float initial_value = RNA_float_get(op->ptr, "initial_value");
+       // float initial_value = RNA_float_get(op->ptr, "initial_value");
 
        mode = RNA_int_get(op->ptr, "mode");
        RNA_int_get_array(op->ptr, "initial_mouse", initial_mouse);