2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2014-01-13 13:47:33 +01:00
|
|
|
|
2019-02-17 22:08:12 +01:00
|
|
|
/** \file
|
|
|
|
* \ingroup datatoc
|
2014-01-13 13:47:33 +01:00
|
|
|
*/
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
#include <cassert>
|
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2014-01-13 13:47:33 +01:00
|
|
|
|
|
|
|
/* for bool */
|
|
|
|
#include "../blenlib/BLI_sys_types.h"
|
|
|
|
|
|
|
|
/* for DIR */
|
2014-01-14 22:57:00 +01:00
|
|
|
#if !defined(WIN32) || defined(FREEWINDOWS)
|
2014-01-13 13:47:33 +01:00
|
|
|
# include <dirent.h>
|
|
|
|
#endif
|
|
|
|
|
2021-10-04 04:12:38 +02:00
|
|
|
#include <png.h>
|
2014-01-13 13:47:33 +01:00
|
|
|
|
2014-01-14 22:38:00 +01:00
|
|
|
/* for Win32 DIR functions */
|
|
|
|
#ifdef WIN32
|
|
|
|
# include "../blenlib/BLI_winstuff.h"
|
|
|
|
#endif
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
#ifdef WIN32
|
|
|
|
# define SEP '\\'
|
|
|
|
#else
|
|
|
|
# define SEP '/'
|
|
|
|
#endif
|
|
|
|
|
2023-07-31 01:58:39 +02:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/** \name Endian Defines
|
|
|
|
* \{ */
|
|
|
|
|
|
|
|
#define L_ENDIAN 1
|
|
|
|
#define B_ENDIAN 0
|
|
|
|
#ifdef __BIG_ENDIAN__
|
|
|
|
# define ENDIAN_ORDER B_ENDIAN
|
|
|
|
#else
|
|
|
|
# define ENDIAN_ORDER L_ENDIAN
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** \} */
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
2023-07-31 01:57:46 +02:00
|
|
|
/** \name Utility Functions
|
|
|
|
* \{ */
|
2014-01-13 13:47:33 +01:00
|
|
|
|
2023-05-30 02:31:31 +02:00
|
|
|
static bool path_test_extension(const char *filepath, const char *ext)
|
2014-01-13 13:47:33 +01:00
|
|
|
{
|
2023-05-30 02:31:31 +02:00
|
|
|
const size_t a = strlen(filepath);
|
2014-01-13 13:47:33 +01:00
|
|
|
const size_t b = strlen(ext);
|
2023-05-30 02:31:31 +02:00
|
|
|
return !(a == 0 || b == 0 || b >= a) && (strcmp(ext, filepath + a - b) == 0);
|
2014-01-13 13:47:33 +01:00
|
|
|
}
|
|
|
|
|
2021-06-13 06:47:25 +02:00
|
|
|
static void endian_switch_uint32(uint *val)
|
2014-01-13 13:47:33 +01:00
|
|
|
{
|
2021-06-13 06:47:25 +02:00
|
|
|
uint tval = *val;
|
2022-09-25 07:14:13 +02:00
|
|
|
*val = (tval >> 24) | ((tval << 8) & 0x00ff0000) | ((tval >> 8) & 0x0000ff00) | (tval << 24);
|
2014-01-13 13:47:33 +01:00
|
|
|
}
|
|
|
|
|
2023-05-30 02:31:31 +02:00
|
|
|
static const char *path_slash_rfind(const char *path)
|
2020-12-02 10:27:48 +01:00
|
|
|
{
|
2023-05-30 02:31:31 +02:00
|
|
|
const char *const lfslash = strrchr(path, '/');
|
|
|
|
const char *const lbslash = strrchr(path, '\\');
|
2020-12-02 10:27:48 +01:00
|
|
|
|
|
|
|
if (!lfslash) {
|
|
|
|
return lbslash;
|
|
|
|
}
|
|
|
|
if (!lbslash) {
|
|
|
|
return lfslash;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (lfslash > lbslash) ? lfslash : lbslash;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *path_basename(const char *path)
|
|
|
|
{
|
|
|
|
const char *const filename = path_slash_rfind(path);
|
|
|
|
return filename ? filename + 1 : path;
|
|
|
|
}
|
|
|
|
|
2023-06-20 05:23:33 +02:00
|
|
|
static bool path_join(char *filepath,
|
|
|
|
size_t filepath_maxncpy,
|
|
|
|
const char *dirpath,
|
|
|
|
const char *filename)
|
|
|
|
{
|
|
|
|
int dirpath_len = strlen(dirpath);
|
|
|
|
if (dirpath_len && dirpath[dirpath_len - 1] == SEP) {
|
|
|
|
dirpath_len--;
|
|
|
|
}
|
|
|
|
const int filename_len = strlen(filename);
|
|
|
|
if (dirpath_len + 1 + filename_len + 1 > filepath_maxncpy) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memcpy(filepath, dirpath, dirpath_len);
|
|
|
|
filepath[dirpath_len] = SEP;
|
|
|
|
memcpy(filepath + dirpath_len + 1, filename, filename_len + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-31 01:57:46 +02:00
|
|
|
/** \} */
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
2023-07-31 01:57:46 +02:00
|
|
|
/** \name Write a PNG from RGBA Pixels
|
|
|
|
* \{ */
|
2014-01-13 13:47:33 +01:00
|
|
|
|
2023-05-03 08:58:46 +02:00
|
|
|
static bool write_png(const char *filepath, const uint *pixels, const int width, const int height)
|
2014-01-13 13:47:33 +01:00
|
|
|
{
|
|
|
|
png_structp png_ptr;
|
|
|
|
png_infop info_ptr;
|
2023-07-27 13:10:42 +02:00
|
|
|
png_bytepp row_pointers = nullptr;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
FILE *fp;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
const int bytesperpixel = 4;
|
|
|
|
const int compression = 9;
|
|
|
|
int i;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-05-03 08:58:46 +02:00
|
|
|
fp = fopen(filepath, "wb");
|
2023-07-27 13:10:42 +02:00
|
|
|
if (fp == nullptr) {
|
2023-05-03 08:58:46 +02:00
|
|
|
printf("%s: Cannot open file for writing '%s'\n", __func__, filepath);
|
2014-01-13 13:47:33 +01:00
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-27 13:10:42 +02:00
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
|
|
|
|
if (png_ptr == nullptr) {
|
2023-05-03 08:58:46 +02:00
|
|
|
printf("%s: Cannot png_create_write_struct for file: '%s'\n", __func__, filepath);
|
2014-01-13 13:47:33 +01:00
|
|
|
fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
info_ptr = png_create_info_struct(png_ptr);
|
2023-07-27 13:10:42 +02:00
|
|
|
if (info_ptr == nullptr) {
|
|
|
|
png_destroy_write_struct(&png_ptr, (png_infopp) nullptr);
|
2023-05-03 08:58:46 +02:00
|
|
|
printf("%s: Cannot png_create_info_struct for file: '%s'\n", __func__, filepath);
|
2014-01-13 13:47:33 +01:00
|
|
|
fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
if (setjmp(png_jmpbuf(png_ptr))) {
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
2023-05-03 08:58:46 +02:00
|
|
|
printf("%s: Cannot setjmp for file: '%s'\n", __func__, filepath);
|
2014-01-13 13:47:33 +01:00
|
|
|
fclose(fp);
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* write the file */
|
|
|
|
png_init_io(png_ptr, fp);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
png_set_compression_level(png_ptr, compression);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* 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);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* write the file header information */
|
|
|
|
png_write_info(png_ptr, info_ptr);
|
|
|
|
|
2023-07-31 01:58:39 +02:00
|
|
|
if (ENDIAN_ORDER == L_ENDIAN) {
|
|
|
|
png_set_swap(png_ptr);
|
|
|
|
}
|
2014-01-13 13:47:33 +01:00
|
|
|
|
|
|
|
/* allocate memory for an array of row-pointers */
|
|
|
|
row_pointers = (png_bytepp)malloc(height * sizeof(png_bytep));
|
2023-07-27 13:10:42 +02:00
|
|
|
if (row_pointers == nullptr) {
|
2023-05-03 08:58:46 +02:00
|
|
|
printf("%s: Cannot allocate row-pointers array for file '%s'\n", __func__, filepath);
|
2014-01-13 13:47:33 +01:00
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
|
|
|
if (fp) {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* set the individual row-pointers to point at the correct offsets */
|
|
|
|
for (i = 0; i < height; i++) {
|
2022-09-25 09:04:52 +02:00
|
|
|
row_pointers[height - 1 - i] = (png_bytep)(((const uchar *)pixels) +
|
|
|
|
(i * width) * bytesperpixel * sizeof(uchar));
|
2014-01-13 13:47:33 +01:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* write out the entire image data in one call */
|
|
|
|
png_write_image(png_ptr, row_pointers);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* write the additional chunks to the PNG file (not really needed) */
|
|
|
|
png_write_end(png_ptr, info_ptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* clean up */
|
|
|
|
free(row_pointers);
|
|
|
|
png_destroy_write_struct(&png_ptr, &info_ptr);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
fflush(fp);
|
|
|
|
fclose(fp);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-31 01:57:46 +02:00
|
|
|
/** \} */
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
2023-07-31 01:57:46 +02:00
|
|
|
/** \name Merge Icon-Data from Files
|
|
|
|
* \{ */
|
2014-01-13 13:47:33 +01:00
|
|
|
|
|
|
|
struct IconHead {
|
2021-06-13 06:47:25 +02:00
|
|
|
uint icon_w, icon_h;
|
|
|
|
uint orig_x, orig_y;
|
|
|
|
uint canvas_w, canvas_h;
|
2014-01-13 13:47:33 +01:00
|
|
|
};
|
|
|
|
|
2020-12-02 10:27:48 +01:00
|
|
|
struct IconInfo {
|
2023-07-27 13:49:56 +02:00
|
|
|
IconHead head;
|
2020-12-02 10:27:48 +01:00
|
|
|
char *file_name;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct IconMergeContext {
|
|
|
|
/* Information about all icons read from disk.
|
|
|
|
* Is used for sanity checks like prevention of two files defining icon for
|
|
|
|
* the same position on canvas. */
|
|
|
|
int num_read_icons;
|
2023-07-27 13:49:56 +02:00
|
|
|
IconInfo *read_icons;
|
2020-12-02 10:27:48 +01:00
|
|
|
};
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
static void icon_merge_context_init(IconMergeContext *context)
|
2020-12-02 10:27:48 +01:00
|
|
|
{
|
|
|
|
context->num_read_icons = 0;
|
2023-07-27 13:10:42 +02:00
|
|
|
context->read_icons = nullptr;
|
2020-12-02 10:27:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get icon information from the context which matches given icon head.
|
|
|
|
* Is used to check whether icon is re-defined, and to provide useful information about which
|
|
|
|
* files are conflicting. */
|
2023-07-27 13:49:56 +02:00
|
|
|
static IconInfo *icon_merge_context_info_for_icon_head(IconMergeContext *context,
|
|
|
|
IconHead *icon_head)
|
2020-12-02 10:27:48 +01:00
|
|
|
{
|
2023-07-27 13:10:42 +02:00
|
|
|
if (context->read_icons == nullptr) {
|
|
|
|
return nullptr;
|
2020-12-02 10:27:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < context->num_read_icons; i++) {
|
2023-07-27 13:49:56 +02:00
|
|
|
IconInfo *read_icon_info = &context->read_icons[i];
|
|
|
|
const IconHead *read_icon_head = &read_icon_info->head;
|
2020-12-02 10:27:48 +01:00
|
|
|
if (read_icon_head->orig_x == icon_head->orig_x && read_icon_head->orig_y == icon_head->orig_y)
|
|
|
|
{
|
|
|
|
return read_icon_info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:10:42 +02:00
|
|
|
return nullptr;
|
2020-12-02 10:27:48 +01:00
|
|
|
}
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
static void icon_merge_context_register_icon(IconMergeContext *context,
|
2020-12-02 10:27:48 +01:00
|
|
|
const char *file_name,
|
2023-07-27 13:49:56 +02:00
|
|
|
const IconHead *icon_head)
|
2020-12-02 10:27:48 +01:00
|
|
|
{
|
2023-07-27 13:10:42 +02:00
|
|
|
context->read_icons = static_cast<IconInfo *>(
|
2023-07-27 13:49:56 +02:00
|
|
|
realloc(context->read_icons, sizeof(IconInfo) * (context->num_read_icons + 1)));
|
2020-12-02 10:27:48 +01:00
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
IconInfo *icon_info = &context->read_icons[context->num_read_icons];
|
2020-12-02 10:27:48 +01:00
|
|
|
icon_info->head = *icon_head;
|
|
|
|
icon_info->file_name = strdup(path_basename(file_name));
|
|
|
|
|
|
|
|
context->num_read_icons++;
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
static void icon_merge_context_free(IconMergeContext *context)
|
2020-12-02 10:27:48 +01:00
|
|
|
{
|
2023-07-27 13:10:42 +02:00
|
|
|
if (context->read_icons != nullptr) {
|
2020-12-02 10:27:48 +01:00
|
|
|
for (int i = 0; i < context->num_read_icons; i++) {
|
|
|
|
free(context->read_icons[i].file_name);
|
|
|
|
}
|
|
|
|
free(context->read_icons);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
static bool icon_decode_head(FILE *f_src, IconHead *r_head)
|
2014-01-13 13:47:33 +01:00
|
|
|
{
|
|
|
|
if (fread(r_head, 1, sizeof(*r_head), f_src) == sizeof(*r_head)) {
|
2023-07-31 01:58:39 +02:00
|
|
|
if (ENDIAN_ORDER == B_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);
|
|
|
|
}
|
2014-01-13 13:47:33 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
static bool icon_decode(FILE *f_src, IconHead *r_head, uint **r_pixels)
|
2014-01-13 13:47:33 +01:00
|
|
|
{
|
2021-06-13 06:47:25 +02:00
|
|
|
uint *pixels;
|
|
|
|
uint pixels_size;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
if (!icon_decode_head(f_src, r_head)) {
|
|
|
|
printf("%s: failed to read header\n", __func__);
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
pixels_size = sizeof(char[4]) * r_head->icon_w * r_head->icon_h;
|
2023-07-27 13:10:42 +02:00
|
|
|
pixels = static_cast<uint *>(malloc(pixels_size));
|
|
|
|
if (pixels == nullptr) {
|
2014-01-13 13:47:33 +01:00
|
|
|
printf("%s: failed to allocate pixels\n", __func__);
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
if (fread(pixels, 1, pixels_size, f_src) != pixels_size) {
|
|
|
|
printf("%s: failed to read pixels\n", __func__);
|
|
|
|
free(pixels);
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
*r_pixels = pixels;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
static bool icon_read(const char *file_src, IconHead *r_head, uint **r_pixels)
|
2014-01-13 13:47:33 +01:00
|
|
|
{
|
|
|
|
FILE *f_src;
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
f_src = fopen(file_src, "rb");
|
2023-07-27 13:10:42 +02:00
|
|
|
if (f_src == nullptr) {
|
2014-01-13 13:47:33 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
static bool icon_merge(IconMergeContext *context,
|
2020-12-02 10:27:48 +01:00
|
|
|
const char *file_src,
|
2021-06-13 06:47:28 +02:00
|
|
|
uint32_t **r_pixels_canvas,
|
2021-06-13 06:47:25 +02:00
|
|
|
uint *r_canvas_w,
|
|
|
|
uint *r_canvas_h)
|
2014-01-13 13:47:33 +01:00
|
|
|
{
|
2023-07-27 13:49:56 +02:00
|
|
|
IconHead head;
|
2021-06-13 06:47:25 +02:00
|
|
|
uint *pixels;
|
2014-01-13 13:47:33 +01:00
|
|
|
|
2021-06-13 06:47:25 +02:00
|
|
|
uint x, y;
|
2014-01-13 13:47:33 +01:00
|
|
|
|
|
|
|
/* canvas */
|
2021-06-13 06:47:28 +02:00
|
|
|
uint32_t *pixels_canvas;
|
2021-06-13 06:47:25 +02:00
|
|
|
uint canvas_w, canvas_h;
|
2014-01-13 13:47:33 +01:00
|
|
|
|
|
|
|
if (!icon_read(file_src, &head, &pixels)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
const IconInfo *read_icon_info = icon_merge_context_info_for_icon_head(context, &head);
|
2023-07-27 13:10:42 +02:00
|
|
|
if (read_icon_info != nullptr) {
|
2020-12-02 10:27:48 +01:00
|
|
|
printf(
|
|
|
|
"Conflicting icon files %s and %s\n", path_basename(file_src), read_icon_info->file_name);
|
|
|
|
free(pixels);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
icon_merge_context_register_icon(context, file_src, &head);
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
if (*r_canvas_w == 0) {
|
|
|
|
/* init once */
|
|
|
|
*r_canvas_w = head.canvas_w;
|
|
|
|
*r_canvas_h = head.canvas_h;
|
2023-07-27 13:10:42 +02:00
|
|
|
*r_pixels_canvas = static_cast<uint32_t *>(
|
|
|
|
calloc(1, (head.canvas_w * head.canvas_h) * sizeof(uint32_t)));
|
2019-04-17 06:17:24 +02:00
|
|
|
}
|
2014-01-13 13:47:33 +01:00
|
|
|
|
|
|
|
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++) {
|
2021-06-13 06:47:25 +02:00
|
|
|
uint pixel;
|
|
|
|
uint dst_x, dst_y;
|
|
|
|
uint pixel_xy_dst;
|
2014-01-13 13:47:33 +01:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2014-06-18 07:48:22 +02:00
|
|
|
/* only for bounds check */
|
|
|
|
(void)canvas_h;
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
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;
|
2023-07-27 13:49:56 +02:00
|
|
|
const dirent *fname;
|
2014-01-13 13:47:33 +01:00
|
|
|
char filepath[1024];
|
|
|
|
int found = 0, fail = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-27 13:49:56 +02:00
|
|
|
IconMergeContext context;
|
2020-12-02 10:27:48 +01:00
|
|
|
|
2023-07-27 13:10:42 +02:00
|
|
|
uint32_t *pixels_canvas = nullptr;
|
2021-06-13 06:47:25 +02:00
|
|
|
uint canvas_w = 0, canvas_h = 0;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-12-02 10:27:48 +01:00
|
|
|
icon_merge_context_init(&context);
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
errno = 0;
|
|
|
|
dir = opendir(path_src);
|
2023-07-27 13:10:42 +02:00
|
|
|
if (dir == nullptr) {
|
2014-01-13 13:47:33 +01:00
|
|
|
printf(
|
|
|
|
"%s: failed to dir '%s', (%s)\n", __func__, path_src, errno ? strerror(errno) : "unknown");
|
|
|
|
return false;
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2023-07-27 13:10:42 +02:00
|
|
|
while ((fname = readdir(dir)) != nullptr) {
|
2014-01-13 13:47:33 +01:00
|
|
|
if (path_test_extension(fname->d_name, ".dat")) {
|
2023-06-20 05:23:33 +02:00
|
|
|
if (!path_join(filepath, sizeof(filepath), path_src, fname->d_name)) {
|
|
|
|
printf("%s: path is too long (%s, %s)\n", __func__, path_src, fname->d_name);
|
|
|
|
return false;
|
|
|
|
}
|
2020-12-02 10:27:48 +01:00
|
|
|
if (icon_merge(&context, filepath, &pixels_canvas, &canvas_w, &canvas_h)) {
|
2014-01-13 13:47:33 +01:00
|
|
|
found++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
fail++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-12-02 10:27:48 +01:00
|
|
|
icon_merge_context_free(&context);
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
closedir(dir);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
if (found == 0) {
|
|
|
|
printf("%s: dir '%s' has no icons\n", __func__, path_src);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
if (fail != 0) {
|
|
|
|
printf("%s: dir '%s' failed %d icons\n", __func__, path_src, fail);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-24 07:56:58 +02:00
|
|
|
/* Write pixels. */
|
2014-01-13 13:47:33 +01:00
|
|
|
write_png(file_dst, pixels_canvas, canvas_w, canvas_h);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
free(pixels_canvas);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2020-12-02 10:27:48 +01:00
|
|
|
return (fail == 0);
|
2014-01-13 13:47:33 +01:00
|
|
|
}
|
|
|
|
|
2023-07-31 01:57:46 +02:00
|
|
|
/** \} */
|
|
|
|
|
2014-01-13 13:47:33 +01:00
|
|
|
/* -------------------------------------------------------------------- */
|
2023-07-31 01:57:46 +02:00
|
|
|
/** \name Main & Parse Arguments
|
|
|
|
* \{ */
|
2014-01-13 13:47:33 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-07-31 01:57:46 +02:00
|
|
|
|
|
|
|
/** \} */
|