Compare commits
9 Commits
geometry-n
...
thumbnaile
Author | SHA1 | Date | |
---|---|---|---|
9c124b869f | |||
2af453291f | |||
b1111332cb | |||
119d30f8bd | |||
243d7201e2 | |||
5bd03fc03b | |||
8bf1f0e272 | |||
b30922a515 | |||
943f66ab47 |
@@ -1,193 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# ##### BEGIN GPL LICENSE BLOCK #####
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software Foundation,
|
||||
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
#
|
||||
# ##### END GPL LICENSE BLOCK #####
|
||||
|
||||
# <pep8 compliant>
|
||||
|
||||
"""
|
||||
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()
|
Submodule release/datafiles/locale updated: a2fb7b56b6...b0b6396312
Submodule release/scripts/addons updated: 52b58daa97...c4519cc9a8
Submodule release/scripts/addons_contrib updated: af1d8170e6...b52e7760ff
@@ -68,7 +68,6 @@ set(SRC_DNA_INC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_outliner_types.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_packedFile_types.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_particle_types.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_curveprofile_types.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_rigidbody_types.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_scene_types.h
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/makesdna/DNA_screen_types.h
|
||||
@@ -114,6 +113,7 @@ add_subdirectory(gpencil_modifiers)
|
||||
add_subdirectory(shader_fx)
|
||||
add_subdirectory(makesdna)
|
||||
add_subdirectory(makesrna)
|
||||
add_subdirectory(blendthumb)
|
||||
|
||||
if(WITH_COMPOSITOR)
|
||||
add_subdirectory(compositor)
|
||||
@@ -154,7 +154,3 @@ endif()
|
||||
if(WITH_ALEMBIC)
|
||||
add_subdirectory(alembic)
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
add_subdirectory(blendthumb)
|
||||
endif()
|
||||
|
@@ -19,20 +19,76 @@
|
||||
# ***** END GPL LICENSE BLOCK *****
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
include_directories(${ZLIB_INCLUDE_DIRS})
|
||||
|
||||
set(SRC
|
||||
src/BlenderThumb.cpp
|
||||
src/BlendThumb.def
|
||||
src/BlendThumb.rc
|
||||
src/Dll.cpp
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Build thumbextract library
|
||||
include_directories(
|
||||
SYSTEM
|
||||
${ZLIB_INCLUDE_DIRS}
|
||||
)
|
||||
|
||||
add_library(BlendThumb SHARED ${SRC})
|
||||
target_link_libraries(BlendThumb ${ZLIB_LIBRARIES})
|
||||
include_directories(
|
||||
../blenkernel
|
||||
../makesdna
|
||||
../blendthumb
|
||||
)
|
||||
|
||||
set(SRC
|
||||
thumbextract.h
|
||||
src/thumbextract.cpp
|
||||
)
|
||||
|
||||
add_library(ThumbExtract STATIC ${SRC})
|
||||
target_link_libraries(ThumbExtract ${ZLIB_LIBRARIES})
|
||||
|
||||
install(
|
||||
FILES $<TARGET_FILE:BlendThumb>
|
||||
FILES $<TARGET_FILE:ThumbExtract>
|
||||
COMPONENT Blender
|
||||
DESTINATION "."
|
||||
)
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Build blender-thumbnailer executable
|
||||
|
||||
link_directories(${LIBRARY_OUTPUT_PATH})
|
||||
|
||||
set(SRC
|
||||
src/blender-thumbnailer.cpp
|
||||
)
|
||||
|
||||
add_executable(blender-thumbnailer ${SRC})
|
||||
|
||||
target_link_libraries(blender-thumbnailer ThumbExtract)
|
||||
|
||||
add_dependencies(blender-thumbnailer ThumbExtract)
|
||||
|
||||
install(
|
||||
FILES $<TARGET_FILE:blender-thumbnailer>
|
||||
COMPONENT Blender
|
||||
DESTINATION "."
|
||||
)
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Build BlendThumb dll
|
||||
|
||||
if(WIN32)
|
||||
|
||||
set(SRC
|
||||
src/BlenderThumb.cpp
|
||||
src/BlendThumb.def
|
||||
src/BlendThumb.rc
|
||||
src/Dll.cpp
|
||||
)
|
||||
|
||||
add_library(BlendThumb SHARED ${SRC})
|
||||
target_link_libraries(BlendThumb ThumbExtract)
|
||||
|
||||
add_dependencies(BlendThumb ThumbExtract)
|
||||
|
||||
install(
|
||||
FILES $<TARGET_FILE:BlendThumb>
|
||||
COMPONENT Blender
|
||||
DESTINATION "."
|
||||
)
|
||||
endif()
|
||||
|
@@ -17,6 +17,8 @@
|
||||
#include <shlwapi.h>
|
||||
#include <thumbcache.h> // For IThumbnailProvider.
|
||||
#include <new>
|
||||
#include <string>
|
||||
#include "thumbextract.h"
|
||||
|
||||
#pragma comment(lib, "shlwapi.lib")
|
||||
|
||||
@@ -95,227 +97,124 @@ IFACEMETHODIMP CBlendThumb::Initialize(IStream *pStream, DWORD)
|
||||
}
|
||||
|
||||
#include <math.h>
|
||||
#include <zlib.h>
|
||||
#include "Wincodec.h"
|
||||
const unsigned char gzip_magic[3] = {0x1f, 0x8b, 0x08};
|
||||
|
||||
// IThumbnailProvider
|
||||
IFACEMETHODIMP CBlendThumb::GetThumbnail(UINT cx, HBITMAP *phbmp, WTS_ALPHATYPE *pdwAlpha)
|
||||
{
|
||||
ULONG BytesRead;
|
||||
HRESULT hr = S_FALSE;
|
||||
LARGE_INTEGER SeekPos;
|
||||
|
||||
// Compressed?
|
||||
unsigned char in_magic[3];
|
||||
_pStream->Read(&in_magic, 3, &BytesRead);
|
||||
bool gzipped = true;
|
||||
for (int i = 0; i < 3; i++)
|
||||
if (in_magic[i] != gzip_magic[i]) {
|
||||
gzipped = false;
|
||||
break;
|
||||
}
|
||||
// read first 70KB of the file
|
||||
// thumbnail is currently always inside the first 65KB...if it moves or
|
||||
// enlargens this line will have to change or go!
|
||||
const int BytesToRead = 1024 * 70;
|
||||
|
||||
if (gzipped) {
|
||||
// Zlib inflate
|
||||
z_stream stream;
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.opaque = Z_NULL;
|
||||
// get length of file
|
||||
ULARGE_INTEGER StreamSize;
|
||||
IStream_Size(_pStream, &StreamSize);
|
||||
|
||||
// Get compressed file length
|
||||
SeekPos.QuadPart = 0;
|
||||
_pStream->Seek(SeekPos, STREAM_SEEK_END, NULL);
|
||||
// determine how many bytes are available for reading from file
|
||||
const int BytesAvailable = (StreamSize.QuadPart < BytesToRead) ? StreamSize.QuadPart :
|
||||
BytesToRead;
|
||||
|
||||
// Get compressed and uncompressed size
|
||||
uLong source_size;
|
||||
uLongf dest_size;
|
||||
// SeekPos.QuadPart = -4; // last 4 bytes define size of uncompressed file
|
||||
// ULARGE_INTEGER Tell;
|
||||
//_pStream->Seek(SeekPos,STREAM_SEEK_END,&Tell);
|
||||
// source_size = (uLong)Tell.QuadPart + 4; // src
|
||||
//_pStream->Read(&dest_size,4,&BytesRead); // dest
|
||||
dest_size = 1024 * 70; // thumbnail is currently always inside the first 65KB...if it moves or
|
||||
// enlargens this line will have to change or go!
|
||||
source_size = (uLong)max(SeekPos.QuadPart, dest_size); // for safety, assume no compression
|
||||
// Create ThumbData struct
|
||||
ThumbData TData;
|
||||
TData.Data = std::string(BytesAvailable, 0x00);
|
||||
TData.Width = 0;
|
||||
TData.Height = 0;
|
||||
TData.Size = 0;
|
||||
|
||||
// Input
|
||||
Bytef *src = new Bytef[source_size];
|
||||
stream.next_in = (Bytef *)src;
|
||||
stream.avail_in = (uInt)source_size;
|
||||
// Move to the beginning of the IStream and read bytes
|
||||
// from IStream interface to TData.Data string
|
||||
ULONG BytesRead;
|
||||
char *ptrThumbData = reinterpret_cast<char *>(&TData.Data[0]);
|
||||
|
||||
// Output
|
||||
Bytef *dest = new Bytef[dest_size];
|
||||
stream.next_out = (Bytef *)dest;
|
||||
stream.avail_out = dest_size;
|
||||
|
||||
// IStream to src
|
||||
SeekPos.QuadPart = 0;
|
||||
_pStream->Seek(SeekPos, STREAM_SEEK_SET, NULL);
|
||||
_pStream->Read(src, source_size, &BytesRead);
|
||||
|
||||
// Do the inflation
|
||||
int err;
|
||||
err = inflateInit2(&stream, 16); // 16 means "gzip"...nice!
|
||||
err = inflate(&stream, Z_FINISH);
|
||||
err = inflateEnd(&stream);
|
||||
|
||||
// Replace the IStream, which is read-only
|
||||
_pStream->Release();
|
||||
_pStream = SHCreateMemStream(dest, dest_size);
|
||||
|
||||
delete[] src;
|
||||
delete[] dest;
|
||||
}
|
||||
|
||||
// Blender version, early out if sub 2.5
|
||||
SeekPos.QuadPart = 9;
|
||||
SeekPos.QuadPart = 0;
|
||||
_pStream->Seek(SeekPos, STREAM_SEEK_SET, NULL);
|
||||
char version[4];
|
||||
version[3] = '\0';
|
||||
_pStream->Read(&version, 3, &BytesRead);
|
||||
if (BytesRead != 3) {
|
||||
_pStream->Read(ptrThumbData, (ULONG)BytesAvailable, &BytesRead);
|
||||
if (BytesRead != (ULONG)BytesAvailable) {
|
||||
return E_UNEXPECTED;
|
||||
}
|
||||
int iVersion = atoi(version);
|
||||
if (iVersion < 250) {
|
||||
|
||||
int ExtractionResult = ExtractThumbnail(&TData);
|
||||
|
||||
if (ExtractionResult != BT_OK) {
|
||||
return S_FALSE;
|
||||
}
|
||||
|
||||
// 32 or 64 bit blend?
|
||||
SeekPos.QuadPart = 7;
|
||||
_pStream->Seek(SeekPos, STREAM_SEEK_SET, NULL);
|
||||
// Extraction is performed successfully
|
||||
|
||||
char _PointerSize;
|
||||
_pStream->Read(&_PointerSize, 1, &BytesRead);
|
||||
// Convert to BGRA for Windows
|
||||
for (int i = 0; i < TData.Size; i += 4) {
|
||||
#define RED_BYTE TData.Data[i]
|
||||
#define BLUE_BYTE TData.Data[i + 2]
|
||||
|
||||
int PointerSize = _PointerSize == '_' ? 4 : 8;
|
||||
int HeaderSize = 16 + PointerSize;
|
||||
char red = RED_BYTE;
|
||||
RED_BYTE = BLUE_BYTE;
|
||||
BLUE_BYTE = red;
|
||||
}
|
||||
|
||||
// Find and read thumbnail ("TEST") block
|
||||
SeekPos.QuadPart = 12;
|
||||
_pStream->Seek(SeekPos, STREAM_SEEK_SET, NULL);
|
||||
int BlockOffset = 12;
|
||||
while (_pStream) {
|
||||
// Scan current block
|
||||
char BlockName[5];
|
||||
BlockName[4] = '\0';
|
||||
int BlockSize = 0;
|
||||
// Create image
|
||||
ptrThumbData = reinterpret_cast<char *>(&TData.Data[0]);
|
||||
*phbmp = CreateBitmap(TData.Width, TData.Height, 1, 32, ptrThumbData);
|
||||
if (!*phbmp) {
|
||||
return E_FAIL;
|
||||
}
|
||||
*pdwAlpha = WTSAT_ARGB; // it's actually BGRA, not sure why this works
|
||||
|
||||
if (_pStream->Read(BlockName, 4, &BytesRead) == S_OK &&
|
||||
_pStream->Read((void *)&BlockSize, 4, &BytesRead) == S_OK) {
|
||||
if (strcmp(BlockName, "TEST") != 0) {
|
||||
SeekPos.QuadPart = BlockOffset += HeaderSize + BlockSize;
|
||||
_pStream->Seek(SeekPos, STREAM_SEEK_SET, NULL);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
else {
|
||||
break; // eof
|
||||
}
|
||||
|
||||
// Found the block
|
||||
SeekPos.QuadPart = BlockOffset + HeaderSize;
|
||||
_pStream->Seek(SeekPos, STREAM_SEEK_SET, NULL);
|
||||
|
||||
int width, height;
|
||||
_pStream->Read((char *)&width, 4, &BytesRead);
|
||||
_pStream->Read((char *)&height, 4, &BytesRead);
|
||||
BlockSize -= 8;
|
||||
|
||||
// Isolate RGBA data
|
||||
char *pRGBA = new char[BlockSize];
|
||||
_pStream->Read(pRGBA, BlockSize, &BytesRead);
|
||||
|
||||
if (BytesRead != (ULONG)BlockSize) {
|
||||
return E_UNEXPECTED;
|
||||
}
|
||||
|
||||
// Convert to BGRA for Windows
|
||||
for (int i = 0; i < BlockSize; i += 4) {
|
||||
#define RED_BYTE pRGBA[i]
|
||||
#define BLUE_BYTE pRGBA[i + 2]
|
||||
|
||||
char red = RED_BYTE;
|
||||
RED_BYTE = BLUE_BYTE;
|
||||
BLUE_BYTE = red;
|
||||
}
|
||||
|
||||
// Flip vertically (Blender stores it upside-down)
|
||||
unsigned int LineSize = width * 4;
|
||||
char *FlippedImage = new char[BlockSize];
|
||||
for (int i = 0; i < height; i++) {
|
||||
if (0 != memcpy_s(&FlippedImage[(height - i - 1) * LineSize],
|
||||
LineSize,
|
||||
&pRGBA[i * LineSize],
|
||||
LineSize)) {
|
||||
return E_UNEXPECTED;
|
||||
}
|
||||
}
|
||||
delete[] pRGBA;
|
||||
pRGBA = FlippedImage;
|
||||
|
||||
// Create image
|
||||
*phbmp = CreateBitmap(width, height, 1, 32, pRGBA);
|
||||
if (!*phbmp) {
|
||||
return E_FAIL;
|
||||
}
|
||||
*pdwAlpha = WTSAT_ARGB; // it's actually BGRA, not sure why this works
|
||||
|
||||
// Scale down if required
|
||||
if ((unsigned)width > cx || (unsigned)height > cx) {
|
||||
float scale = 1.0f / (max(width, height) / (float)cx);
|
||||
LONG NewWidth = (LONG)(width * scale);
|
||||
LONG NewHeight = (LONG)(height * scale);
|
||||
// Scale down if required
|
||||
if ((unsigned)TData.Width > cx || (unsigned)TData.Height > cx) {
|
||||
float scale = 1.0f / (max(TData.Width, TData.Height) / (float)cx);
|
||||
LONG NewWidth = (LONG)(TData.Width * scale);
|
||||
LONG NewHeight = (LONG)(TData.Height * scale);
|
||||
|
||||
#ifdef _DEBUG
|
||||
# if 0
|
||||
MessageBox(0, "Attach now", "Debugging", MB_OK);
|
||||
# endif
|
||||
#endif
|
||||
IWICImagingFactory *pImgFac;
|
||||
hr = CoCreateInstance(
|
||||
CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pImgFac));
|
||||
IWICImagingFactory *pImgFac;
|
||||
hr = CoCreateInstance(
|
||||
CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pImgFac));
|
||||
|
||||
IWICBitmap *WICBmp;
|
||||
hr = pImgFac->CreateBitmapFromHBITMAP(*phbmp, 0, WICBitmapUseAlpha, &WICBmp);
|
||||
IWICBitmap *WICBmp;
|
||||
hr = pImgFac->CreateBitmapFromHBITMAP(*phbmp, 0, WICBitmapUseAlpha, &WICBmp);
|
||||
|
||||
BITMAPINFO bmi = {};
|
||||
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
|
||||
bmi.bmiHeader.biWidth = NewWidth;
|
||||
bmi.bmiHeader.biHeight = -NewHeight;
|
||||
bmi.bmiHeader.biPlanes = 1;
|
||||
bmi.bmiHeader.biBitCount = 32;
|
||||
bmi.bmiHeader.biCompression = BI_RGB;
|
||||
BITMAPINFO bmi = {};
|
||||
bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader);
|
||||
bmi.bmiHeader.biWidth = NewWidth;
|
||||
bmi.bmiHeader.biHeight = -NewHeight;
|
||||
bmi.bmiHeader.biPlanes = 1;
|
||||
bmi.bmiHeader.biBitCount = 32;
|
||||
bmi.bmiHeader.biCompression = BI_RGB;
|
||||
|
||||
BYTE *pBits;
|
||||
HBITMAP ResizedHBmp = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0);
|
||||
hr = ResizedHBmp ? S_OK : E_OUTOFMEMORY;
|
||||
if (SUCCEEDED(hr)) {
|
||||
IWICBitmapScaler *pIScaler;
|
||||
hr = pImgFac->CreateBitmapScaler(&pIScaler);
|
||||
hr = pIScaler->Initialize(WICBmp, NewWidth, NewHeight, WICBitmapInterpolationModeFant);
|
||||
|
||||
WICRect rect = {0, 0, NewWidth, NewHeight};
|
||||
hr = pIScaler->CopyPixels(&rect, NewWidth * 4, NewWidth * NewHeight * 4, pBits);
|
||||
|
||||
BYTE *pBits;
|
||||
HBITMAP ResizedHBmp = CreateDIBSection(NULL, &bmi, DIB_RGB_COLORS, (void **)&pBits, NULL, 0);
|
||||
hr = ResizedHBmp ? S_OK : E_OUTOFMEMORY;
|
||||
if (SUCCEEDED(hr)) {
|
||||
IWICBitmapScaler *pIScaler;
|
||||
hr = pImgFac->CreateBitmapScaler(&pIScaler);
|
||||
hr = pIScaler->Initialize(WICBmp, NewWidth, NewHeight, WICBitmapInterpolationModeFant);
|
||||
|
||||
WICRect rect = {0, 0, NewWidth, NewHeight};
|
||||
hr = pIScaler->CopyPixels(&rect, NewWidth * 4, NewWidth * NewHeight * 4, pBits);
|
||||
|
||||
if (SUCCEEDED(hr)) {
|
||||
DeleteObject(*phbmp);
|
||||
*phbmp = ResizedHBmp;
|
||||
}
|
||||
else {
|
||||
DeleteObject(ResizedHBmp);
|
||||
}
|
||||
|
||||
pIScaler->Release();
|
||||
DeleteObject(*phbmp);
|
||||
*phbmp = ResizedHBmp;
|
||||
}
|
||||
WICBmp->Release();
|
||||
pImgFac->Release();
|
||||
else {
|
||||
DeleteObject(ResizedHBmp);
|
||||
}
|
||||
|
||||
pIScaler->Release();
|
||||
}
|
||||
else {
|
||||
hr = S_OK;
|
||||
}
|
||||
break;
|
||||
WICBmp->Release();
|
||||
pImgFac->Release();
|
||||
}
|
||||
else {
|
||||
hr = S_OK;
|
||||
}
|
||||
return hr;
|
||||
}
|
||||
|
44
source/blender/blendthumb/src/blender-thumbnailer.cpp
Normal file
44
source/blender/blendthumb/src/blender-thumbnailer.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* The Original Code is Copyright (C) 2008 Blender Foundation.
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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.exe
|
||||
* Exec = blender-thumbnailer.exe % u %o
|
||||
* MimeType = application/x-blender;
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include "thumbextract.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
if (argc < 3) {
|
||||
std::cout << "Usage: BlendThumbTest <input.blend> <output.png>";
|
||||
return -1;
|
||||
}
|
||||
|
||||
int result = PrepareThumbnail(argv[1], argv[2]);
|
||||
|
||||
return result;
|
||||
}
|
533
source/blender/blendthumb/src/thumbextract.cpp
Normal file
533
source/blender/blendthumb/src/thumbextract.cpp
Normal file
@@ -0,0 +1,533 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* The Original Code is Copyright (C) 2008 Blender Foundation.
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
#include "thumbextract.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <zlib.h>
|
||||
#include <string>
|
||||
#include "BKE_global.h"
|
||||
#include <vector>
|
||||
|
||||
// Used for writing int to bytes and reading int from bytes
|
||||
enum class ByteOrder {
|
||||
LITTLE_ENDIAN_BYTE_ORDER = 1,
|
||||
BIG_ENDIAN_BYTE_ORDER = 2,
|
||||
NETWORK_BYTE_ORDER = 3
|
||||
};
|
||||
|
||||
#pragma region FunctionDeclaration
|
||||
|
||||
// API functions
|
||||
// defined in thumbextract.h
|
||||
// int PrepareThumbnail(const char *path, const char *dest)
|
||||
// int ExtractThumbnail(ThumbData *TData)
|
||||
|
||||
// Main functions
|
||||
int ReadThumbnail(ThumbData *FileStream, const int BHeadSize, const bool EndianSwitchRequired);
|
||||
int WritePNG(ThumbData *Data, const char *dest);
|
||||
|
||||
// Helper functions
|
||||
std::string ReadBytesFromFile(std::ifstream &File, const int BytesToRead);
|
||||
int FlipVertically(ThumbData *TData);
|
||||
std::string GZipInflate(const std::string &InputStream);
|
||||
std::string ZlibCompress(const std::string &Data, const int CompressLevel = Z_NO_COMPRESSION);
|
||||
std::string CreatePNGChunk(const std::string Tag, const std::string &Data);
|
||||
void AddFilterToRows(ThumbData *TData);
|
||||
bool IsFileCompressed(const std::string &FileStream);
|
||||
bool IsValidBlenderFile(const std::string &FileStream);
|
||||
bool CheckValidVersion(const std::string &FileStream);
|
||||
const int GetPointerSize(const std::string &FileStream);
|
||||
bool IsEndianSwitchRequired(const std::string &FileStream);
|
||||
unsigned int ConvertBytesToInt(const std::string &Bytes,
|
||||
ByteOrder ByteOrder = ByteOrder::LITTLE_ENDIAN_BYTE_ORDER);
|
||||
std::string ConvertIntToBytes(const unsigned int Number,
|
||||
ByteOrder ByteOrder = ByteOrder::LITTLE_ENDIAN_BYTE_ORDER);
|
||||
|
||||
// Utility functions
|
||||
void AddBufferToString(std::string &Container, const char *buffer, uLongf length);
|
||||
|
||||
// Functions used for debugging
|
||||
void DebugDumpData(const std::string &Data, std::string dest);
|
||||
|
||||
#pragma endregion
|
||||
|
||||
/**
|
||||
* This function opens .blend file from path, extracts thumbnail from file,
|
||||
* if there is one, and writes .png image to dest folder.
|
||||
* return 0 if everything ok, -1 otherwise
|
||||
*/
|
||||
int PrepareThumbnail(const char *path, const char *dest)
|
||||
{
|
||||
std::ifstream BlendFile;
|
||||
BlendFile.open(path, std::ifstream::in | std::ifstream::binary);
|
||||
if (BlendFile.is_open()) {
|
||||
|
||||
// read first 70KB of the file
|
||||
// thumbnail is currently always inside the first 65KB...if it moves or
|
||||
// enlargens this line will have to change or go!
|
||||
const int BytesToRead = 1024 * 70;
|
||||
std::string BlendFileStream;
|
||||
BlendFileStream = ReadBytesFromFile(BlendFile, BytesToRead);
|
||||
|
||||
if (!BlendFileStream.empty()) {
|
||||
ThumbData TData;
|
||||
|
||||
TData.Data = BlendFileStream;
|
||||
TData.Width = 0;
|
||||
TData.Height = 0;
|
||||
TData.Size = 0;
|
||||
|
||||
int err = ExtractThumbnail(&TData);
|
||||
|
||||
if (err == BT_OK) {
|
||||
int err2 = WritePNG(&TData, dest);
|
||||
if (err2 == BT_OK) {
|
||||
return BT_OK;
|
||||
}
|
||||
else {
|
||||
return err2;
|
||||
}
|
||||
}
|
||||
else {
|
||||
return err;
|
||||
}
|
||||
}
|
||||
return BT_FILE_ERR;
|
||||
}
|
||||
else {
|
||||
return BT_FILE_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This function extracts actual thumbnail from the .blend file
|
||||
* Data that is extracted is stored in ThumbData object.
|
||||
*
|
||||
* Function returns 0 if everything ok, -1 otherwise
|
||||
*/
|
||||
int ExtractThumbnail(ThumbData *TData)
|
||||
{
|
||||
if (!TData->Data.empty()) {
|
||||
|
||||
if (IsFileCompressed(TData->Data)) {
|
||||
// Inflate
|
||||
TData->Data = GZipInflate(TData->Data);
|
||||
if (TData->Data.empty()) {
|
||||
// something went wrong with decompression
|
||||
return BT_DECOMPRESS_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
// is valid Blender file - Header starts with "Blender"?
|
||||
if (!IsValidBlenderFile(TData->Data)) {
|
||||
return BT_INVALID_FILE;
|
||||
}
|
||||
|
||||
// Check version; if less then 2.5 then out - there is no thumbnail
|
||||
if (!CheckValidVersion(TData->Data)) {
|
||||
return BT_EARLY_VERSION;
|
||||
}
|
||||
|
||||
// 32 or 64-bit blender?
|
||||
int BHeadSize = 16 + GetPointerSize(TData->Data);
|
||||
const bool EndianSwitchRequired = IsEndianSwitchRequired(TData->Data);
|
||||
|
||||
// find and read Thumbnail block
|
||||
if (int err = ReadThumbnail(TData, BHeadSize, EndianSwitchRequired) != BT_OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// Flip vertically
|
||||
if (FlipVertically(TData) != BT_OK) {
|
||||
return BT_ERROR;
|
||||
}
|
||||
|
||||
return BT_OK;
|
||||
}
|
||||
// input was empty string
|
||||
return BT_ERROR;
|
||||
}
|
||||
|
||||
std::string ReadBytesFromFile(std::ifstream &File, const int BytesToRead)
|
||||
{
|
||||
if (File.is_open()) {
|
||||
|
||||
// get length of file
|
||||
File.seekg(0, std::ios::end);
|
||||
int FileLength = File.tellg();
|
||||
|
||||
// determine how many bytes are available for reading from file
|
||||
int BytesAvailable = (FileLength < BytesToRead) ? FileLength : BytesToRead;
|
||||
|
||||
// std::vector<char> temp(BytesAvailable, 0x00);
|
||||
std::string temp(BytesAvailable, 0x00);
|
||||
char *ptrTemp = reinterpret_cast<char *>(&temp[0]);
|
||||
|
||||
File.seekg(0, std::ios::beg);
|
||||
File.read(ptrTemp, BytesAvailable);
|
||||
if (File) {
|
||||
return temp;
|
||||
}
|
||||
else {
|
||||
// oops - something went wrong when reading a file
|
||||
return std::string();
|
||||
}
|
||||
}
|
||||
else {
|
||||
return std::string();
|
||||
}
|
||||
}
|
||||
|
||||
int WritePNG(ThumbData *TData, const char *dest)
|
||||
{
|
||||
std::ofstream PNGFile;
|
||||
PNGFile.open(dest, std::ofstream::out | std::ofstream::binary);
|
||||
|
||||
if (PNGFile.is_open()) {
|
||||
|
||||
if (TData->Data.empty()) {
|
||||
return BT_ERROR;
|
||||
}
|
||||
|
||||
// This is standard PNG file header. Every PNG file starts with it
|
||||
std::string PNGHeader{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A};
|
||||
|
||||
// creating IHDR chunk
|
||||
std::string IHdrTemp;
|
||||
IHdrTemp.resize(13);
|
||||
IHdrTemp = ConvertIntToBytes(TData->Width, ByteOrder::NETWORK_BYTE_ORDER) +
|
||||
ConvertIntToBytes(TData->Height, ByteOrder::NETWORK_BYTE_ORDER);
|
||||
IHdrTemp += std::string{0x08}; // Bit Depth
|
||||
IHdrTemp += std::string{0x06}; // Color Type
|
||||
IHdrTemp += std::string{0x00}; // Compression method
|
||||
IHdrTemp += std::string{0x00}; // Filter method
|
||||
IHdrTemp += std::string{0x00}; // Interlace method
|
||||
|
||||
std::string IHdr = CreatePNGChunk(std::string("IHDR"), IHdrTemp);
|
||||
|
||||
// In the image data sent to the compression
|
||||
// step, each scanline is preceded by a filter type byte containing the
|
||||
// numeric code of the filter algorithm used for that scanline.
|
||||
AddFilterToRows(TData);
|
||||
|
||||
std::string IDat = CreatePNGChunk(std::string("IDAT"), ZlibCompress(TData->Data));
|
||||
|
||||
// creating IEND chunk
|
||||
std::string IEnd = CreatePNGChunk(std::string("IEND"), std::string());
|
||||
|
||||
std::string PNGData = PNGHeader + IHdr + IDat + IEnd;
|
||||
PNGFile.write(&PNGData[0], PNGData.size());
|
||||
|
||||
return BT_OK;
|
||||
}
|
||||
return BT_FILE_ERR;
|
||||
}
|
||||
|
||||
void AddFilterToRows(ThumbData *TData)
|
||||
{
|
||||
const unsigned int LineSize = TData->Width * 4;
|
||||
std::string Temp;
|
||||
|
||||
for (int i = 0; i < TData->Height; i++) {
|
||||
Temp.append(std::string{0x00});
|
||||
Temp.append(TData->Data, (size_t)i * (size_t)LineSize, LineSize);
|
||||
}
|
||||
|
||||
TData->Data = Temp;
|
||||
}
|
||||
|
||||
std::string ZlibCompress(const std::string &Data, const int CompressLevel)
|
||||
{
|
||||
unsigned long SourceLength = Data.size();
|
||||
uLongf DestinationLength = compressBound(SourceLength);
|
||||
|
||||
std::string DestinationData(DestinationLength, 0x00);
|
||||
char *ptrDestinationData = reinterpret_cast<char *>(&DestinationData[0]);
|
||||
|
||||
Bytef *SourceData = (Bytef *)Data.data();
|
||||
int return_value = compress2(
|
||||
(Bytef *)ptrDestinationData, &DestinationLength, SourceData, SourceLength, CompressLevel);
|
||||
|
||||
if (return_value != Z_OK) {
|
||||
// something went wrong with compression of data
|
||||
return std::string();
|
||||
}
|
||||
|
||||
return DestinationData;
|
||||
}
|
||||
|
||||
std::string CreatePNGChunk(const std::string Tag, const std::string &Data)
|
||||
{
|
||||
std::string ChunkHead(Tag);
|
||||
ChunkHead += Data;
|
||||
|
||||
uLong iCRC = crc32(0L, Z_NULL, 0);
|
||||
iCRC = crc32(iCRC, (Bytef *)&ChunkHead[0], ChunkHead.size());
|
||||
std::string Length = ConvertIntToBytes(Data.size(), ByteOrder::NETWORK_BYTE_ORDER);
|
||||
std::string sCRC = ConvertIntToBytes(iCRC, ByteOrder::NETWORK_BYTE_ORDER);
|
||||
|
||||
return Length + ChunkHead + sCRC;
|
||||
}
|
||||
|
||||
bool IsFileCompressed(const std::string &FileStream)
|
||||
{
|
||||
const unsigned char GzipMagic[3] = {0x1f, 0x8b, 0x08}; /* Gzip magic number*/
|
||||
|
||||
bool gzipped = true;
|
||||
for (int i = 0; i < 3; i++)
|
||||
if (FileStream[i] != GzipMagic[i]) {
|
||||
gzipped = false;
|
||||
break;
|
||||
}
|
||||
|
||||
return gzipped;
|
||||
}
|
||||
|
||||
std::string GZipInflate(const std::string &InputStream)
|
||||
{
|
||||
if (InputStream.size() == 0) {
|
||||
return InputStream;
|
||||
}
|
||||
|
||||
int DestinationLength = InputStream.size() * 2;
|
||||
|
||||
z_stream stream;
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.opaque = Z_NULL;
|
||||
|
||||
// Input
|
||||
stream.next_in = (Bytef *)InputStream.c_str();
|
||||
stream.avail_in = InputStream.size();
|
||||
|
||||
// Output
|
||||
std::string DestinationData(DestinationLength, 0x00);
|
||||
char *ptrDestinationData = reinterpret_cast<char *>(&DestinationData[0]);
|
||||
|
||||
stream.next_out = (Bytef *)ptrDestinationData;
|
||||
stream.avail_out = DestinationLength;
|
||||
|
||||
// Do the inflation
|
||||
int err;
|
||||
if (inflateInit2(&stream, 16) != Z_OK) { // 16 means "gzip" ... nice!
|
||||
return std::string();
|
||||
}
|
||||
|
||||
err = inflate(&stream, Z_FINISH);
|
||||
|
||||
if ((err != Z_OK) && (err != Z_BUF_ERROR) && (err != Z_STREAM_END)) {
|
||||
return std::string();
|
||||
}
|
||||
|
||||
err = inflateEnd(&stream);
|
||||
|
||||
return DestinationData;
|
||||
}
|
||||
|
||||
bool IsValidBlenderFile(const std::string &FileStream)
|
||||
{
|
||||
if ((FileStream.substr(0, 7) == std::string("BLENDER")) &&
|
||||
((FileStream[7] == '_') || (FileStream[7] == '-')) &&
|
||||
((FileStream[8] == 'v') || (FileStream[8] == 'V')) && (isdigit(FileStream[9])) &&
|
||||
(isdigit(FileStream[10])) && (isdigit(FileStream[11]))) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CheckValidVersion(const std::string &FileStream)
|
||||
// returns false if version is less than 2.5
|
||||
{
|
||||
std::string sVer = FileStream.substr(9, 3);
|
||||
return (atoi(sVer.c_str()) < 250) ? false : true;
|
||||
}
|
||||
|
||||
const int GetPointerSize(const std::string &FileStream)
|
||||
{
|
||||
return FileStream[7] == '_' ? 4 : 8;
|
||||
}
|
||||
|
||||
bool IsEndianSwitchRequired(const std::string &FileStream)
|
||||
{
|
||||
return (((FileStream[8] == 'v') ? L_ENDIAN : B_ENDIAN) != ENDIAN_ORDER);
|
||||
}
|
||||
|
||||
std::string DoEndianSwitch(std::string Input)
|
||||
{
|
||||
std::string temp;
|
||||
for (std::string::reverse_iterator rit = Input.rbegin(); rit != Input.rend(); ++rit) {
|
||||
temp += *rit;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
bool AreDimValid(int Width, int Height, int BlockSize)
|
||||
{
|
||||
if ((Width < 0) || (Height < 0)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (((2 + Width * Height) * (int)sizeof(int)) != BlockSize) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
int ReadThumbnail(ThumbData *FileStream, const int BHeadSize, const bool EndianSwitchRequired)
|
||||
{
|
||||
int Offset = 12; // initial offset is set to 12 - it is the size of File Header
|
||||
while (true) {
|
||||
if (Offset + BHeadSize + 8 - 1 > (int)FileStream->Data.size()) {
|
||||
// we would reach end of data before finding a thumbnail
|
||||
return BT_INVALID_THUMB;
|
||||
}
|
||||
|
||||
std::string BlockCode = FileStream->Data.substr(Offset, 4);
|
||||
std::string SBlockSize = FileStream->Data.substr((size_t)Offset + 4, 4);
|
||||
|
||||
if (EndianSwitchRequired) {
|
||||
SBlockSize = DoEndianSwitch(SBlockSize);
|
||||
}
|
||||
int BlockSize = ConvertBytesToInt(SBlockSize);
|
||||
|
||||
if (BlockCode.compare(std::string("TEST")) == 0) {
|
||||
|
||||
std::string TWidth = FileStream->Data.substr((size_t)Offset + (size_t)BHeadSize, 4);
|
||||
std::string THeight = FileStream->Data.substr((size_t)Offset + (size_t)BHeadSize + 4, 4);
|
||||
if (EndianSwitchRequired) {
|
||||
TWidth = DoEndianSwitch(TWidth);
|
||||
THeight = DoEndianSwitch(THeight);
|
||||
}
|
||||
|
||||
FileStream->Width = ConvertBytesToInt(TWidth);
|
||||
FileStream->Height = ConvertBytesToInt(THeight);
|
||||
FileStream->Size = BlockSize - 8;
|
||||
|
||||
if (!AreDimValid(FileStream->Width, FileStream->Height, BlockSize)) {
|
||||
// Thumbnail is invalid
|
||||
return BT_INVALID_THUMB;
|
||||
}
|
||||
FileStream->Data = FileStream->Data.substr((size_t)Offset + (size_t)BHeadSize + 8,
|
||||
(size_t)BlockSize - 8);
|
||||
|
||||
return BT_OK;
|
||||
}
|
||||
else {
|
||||
Offset += BHeadSize + BlockSize;
|
||||
}
|
||||
}
|
||||
|
||||
return BT_INVALID_THUMB;
|
||||
}
|
||||
|
||||
int FlipVertically(ThumbData *TData)
|
||||
{
|
||||
const unsigned int LineSize = TData->Width * 4;
|
||||
const unsigned int BlockSize = TData->Width * TData->Height * 4;
|
||||
|
||||
std::string FlippedImage;
|
||||
FlippedImage.resize(BlockSize);
|
||||
|
||||
for (int i = 0; i < TData->Height; i++) {
|
||||
FlippedImage.replace(((size_t)TData->Height - i - 1) * LineSize,
|
||||
LineSize,
|
||||
TData->Data.substr((size_t)i * (size_t)LineSize, LineSize));
|
||||
}
|
||||
TData->Data = FlippedImage;
|
||||
|
||||
return BT_OK;
|
||||
}
|
||||
|
||||
unsigned int ConvertBytesToInt(const std::string &Bytes, ByteOrder ByteOrder)
|
||||
{
|
||||
unsigned int Result = 0;
|
||||
|
||||
switch (ByteOrder) {
|
||||
case ByteOrder::LITTLE_ENDIAN_BYTE_ORDER:
|
||||
Result = static_cast<int>((unsigned char)(Bytes[0]) | (unsigned char)(Bytes[1]) << 8 |
|
||||
(unsigned char)(Bytes[2]) << 16 | (unsigned char)(Bytes[3]) << 24);
|
||||
break;
|
||||
case ByteOrder::BIG_ENDIAN_BYTE_ORDER:
|
||||
Result = static_cast<int>((unsigned char)(Bytes[3]) | (unsigned char)(Bytes[2]) << 8 |
|
||||
(unsigned char)(Bytes[1]) << 16 | (unsigned char)(Bytes[0]) << 24);
|
||||
break;
|
||||
case ByteOrder::NETWORK_BYTE_ORDER:
|
||||
Result = static_cast<int>((unsigned char)(Bytes[3]) | (unsigned char)(Bytes[2]) << 8 |
|
||||
(unsigned char)(Bytes[1]) << 16 | (unsigned char)(Bytes[0]) << 24);
|
||||
break;
|
||||
}
|
||||
return Result;
|
||||
}
|
||||
|
||||
std::string ConvertIntToBytes(const unsigned int Number, ByteOrder ByteOrder)
|
||||
{
|
||||
std::string Result{0x00, 0x00, 0x00, 0x00};
|
||||
|
||||
switch (ByteOrder) {
|
||||
case ByteOrder::LITTLE_ENDIAN_BYTE_ORDER:
|
||||
Result[3] = (Number >> 24) & 0xFF;
|
||||
Result[2] = (Number >> 16) & 0xFF;
|
||||
Result[1] = (Number >> 8) & 0xFF;
|
||||
Result[0] = Number & 0xFF;
|
||||
break;
|
||||
case ByteOrder::BIG_ENDIAN_BYTE_ORDER:
|
||||
Result[0] = (Number >> 24) & 0xFF;
|
||||
Result[1] = (Number >> 16) & 0xFF;
|
||||
Result[2] = (Number >> 8) & 0xFF;
|
||||
Result[3] = Number & 0xFF;
|
||||
break;
|
||||
case ByteOrder::NETWORK_BYTE_ORDER:
|
||||
Result[0] = (Number >> 24) & 0xFF;
|
||||
Result[1] = (Number >> 16) & 0xFF;
|
||||
Result[2] = (Number >> 8) & 0xFF;
|
||||
Result[3] = Number & 0xFF;
|
||||
break;
|
||||
}
|
||||
|
||||
return Result;
|
||||
}
|
||||
|
||||
#pragma region UtilityFunctions
|
||||
|
||||
void AddBufferToString(std::string &Container, const char *buffer, uLongf length)
|
||||
{
|
||||
for (int Index = 0; Index < length; Index++) {
|
||||
char CurrentChar = buffer[Index];
|
||||
Container.push_back(CurrentChar);
|
||||
}
|
||||
}
|
||||
|
||||
#pragma endregion
|
||||
|
||||
#pragma region DebugFunctions
|
||||
|
||||
void DebugDumpData(const std::string &Data, std::string dest)
|
||||
{
|
||||
std::ofstream DumpFile;
|
||||
DumpFile.open(dest, std::ofstream::out | std::ofstream::binary);
|
||||
|
||||
if (DumpFile.is_open()) {
|
||||
DumpFile.write(&Data[0], Data.size());
|
||||
}
|
||||
}
|
||||
|
||||
#pragma endregion
|
47
source/blender/blendthumb/thumbextract.h
Normal file
47
source/blender/blendthumb/thumbextract.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
*
|
||||
* The Original Code is Copyright (C) 2008 Blender Foundation.
|
||||
* All rights reserved.
|
||||
*/
|
||||
|
||||
#ifndef __THUMBEXTRACT_H__
|
||||
#define __THUMBEXTRACT_H__
|
||||
|
||||
#include <string>
|
||||
|
||||
typedef struct ThumbData {
|
||||
std::string Data;
|
||||
int Width;
|
||||
int Height;
|
||||
int Size;
|
||||
} ThumbData;
|
||||
|
||||
enum ThumbStatus {
|
||||
BT_OK = 0,
|
||||
BT_FILE_ERR = 1,
|
||||
BT_COMPRES_ERR = 2,
|
||||
BT_DECOMPRESS_ERR = 3,
|
||||
BT_INVALID_FILE = 4,
|
||||
BT_EARLY_VERSION = 5,
|
||||
BT_INVALID_THUMB = 6,
|
||||
BT_ERROR = 9
|
||||
};
|
||||
|
||||
extern "C" int PrepareThumbnail(const char *path, const char *dest);
|
||||
|
||||
extern "C" int ExtractThumbnail(ThumbData *TData);
|
||||
|
||||
#endif /* __THUMBEXTRACT_H__ */
|
Reference in New Issue
Block a user