ico-load.c 26.7 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2
3
4
5
6
 * Copyright (C) 1995-1997 Spencer Kimball and Peter Mattis
 *
 * GIMP Plug-in for Windows Icon files.
 * Copyright (C) 2002 Christian Kreibich <christian@whoop.org>.
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
11
12
13
14
15
16
17
 * (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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
20
21
22
23
24
 */

#include "config.h"

#include <errno.h>
#include <string.h>
25
26

#include <glib/gstdio.h>
27
28

#include <libgimp/gimp.h>
Sven Neumann's avatar
Sven Neumann committed
29
#include <libgimp/gimpui.h>
30

31
32
#include <png.h>

33
34
/* #define ICO_DBG */

Michael Natterer's avatar
Michael Natterer committed
35
36
#include "ico.h"
#include "ico-load.h"
37
38
39
40
41
42
43
44
45

#include "libgimp/stdplugins-intl.h"


#define A_VAL(p) ((guchar *)(p))[3]
#define R_VAL(p) ((guchar *)(p))[2]
#define G_VAL(p) ((guchar *)(p))[1]
#define B_VAL(p) ((guchar *)(p))[0]

46
47
48
49
#define A_VAL_GIMP(p) ((guchar *)(p))[3]
#define R_VAL_GIMP(p) ((guchar *)(p))[0]
#define G_VAL_GIMP(p) ((guchar *)(p))[1]
#define B_VAL_GIMP(p) ((guchar *)(p))[2]
50
51


52
53
54
55
56
57
58
59
60
static gint       ico_read_int8  (FILE        *fp,
                                  guint8      *data,
                                  gint         count);
static gint       ico_read_int16 (FILE        *fp,
                                  guint16     *data,
                                  gint         count);
static gint       ico_read_int32 (FILE        *fp,
                                  guint32     *data,
                                  gint         count);
61
62

static gint
63
64
65
ico_read_int32 (FILE    *fp,
                guint32 *data,
                gint     count)
66
67
68
69
70
71
{
  gint i, total;

  total = count;
  if (count > 0)
    {
72
      ico_read_int8 (fp, (guint8 *) data, count * 4);
73
      for (i = 0; i < count; i++)
74
        data[i] = GUINT32_FROM_LE (data[i]);
75
76
77
78
79
80
81
    }

  return total * 4;
}


static gint
82
83
84
ico_read_int16 (FILE    *fp,
                guint16 *data,
                gint     count)
85
86
87
88
89
90
{
  gint i, total;

  total = count;
  if (count > 0)
    {
91
      ico_read_int8 (fp, (guint8 *) data, count * 2);
92
      for (i = 0; i < count; i++)
93
        data[i] = GUINT16_FROM_LE (data[i]);
94
95
96
97
98
99
100
101
    }

  return total * 2;
}


static gint
ico_read_int8 (FILE   *fp,
102
103
               guint8 *data,
               gint    count)
104
105
106
107
108
109
110
111
112
113
{
  gint total;
  gint bytes;

  total = count;
  while (count > 0)
    {
      bytes = fread ((gchar *) data, sizeof (gchar), count, fp);
      if (bytes <= 0) /* something bad happened */
        break;
114

115
116
117
118
119
120
121
122
      count -= bytes;
      data += bytes;
    }

  return total;
}


123
static IcoFileHeader
Sven Neumann's avatar
Sven Neumann committed
124
ico_read_init (FILE *fp)
125
{
Sven Neumann's avatar
Sven Neumann committed
126
  IcoFileHeader header;
127

Sven Neumann's avatar
Sven Neumann committed
128
  /* read and check file header */
129
130
131
132
  if (! ico_read_int16 (fp, &header.reserved, 1)      ||
      ! ico_read_int16 (fp, &header.resource_type, 1) ||
      ! ico_read_int16 (fp, &header.icon_count, 1)    ||
      header.reserved != 0 ||
133
      (header.resource_type != 1 && header.resource_type != 2))
134
    {
135
136
      header.icon_count = 0;
      return header;
137
    }
138

139
  return header;
140
141
142
}


143
static gboolean
Sven Neumann's avatar
Sven Neumann committed
144
ico_read_size (FILE        *fp,
Alx Sa's avatar
Alx Sa committed
145
               gint32       file_offset,
Sven Neumann's avatar
Sven Neumann committed
146
               IcoLoadInfo *info)
147
{
148
149
150
151
152
  png_structp png_ptr;
  png_infop   info_ptr;
  png_uint_32 w, h;
  gint32      bpp;
  gint32      color_type;
Sven Neumann's avatar
Sven Neumann committed
153
  guint32     magic;
154

Alx Sa's avatar
Alx Sa committed
155
  if (fseek (fp, info->offset + file_offset, SEEK_SET) < 0)
Sven Neumann's avatar
Sven Neumann committed
156
    return FALSE;
157

Sven Neumann's avatar
Sven Neumann committed
158
  ico_read_int32 (fp, &magic, 1);
159

160
161
162
163
  if (magic == ICO_PNG_MAGIC)
    {
      png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL,
                                        NULL);
164
      if (! png_ptr)
165
        return FALSE;
166

167
      info_ptr = png_create_info_struct (png_ptr);
168
      if (! info_ptr)
169
170
171
172
        {
          png_destroy_read_struct (&png_ptr, NULL, NULL);
          return FALSE;
        }
173

174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
      if (setjmp (png_jmpbuf (png_ptr)))
        {
          png_destroy_read_struct (&png_ptr, NULL, NULL);
          return FALSE;
        }
      png_init_io (png_ptr, fp);
      png_set_sig_bytes (png_ptr, 4);
      png_read_info (png_ptr, info_ptr);
      png_get_IHDR (png_ptr, info_ptr, &w, &h, &bpp, &color_type,
                    NULL, NULL, NULL);
      png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
      info->width = w;
      info->height = h;
      D(("ico_read_size: PNG: %ix%i\n", info->width, info->height));
      return TRUE;
    }
  else if (magic == 40)
Sven Neumann's avatar
Sven Neumann committed
191
    {
192
193
      if (ico_read_int32 (fp, &info->width, 1) &&
          ico_read_int32 (fp, &info->height, 1))
Sven Neumann's avatar
Sven Neumann committed
194
195
196
197
198
199
200
201
202
203
204
205
206
        {
          info->height /= 2;
          D(("ico_read_size: ICO: %ix%i\n", info->width, info->height));
          return TRUE;
        }
      else
        {
          info->width = 0;
          info->height = 0;
          return FALSE;
        }
    }
  return FALSE;
207
208
}

Sven Neumann's avatar
Sven Neumann committed
209
static IcoLoadInfo*
210
211
ico_read_info (FILE    *fp,
               gint     icon_count,
Alx Sa's avatar
Alx Sa committed
212
               gint32   file_offset,
213
               GError **error)
214
{
Sven Neumann's avatar
Sven Neumann committed
215
216
217
  gint            i;
  IcoFileEntry   *entries;
  IcoLoadInfo    *info;
218

Sven Neumann's avatar
Sven Neumann committed
219
220
  /* read icon entries */
  entries = g_new (IcoFileEntry, icon_count);
221
  if (fread (entries, sizeof (IcoFileEntry), icon_count, fp) <= 0)
Sven Neumann's avatar
Sven Neumann committed
222
    {
223
224
      g_set_error (error, G_FILE_ERROR, 0,
                   _("Could not read '%lu' bytes"),
Jehan's avatar
Jehan committed
225
                   (long unsigned int) sizeof (IcoFileEntry));
Sven Neumann's avatar
Sven Neumann committed
226
227
228
      g_free (entries);
      return NULL;
    }
229

Sven Neumann's avatar
Sven Neumann committed
230
231
  info = g_new (IcoLoadInfo, icon_count);
  for (i = 0; i < icon_count; i++)
232
    {
233
      info[i].width  = entries[i].width;
Sven Neumann's avatar
Sven Neumann committed
234
      info[i].height = entries[i].height;
235
      info[i].planes = entries[i].planes;
236
237
      info[i].bpp    = GUINT16_FROM_LE (entries[i].bpp);
      info[i].size   = GUINT32_FROM_LE (entries[i].size);
Sven Neumann's avatar
Sven Neumann committed
238
      info[i].offset = GUINT32_FROM_LE (entries[i].offset);
239

Sven Neumann's avatar
Sven Neumann committed
240
241
      if (info[i].width == 0 || info[i].height == 0)
        {
Alx Sa's avatar
Alx Sa committed
242
          ico_read_size (fp, file_offset, info + i);
Sven Neumann's avatar
Sven Neumann committed
243
        }
244

Sven Neumann's avatar
Sven Neumann committed
245
246
247
      D(("ico_read_info: %ix%i (%i bits, size: %i, offset: %i)\n",
         info[i].width, info[i].height, info[i].bpp,
         info[i].size, info[i].offset));
248
249
250
251
252
253
254
255
256

      if (info[i].width == 0 || info[i].height == 0)
        {
          g_set_error (error, G_FILE_ERROR, 0,
                       _("Icon #%d has zero width or height"), i);
          g_free (info);
          g_free (entries);
          return NULL;
        }
257
258
    }

Sven Neumann's avatar
Sven Neumann committed
259
  g_free (entries);
260

Sven Neumann's avatar
Sven Neumann committed
261
  return info;
262
263
}

264
265
266
static gboolean
ico_read_png (FILE    *fp,
              guint32  header,
267
              guchar  *buf,
268
269
270
271
272
273
274
275
276
277
278
279
280
281
              gint     maxsize,
              gint    *width,
              gint    *height)
{
  png_structp   png_ptr;
  png_infop     info;
  png_uint_32   w;
  png_uint_32   h;
  gint32        bit_depth;
  gint32        color_type;
  guint32     **rows;
  gint          i;

  png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
282
  if (! png_ptr)
283
284
    return FALSE;
  info = png_create_info_struct (png_ptr);
285
  if (! info)
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
    {
      png_destroy_read_struct (&png_ptr, NULL, NULL);
      return FALSE;
    }

  if (setjmp (png_jmpbuf (png_ptr)))
    {
      png_destroy_read_struct (&png_ptr, &info, NULL);
      return FALSE;
    }

  png_init_io (png_ptr, fp);
  png_set_sig_bytes (png_ptr, 4);
  png_read_info (png_ptr, info);
  png_get_IHDR (png_ptr, info, &w, &h, &bit_depth, &color_type,
                NULL, NULL, NULL);
  if (w*h*4 > maxsize)
    {
      png_destroy_read_struct (&png_ptr, &info, NULL);
      return FALSE;
    }
  D(("ico_read_png: %ix%i, %i bits, %i type\n", (gint)w, (gint)h,
     bit_depth, color_type));
  switch (color_type)
    {
    case PNG_COLOR_TYPE_GRAY:
312
      png_set_expand_gray_1_2_4_to_8 (png_ptr);
313
      if (bit_depth == 16)
314
315
316
317
318
        png_set_strip_16 (png_ptr);
      png_set_gray_to_rgb (png_ptr);
      png_set_add_alpha (png_ptr, 0xff, PNG_FILLER_AFTER);
      break;
    case PNG_COLOR_TYPE_GRAY_ALPHA:
319
      png_set_expand_gray_1_2_4_to_8 (png_ptr);
320
      if (bit_depth == 16)
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
        png_set_strip_16 (png_ptr);
      png_set_gray_to_rgb (png_ptr);
      break;
    case PNG_COLOR_TYPE_PALETTE:
      png_set_palette_to_rgb (png_ptr);
      if (png_get_valid (png_ptr, info, PNG_INFO_tRNS))
        png_set_tRNS_to_alpha (png_ptr);
      else
        png_set_add_alpha (png_ptr, 0xff, PNG_FILLER_AFTER);
      break;
    case PNG_COLOR_TYPE_RGB:
      if (bit_depth == 16)
        png_set_strip_16 (png_ptr);
      png_set_add_alpha (png_ptr, 0xff, PNG_FILLER_AFTER);
      break;
    case PNG_COLOR_TYPE_RGB_ALPHA:
      if (bit_depth == 16)
        png_set_strip_16 (png_ptr);
      break;
    }

  *width = w;
  *height = h;
  rows = g_new (guint32*, h);
345
  rows[0] = (guint32*) buf;
346
347
348
349
350
351
352
353
  for (i = 1; i < h; i++)
    rows[i] = rows[i-1] + w;
  png_read_image (png_ptr, (png_bytepp) rows);
  png_destroy_read_struct (&png_ptr, &info, NULL);
  g_free (rows);
  return TRUE;
}

354
gint
355
356
357
ico_get_bit_from_data (const guint8 *data,
                       gint          line_width,
                       gint          bit)
358
359
360
361
362
363
364
365
366
367
368
{
  gint line;
  gint width32;
  gint offset;
  gint result;

  /* width per line in multiples of 32 bits */
  width32 = (line_width % 32 == 0 ? line_width/32 : line_width/32 + 1);
  line = bit / line_width;
  offset = bit % line_width;

369
  result = (data[line * width32 * 4 + offset/8] & (1 << (7 - (offset % 8))));
370
371
372
373
374
375

  return (result ? 1 : 0);
}


gint
376
377
378
ico_get_nibble_from_data (const guint8 *data,
                          gint          line_width,
                          gint          nibble)
379
380
381
382
383
384
385
386
387
388
389
{
  gint line;
  gint width32;
  gint offset;
  gint result;

  /* width per line in multiples of 32 bits */
  width32 = (line_width % 8 == 0 ? line_width/8 : line_width/8 + 1);
  line = nibble / line_width;
  offset = nibble % line_width;

Sven Neumann's avatar
Sven Neumann committed
390
  result =
391
    (data[line * width32 * 4 + offset/2] & (0x0F << (4 * (1 - offset % 2))));
392

393
  if (offset % 2 == 0)
394
395
396
397
398
399
    result = result >> 4;

  return result;
}

gint
Sven Neumann's avatar
Sven Neumann committed
400
401
402
ico_get_byte_from_data (const guint8 *data,
                        gint          line_width,
                        gint          byte)
403
404
405
406
407
408
{
  gint line;
  gint width32;
  gint offset;

  /* width per line in multiples of 32 bits */
Sven Neumann's avatar
Sven Neumann committed
409
  width32 = (line_width % 4 == 0 ? line_width / 4 : line_width / 4 + 1);
410
411
412
413
414
415
  line = byte / line_width;
  offset = byte % line_width;

  return data[line * width32 * 4 + offset];
}

Sven Neumann's avatar
Sven Neumann committed
416
417
418
static gboolean
ico_read_icon (FILE    *fp,
               guint32  header_size,
419
               guchar  *buf,
Sven Neumann's avatar
Sven Neumann committed
420
421
422
               gint     maxsize,
               gint    *width,
               gint    *height)
423
{
Sven Neumann's avatar
Sven Neumann committed
424
425
426
427
428
429
  IcoFileDataHeader   data;
  gint                length;
  gint                x, y, w, h;
  guchar             *xor_map, *and_map;
  guint32            *palette;
  guint32            *dest_vec;
430
431
  guchar             *row;
  gint                rowstride;
Sven Neumann's avatar
Sven Neumann committed
432
433
434
435
436
437
438
439
440
441
442
443
444
445

  palette = NULL;

  data.header_size = header_size;
  ico_read_int32 (fp, &data.width, 1);
  ico_read_int32 (fp, &data.height, 1);
  ico_read_int16 (fp, &data.planes, 1);
  ico_read_int16 (fp, &data.bpp, 1);
  ico_read_int32 (fp, &data.compression, 1);
  ico_read_int32 (fp, &data.image_size, 1);
  ico_read_int32 (fp, &data.x_res, 1);
  ico_read_int32 (fp, &data.y_res, 1);
  ico_read_int32 (fp, &data.used_clrs, 1);
  ico_read_int32 (fp, &data.important_clrs, 1);
446

Sven Neumann's avatar
Sven Neumann committed
447
448
449
450
451
  D(("  header size %i, "
     "w %i, h %i, planes %i, size %i, bpp %i, used %i, imp %i.\n",
     data.header_size, data.width, data.height,
     data.planes, data.image_size, data.bpp,
     data.used_clrs, data.important_clrs));
452

453
454
  if (data.planes      != 1 ||
      data.compression != 0)
Sven Neumann's avatar
Sven Neumann committed
455
456
457
458
    {
      D(("skipping image: invalid header\n"));
      return FALSE;
    }
459

460
461
462
463
464
  if (data.bpp != 1  &&
      data.bpp != 4  &&
      data.bpp != 8  &&
      data.bpp != 24 &&
      data.bpp != 32)
Sven Neumann's avatar
Sven Neumann committed
465
466
467
468
    {
      D(("skipping image: invalid depth: %i\n", data.bpp));
      return FALSE;
    }
469

Sven Neumann's avatar
Sven Neumann committed
470
471
472
473
474
  if (data.width * data.height * 2 > maxsize)
    {
      D(("skipping image: too large\n"));
      return FALSE;
    }
475

Sven Neumann's avatar
Sven Neumann committed
476
477
  w = data.width;
  h = data.height / 2;
478

Sven Neumann's avatar
Sven Neumann committed
479
480
481
482
  if (data.bpp <= 8)
    {
      if (data.used_clrs == 0)
        data.used_clrs = (1 << data.bpp);
483

Sven Neumann's avatar
Sven Neumann committed
484
485
      D(("  allocating a %i-slot palette for %i bpp.\n",
         data.used_clrs, data.bpp));
486

Sven Neumann's avatar
Sven Neumann committed
487
488
489
490
491
492
493
494
495
496
497
498
499
      palette = g_new0 (guint32, data.used_clrs);
      ico_read_int8 (fp, (guint8 *) palette, data.used_clrs * 4);
    }

  xor_map = ico_alloc_map (w, h, data.bpp, &length);
  ico_read_int8 (fp, xor_map, length);
  D(("  length of xor_map: %i\n", length));

  /* Read in and_map. It's padded out to 32 bits per line: */
  and_map = ico_alloc_map (w, h, 1, &length);
  ico_read_int8 (fp, and_map, length);
  D(("  length of and_map: %i\n", length));

500
  dest_vec = (guint32 *) buf;
Sven Neumann's avatar
Sven Neumann committed
501
  switch (data.bpp)
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
    {
    case 1:
      for (y = 0; y < h; y++)
        for (x = 0; x < w; x++)
          {
            guint32 color = palette[ico_get_bit_from_data (xor_map,
                                                           w, y * w + x)];
            guint32 *dest = dest_vec + (h - 1 - y) * w + x;

            R_VAL_GIMP (dest) = R_VAL (&color);
            G_VAL_GIMP (dest) = G_VAL (&color);
            B_VAL_GIMP (dest) = B_VAL (&color);

            if (ico_get_bit_from_data (and_map, w, y * w + x))
              A_VAL_GIMP (dest) = 0;
            else
              A_VAL_GIMP (dest) = 255;
          }
      break;

    case 4:
      for (y = 0; y < h; y++)
        for (x = 0; x < w; x++)
          {
            guint32 color = palette[ico_get_nibble_from_data (xor_map,
                                                              w, y * w + x)];
            guint32 *dest = dest_vec + (h - 1 - y) * w + x;

            R_VAL_GIMP (dest) = R_VAL (&color);
            G_VAL_GIMP (dest) = G_VAL (&color);
            B_VAL_GIMP (dest) = B_VAL (&color);

            if (ico_get_bit_from_data (and_map, w, y * w + x))
              A_VAL_GIMP (dest) = 0;
            else
              A_VAL_GIMP (dest) = 255;
          }
      break;

    case 8:
      for (y = 0; y < h; y++)
        for (x = 0; x < w; x++)
          {
            guint32 color = palette[ico_get_byte_from_data (xor_map,
                                                            w, y * w + x)];
            guint32 *dest = dest_vec + (h - 1 - y) * w + x;

            R_VAL_GIMP (dest) = R_VAL (&color);
            G_VAL_GIMP (dest) = G_VAL (&color);
            B_VAL_GIMP (dest) = B_VAL (&color);

            if (ico_get_bit_from_data (and_map, w, y * w + x))
              A_VAL_GIMP (dest) = 0;
            else
              A_VAL_GIMP (dest) = 255;
          }
      break;

    default:
      {
562
563
564
        gint bytespp = data.bpp / 8;

        rowstride = ico_rowstride (w, data.bpp);
565
566

        for (y = 0; y < h; y++)
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
          {
            row = xor_map + rowstride * y;

            for (x = 0; x < w; x++)
              {
                guint32 *dest = dest_vec + (h - 1 - y) * w + x;

                B_VAL_GIMP (dest) = row[0];
                G_VAL_GIMP (dest) = row[1];
                R_VAL_GIMP (dest) = row[2];

                if (data.bpp < 32)
                  {
                    if (ico_get_bit_from_data (and_map, w, y * w + x))
                      A_VAL_GIMP (dest) = 0;
                    else
                      A_VAL_GIMP (dest) = 255;
                  }
                else
                  {
                    A_VAL_GIMP (dest) = row[3];
                  }

                row += bytespp;
              }
          }
593
594
      }
    }
Sven Neumann's avatar
Sven Neumann committed
595
596
597
598
599
600
601
602
603
  if (palette)
    g_free (palette);
  g_free (xor_map);
  g_free (and_map);
  *width = w;
  *height = h;
  return TRUE;
}

604
static GimpLayer *
Sven Neumann's avatar
Sven Neumann committed
605
ico_load_layer (FILE        *fp,
606
                GimpImage   *image,
Sven Neumann's avatar
Sven Neumann committed
607
                gint32       icon_num,
608
                guchar      *buf,
Sven Neumann's avatar
Sven Neumann committed
609
                gint         maxsize,
Alx Sa's avatar
Alx Sa committed
610
                gint32       file_offset,
Sven Neumann's avatar
Sven Neumann committed
611
612
                IcoLoadInfo *info)
{
613
  gint        width, height;
614
  GimpLayer  *layer;
615
616
617
  guint32     first_bytes;
  GeglBuffer *buffer;
  gchar       name[ICO_MAXBUF];
Sven Neumann's avatar
Sven Neumann committed
618

Alx Sa's avatar
Alx Sa committed
619
  if (fseek (fp, info->offset + file_offset, SEEK_SET) < 0 ||
620
      ! ico_read_int32 (fp, &first_bytes, 1))
621
    return NULL;
622

623
624
  if (first_bytes == ICO_PNG_MAGIC)
    {
625
      if (!ico_read_png (fp, first_bytes, buf, maxsize, &width, &height))
626
        return NULL;
627
628
    }
  else if (first_bytes == 40)
Sven Neumann's avatar
Sven Neumann committed
629
    {
630
      if (!ico_read_icon (fp, first_bytes, buf, maxsize, &width, &height))
631
        return NULL;
Sven Neumann's avatar
Sven Neumann committed
632
633
634
    }
  else
    {
635
      return NULL;
Sven Neumann's avatar
Sven Neumann committed
636
    }
637

Sven Neumann's avatar
Sven Neumann committed
638
  /* read successfully. add to image */
639
640
  g_snprintf (name, sizeof (name), _("Icon #%i"), icon_num+1);
  layer = gimp_layer_new (image, name, width, height,
641
                          GIMP_RGBA_IMAGE,
642
643
                          100,
                          gimp_image_get_default_new_layer_mode (image));
644
  gimp_image_insert_layer (image, layer, NULL, icon_num);
645

646
  buffer = gimp_drawable_get_buffer (GIMP_DRAWABLE (layer));
647
648
649
650
651

  gegl_buffer_set (buffer, GEGL_RECTANGLE (0, 0, width, height), 0,
                   NULL, buf, GEGL_AUTO_ROWSTRIDE);

  g_object_unref (buffer);
652
653
654

  return layer;
}
655

Sven Neumann's avatar
Sven Neumann committed
656

657
GimpImage *
658
ico_load_image (GFile        *file,
Alx Sa's avatar
Alx Sa committed
659
                gint32       *file_offset,
660
                GError      **error)
661
{
662
663
664
665
666
667
668
669
670
671
  FILE          *fp;
  IcoFileHeader  header;
  IcoLoadInfo   *info;
  gint           max_width, max_height;
  gint           i;
  GimpImage     *image;
  guchar        *buf;
  guint          icon_count;
  gint           maxsize;
  gchar         *str;
672

Alx Sa's avatar
Alx Sa committed
673
674
675
  if (! file_offset)
    gimp_progress_init_printf (_("Opening '%s'"),
                               gimp_file_get_utf8_name (file));
676

677
  fp = g_fopen (g_file_peek_path (file), "rb");
678

679
  if (! fp)
680
    {
681
682
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
683
                   gimp_file_get_utf8_name (file), g_strerror (errno));
684
      return NULL;
685
    }
686

Alx Sa's avatar
Alx Sa committed
687
688
689
  if (file_offset)
    fseek (fp, *file_offset, SEEK_SET);

690
691
  header = ico_read_init (fp);
  icon_count = header.icon_count;
Sven Neumann's avatar
Sven Neumann committed
692
693
694
  if (!icon_count)
    {
      fclose (fp);
695
      return NULL;
Sven Neumann's avatar
Sven Neumann committed
696
    }
697

Alx Sa's avatar
Alx Sa committed
698
  info = ico_read_info (fp, icon_count, file_offset ? *file_offset : 0, error);
699
  if (! info)
Sven Neumann's avatar
Sven Neumann committed
700
701
    {
      fclose (fp);
702
      return NULL;
Sven Neumann's avatar
Sven Neumann committed
703
    }
704

Sven Neumann's avatar
Sven Neumann committed
705
706
707
708
709
  /* find width and height of image */
  max_width = 0;
  max_height = 0;
  for (i = 0; i < icon_count; i++)
    {
710
      if (info[i].width > max_width)
Sven Neumann's avatar
Sven Neumann committed
711
        max_width = info[i].width;
712
      if (info[i].height > max_height)
Sven Neumann's avatar
Sven Neumann committed
713
714
        max_height = info[i].height;
    }
715
  if (max_width <= 0 || max_height <= 0)
Sven Neumann's avatar
Sven Neumann committed
716
717
718
    {
      g_free (info);
      fclose (fp);
719
      return NULL;
Sven Neumann's avatar
Sven Neumann committed
720
721
    }
  D(("image size: %ix%i\n", max_width, max_height));
722

Sven Neumann's avatar
Sven Neumann committed
723
  image = gimp_image_new (max_width, max_height, GIMP_RGB);
Alx Sa's avatar
Alx Sa committed
724
725
  if (! file_offset)
    gimp_image_set_file (image, file);
726

Sven Neumann's avatar
Sven Neumann committed
727
  maxsize = max_width * max_height * 4;
728
  buf = g_new (guchar, max_width * max_height * 4);
Sven Neumann's avatar
Sven Neumann committed
729
730
  for (i = 0; i < icon_count; i++)
    {
731
732
      GimpLayer *layer;

Alx Sa's avatar
Alx Sa committed
733
      layer = ico_load_layer (fp, image, i, buf, maxsize, file_offset ? *file_offset : 0, info+i);
734
735
736
737
738
739
740
741
742
743
744
745
746
747

      /* Save CUR hot spot information */
      if (header.resource_type == 2)
        {
          GimpParasite *parasite;

          str = g_strdup_printf ("%d %d", info[i].planes, info[i].bpp);
          parasite = gimp_parasite_new ("cur-hot-spot",
                                        GIMP_PARASITE_PERSISTENT,
                                        strlen (str) + 1, (gpointer) str);
          g_free (str);
          gimp_item_attach_parasite (GIMP_ITEM (layer), parasite);
          gimp_parasite_free (parasite);
        }
Sven Neumann's avatar
Sven Neumann committed
748
    }
Alx Sa's avatar
Alx Sa committed
749
750
751
752

  if (file_offset)
    *file_offset = ftell (fp);

753
  g_free (buf);
Sven Neumann's avatar
Sven Neumann committed
754
755
  g_free (info);
  fclose (fp);
756

Alx Sa's avatar
Alx Sa committed
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
  /* Don't update progress here if .ani file */
  if (! file_offset)
    gimp_progress_update (1.0);

  return image;
}

/* Ported from James Huang's ani.c code, under the GPL license, version 3
 * or any later version of the license */
GimpImage *
ani_load_image (GFile   *file,
                gboolean load_thumb,
                gint    *width,
                gint    *height,
                GError **error)
{
  FILE         *fp;
  GimpImage    *image = NULL;
  GimpParasite *parasite;
  gchar         id[4];
  guint32       size;
  gint32        file_offset;
  gint          frame = 1;
  AniFileHeader header;
  gchar         inam[G_MAXSHORT] = {0};
  gchar         iart[G_MAXSHORT] = {0};
  gchar        *str;

  gimp_progress_init_printf (_("Opening '%s'"),
                             gimp_file_get_utf8_name (file));

  fp = g_fopen (g_file_peek_path (file), "rb");

  if (! fp)
    {
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
                   gimp_file_get_utf8_name (file), g_strerror (errno));
      return NULL;
    }

  while (fread (id, 1, 4, fp) == 4)
    {
      if (memcmp (id, "RIFF", 4) == 0 )
        {
          fread (&size, sizeof (size), 1, fp);
        }
      else if (memcmp (id, "anih", 4) == 0)
        {
          fread (&size, sizeof (size), 1, fp);
          fread (&header, sizeof (header), 1, fp);
        }
      else if (memcmp (id, "rate", 4) == 0)
        {
          fread (&size, sizeof (size), 1, fp);
          fseek (fp, size, SEEK_CUR);
        }
      else if (memcmp (id, "seq ", 4) == 0)
        {
          fread (&size, sizeof (size), 1, fp);
          fseek (fp, size, SEEK_CUR);
        }
      else if (memcmp (id, "LIST", 4) == 0)
        {
          fread (&size, sizeof (size), 1, fp);
        }
      else if (memcmp (id, "INAM", 4) == 0)
        {
          fread (&size, sizeof (size), 1, fp);
          fread (&inam, sizeof (char), size, fp);
          inam[size] = '\0';
        }
      else if (memcmp (id, "IART", 4) == 0)
        {
          fread (&size, sizeof (size), 1, fp);
          fread (&iart, sizeof (char), size, fp);
          iart[size] = '\0';
        }
      else if (memcmp (id, "icon", 4) == 0)
        {
          fread (&size, sizeof (size), 1, fp);
          file_offset = ftell (fp);
          if (load_thumb)
            {
              image = ico_load_thumbnail_image (file, width, height, file_offset, error);
              break;
            }
           else
             {
               if (! image)
                 {
                   image = ico_load_image (file, &file_offset, error);
                 }
               else
                 {
                   GimpImage    *temp_image = NULL;
                   GimpLayer   **layers;
                   GimpLayer    *new_layer;
                   gint          nlayers;

                   temp_image = ico_load_image (file, &file_offset, error);
                   layers = gimp_image_get_layers (temp_image, &nlayers);
                   if (layers)
                     {
                       for (gint i = 0; i < nlayers; i++)
                         {
                           new_layer = gimp_layer_new_from_drawable (GIMP_DRAWABLE (layers[i]),
                                                                     image);
                           gimp_image_insert_layer (image, new_layer, NULL, frame);
                           frame++;
                         }
                     }
                   gimp_image_delete (temp_image);
                 }

               /* Update position after reading icon data */
               fseek (fp, file_offset, SEEK_SET);
               if (header.frames > 0)
                 gimp_progress_update ((gdouble) frame /
                                       (gdouble) header.frames);
             }
        }
    }
  fclose (fp);

  /* Saving header metadata */
  str = g_strdup_printf ("%d", header.jif_rate);
  parasite = gimp_parasite_new ("ani-header",
                                GIMP_PARASITE_PERSISTENT,
                                strlen (str) + 1, (gpointer) str);
  g_free (str);
  gimp_image_attach_parasite (image, parasite);
  gimp_parasite_free (parasite);

  /* Saving INFO block */
  if (strlen (inam) > 0)
    {
      str = g_strdup_printf ("%s", inam);
      parasite = gimp_parasite_new ("ani-info-inam",
                                    GIMP_PARASITE_PERSISTENT,
                                    strlen (str) + 1, (gpointer) str);
      g_free (str);
      gimp_image_attach_parasite (image, parasite);
      gimp_parasite_free (parasite);
    }

  if (strlen (iart) > 0)
    {
      str = g_strdup_printf ("%s", iart);
      parasite = gimp_parasite_new ("ani-info-iart",
                                    GIMP_PARASITE_PERSISTENT,
                                    strlen (str) + 1, (gpointer) str);
      g_free (str);
      gimp_image_attach_parasite (image, parasite);
      gimp_parasite_free (parasite);
    }

  gimp_image_set_file (image, file);
Sven Neumann's avatar
Sven Neumann committed
915
  gimp_progress_update (1.0);
916

917
  return image;
918
}
919

920
GimpImage *
921
922
923
ico_load_thumbnail_image (GFile   *file,
                          gint    *width,
                          gint    *height,
Alx Sa's avatar
Alx Sa committed
924
                          gint32   file_offset,
925
                          GError **error)
926
{
927
928
929
930
931
932
933
934
935
936
  FILE          *fp;
  IcoLoadInfo   *info;
  IcoFileHeader  header;
  GimpImage     *image;
  gint           w     = 0;
  gint           h     = 0;
  gint           bpp   = 0;
  gint           match = 0;
  gint           i, icon_count;
  guchar        *buf;
937

938
  gimp_progress_init_printf (_("Opening thumbnail for '%s'"),
939
                             gimp_file_get_utf8_name (file));
940

941
  fp = g_fopen (g_file_peek_path (file), "rb");
942

943
  if (! fp)
Sven Neumann's avatar
Sven Neumann committed
944
    {
945
946
      g_set_error (error, G_FILE_ERROR, g_file_error_from_errno (errno),
                   _("Could not open '%s' for reading: %s"),
947
                   gimp_file_get_utf8_name (file), g_strerror (errno));
948
      return NULL;
Sven Neumann's avatar
Sven Neumann committed
949
    }
950

Alx Sa's avatar
Alx Sa committed
951
952
953
  if (file_offset > 0)
    fseek (fp, file_offset, SEEK_SET);

954
955
  header = ico_read_init (fp);
  icon_count = header.icon_count;
956
  if (! icon_count)
Sven Neumann's avatar
Sven Neumann committed
957
958
    {
      fclose (fp);
959
      return NULL;
Sven Neumann's avatar
Sven Neumann committed
960
    }
961
962

  D(("*** %s: Microsoft icon file, containing %i icon(s)\n",
963
     gimp_file_get_utf8_name (file), icon_count));
964

Alx Sa's avatar
Alx Sa committed
965
  info = ico_read_info (fp, icon_count, file_offset, error);
966
  if (! info)
Sven Neumann's avatar
Sven Neumann committed
967
968
    {
      fclose (fp);
969
      return NULL;
Sven Neumann's avatar
Sven Neumann committed
970
    }
971
972

  /* Do a quick scan of the icons in the file to find the best match */
Sven Neumann's avatar
Sven Neumann committed
973
  for (i = 0; i < icon_count; i++)
974
    {
Sven Neumann's avatar
Sven Neumann committed
975
976
      if ((info[i].width  > w && w < *width) ||
          (info[i].height > h && h < *height))
977
        {
Sven Neumann's avatar
Sven Neumann committed
978
979
980
          w = info[i].width;
          h = info[i].height;
          bpp = info[i].bpp;
981

Sven Neumann's avatar
Sven Neumann committed
982
983
          match = i;
        }
984
985
986
      else if (w == info[i].width  &&
               h == info[i].height &&
               info[i].bpp > bpp)
Sven Neumann's avatar
Sven Neumann committed
987
988
989
        {
          /* better quality */
          bpp = info[i].bpp;
990
991
992
993
          match = i;
        }
    }

Sven Neumann's avatar
Sven Neumann committed
994
  if (w <= 0 || h <= 0)
995
    return NULL;
996
997

  image = gimp_image_new (w, h, GIMP_RGB);
998
  buf = g_new (guchar, w*h*4);
Alx Sa's avatar
Alx Sa committed
999
  ico_load_layer (fp, image, match, buf, w*h*4, file_offset, info+match);
1000
  g_free (buf);
For faster browsing, not all history is shown. View entire blame