~siretart/ubuntu/utopic/blender/libav10

« back to all changes in this revision

Viewing changes to release/bin/blender-thumbnailer.py

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-07-23 08:54:18 UTC
  • mfrom: (14.2.16 sid)
  • mto: (14.2.19 sid)
  • mto: This revision was merged to the branch mainline in revision 42.
  • Revision ID: package-import@ubuntu.com-20120723085418-9foz30v6afaf5ffs
Tags: 2.63a-2
* debian/: Cycles support added (Closes: #658075)
  For now, this top feature has been enabled only
  on [any-amd64 any-i386] architectures because
  of OpenImageIO failing on all others
* debian/: scripts installation path changed
  from /usr/lib to /usr/share:
  + debian/patches/: patchset re-worked for path changing
  + debian/control: "Breaks" field added on yafaray-exporter

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3.2
 
2
 
 
3
# ##### BEGIN GPL LICENSE BLOCK #####
 
4
#
 
5
#  This program is free software; you can redistribute it and/or
 
6
#  modify it under the terms of the GNU General Public License
 
7
#  as published by the Free Software Foundation; either version 2
 
8
#  of the License, or (at your option) any later version.
 
9
#
 
10
#  This program is distributed in the hope that it will be useful,
 
11
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
#  GNU General Public License for more details.
 
14
#
 
15
#  You should have received a copy of the GNU General Public License
 
16
#  along with this program; if not, write to the Free Software Foundation,
 
17
#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 
18
#
 
19
# ##### END GPL LICENSE BLOCK #####
 
20
 
 
21
# <pep8 compliant>
 
22
 
 
23
"""
 
24
Thumbnailer runs with python 2.6 and 3.x.
 
25
To run automatically with nautilus:
 
26
   gconftool --type boolean --set /desktop/gnome/thumbnailers/application@x-blender/enable true
 
27
   gconftool --type string --set /desktop/gnome/thumbnailers/application@x-blender/command "/usr/share/blender/scripts/blender-thumbnailer.py %i %o"
 
28
"""
 
29
 
 
30
import struct
 
31
 
 
32
 
 
33
def open_wrapper_get():
 
34
    """ wrap OS spesific read functionality here, fallback to 'open()'
 
35
    """
 
36
 
 
37
    def open_gio(path, mode):
 
38
        g_file = gio.File(path).read()
 
39
        g_file.orig_seek = g_file.seek
 
40
 
 
41
        def new_seek(offset, whence=0):
 
42
            return g_file.orig_seek(offset, [1, 0, 2][whence])
 
43
 
 
44
        g_file.seek = new_seek
 
45
        return g_file
 
46
 
 
47
    try:
 
48
        import gio
 
49
        return open_gio
 
50
    except ImportError:
 
51
        return open
 
52
 
 
53
 
 
54
def blend_extract_thumb(path):
 
55
    import os
 
56
    open_wrapper = open_wrapper_get()
 
57
 
 
58
    # def MAKE_ID(tag): ord(tag[0])<<24 | ord(tag[1])<<16 | ord(tag[2])<<8 | ord(tag[3])
 
59
    REND = 1145980242  # MAKE_ID(b'REND')
 
60
    TEST = 1414743380  # MAKE_ID(b'TEST')
 
61
 
 
62
    blendfile = open_wrapper(path, 'rb')
 
63
 
 
64
    head = blendfile.read(12)
 
65
 
 
66
    if head[0:2] == b'\x1f\x8b':  # gzip magic
 
67
        import gzip
 
68
        blendfile.close()
 
69
        blendfile = gzip.GzipFile('', 'rb', 0, open_wrapper(path, 'rb'))
 
70
        head = blendfile.read(12)
 
71
 
 
72
    if not head.startswith(b'BLENDER'):
 
73
        blendfile.close()
 
74
        return None, 0, 0
 
75
 
 
76
    is_64_bit = (head[7] == b'-'[0])
 
77
 
 
78
    # true for PPC, false for X86
 
79
    is_big_endian = (head[8] == b'V'[0])
 
80
 
 
81
    # blender pre 2.5 had no thumbs
 
82
    if head[9:11] <= b'24':
 
83
        return None, 0, 0
 
84
 
 
85
    sizeof_bhead = 24 if is_64_bit else 20
 
86
    int_endian_pair = '>ii' if is_big_endian else '<ii'
 
87
 
 
88
    while True:
 
89
        bhead = blendfile.read(sizeof_bhead)
 
90
 
 
91
        if len(bhead) < sizeof_bhead:
 
92
            return None, 0, 0
 
93
 
 
94
        code, length = struct.unpack(int_endian_pair, bhead[0:8])  # 8 == sizeof(int) * 2
 
95
 
 
96
        if code == REND:
 
97
            blendfile.seek(length, os.SEEK_CUR)
 
98
        else:
 
99
            break
 
100
 
 
101
    if code != TEST:
 
102
        return None, 0, 0
 
103
 
 
104
    try:
 
105
        x, y = struct.unpack(int_endian_pair, blendfile.read(8))  # 8 == sizeof(int) * 2
 
106
    except struct.error:
 
107
        return None, 0, 0
 
108
 
 
109
    length -= 8  # sizeof(int) * 2
 
110
 
 
111
    if length != x * y * 4:
 
112
        return None, 0, 0
 
113
 
 
114
    image_buffer = blendfile.read(length)
 
115
 
 
116
    if len(image_buffer) != length:
 
117
        return None, 0, 0
 
118
 
 
119
    return image_buffer, x, y
 
120
 
 
121
 
 
122
def write_png(buf, width, height):
 
123
    import zlib
 
124
 
 
125
    # reverse the vertical line order and add null bytes at the start
 
126
    width_byte_4 = width * 4
 
127
    raw_data = b"".join(b'\x00' + buf[span:span + width_byte_4] for span in range((height - 1) * width * 4, -1, - width_byte_4))
 
128
 
 
129
    def png_pack(png_tag, data):
 
130
        chunk_head = png_tag + data
 
131
        return struct.pack("!I", len(data)) + chunk_head + struct.pack("!I", 0xFFFFFFFF & zlib.crc32(chunk_head))
 
132
 
 
133
    return b"".join([
 
134
        b'\x89PNG\r\n\x1a\n',
 
135
        png_pack(b'IHDR', struct.pack("!2I5B", width, height, 8, 6, 0, 0, 0)),
 
136
        png_pack(b'IDAT', zlib.compress(raw_data, 9)),
 
137
        png_pack(b'IEND', b'')])
 
138
 
 
139
 
 
140
if __name__ == '__main__':
 
141
    import sys
 
142
 
 
143
    if len(sys.argv) < 3:
 
144
        print("Expected 2 arguments <input.blend> <output.png>")
 
145
    else:
 
146
        file_in = sys.argv[-2]
 
147
 
 
148
        buf, width, height = blend_extract_thumb(file_in)
 
149
 
 
150
        if buf:
 
151
            file_out = sys.argv[-1]
 
152
 
 
153
            f = open(file_out, "wb")
 
154
            f.write(write_png(buf, width, height))
 
155
            f.close()