Merge branch 'blender2.7'
[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(FREEWINDOWS)
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 /* -------------------------------------------------------------------- */
54 /* Utility functions */
55
56 static int path_ensure_slash(char *string)
57 {
58         int len = strlen(string);
59         if (len == 0 || string[len - 1] != SEP) {
60                 string[len] = SEP;
61                 string[len + 1] = '\0';
62                 return len + 1;
63         }
64         return len;
65 }
66
67 static bool path_test_extension(const char *str, const char *ext)
68 {
69         const size_t a = strlen(str);
70         const size_t b = strlen(ext);
71         return !(a == 0 || b == 0 || b >= a) && (strcmp(ext, str + a - b) == 0);
72 }
73
74 static void endian_switch_uint32(unsigned int *val)
75 {
76         unsigned int tval = *val;
77         *val = ((tval >> 24))             |
78                ((tval << 8) & 0x00ff0000) |
79                ((tval >> 8) & 0x0000ff00) |
80                ((tval << 24));
81 }
82
83 /* -------------------------------------------------------------------- */
84 /* Write a PNG from RGBA pixels */
85
86 static bool write_png(const char *name, const unsigned int *pixels,
87                      const int width, const int height)
88 {
89         png_structp png_ptr;
90         png_infop info_ptr;
91         png_bytepp row_pointers = NULL;
92
93         FILE *fp;
94
95         const int bytesperpixel = 4;
96         const int compression = 9;
97         int i;
98
99         fp = fopen(name, "wb");
100         if (fp == NULL) {
101                 printf("%s: Cannot open file for writing '%s'\n", __func__, name);
102                 return false;
103         }
104
105         png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
106                                           NULL, NULL, NULL);
107         if (png_ptr == NULL) {
108                 printf("%s: Cannot png_create_write_struct for file: '%s'\n", __func__, name);
109                 fclose(fp);
110                 return false;
111         }
112
113         info_ptr = png_create_info_struct(png_ptr);
114         if (info_ptr == NULL) {
115                 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
116                 printf("%s: Cannot png_create_info_struct for file: '%s'\n", __func__, name);
117                 fclose(fp);
118                 return false;
119         }
120
121         if (setjmp(png_jmpbuf(png_ptr))) {
122                 png_destroy_write_struct(&png_ptr, &info_ptr);
123                 printf("%s: Cannot setjmp for file: '%s'\n", __func__, name);
124                 fclose(fp);
125                 return false;
126         }
127
128         /* write the file */
129         png_init_io(png_ptr, fp);
130
131         png_set_compression_level(png_ptr, compression);
132
133         /* png image settings */
134         png_set_IHDR(png_ptr,
135                      info_ptr,
136                      width,
137                      height,
138                      8,
139                      PNG_COLOR_TYPE_RGBA,
140                      PNG_INTERLACE_NONE,
141                      PNG_COMPRESSION_TYPE_DEFAULT,
142                      PNG_FILTER_TYPE_DEFAULT);
143
144         /* write the file header information */
145         png_write_info(png_ptr, info_ptr);
146
147 #ifdef __LITTLE_ENDIAN__
148         png_set_swap(png_ptr);
149 #endif
150
151         /* allocate memory for an array of row-pointers */
152         row_pointers = (png_bytepp) malloc(height * sizeof(png_bytep));
153         if (row_pointers == NULL) {
154                 printf("%s: Cannot allocate row-pointers array for file '%s'\n", __func__, name);
155                 png_destroy_write_struct(&png_ptr, &info_ptr);
156                 if (fp) {
157                         fclose(fp);
158                 }
159                 return false;
160         }
161
162         /* set the individual row-pointers to point at the correct offsets */
163         for (i = 0; i < height; i++) {
164                 row_pointers[height - 1 - i] = (png_bytep)
165                                                (((const unsigned char *)pixels) +
166                                                 (i * width) * bytesperpixel * sizeof(unsigned char));
167         }
168
169         /* write out the entire image data in one call */
170         png_write_image(png_ptr, row_pointers);
171
172         /* write the additional chunks to the PNG file (not really needed) */
173         png_write_end(png_ptr, info_ptr);
174
175         /* clean up */
176         free(row_pointers);
177         png_destroy_write_struct(&png_ptr, &info_ptr);
178
179         fflush(fp);
180         fclose(fp);
181
182         return true;
183 }
184
185
186 /* -------------------------------------------------------------------- */
187 /* Merge icon-data from files */
188
189 struct IconHead {
190         unsigned int icon_w, icon_h;
191         unsigned int orig_x, orig_y;
192         unsigned int canvas_w, canvas_h;
193 };
194
195 static bool icon_decode_head(FILE *f_src,
196                             struct IconHead *r_head)
197 {
198         if (fread(r_head, 1, sizeof(*r_head), f_src) == sizeof(*r_head)) {
199 #ifndef __LITTLE_ENDIAN__
200                 endian_switch_uint32(&r_head->icon_w);
201                 endian_switch_uint32(&r_head->icon_h);
202                 endian_switch_uint32(&r_head->orig_x);
203                 endian_switch_uint32(&r_head->orig_y);
204                 endian_switch_uint32(&r_head->canvas_w);
205                 endian_switch_uint32(&r_head->canvas_h);
206 #endif
207                 return true;
208         }
209
210         /* quiet warning */
211         (void)endian_switch_uint32;
212
213         return false;
214 }
215
216 static bool icon_decode(FILE *f_src,
217                         struct IconHead *r_head, unsigned int **r_pixels)
218 {
219         unsigned int *pixels;
220         unsigned int pixels_size;
221
222         if (!icon_decode_head(f_src, r_head)) {
223                 printf("%s: failed to read header\n", __func__);
224                 return false;
225         }
226
227         pixels_size = sizeof(char[4]) * r_head->icon_w * r_head->icon_h;
228         pixels = malloc(pixels_size);
229         if (pixels == NULL) {
230                 printf("%s: failed to allocate pixels\n", __func__);
231                 return false;
232         }
233
234         if (fread(pixels, 1, pixels_size, f_src) != pixels_size) {
235                 printf("%s: failed to read pixels\n", __func__);
236                 free(pixels);
237                 return false;
238         }
239
240         *r_pixels = pixels;
241         return true;
242 }
243
244 static bool icon_read(const char *file_src,
245                       struct IconHead *r_head, unsigned int **r_pixels)
246 {
247         FILE *f_src;
248         bool success;
249
250         f_src = fopen(file_src, "rb");
251         if (f_src == NULL) {
252                 printf("%s: failed to open '%s'\n", __func__, file_src);
253                 return false;
254         }
255
256         success = icon_decode(f_src, r_head, r_pixels);
257
258         fclose(f_src);
259         return success;
260 }
261
262 static bool icon_merge(const char *file_src,
263                        unsigned int **r_pixels_canvas,
264                        unsigned int *r_canvas_w, unsigned int *r_canvas_h)
265 {
266         struct IconHead head;
267         unsigned int *pixels;
268
269         unsigned int x, y;
270
271         /* canvas */
272         unsigned int *pixels_canvas;
273         unsigned int canvas_w, canvas_h;
274
275         if (!icon_read(file_src, &head, &pixels)) {
276                 return false;
277         }
278
279         if (*r_canvas_w == 0) {
280                 /* init once */
281                 *r_canvas_w = head.canvas_w;
282                 *r_canvas_h = head.canvas_h;
283                 *r_pixels_canvas = calloc(1, (head.canvas_w * head.canvas_h) * sizeof(unsigned char[4]));
284         }
285
286         canvas_w = *r_canvas_w;
287         canvas_h = *r_canvas_h;
288         pixels_canvas = *r_pixels_canvas;
289
290         assert(head.canvas_w == canvas_w);
291         assert(head.canvas_h == canvas_h);
292
293         for (x = 0; x < head.icon_w; x++) {
294                 for (y = 0; y < head.icon_h; y++) {
295                         unsigned int pixel;
296                         unsigned int dst_x, dst_y;
297                         unsigned int pixel_xy_dst;
298
299
300                         /* get pixel */
301                         pixel = pixels[(y * head.icon_w) + x];
302
303                         /* set pixel */
304                         dst_x = head.orig_x + x;
305                         dst_y = head.orig_y + y;
306                         pixel_xy_dst = (dst_y * canvas_w) + dst_x;
307                         assert(pixel_xy_dst < (canvas_w * canvas_h));
308                         pixels_canvas[pixel_xy_dst] = pixel;
309                 }
310         }
311
312         free(pixels);
313
314         /* only for bounds check */
315         (void)canvas_h;
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 }