Fix for icon generation with MSVC
[blender.git] / source / blender / datatoc / datatoc_icon.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * ***** END GPL LICENSE BLOCK *****
19  */
20
21 /** \file blender/datatoc/datatoc_icon.c
22  *  \ingroup datatoc
23  */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include <errno.h>
30
31 /* for bool */
32 #include "../blenlib/BLI_sys_types.h"
33
34 /* for DIR */
35 #if !defined(WIN32) || defined(__MINGW64__)
36 #  include <dirent.h>
37 #endif
38
39 #include "png.h"
40
41
42 /* for Win32 DIR functions */
43 #ifdef WIN32
44 #  include "../blenlib/BLI_winstuff.h"
45 #endif
46
47 #ifdef WIN32
48 #  define SEP '\\'
49 #else
50 #  define SEP '/'
51 #endif
52
53 #if defined(_MSC_VER)
54 #  define __func__ __FUNCTION__
55 #endif
56
57 /* -------------------------------------------------------------------- */
58 /* Utility functions */
59
60 static int path_ensure_slash(char *string)
61 {
62         int len = strlen(string);
63         if (len == 0 || string[len - 1] != SEP) {
64                 string[len] = SEP;
65                 string[len + 1] = '\0';
66                 return len + 1;
67         }
68         return len;
69 }
70
71 static bool path_test_extension(const char *str, const char *ext)
72 {
73         const size_t a = strlen(str);
74         const size_t b = strlen(ext);
75         return !(a == 0 || b == 0 || b >= a) && (strcmp(ext, str + a - b) == 0);
76 }
77
78 static void endian_switch_uint32(unsigned int *val)
79 {
80         unsigned int tval = *val;
81         *val = ((tval >> 24))             |
82                ((tval << 8) & 0x00ff0000) |
83                ((tval >> 8) & 0x0000ff00) |
84                ((tval << 24));
85 }
86
87 /* -------------------------------------------------------------------- */
88 /* Write a PNG from RGBA pixels */
89
90 static bool write_png(const char *name, const unsigned int *pixels,
91                      const int width, const int height)
92 {
93         png_structp png_ptr;
94         png_infop info_ptr;
95         png_bytepp row_pointers = NULL;
96
97         FILE *fp;
98
99         const int bytesperpixel = 4;
100         const int compression = 9;
101         int i;
102
103         fp = fopen(name, "wb");
104         if (fp == NULL) {
105                 printf("%s: Cannot open file for writing '%s'\n", __func__, name);
106                 return false;
107         }
108
109         png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
110                                           NULL, NULL, NULL);
111         if (png_ptr == NULL) {
112                 printf("%s: Cannot png_create_write_struct for file: '%s'\n", __func__, name);
113                 fclose(fp);
114                 return false;
115         }
116
117         info_ptr = png_create_info_struct(png_ptr);
118         if (info_ptr == NULL) {
119                 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
120                 printf("%s: Cannot png_create_info_struct for file: '%s'\n", __func__, name);
121                 fclose(fp);
122                 return false;
123         }
124
125         if (setjmp(png_jmpbuf(png_ptr))) {
126                 png_destroy_write_struct(&png_ptr, &info_ptr);
127                 printf("%s: Cannot setjmp for file: '%s'\n", __func__, name);
128                 fclose(fp);
129                 return false;
130         }
131
132         /* write the file */
133         png_init_io(png_ptr, fp);
134
135         png_set_compression_level(png_ptr, compression);
136
137         /* png image settings */
138         png_set_IHDR(png_ptr,
139                      info_ptr,
140                      width,
141                      height,
142                      8,
143                      PNG_COLOR_TYPE_RGBA,
144                      PNG_INTERLACE_NONE,
145                      PNG_COMPRESSION_TYPE_DEFAULT,
146                      PNG_FILTER_TYPE_DEFAULT);
147
148         /* write the file header information */
149         png_write_info(png_ptr, info_ptr);
150
151 #ifdef __LITTLE_ENDIAN__
152         png_set_swap(png_ptr);
153 #endif
154
155         /* allocate memory for an array of row-pointers */
156         row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep));
157         if (row_pointers == NULL) {
158                 printf("%s: Cannot allocate row-pointers array for file '%s'\n", __func__, name);
159                 png_destroy_write_struct(&png_ptr, &info_ptr);
160                 if (fp) {
161                         fclose(fp);
162                 }
163                 return false;
164         }
165
166         /* set the individual row-pointers to point at the correct offsets */
167         for (i = 0; i < height; i++) {
168                 row_pointers[height - 1 - i] = (png_bytep)
169                                                (((unsigned char *)pixels) + (i * width) * bytesperpixel * sizeof(unsigned char));
170         }
171
172         /* write out the entire image data in one call */
173         png_write_image(png_ptr, row_pointers);
174
175         /* write the additional chunks to the PNG file (not really needed) */
176         png_write_end(png_ptr, info_ptr);
177
178         /* clean up */
179         free(row_pointers);
180         png_destroy_write_struct(&png_ptr, &info_ptr);
181
182         fflush(fp);
183         fclose(fp);
184
185         return true;
186 }
187
188
189 /* -------------------------------------------------------------------- */
190 /* Merge icon-data from files */
191
192 struct IconHead {
193         unsigned int icon_w, icon_h;
194         unsigned int orig_x, orig_y;
195         unsigned int canvas_w, canvas_h;
196 };
197
198 static bool icon_decode_head(FILE *f_src,
199                             struct IconHead *r_head)
200 {
201         if (fread(r_head, 1, sizeof(*r_head), f_src) == sizeof(*r_head)) {
202 #ifndef __LITTLE_ENDIAN__
203                 endian_switch_uint32(&r_head->icon_w);
204                 endian_switch_uint32(&r_head->icon_h);
205                 endian_switch_uint32(&r_head->orig_x);
206                 endian_switch_uint32(&r_head->orig_y);
207                 endian_switch_uint32(&r_head->canvas_w);
208                 endian_switch_uint32(&r_head->canvas_h);
209 #endif
210                 return true;
211         }
212
213         /* quiet warning */
214         (void)endian_switch_uint32;
215
216         return false;
217 }
218
219 static bool icon_decode(FILE *f_src,
220                         struct IconHead *r_head, unsigned int **r_pixels)
221 {
222         unsigned int *pixels;
223         unsigned int pixels_size;
224
225         if (!icon_decode_head(f_src, r_head)) {
226                 printf("%s: failed to read header\n", __func__);
227                 return false;
228         }
229
230         pixels_size = sizeof(char[4]) * r_head->icon_w * r_head->icon_h;
231         pixels = malloc(pixels_size);
232         if (pixels == NULL) {
233                 printf("%s: failed to allocate pixels\n", __func__);
234                 return false;
235         }
236
237         if (fread(pixels, 1, pixels_size, f_src) != pixels_size) {
238                 printf("%s: failed to read pixels\n", __func__);
239                 free(pixels);
240                 return false;
241         }
242
243         *r_pixels = pixels;
244         return true;
245 }
246
247 static bool icon_read(const char *file_src,
248                       struct IconHead *r_head, unsigned int **r_pixels)
249 {
250         FILE *f_src;
251         bool success;
252
253         f_src = fopen(file_src, "rb");
254         if (f_src == NULL) {
255                 printf("%s: failed to open '%s'\n", __func__, file_src);
256                 return false;
257         }
258
259         success = icon_decode(f_src, r_head, r_pixels);
260
261         fclose(f_src);
262         return success;
263 }
264
265 static bool icon_merge(const char *file_src,
266                        unsigned int **r_pixels_canvas,
267                        unsigned int *r_canvas_w, unsigned int *r_canvas_h)
268 {
269         struct IconHead head;
270         unsigned int *pixels;
271
272         unsigned int x, y;
273
274         /* canvas */
275         unsigned int *pixels_canvas;
276         unsigned int canvas_w, canvas_h;
277
278         if (!icon_read(file_src, &head, &pixels)) {
279                 return false;
280         }
281
282         if (*r_canvas_w == 0) {
283                 /* init once */
284                 *r_canvas_w = head.canvas_w;
285                 *r_canvas_h = head.canvas_h;
286                 *r_pixels_canvas = calloc(1, (head.canvas_w * head.canvas_h) * sizeof(unsigned char[4]));
287         }
288
289         canvas_w = *r_canvas_w;
290         canvas_h = *r_canvas_h;
291         pixels_canvas = *r_pixels_canvas;
292
293         assert(head.canvas_w == canvas_w);
294         assert(head.canvas_h == canvas_h);
295
296         for (x = 0; x < head.icon_w; x++) {
297                 for (y = 0; y < head.icon_h; y++) {
298                         unsigned int pixel;
299                         unsigned int dst_x, dst_y;
300                         unsigned int pixel_xy_dst;
301
302
303                         /* get pixel */
304                         pixel = pixels[(y * head.icon_w) + x];
305
306                         /* set pixel */
307                         dst_x = head.orig_x + x;
308                         dst_y = head.orig_y + y;
309                         pixel_xy_dst = (dst_y * canvas_w) + dst_x;
310                         assert(pixel_xy_dst < (canvas_w * canvas_h));
311                         pixels_canvas[pixel_xy_dst] = pixel;
312                 }
313         }
314
315         free(pixels);
316
317         return true;
318 }
319
320 static bool icondir_to_png(const char *path_src, const char *file_dst)
321 {
322         /* Takes a path full of 'dat' files and writes out */
323         DIR *dir;
324         const struct dirent *fname;
325         char filepath[1024];
326         char *filename;
327         int path_str_len;
328         int found = 0, fail = 0;
329
330         unsigned int *pixels_canvas = NULL;
331         unsigned int canvas_w = 0, canvas_h = 0;
332
333         errno = 0;
334         dir = opendir(path_src);
335         if (dir == NULL) {
336                 printf("%s: failed to dir '%s', (%s)\n", __func__, path_src, errno ? strerror(errno) : "unknown");
337                 return false;
338         }
339
340         strcpy(filepath, path_src);
341         path_str_len = path_ensure_slash(filepath);
342         filename = &filepath[path_str_len];
343
344
345         while ((fname = readdir(dir)) != NULL) {
346                 if (path_test_extension(fname->d_name, ".dat")) {
347
348                         strcpy(filename, fname->d_name);
349
350                         if (icon_merge(filepath, &pixels_canvas, &canvas_w, &canvas_h)) {
351                                 found++;
352                         }
353                         else {
354                                 fail++;
355                         }
356                 }
357         }
358
359         closedir(dir);
360
361         if (found == 0) {
362                 printf("%s: dir '%s' has no icons\n", __func__, path_src);
363         }
364
365         if (fail != 0) {
366                 printf("%s: dir '%s' failed %d icons\n", __func__, path_src, fail);
367         }
368
369         /* write pixels  */
370         write_png(file_dst, pixels_canvas, canvas_w, canvas_h);
371
372         free(pixels_canvas);
373
374         return true;
375 }
376
377
378 /* -------------------------------------------------------------------- */
379 /* Main and parse args */
380
381 int main(int argc, char **argv)
382 {
383         const char *path_src;
384         const char *file_dst;
385         
386
387         if (argc < 3) {
388                 printf("Usage: datatoc_icon <dir_icons> <data_icon_to.png>\n");
389                 exit(1);
390         }
391
392         path_src = argv[1];
393         file_dst = argv[2];
394
395         return (icondir_to_png(path_src, file_dst) == true) ? 0 : 1;
396 }