#!/usr/bin/env python3

####################################
#                                  #
#        COPYRIGHT NOTICE          #
#                                  #
# This file is a part of Victori-  #
# ous Children Studio Organizer.   #
# Or simply VCStudio. Copyright    #
# of J.Y.Amihud. But don't be sad  #
# because I released the entire    #
# project under a GNU GPL license. #
# You may use Version 3 or later.  #
# See www.gnu.org/licenses if your #
# copy has no License file. Please #
# note. Ones I used the GPL v2 for #
# it. It's no longer the case.     #
#                                  #
####################################

####################################
# This code originally was a part  #
# of Blender. But I yanked it. And #
# this version is now GPLv3, baby  #
####################################

####################################
# Changes from the previous origi- #
# nal version include.             #
# Changing the GPL license block.  #
####################################



"""
Thumbnailer runs with python 2.7 and 3.x.
To run automatically with a file manager such as Nautilus, save this file
in a directory that is listed in PATH environment variable, and create
blender.thumbnailer file in ${HOME}/.local/share/thumbnailers/ directory
with the following contents:

[Thumbnailer Entry]
TryExec=blender-thumbnailer.py
Exec=blender-thumbnailer.py %u %o
MimeType=application/x-blender;
"""

import struct


def open_wrapper_get():
    """ wrap OS specific read functionality here, fallback to 'open()'
    """

    class GFileWrapper:
        __slots__ = ("mode", "g_file")

        def __init__(self, url, mode='r'):
            self.mode = mode  # used in gzip module
            self.g_file = Gio.File.parse_name(url).read(None)

        def read(self, size):
            return self.g_file.read_bytes(size, None).get_data()

        def seek(self, offset, whence=0):
            self.g_file.seek(offset, [1, 0, 2][whence], None)
            return self.g_file.tell()

        def tell(self):
            return self.g_file.tell()

        def close(self):
            self.g_file.close(None)

    def open_local_url(url, mode='r'):
        o = urlparse(url)
        if o.scheme == '':
            path = o.path
        elif o.scheme == 'file':
            path = unquote(o.path)
        else:
            raise(IOError('URL scheme "%s" needs gi.repository.Gio module' % o.scheme))
        return open(path, mode)

    try:
        from gi.repository import Gio
        return GFileWrapper
    except ImportError:
        try:
            # Python 3
            from urllib.parse import urlparse, unquote
        except ImportError:
            # Python 2
            from urlparse import urlparse
            from urllib import unquote
        return open_local_url


def blend_extract_thumb(path):
    import os
    open_wrapper = open_wrapper_get()

    REND = b'REND'
    TEST = b'TEST'

    blendfile = open_wrapper(path, 'rb')

    head = blendfile.read(12)

    if head[0:2] == b'\x1f\x8b':  # gzip magic
        import gzip
        blendfile.close()
        blendfile = gzip.GzipFile('', 'rb', 0, open_wrapper(path, 'rb'))
        head = blendfile.read(12)

    if not head.startswith(b'BLENDER'):
        blendfile.close()
        return None, 0, 0

    is_64_bit = (head[7] == b'-'[0])

    # true for PPC, false for X86
    is_big_endian = (head[8] == b'V'[0])

    # 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 = int_endian + 'i'

    while True:
        bhead = blendfile.read(sizeof_bhead)

        if len(bhead) < sizeof_bhead:
            return None, 0, 0

        code = bhead[:4]
        length = struct.unpack(int_endian, bhead[4:8])[0]  # 4 == sizeof(int)

        if code == REND:
            blendfile.seek(length, os.SEEK_CUR)
        else:
            break

    if code != TEST:
        return None, 0, 0

    try:
        x, y = struct.unpack(int_endian_pair, blendfile.read(8))  # 8 == sizeof(int) * 2
    except struct.error:
        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

    return image_buffer, x, y


def write_png(buf, width, height):
    import zlib

    # 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))

    return b"".join([
        b'\x89PNG\r\n\x1a\n',
        png_pack(b'IHDR', struct.pack("!2I5B", width, height, 8, 6, 0, 0, 0)),
        png_pack(b'IDAT', zlib.compress(raw_data, 9)),
        png_pack(b'IEND', b'')])


def main():
    import sys

    if len(sys.argv) < 3:
        print("Expected 2 arguments <input.blend> <output.png>")
    else:
        file_in = sys.argv[-2]

        buf, width, height = blend_extract_thumb(file_in)

        if buf:
            file_out = sys.argv[-1]

            f = open(file_out, "wb")
            f.write(write_png(buf, width, height))
            f.close()


if __name__ == '__main__':
    main()