Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2014-01-13 16:47:33 +0400
committerCampbell Barton <ideasman42@gmail.com>2014-01-13 17:14:03 +0400
commit4c9a3a53bd3154954bc2bcf73684b87a79332a71 (patch)
treeca297c79f831f46117e1638640a187e98ddbd01c /source/blender/datatoc
parent08981f3a91a687efa8d28d308d6033e5a9e68b1f (diff)
UI Icons: store icons in git as uncompressed pixmaps (D196)
this allows for updating icons without committing a new PNG each time (which is inefficient with git). The data files are converted into a PNG at builds time and used just as they were before.
Diffstat (limited to 'source/blender/datatoc')
-rw-r--r--source/blender/datatoc/CMakeLists.txt20
-rw-r--r--source/blender/datatoc/datatoc_icon.c390
-rwxr-xr-xsource/blender/datatoc/datatoc_icon.py155
-rwxr-xr-xsource/blender/datatoc/datatoc_icon_split.py292
-rwxr-xr-xsource/blender/datatoc/datatoc_icon_split_to_png.py71
5 files changed, 927 insertions, 1 deletions
diff --git a/source/blender/datatoc/CMakeLists.txt b/source/blender/datatoc/CMakeLists.txt
index 125e653ee5c..78ae00d8a83 100644
--- a/source/blender/datatoc/CMakeLists.txt
+++ b/source/blender/datatoc/CMakeLists.txt
@@ -20,10 +20,28 @@
# -----------------------------------------------------------------------------
-# Build makesdna executable
+# Build datatoc executable
set(SRC
datatoc.c
)
# SRC_DNA_INC is defined in the parent dir
add_executable(datatoc ${SRC})
+
+
+# -----------------------------------------------------------------------------
+# Build datatoc_icon executable
+
+if(NOT WITH_HEADLESS)
+
+ set(SRC
+ datatoc_icon.c
+ )
+
+ include_directories(${PNG_INCLUDE_DIR})
+
+ add_executable(datatoc_icon ${SRC})
+
+ link_directories(${PNG_LIBPATH} ${ZLIB_LIBPATH})
+ target_link_libraries(datatoc_icon ${PNG_LIBRARIES})
+endif()
diff --git a/source/blender/datatoc/datatoc_icon.c b/source/blender/datatoc/datatoc_icon.c
new file mode 100644
index 00000000000..82eeb3af9b2
--- /dev/null
+++ b/source/blender/datatoc/datatoc_icon.c
@@ -0,0 +1,390 @@
+/*
+ * ***** 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 *****
+ */
+
+/** \file blender/datatoc/datatoc_icon.c
+ * \ingroup datatoc
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <errno.h>
+
+/* for bool */
+#include "../blenlib/BLI_sys_types.h"
+
+/* for DIR */
+#ifndef WIN32
+# include <dirent.h>
+#endif
+
+#include "png.h"
+
+#ifdef WIN32
+# define SEP '\\'
+#else
+# define SEP '/'
+#endif
+
+#if defined(_MSC_VER)
+# define __func__ __FUNCTION__
+#endif
+
+/* -------------------------------------------------------------------- */
+/* Utility functions */
+
+static int path_ensure_slash(char *string)
+{
+ int len = strlen(string);
+ if (len == 0 || string[len - 1] != SEP) {
+ string[len] = SEP;
+ string[len + 1] = '\0';
+ return len + 1;
+ }
+ return len;
+}
+
+static bool path_test_extension(const char *str, const char *ext)
+{
+ const size_t a = strlen(str);
+ const size_t b = strlen(ext);
+ return !(a == 0 || b == 0 || b >= a) && (strcmp(ext, str + a - b) == 0);
+}
+
+static void endian_switch_uint32(unsigned int *val)
+{
+ unsigned int tval = *val;
+ *val = ((tval >> 24)) |
+ ((tval << 8) & 0x00ff0000) |
+ ((tval >> 8) & 0x0000ff00) |
+ ((tval << 24));
+}
+
+/* -------------------------------------------------------------------- */
+/* Write a PNG from RGBA pixels */
+
+static bool write_png(const char *name, const unsigned int *pixels,
+ const int width, const int height)
+{
+ png_structp png_ptr;
+ png_infop info_ptr;
+ png_bytepp row_pointers = NULL;
+
+ FILE *fp;
+
+ const int bytesperpixel = 4;
+ const int compression = 9;
+ int i;
+
+ fp = fopen(name, "wb");
+ if (fp == NULL) {
+ printf("%s: Cannot open file for writing '%s'\n", __func__, name);
+ return false;
+ }
+
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+ NULL, NULL, NULL);
+ if (png_ptr == NULL) {
+ printf("%s: Cannot png_create_write_struct for file: '%s'\n", __func__, name);
+ fclose(fp);
+ return false;
+ }
+
+ info_ptr = png_create_info_struct(png_ptr);
+ if (info_ptr == NULL) {
+ png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
+ printf("%s: Cannot png_create_info_struct for file: '%s'\n", __func__, name);
+ fclose(fp);
+ return false;
+ }
+
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ printf("%s: Cannot setjmp for file: '%s'\n", __func__, name);
+ fclose(fp);
+ return false;
+ }
+
+ /* write the file */
+ png_init_io(png_ptr, fp);
+
+ png_set_compression_level(png_ptr, compression);
+
+ /* png image settings */
+ png_set_IHDR(png_ptr,
+ info_ptr,
+ width,
+ height,
+ 8,
+ PNG_COLOR_TYPE_RGBA,
+ PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT,
+ PNG_FILTER_TYPE_DEFAULT);
+
+ /* write the file header information */
+ png_write_info(png_ptr, info_ptr);
+
+#ifdef __LITTLE_ENDIAN__
+ png_set_swap(png_ptr);
+#endif
+
+ /* allocate memory for an array of row-pointers */
+ row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep));
+ if (row_pointers == NULL) {
+ printf("%s: Cannot allocate row-pointers array for file '%s'\n", __func__, name);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ if (fp) {
+ fclose(fp);
+ }
+ return false;
+ }
+
+ /* set the individual row-pointers to point at the correct offsets */
+ for (i = 0; i < height; i++) {
+ row_pointers[height - 1 - i] = (png_bytep)
+ (((unsigned char *)pixels) + (i * width) * bytesperpixel * sizeof(unsigned char));
+ }
+
+ /* write out the entire image data in one call */
+ png_write_image(png_ptr, row_pointers);
+
+ /* write the additional chunks to the PNG file (not really needed) */
+ png_write_end(png_ptr, info_ptr);
+
+ /* clean up */
+ free(row_pointers);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+
+ fflush(fp);
+ fclose(fp);
+
+ return true;
+}
+
+
+/* -------------------------------------------------------------------- */
+/* Merge icon-data from files */
+
+struct IconHead {
+ unsigned int icon_w, icon_h;
+ unsigned int orig_x, orig_y;
+ unsigned int canvas_w, canvas_h;
+};
+
+static bool icon_decode_head(FILE *f_src,
+ struct IconHead *r_head)
+{
+ if (fread(r_head, 1, sizeof(*r_head), f_src) == sizeof(*r_head)) {
+#ifndef __LITTLE_ENDIAN__
+ endian_switch_uint32(&r_head->icon_w);
+ endian_switch_uint32(&r_head->icon_h);
+ endian_switch_uint32(&r_head->orig_x);
+ endian_switch_uint32(&r_head->orig_y);
+ endian_switch_uint32(&r_head->canvas_w);
+ endian_switch_uint32(&r_head->canvas_h);
+#endif
+ return true;
+ }
+
+ /* quiet warning */
+ (void)endian_switch_uint32;
+
+ return false;
+}
+
+static bool icon_decode(FILE *f_src,
+ struct IconHead *r_head, unsigned int **r_pixels)
+{
+ unsigned int *pixels;
+ unsigned int pixels_size;
+
+ if (!icon_decode_head(f_src, r_head)) {
+ printf("%s: failed to read header\n", __func__);
+ return false;
+ }
+
+ pixels_size = sizeof(char[4]) * r_head->icon_w * r_head->icon_h;
+ pixels = malloc(pixels_size);
+ if (pixels == NULL) {
+ printf("%s: failed to allocate pixels\n", __func__);
+ return false;
+ }
+
+ if (fread(pixels, 1, pixels_size, f_src) != pixels_size) {
+ printf("%s: failed to read pixels\n", __func__);
+ free(pixels);
+ return false;
+ }
+
+ *r_pixels = pixels;
+ return true;
+}
+
+static bool icon_read(const char *file_src,
+ struct IconHead *r_head, unsigned int **r_pixels)
+{
+ FILE *f_src;
+ bool success;
+
+ f_src = fopen(file_src, "rb");
+ if (f_src == NULL) {
+ printf("%s: failed to open '%s'\n", __func__, file_src);
+ return false;
+ }
+
+ success = icon_decode(f_src, r_head, r_pixels);
+
+ fclose(f_src);
+ return success;
+}
+
+static bool icon_merge(const char *file_src,
+ unsigned int **r_pixels_canvas,
+ unsigned int *r_canvas_w, unsigned int *r_canvas_h)
+{
+ struct IconHead head;
+ unsigned int *pixels;
+
+ unsigned int x, y;
+
+ /* canvas */
+ unsigned int *pixels_canvas;
+ unsigned int canvas_w, canvas_h;
+
+ if (!icon_read(file_src, &head, &pixels)) {
+ return false;
+ }
+
+ if (*r_canvas_w == 0) {
+ /* init once */
+ *r_canvas_w = head.canvas_w;
+ *r_canvas_h = head.canvas_h;
+ *r_pixels_canvas = calloc(1, (head.canvas_w * head.canvas_h) * sizeof(unsigned char[4]));
+ }
+
+ canvas_w = *r_canvas_w;
+ canvas_h = *r_canvas_h;
+ pixels_canvas = *r_pixels_canvas;
+
+ assert(head.canvas_w == canvas_w);
+ assert(head.canvas_h == canvas_h);
+
+ for (x = 0; x < head.icon_w; x++) {
+ for (y = 0; y < head.icon_h; y++) {
+ unsigned int pixel;
+ unsigned int dst_x, dst_y;
+ unsigned int pixel_xy_dst;
+
+
+ /* get pixel */
+ pixel = pixels[(y * head.icon_w) + x];
+
+ /* set pixel */
+ dst_x = head.orig_x + x;
+ dst_y = head.orig_y + y;
+ pixel_xy_dst = (dst_y * canvas_w) + dst_x;
+ assert(pixel_xy_dst < (canvas_w * canvas_h));
+ pixels_canvas[pixel_xy_dst] = pixel;
+ }
+ }
+
+ free(pixels);
+
+ return true;
+}
+
+static bool icondir_to_png(const char *path_src, const char *file_dst)
+{
+ /* Takes a path full of 'dat' files and writes out */
+ DIR *dir;
+ const struct dirent *fname;
+ char filepath[1024];
+ char *filename;
+ int path_str_len;
+ int found = 0, fail = 0;
+
+ unsigned int *pixels_canvas = NULL;
+ unsigned int canvas_w = 0, canvas_h = 0;
+
+ errno = 0;
+ dir = opendir(path_src);
+ if (dir == NULL) {
+ printf("%s: failed to dir '%s', (%s)\n", __func__, path_src, errno ? strerror(errno) : "unknown");
+ return false;
+ }
+
+ strcpy(filepath, path_src);
+ path_str_len = path_ensure_slash(filepath);
+ filename = &filepath[path_str_len];
+
+
+ while ((fname = readdir(dir)) != NULL) {
+ if (path_test_extension(fname->d_name, ".dat")) {
+
+ strcpy(filename, fname->d_name);
+
+ if (icon_merge(filepath, &pixels_canvas, &canvas_w, &canvas_h)) {
+ found++;
+ }
+ else {
+ fail++;
+ }
+ }
+ }
+
+ closedir(dir);
+
+ if (found == 0) {
+ printf("%s: dir '%s' has no icons\n", __func__, path_src);
+ }
+
+ if (fail != 0) {
+ printf("%s: dir '%s' failed %d icons\n", __func__, path_src, fail);
+ }
+
+ /* write pixels */
+ write_png(file_dst, pixels_canvas, canvas_w, canvas_h);
+
+ free(pixels_canvas);
+
+ return true;
+}
+
+
+/* -------------------------------------------------------------------- */
+/* Main and parse args */
+
+int main(int argc, char **argv)
+{
+ const char *path_src;
+ const char *file_dst;
+
+
+ if (argc < 3) {
+ printf("Usage: datatoc_icon <dir_icons> <data_icon_to.png>\n");
+ exit(1);
+ }
+
+ path_src = argv[1];
+ file_dst = argv[2];
+
+ return (icondir_to_png(path_src, file_dst) == true) ? 0 : 1;
+}
diff --git a/source/blender/datatoc/datatoc_icon.py b/source/blender/datatoc/datatoc_icon.py
new file mode 100755
index 00000000000..4f995a3219a
--- /dev/null
+++ b/source/blender/datatoc/datatoc_icon.py
@@ -0,0 +1,155 @@
+#!/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>
+_IS_BIG_ENDIAN = (__import__("sys").byteorder != 'little')
+
+
+def write_png(buf, width, height):
+ import zlib
+ import struct
+ # 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 icon_decode_head(f_src):
+ import struct
+
+ # 2 ints
+ temp_data = f_src.read(4 * 2)
+ icon_w, icon_h = struct.unpack('<2I', temp_data)
+
+ temp_data = f_src.read(4 * 2)
+ orig_x, orig_y = struct.unpack('<2I', temp_data)
+
+ temp_data = f_src.read(4 * 2)
+ canvas_w, canvas_h = struct.unpack('<2I', temp_data)
+
+ return (icon_w, icon_h,
+ orig_x, orig_y,
+ canvas_w, canvas_h)
+
+
+def icon_decode(f_src):
+ head = icon_decode_head(f_src)
+
+ (icon_w, icon_h,
+ orig_x, orig_y,
+ canvas_w, canvas_h) = head
+
+ # pixels
+ import array
+
+ pixels = f_src.read(icon_w * icon_h * 4)
+ pixels = array.array('I', pixels)
+ if _IS_BIG_ENDIAN:
+ pixels.byteswap()
+
+ return head, pixels
+
+
+def icon_read(file_src):
+ with open(file_src, 'rb') as f_src:
+ head, pixels = icon_decode(f_src)
+ return head, pixels
+
+
+def icon_merge(file_src, pixels_canvas, canvas_w, canvas_h):
+ """ Takes an icon filepath and merges into a pixel array
+ """
+ head, pixels = icon_read(file_src)
+
+ (icon_w, icon_h,
+ orig_x, orig_y,
+ w_canvas_test, h_canvas_test) = head
+
+ assert(w_canvas_test == canvas_w)
+ assert(h_canvas_test == canvas_h)
+
+ for x in range(icon_w):
+ for y in range(icon_h):
+ # get pixel
+ pixel = pixels[(y * icon_w) + x]
+
+ # set pixel
+ dst_x = orig_x + x
+ dst_y = orig_y + y
+ pixels_canvas[(dst_y * canvas_w) + dst_x] = pixel
+
+
+def icondir_to_png(path_src, file_dst):
+ """ Takes a path full of 'dat' files and writes out
+ """
+ import os
+ import array
+
+ files = [os.path.join(path_src, f) for f in os.listdir(path_src) if f.endswith(".dat")]
+
+ with open(files[0], 'rb') as f_src:
+ (icon_w, icon_h,
+ orig_x, orig_y,
+ canvas_w, canvas_h) = icon_decode_head(f_src)
+
+ # load in pixel data
+ pixels_canvas = array.array('I', [0]) * (canvas_w * canvas_h)
+ for f in files:
+ icon_merge(f, pixels_canvas, canvas_w, canvas_h)
+
+ # write pixels
+ with open(file_dst, 'wb') as f_dst:
+ import sys
+ # py2/3 compat
+ if sys.version.startswith("2"):
+ pixels_data = pixels_canvas.tostring()
+ else:
+ pixels_data = pixels_canvas.tobytes()
+
+ image_data = write_png(pixels_data, canvas_w, canvas_h)
+ f_dst.write(image_data)
+
+
+def main_ex(argv):
+ import os
+
+ path_src = argv[-2].rstrip(os.sep)
+ file_dst = argv[-1]
+
+ icondir_to_png(path_src, file_dst)
+
+
+def main():
+ import sys
+ main_ex(sys.argv)
+
+
+if __name__ == "__main__":
+ main()
+
diff --git a/source/blender/datatoc/datatoc_icon_split.py b/source/blender/datatoc/datatoc_icon_split.py
new file mode 100755
index 00000000000..44d8e5fd0fb
--- /dev/null
+++ b/source/blender/datatoc/datatoc_icon_split.py
@@ -0,0 +1,292 @@
+# ##### 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>
+
+"""
+This script dices up PNG into small files to store in version control.
+
+Example:
+
+./blender.bin \
+ --background -noaudio \
+ --python ./release/datafiles/icon_dice.py -- \
+ --image=./release/datafiles/blender_icons16.png \
+ --output=./release/datafiles/blender_icons16
+ --output_prefix=icon16_
+ --name_style=UI_ICONS
+ --parts_x 26 --parts_y 32 \
+ --minx=10 --maxx 10 --miny 10 --maxy 10
+ --minx_icon 2 --maxx_icon 2 --miny_icon 2 --maxy_icon 2 \
+ --spacex_icon 1 --spacey_icon 1
+
+"""
+
+import os
+
+SOURCE_DIR = os.path.normpath(os.path.join(os.path.dirname(__file__), "..", "..", ".."))
+VERBOSE = False
+
+
+def image_from_file__bpy(filepath):
+ import bpy
+
+ image = bpy.data.images.load(filepath)
+ image.reload()
+
+ pixel_w, pixel_h = image.size
+ pixels = image.pixels[:]
+ return pixels, pixel_w, pixel_h
+
+
+def image_from_file(filepath):
+ """
+ Return pixels, w, h from an image.
+
+ note: bpy import is ONLY used here.
+ """
+
+ try:
+ import bpy
+ except:
+ bpy = None
+
+ if bpy is not None:
+ pixels, pixel_w, pixel_h = image_from_file__bpy(filepath)
+ #else:
+ # pixels, pixel_w, pixel_h = image_from_file__py(filepath)
+
+ return pixels, pixel_w, pixel_h
+
+
+def write_subimage(sub_x, sub_y, sub_w, sub_h,
+ filepath,
+ pixels, pixel_w, pixel_h):
+ import struct
+
+ # first check if the icon is worth writing
+ is_fill = False
+ for y in range(sub_h):
+ for x in range(sub_w):
+ i = (sub_x + x) + ((sub_y + y) * pixel_w)
+ a = pixels[(i * 4) + 3]
+ if a != 0.0:
+ is_fill = True
+ break
+
+ if not is_fill:
+ # print("skipping:", filepath)
+ return
+
+ with open(filepath, 'wb') as f:
+
+ f.write(struct.pack('<6I',
+ sub_w, sub_h,
+ sub_x, sub_y,
+ # redundant but including to maintain consistency
+ pixel_w, pixel_h,
+ ))
+
+ for y in range(sub_h):
+ for x in range(sub_w):
+ i = (sub_x + x) + ((sub_y + y) * pixel_w)
+ rgba = pixels[(i * 4):(i * 4) + 4]
+ c = sum((int(p * 255) << (8 * i)) for i, p in enumerate(rgba))
+ f.write(struct.pack("<I", c))
+
+
+_dice_icon_name_cache = {}
+
+
+def dice_icon_name(x, y, parts_x, parts_y,
+ name_style=None, prefix=""):
+ """
+ How to name icons, this is mainly for what name we get in git,
+ the actual names don't really matter, its just nice to have the
+ name match up with something recognizable for commits.
+ """
+ if name_style == 'UI_ICONS':
+
+ # Init on demand
+ if not _dice_icon_name_cache:
+ import re
+
+ # Search for eg: DEF_ICON(BRUSH_NUDGE) --> BRUSH_NUDGE
+ re_icon = re.compile('^\s*DEF_ICON\(\s*([A-Za-z0-9_]+)\s*\).*$')
+
+ ui_icons_h = os.path.join(SOURCE_DIR, "source", "blender", "editors", "include", "UI_icons.h")
+ with open(ui_icons_h, 'r', encoding="utf-8") as f:
+ for l in f:
+ match = re_icon.search(l)
+ if match:
+ icon_name = match.group(1).lower()
+ # print(l.rstrip())
+ _dice_icon_name_cache[len(_dice_icon_name_cache)] = icon_name
+ # ---- Done with icon cache
+
+ index = (y * parts_x) + x
+ icon_name = _dice_icon_name_cache[index]
+
+ # for debugging its handy to sort by number
+ #~ id_str = "%03d_%s%s.dat" % (index, prefix, icon_name)
+
+ id_str = "%s%s.dat" % (prefix, icon_name)
+
+ elif name_style == "":
+ # flip so icons are numbered from top-left
+ # because new icons will be added at the bottom
+ y_flip = parts_y - (y + 1)
+ id_str = "%s%02xx%02x.dat" % (prefix, x, y_flip)
+ else:
+ raise Exception("Invalid '--name_style' arg")
+
+ return id_str
+
+
+def dice(filepath, output, output_prefix, name_style,
+ parts_x, parts_y,
+ minx, miny, maxx, maxy,
+ minx_icon, miny_icon, maxx_icon, maxy_icon,
+ spacex_icon, spacey_icon,
+ ):
+ import struct
+
+ is_simple = (max(minx, miny, maxx, maxy,
+ minx_icon, miny_icon, maxx_icon, maxy_icon,
+ spacex_icon, spacey_icon) == 0)
+
+ pixels, pixel_w, pixel_h = image_from_file(filepath)
+
+ if not (pixel_w and pixel_h):
+ print("Image not found %r!" % filepath)
+ return
+
+ if not os.path.exists(output):
+ os.mkdir(output)
+
+ if is_simple:
+ pixels_w_clip = pixel_w
+ pixels_h_clip = pixel_h
+
+ icon_w = pixels_w_clip // parts_x
+ icon_h = pixels_h_clip // parts_y
+ icon_w_clip = icon_w
+ icon_h_clip = icon_h
+ else:
+ pixels_w_clip = pixel_w - (minx + maxx)
+ pixels_h_clip = pixel_h - (miny + maxy)
+
+ icon_w = (pixels_w_clip - ((parts_x - 1) * spacex_icon)) // parts_x
+ icon_h = (pixels_h_clip - ((parts_y - 1) * spacey_icon)) // parts_y
+ icon_w_clip = icon_w - (minx_icon + maxx_icon)
+ icon_h_clip = icon_h - (miny_icon + maxy_icon)
+
+ print(pixel_w, pixel_h, icon_w, icon_h)
+
+ for x in range(parts_x):
+ for y in range(parts_y):
+ id_str = dice_icon_name(x, y,
+ parts_x, parts_y,
+ name_style=name_style, prefix=output_prefix)
+ filepath = os.path.join(output, id_str)
+ if VERBOSE:
+ print(" writing:", filepath)
+
+ # simple, no margins
+ if is_simple:
+ sub_x = x * icon_x
+ sub_y = y * icon_h
+ else:
+ sub_x = minx + ((x * (icon_w + spacex_icon)) + minx_icon)
+ sub_y = miny + ((y * (icon_h + spacey_icon)) + miny_icon)
+
+ write_subimage(sub_x, sub_y, icon_w_clip, icon_h_clip,
+ filepath,
+ pixels, pixel_w, pixel_h)
+
+
+def main():
+ import sys
+ import argparse
+
+ epilog = "Run this after updating the SVG file"
+
+ argv = sys.argv
+
+ if "--" not in argv:
+ argv = []
+ else:
+ argv = argv[argv.index("--") + 1:]
+
+ parser = argparse.ArgumentParser(description=__doc__, epilog=epilog)
+
+ # File path options
+ parser.add_argument("--image", dest="image", metavar='FILE',
+ help="Image file")
+
+ parser.add_argument("--output", dest="output", metavar='DIR',
+ help="Output directory")
+
+ parser.add_argument("--output_prefix", dest="output_prefix", metavar='STRING',
+ help="Output prefix")
+
+ # Icon naming option
+ parser.add_argument("--name_style", dest="name_style", metavar='ENUM', type=str,
+ choices=('', 'UI_ICONS'),
+ help="The metod used for naming output data")
+
+ # Options for dicing up the image
+ parser.add_argument("--parts_x", dest="parts_x", metavar='INT', type=int,
+ help="Grid X parts")
+ parser.add_argument("--parts_y", dest="parts_y", metavar='INT', type=int,
+ help="Grid Y parts")
+
+ _help = "Inset from the outer edge (in pixels)"
+ parser.add_argument("--minx", dest="minx", metavar='INT', type=int, help=_help)
+ parser.add_argument("--miny", dest="miny", metavar='INT', type=int, help=_help)
+ parser.add_argument("--maxx", dest="maxx", metavar='INT', type=int, help=_help)
+ parser.add_argument("--maxy", dest="maxy", metavar='INT', type=int, help=_help)
+
+ _help = "Inset from each icons bounds (in pixels)"
+ parser.add_argument("--minx_icon", dest="minx_icon", metavar='INT', type=int, help=_help)
+ parser.add_argument("--miny_icon", dest="miny_icon", metavar='INT', type=int, help=_help)
+ parser.add_argument("--maxx_icon", dest="maxx_icon", metavar='INT', type=int, help=_help)
+ parser.add_argument("--maxy_icon", dest="maxy_icon", metavar='INT', type=int, help=_help)
+
+ _help = "Empty space between icons"
+ parser.add_argument("--spacex_icon", dest="spacex_icon", metavar='INT', type=int, help=_help)
+ parser.add_argument("--spacey_icon", dest="spacey_icon", metavar='INT', type=int, help=_help)
+
+ del _help
+
+ args = parser.parse_args(argv)
+
+ if not argv:
+ print("No args given!")
+ parser.print_help()
+ return
+
+ dice(args.image, args.output, args.output_prefix, args.name_style,
+ args.parts_x, args.parts_y,
+ args.minx, args.miny, args.maxx, args.maxy,
+ args.minx_icon, args.miny_icon, args.maxx_icon, args.maxy_icon,
+ args.spacex_icon, args.spacey_icon,
+ )
+
+if __name__ == "__main__":
+ main()
diff --git a/source/blender/datatoc/datatoc_icon_split_to_png.py b/source/blender/datatoc/datatoc_icon_split_to_png.py
new file mode 100755
index 00000000000..39bbf1110fb
--- /dev/null
+++ b/source/blender/datatoc/datatoc_icon_split_to_png.py
@@ -0,0 +1,71 @@
+#!/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 #####
+
+# This script is just to view the icons
+
+
+def write_png(buf, width, height):
+ import zlib
+ import struct
+ # 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 icondata_to_png(file_src, file_dst):
+ import struct
+
+ with open(file_src, 'rb') as f_src:
+ # 2 ints
+ temp_data = f_src.read(4 * 2)
+ w, h = struct.unpack('<2I', temp_data)
+ temp_data = f_src.read(4 * 2) # (x, y) - ignored
+ temp_data = f_src.read(4 * 2) # (xfrom, yfrom) - ignored
+ # pixels
+ temp_data = f_src.read(w * h * 4)
+
+ buf = write_png(temp_data, w, h)
+
+ with open(file_dst, 'wb') as f_dst:
+ f_dst.write(buf)
+
+
+def main():
+ import sys
+ import os
+
+ for arg in sys.argv[1:]:
+ file_src = arg
+ file_dst = os.path.splitext(arg)[0] + ".png"
+
+ icondata_to_png(file_src, file_dst)
+
+if __name__ == "__main__":
+ main()