~ubuntu-branches/ubuntu/jaunty/gimp/jaunty-security

« back to all changes in this revision

Viewing changes to plug-ins/common/tiff-save.c

  • Committer: Bazaar Package Importer
  • Author(s): Sebastien Bacher
  • Date: 2008-10-06 13:30:41 UTC
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: james.westby@ubuntu.com-20081006133041-3panbkcanaymfsmp
Tags: upstream-2.6.0
ImportĀ upstreamĀ versionĀ 2.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* tiff saving for GIMP
2
 
 *  -Peter Mattis
3
 
 *
4
 
 * The TIFF loading code has been completely revamped by Nick Lamb
5
 
 * njl195@zepler.org.uk -- 18 May 1998
6
 
 * And it now gains support for tiles (and doubtless a zillion bugs)
7
 
 * njl195@zepler.org.uk -- 12 June 1999
8
 
 * LZW patent fuss continues :(
9
 
 * njl195@zepler.org.uk -- 20 April 2000
10
 
 * The code for this filter is based on "tifftopnm" and "pnmtotiff",
11
 
 *  2 programs that are a part of the netpbm package.
12
 
 * khk@khk.net -- 13 May 2000
13
 
 * Added support for ICCPROFILE tiff tag. If this tag is present in a
14
 
 * TIFF file, then a parasite is created and vice versa.
15
 
 * peter@kirchgessner.net -- 29 Oct 2002
16
 
 * Progress bar only when run interactive
17
 
 * Added support for layer offsets - pablo.dangelo@web.de -- 7 Jan 2004
18
 
 * Honor EXTRASAMPLES tag while loading images with alphachannel
19
 
 * pablo.dangelo@web.de -- 16 Jan 2004
20
 
 */
21
 
 
22
 
/*
23
 
 * tifftopnm.c - converts a Tagged Image File to a portable anymap
24
 
 *
25
 
 * Derived by Jef Poskanzer from tif2ras.c, which is:
26
 
 *
27
 
 * Copyright (c) 1990 by Sun Microsystems, Inc.
28
 
 *
29
 
 * Author: Patrick J. Naughton
30
 
 * naughton@wind.sun.com
31
 
 *
32
 
 * Permission to use, copy, modify, and distribute this software and its
33
 
 * documentation for any purpose and without fee is hereby granted,
34
 
 * provided that the above copyright notice appear in all copies and that
35
 
 * both that copyright notice and this permission notice appear in
36
 
 * supporting documentation.
37
 
 *
38
 
 * This file is provided AS IS with no warranties of any kind.  The author
39
 
 * shall have no liability with respect to the infringement of copyrights,
40
 
 * trade secrets or any patents by this file or any part thereof.  In no
41
 
 * event will the author be liable for any lost revenue or profits or
42
 
 * other special, indirect and consequential damages.
43
 
 */
44
 
 
45
 
#include "config.h"
46
 
 
47
 
#include <errno.h>
48
 
#include <string.h>
49
 
 
50
 
#include <sys/types.h>
51
 
#include <fcntl.h>
52
 
 
53
 
#ifdef HAVE_UNISTD_H
54
 
#include <unistd.h>
55
 
#endif
56
 
 
57
 
#include <glib/gstdio.h>
58
 
#ifdef G_OS_WIN32
59
 
#include <libgimpbase/gimpwin32-io.h>
60
 
#endif
61
 
 
62
 
#ifndef _O_BINARY
63
 
#define _O_BINARY 0
64
 
#endif
65
 
 
66
 
#include <tiffio.h>
67
 
 
68
 
#include <libgimp/gimp.h>
69
 
#include <libgimp/gimpui.h>
70
 
 
71
 
#include "libgimp/stdplugins-intl.h"
72
 
 
73
 
 
74
 
#define SAVE_PROC      "file-tiff-save"
75
 
#define SAVE2_PROC     "file-tiff-save2"
76
 
#define PLUG_IN_BINARY "tiff-save"
77
 
 
78
 
 
79
 
typedef struct
80
 
{
81
 
  gint      compression;
82
 
  gint      fillorder;
83
 
  gboolean  save_transp_pixels;
84
 
} TiffSaveVals;
85
 
 
86
 
typedef struct
87
 
{
88
 
  gint32        ID;
89
 
  GimpDrawable *drawable;
90
 
  GimpPixelRgn  pixel_rgn;
91
 
  guchar       *pixels;
92
 
  guchar       *pixel;
93
 
} channel_data;
94
 
 
95
 
/* Declare some local functions.
96
 
 */
97
 
static void   query     (void);
98
 
static void   run       (const gchar      *name,
99
 
                         gint              nparams,
100
 
                         const GimpParam  *param,
101
 
                         gint             *nreturn_vals,
102
 
                         GimpParam       **return_vals);
103
 
 
104
 
static gboolean  image_is_monochrome (gint32 image);
105
 
 
106
 
static gboolean  save_paths             (TIFF         *tif,
107
 
                                         gint32        image);
108
 
static gboolean  save_image             (const gchar  *filename,
109
 
                                         gint32        image,
110
 
                                         gint32        drawable,
111
 
                                         gint32        orig_image);
112
 
 
113
 
static gboolean  save_dialog            (gboolean      has_alpha,
114
 
                                         gboolean      is_monochrome);
115
 
 
116
 
static void      comment_entry_callback (GtkWidget    *widget,
117
 
                                         gpointer      data);
118
 
 
119
 
static void      byte2bit               (const guchar *byteline,
120
 
                                         gint          width,
121
 
                                         guchar       *bitline,
122
 
                                         gboolean      invert);
123
 
 
124
 
static void      tiff_warning           (const gchar *module,
125
 
                                         const gchar *fmt,
126
 
                                         va_list      ap);
127
 
static void      tiff_error             (const gchar *module,
128
 
                                         const gchar *fmt,
129
 
                                         va_list      ap);
130
 
 
131
 
const GimpPlugInInfo PLUG_IN_INFO =
132
 
{
133
 
  NULL,  /* init_proc  */
134
 
  NULL,  /* quit_proc  */
135
 
  query, /* query_proc */
136
 
  run,   /* run_proc   */
137
 
};
138
 
 
139
 
static TiffSaveVals tsvals =
140
 
{
141
 
  COMPRESSION_NONE,    /*  compression    */
142
 
  TRUE,                /*  alpha handling */
143
 
};
144
 
 
145
 
static gchar       *image_comment = NULL;
146
 
static GimpRunMode  run_mode      = GIMP_RUN_INTERACTIVE;
147
 
 
148
 
 
149
 
MAIN ()
150
 
 
151
 
static void
152
 
query (void)
153
 
{
154
 
#define COMMON_SAVE_ARGS \
155
 
    { GIMP_PDB_INT32,    "run-mode",     "Interactive, non-interactive" },\
156
 
    { GIMP_PDB_IMAGE,    "image",        "Input image" },\
157
 
    { GIMP_PDB_DRAWABLE, "drawable",     "Drawable to save" },\
158
 
    { GIMP_PDB_STRING,   "filename",     "The name of the file to save the image in" },\
159
 
    { GIMP_PDB_STRING,   "raw-filename", "The name of the file to save the image in" },\
160
 
    { GIMP_PDB_INT32,    "compression",  "Compression type: { NONE (0), LZW (1), PACKBITS (2), DEFLATE (3), JPEG (4), CCITT G3 Fax (5), CCITT G4 Fax (6) }" }
161
 
 
162
 
  static const GimpParamDef save_args_old[] =
163
 
  {
164
 
    COMMON_SAVE_ARGS
165
 
  };
166
 
 
167
 
  static const GimpParamDef save_args[] =
168
 
  {
169
 
    COMMON_SAVE_ARGS,
170
 
    { GIMP_PDB_INT32, "save-transp-pixels", "Keep the color data masked by an alpha channel intact" }
171
 
  };
172
 
 
173
 
  gimp_install_procedure (SAVE_PROC,
174
 
                          "saves files in the tiff file format",
175
 
                          "Saves files in the Tagged Image File Format.  "
176
 
                          "The value for the saved comment is taken "
177
 
                          "from the 'gimp-comment' parasite.",
178
 
                          "Spencer Kimball & Peter Mattis",
179
 
                          "Spencer Kimball & Peter Mattis",
180
 
                          "1995-1996,2000-2003",
181
 
                          N_("TIFF image"),
182
 
                          "RGB*, GRAY*, INDEXED",
183
 
                          GIMP_PLUGIN,
184
 
                          G_N_ELEMENTS (save_args_old), 0,
185
 
                          save_args_old, NULL);
186
 
 
187
 
  gimp_register_file_handler_mime (SAVE_PROC, "image/tiff");
188
 
  gimp_register_save_handler (SAVE_PROC, "tif,tiff", "");
189
 
 
190
 
  gimp_install_procedure (SAVE2_PROC,
191
 
                          "saves files in the tiff file format",
192
 
                          "Saves files in the Tagged Image File Format.  "
193
 
                          "The value for the saved comment is taken "
194
 
                          "from the 'gimp-comment' parasite.",
195
 
                          "Spencer Kimball & Peter Mattis",
196
 
                          "Spencer Kimball & Peter Mattis",
197
 
                          "1995-1996,2000-2003",
198
 
                          N_("TIFF image"),
199
 
                          "RGB*, GRAY*, INDEXED",
200
 
                          GIMP_PLUGIN,
201
 
                          G_N_ELEMENTS (save_args), 0,
202
 
                          save_args, NULL);
203
 
 
204
 
  gimp_register_file_handler_mime (SAVE2_PROC, "image/tiff");
205
 
}
206
 
 
207
 
static void
208
 
run (const gchar      *name,
209
 
     gint              nparams,
210
 
     const GimpParam  *param,
211
 
     gint             *nreturn_vals,
212
 
     GimpParam       **return_vals)
213
 
{
214
 
  static GimpParam   values[2];
215
 
  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
216
 
  GimpParasite      *parasite;
217
 
  gint32             image;
218
 
  gint32             drawable;
219
 
  gint32             orig_image;
220
 
  GimpExportReturn   export = GIMP_EXPORT_CANCEL;
221
 
 
222
 
  run_mode = param[0].data.d_int32;
223
 
 
224
 
  INIT_I18N ();
225
 
 
226
 
  *nreturn_vals = 1;
227
 
  *return_vals  = values;
228
 
 
229
 
  values[0].type          = GIMP_PDB_STATUS;
230
 
  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
231
 
 
232
 
  TIFFSetWarningHandler (tiff_warning);
233
 
  TIFFSetErrorHandler (tiff_error);
234
 
 
235
 
  if ((strcmp (name, SAVE_PROC) == 0) ||
236
 
      (strcmp (name, SAVE2_PROC) == 0))
237
 
    {
238
 
      /* Plug-in is either file_tiff_save or file_tiff_save2 */
239
 
      image = orig_image = param[1].data.d_int32;
240
 
      drawable = param[2].data.d_int32;
241
 
 
242
 
      /* Do this right this time, if POSSIBLE query for parasites, otherwise
243
 
         or if there isn't one, choose the default comment from the gimprc. */
244
 
 
245
 
      /*  eventually export the image */
246
 
      switch (run_mode)
247
 
        {
248
 
        case GIMP_RUN_INTERACTIVE:
249
 
        case GIMP_RUN_WITH_LAST_VALS:
250
 
          gimp_ui_init (PLUG_IN_BINARY, FALSE);
251
 
          export = gimp_export_image (&image, &drawable, "TIFF",
252
 
                                      (GIMP_EXPORT_CAN_HANDLE_RGB |
253
 
                                       GIMP_EXPORT_CAN_HANDLE_GRAY |
254
 
                                       GIMP_EXPORT_CAN_HANDLE_INDEXED |
255
 
                                       GIMP_EXPORT_CAN_HANDLE_ALPHA ));
256
 
          if (export == GIMP_EXPORT_CANCEL)
257
 
            {
258
 
              values[0].data.d_status = GIMP_PDB_CANCEL;
259
 
              return;
260
 
            }
261
 
          break;
262
 
        default:
263
 
          break;
264
 
        }
265
 
 
266
 
      parasite = gimp_image_parasite_find (orig_image, "gimp-comment");
267
 
      if (parasite)
268
 
        {
269
 
          image_comment = g_strndup (gimp_parasite_data (parasite),
270
 
                                     gimp_parasite_data_size (parasite));
271
 
          gimp_parasite_free (parasite);
272
 
        }
273
 
 
274
 
      switch (run_mode)
275
 
        {
276
 
        case GIMP_RUN_INTERACTIVE:
277
 
          /*  Possibly retrieve data  */
278
 
          gimp_get_data (SAVE_PROC, &tsvals);
279
 
 
280
 
          parasite = gimp_image_parasite_find (orig_image, "tiff-save-options");
281
 
          if (parasite)
282
 
            {
283
 
              const TiffSaveVals *pvals = gimp_parasite_data (parasite);
284
 
 
285
 
              tsvals.compression        = pvals->compression;
286
 
              tsvals.save_transp_pixels = pvals->save_transp_pixels;
287
 
            }
288
 
          gimp_parasite_free (parasite);
289
 
 
290
 
          /*  First acquire information with a dialog  */
291
 
          if (! save_dialog (gimp_drawable_has_alpha (drawable),
292
 
                             image_is_monochrome (image)))
293
 
            status = GIMP_PDB_CANCEL;
294
 
          break;
295
 
 
296
 
        case GIMP_RUN_NONINTERACTIVE:
297
 
          /*  Make sure all the arguments are there!  */
298
 
          if (nparams == 6 || nparams == 7)
299
 
            {
300
 
              switch (param[5].data.d_int32)
301
 
                {
302
 
                case 0: tsvals.compression = COMPRESSION_NONE;      break;
303
 
                case 1: tsvals.compression = COMPRESSION_LZW;       break;
304
 
                case 2: tsvals.compression = COMPRESSION_PACKBITS;  break;
305
 
                case 3: tsvals.compression = COMPRESSION_DEFLATE;   break;
306
 
                case 4: tsvals.compression = COMPRESSION_JPEG;      break;
307
 
                case 5: tsvals.compression = COMPRESSION_CCITTFAX3; break;
308
 
                case 6: tsvals.compression = COMPRESSION_CCITTFAX4; break;
309
 
                default: status = GIMP_PDB_CALLING_ERROR; break;
310
 
                }
311
 
 
312
 
              if (nparams == 7)
313
 
                tsvals.save_transp_pixels = param[6].data.d_int32;
314
 
              else
315
 
                tsvals.save_transp_pixels = TRUE;
316
 
            }
317
 
          else
318
 
            {
319
 
              status = GIMP_PDB_CALLING_ERROR;
320
 
            }
321
 
          break;
322
 
 
323
 
        case GIMP_RUN_WITH_LAST_VALS:
324
 
          /*  Possibly retrieve data  */
325
 
          gimp_get_data (SAVE_PROC, &tsvals);
326
 
 
327
 
          parasite = gimp_image_parasite_find (orig_image, "tiff-save-options");
328
 
          if (parasite)
329
 
            {
330
 
              const TiffSaveVals *pvals = gimp_parasite_data (parasite);
331
 
 
332
 
              tsvals.compression        = pvals->compression;
333
 
              tsvals.save_transp_pixels = pvals->save_transp_pixels;
334
 
            }
335
 
          gimp_parasite_free (parasite);
336
 
          break;
337
 
 
338
 
        default:
339
 
          break;
340
 
        }
341
 
 
342
 
      if (status == GIMP_PDB_SUCCESS)
343
 
        {
344
 
          if (save_image (param[3].data.d_string, image, drawable, orig_image))
345
 
            {
346
 
              /*  Store mvals data  */
347
 
              gimp_set_data (SAVE_PROC, &tsvals, sizeof (TiffSaveVals));
348
 
            }
349
 
          else
350
 
            {
351
 
              status = GIMP_PDB_EXECUTION_ERROR;
352
 
            }
353
 
        }
354
 
 
355
 
      if (export == GIMP_EXPORT_EXPORT)
356
 
        gimp_image_delete (image);
357
 
    }
358
 
  else
359
 
    {
360
 
      status = GIMP_PDB_CALLING_ERROR;
361
 
    }
362
 
 
363
 
  values[0].data.d_status = status;
364
 
}
365
 
 
366
 
static void
367
 
tiff_warning (const gchar *module,
368
 
              const gchar *fmt,
369
 
              va_list      ap)
370
 
{
371
 
  va_list ap_test;
372
 
 
373
 
  /* Workaround for: http://bugzilla.gnome.org/show_bug.cgi?id=131975 */
374
 
  /* Ignore the warnings about unregistered private tags (>= 32768) */
375
 
  if (! strcmp (fmt, "%s: unknown field with tag %d (0x%x) encountered"))
376
 
    {
377
 
      G_VA_COPY (ap_test, ap);
378
 
      if (va_arg (ap_test, char *));  /* ignore first argument */
379
 
      if (va_arg (ap_test, int) >= 32768)
380
 
        return;
381
 
    }
382
 
  /* for older versions of libtiff? */
383
 
  else if (! strcmp (fmt, "unknown field with tag %d (0x%x) ignored"))
384
 
    {
385
 
      G_VA_COPY (ap_test, ap);
386
 
      if (va_arg (ap_test, int) >= 32768)
387
 
        return;
388
 
    }
389
 
 
390
 
  g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, fmt, ap);
391
 
}
392
 
 
393
 
static void
394
 
tiff_error (const gchar *module,
395
 
            const gchar *fmt,
396
 
            va_list      ap)
397
 
{
398
 
  /* Workaround for: http://bugzilla.gnome.org/show_bug.cgi?id=132297 */
399
 
  /* Ignore the errors related to random access and JPEG compression */
400
 
  if (! strcmp (fmt, "Compression algorithm does not support random access"))
401
 
    return;
402
 
  g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, fmt, ap);
403
 
}
404
 
 
405
 
static gboolean
406
 
image_is_monochrome (gint32 image)
407
 
{
408
 
  guchar   *colors;
409
 
  gint      num_colors;
410
 
  gboolean  monochrome = FALSE;
411
 
 
412
 
  g_return_val_if_fail (image != -1, FALSE);
413
 
 
414
 
  colors = gimp_image_get_colormap (image, &num_colors);
415
 
 
416
 
  if (colors)
417
 
    {
418
 
      if (num_colors == 2)
419
 
        {
420
 
          const guchar   bw_map[] = { 0, 0, 0, 255, 255, 255 };
421
 
          const guchar   wb_map[] = { 255, 255, 255, 0, 0, 0 };
422
 
 
423
 
          if (memcmp (colors, bw_map, 6) == 0 ||
424
 
              memcmp (colors, wb_map, 6) == 0)
425
 
            {
426
 
              monochrome = TRUE;
427
 
            }
428
 
        }
429
 
 
430
 
      g_free (colors);
431
 
    }
432
 
 
433
 
  return monochrome;
434
 
}
435
 
 
436
 
 
437
 
static void
438
 
double_to_psd_fixed (gdouble value, gchar *target)
439
 
{
440
 
  gdouble in, frac;
441
 
  gint    i, f;
442
 
 
443
 
  frac = modf (value, &in);
444
 
  if (frac < 0)
445
 
    {
446
 
      in -= 1;
447
 
      frac += 1;
448
 
    }
449
 
 
450
 
  i = (gint) CLAMP (in, -16, 15);
451
 
  f = CLAMP ((gint) (frac * 0xFFFFFF), 0, 0xFFFFFF);
452
 
 
453
 
  target[0] = i & 0xFF;
454
 
  target[1] = (f >> 16) & 0xFF;
455
 
  target[2] = (f >>  8) & 0xFF;
456
 
  target[3] = f & 0xFF;
457
 
}
458
 
 
459
 
 
460
 
static gboolean
461
 
save_paths (TIFF   *tif,
462
 
            gint32  image)
463
 
{
464
 
  gint id = 2000; /* Photoshop paths have IDs >= 2000 */
465
 
  gint num_vectors, *vectors, v;
466
 
  gint num_strokes, *strokes, s;
467
 
  gdouble width, height;
468
 
  GString *ps_tag;
469
 
 
470
 
  width = gimp_image_width (image);
471
 
  height = gimp_image_height (image);
472
 
  vectors = gimp_image_get_vectors (image, &num_vectors);
473
 
 
474
 
  if (num_vectors <= 0)
475
 
    return FALSE;
476
 
 
477
 
  ps_tag = g_string_new ("");
478
 
 
479
 
  /* Only up to 1000 paths supported */
480
 
  for (v = 0; v < MIN (num_vectors, 1000); v++)
481
 
    {
482
 
      GString *data;
483
 
      gchar   *name, *nameend;
484
 
      gsize    len;
485
 
      gint     lenpos;
486
 
      gchar    pointrecord[26] = { 0, };
487
 
      gchar   *tmpname;
488
 
      GError  *err = NULL;
489
 
 
490
 
      data = g_string_new ("8BIM");
491
 
      g_string_append_c (data, id / 256);
492
 
      g_string_append_c (data, id % 256);
493
 
 
494
 
      /*
495
 
       * - use iso8859-1 if possible
496
 
       * - otherwise use UTF-8, prepended with \xef\xbb\xbf (Byte-Order-Mark)
497
 
       */
498
 
      name = gimp_vectors_get_name (vectors[v]);
499
 
      tmpname = g_convert (name, -1, "iso8859-1", "utf-8", NULL, &len, &err);
500
 
 
501
 
      if (tmpname && err == NULL)
502
 
        {
503
 
          g_string_append_c (data, MIN (len, 255));
504
 
          g_string_append_len (data, tmpname, MIN (len, 255));
505
 
          g_free (tmpname);
506
 
        }
507
 
      else
508
 
        {
509
 
          /* conversion failed, we fall back to UTF-8 */
510
 
          len = g_utf8_strlen (name, 255 - 3);  /* need three marker-bytes */
511
 
 
512
 
          nameend = g_utf8_offset_to_pointer (name, len);
513
 
          len = nameend - name; /* in bytes */
514
 
          g_assert (len + 3 <= 255);
515
 
 
516
 
          g_string_append_c (data, len + 3);
517
 
          g_string_append_len (data, "\xEF\xBB\xBF", 3); /* Unicode 0xfeff */
518
 
          g_string_append_len (data, name, len);
519
 
 
520
 
          if (tmpname)
521
 
            g_free (tmpname);
522
 
        }
523
 
 
524
 
      if (data->len % 2)  /* padding to even size */
525
 
        g_string_append_c (data, 0);
526
 
      g_free (name);
527
 
 
528
 
      lenpos = data->len;
529
 
      g_string_append_len (data, "\0\0\0\0", 4); /* will be filled in later */
530
 
      len = data->len; /* to calculate the data size later */
531
 
 
532
 
      pointrecord[1] = 6;  /* fill rule record */
533
 
      g_string_append_len (data, pointrecord, 26);
534
 
 
535
 
      strokes = gimp_vectors_get_strokes (vectors[v], &num_strokes);
536
 
 
537
 
      for (s = 0; s < num_strokes; s++)
538
 
        {
539
 
          GimpVectorsStrokeType type;
540
 
          gdouble  *points;
541
 
          gint      num_points;
542
 
          gboolean  closed;
543
 
          gint      p = 0;
544
 
 
545
 
          type = gimp_vectors_stroke_get_points (vectors[v], strokes[s],
546
 
                                                 &num_points, &points, &closed);
547
 
 
548
 
          if (type != GIMP_VECTORS_STROKE_TYPE_BEZIER ||
549
 
              num_points > 65535 ||
550
 
              num_points % 6)
551
 
            {
552
 
              g_printerr ("tiff-save: unsupported stroke type: "
553
 
                          "%d (%d points)\n", type, num_points);
554
 
              continue;
555
 
            }
556
 
 
557
 
          memset (pointrecord, 0, 26);
558
 
          pointrecord[1] = closed ? 0 : 3;
559
 
          pointrecord[2] = (num_points / 6) / 256;
560
 
          pointrecord[3] = (num_points / 6) % 256;
561
 
          g_string_append_len (data, pointrecord, 26);
562
 
 
563
 
          for (p = 0; p < num_points; p += 6)
564
 
            {
565
 
              pointrecord[1] = closed ? 2 : 5;
566
 
 
567
 
              double_to_psd_fixed (points[p+1] / height, pointrecord + 2);
568
 
              double_to_psd_fixed (points[p+0] / width,  pointrecord + 6);
569
 
              double_to_psd_fixed (points[p+3] / height, pointrecord + 10);
570
 
              double_to_psd_fixed (points[p+2] / width,  pointrecord + 14);
571
 
              double_to_psd_fixed (points[p+5] / height, pointrecord + 18);
572
 
              double_to_psd_fixed (points[p+4] / width,  pointrecord + 22);
573
 
 
574
 
              g_string_append_len (data, pointrecord, 26);
575
 
            }
576
 
        }
577
 
 
578
 
      g_free (strokes);
579
 
 
580
 
      /* fix up the length */
581
 
 
582
 
      len = data->len - len;
583
 
      data->str[lenpos + 0] = (len & 0xFF000000) >> 24;
584
 
      data->str[lenpos + 1] = (len & 0x00FF0000) >> 16;
585
 
      data->str[lenpos + 2] = (len & 0x0000FF00) >>  8;
586
 
      data->str[lenpos + 3] = (len & 0x000000FF) >>  0;
587
 
 
588
 
      g_string_append_len (ps_tag, data->str, data->len);
589
 
      g_string_free (data, TRUE);
590
 
      id ++;
591
 
    }
592
 
 
593
 
  TIFFSetField (tif, TIFFTAG_PHOTOSHOP, ps_tag->len, ps_tag->str);
594
 
  g_string_free (ps_tag, TRUE);
595
 
 
596
 
  g_free (vectors);
597
 
 
598
 
  return TRUE;
599
 
}
600
 
 
601
 
 
602
 
/*
603
 
** pnmtotiff.c - converts a portable anymap to a Tagged Image File
604
 
**
605
 
** Derived by Jef Poskanzer from ras2tif.c, which is:
606
 
**
607
 
** Copyright (c) 1990 by Sun Microsystems, Inc.
608
 
**
609
 
** Author: Patrick J. Naughton
610
 
** naughton@wind.sun.com
611
 
**
612
 
** This file is provided AS IS with no warranties of any kind.  The author
613
 
** shall have no liability with respect to the infringement of copyrights,
614
 
** trade secrets or any patents by this file or any part thereof.  In no
615
 
** event will the author be liable for any lost revenue or profits or
616
 
** other special, indirect and consequential damages.
617
 
*/
618
 
 
619
 
static gboolean
620
 
save_image (const gchar *filename,
621
 
            gint32       image,
622
 
            gint32       layer,
623
 
            gint32       orig_image)  /* the export function might have created a duplicate */
624
 
{
625
 
  TIFF          *tif;
626
 
  gushort        red[256];
627
 
  gushort        grn[256];
628
 
  gushort        blu[256];
629
 
  gint           cols, col, rows, row, i;
630
 
  glong          rowsperstrip;
631
 
  gushort        compression;
632
 
  gushort        extra_samples[1];
633
 
  gboolean       alpha;
634
 
  gshort         predictor;
635
 
  gshort         photometric;
636
 
  gshort         samplesperpixel;
637
 
  gshort         bitspersample;
638
 
  gint           bytesperrow;
639
 
  guchar        *t, *src, *data;
640
 
  guchar        *cmap;
641
 
  gint           colors;
642
 
  gint           success;
643
 
  GimpDrawable  *drawable;
644
 
  GimpImageType  drawable_type;
645
 
  GimpPixelRgn   pixel_rgn;
646
 
  gint           tile_height;
647
 
  gint           y, yend;
648
 
  gint           fd;
649
 
  gboolean       is_bw    = FALSE;
650
 
  gboolean       invert   = TRUE;
651
 
  const guchar   bw_map[] = { 0, 0, 0, 255, 255, 255 };
652
 
  const guchar   wb_map[] = { 255, 255, 255, 0, 0, 0 };
653
 
 
654
 
  compression = tsvals.compression;
655
 
 
656
 
  /* Disabled because this isn't in older releases of libtiff, and it
657
 
     wasn't helping much anyway */
658
 
#if 0
659
 
  if (TIFFFindCODEC((uint16) compression) == NULL)
660
 
    compression = COMPRESSION_NONE; /* CODEC not available */
661
 
#endif
662
 
 
663
 
  predictor = 0;
664
 
  tile_height = gimp_tile_height ();
665
 
  rowsperstrip = tile_height;
666
 
 
667
 
  fd = g_open (filename, O_CREAT | O_TRUNC | O_WRONLY | _O_BINARY, 0644);
668
 
 
669
 
  if (fd == -1)
670
 
    {
671
 
      g_message (_("Could not open '%s' for writing: %s"),
672
 
                 gimp_filename_to_utf8 (filename), g_strerror (errno));
673
 
      return FALSE;
674
 
    }
675
 
 
676
 
  tif = TIFFFdOpen (fd, filename, "w");
677
 
 
678
 
  TIFFSetWarningHandler (tiff_warning);
679
 
  TIFFSetErrorHandler (tiff_error);
680
 
 
681
 
  gimp_progress_init_printf (_("Saving '%s'"),
682
 
                             gimp_filename_to_utf8 (filename));
683
 
 
684
 
  drawable = gimp_drawable_get (layer);
685
 
  drawable_type = gimp_drawable_type (layer);
686
 
  gimp_pixel_rgn_init (&pixel_rgn, drawable,
687
 
                       0, 0, drawable->width, drawable->height, FALSE, FALSE);
688
 
 
689
 
  cols = drawable->width;
690
 
  rows = drawable->height;
691
 
 
692
 
  gimp_tile_cache_ntiles (1 + drawable->width / gimp_tile_width ());
693
 
 
694
 
  switch (drawable_type)
695
 
    {
696
 
    case GIMP_RGB_IMAGE:
697
 
      predictor       = 2;
698
 
      samplesperpixel = 3;
699
 
      bitspersample   = 8;
700
 
      photometric     = PHOTOMETRIC_RGB;
701
 
      bytesperrow     = cols * 3;
702
 
      alpha           = FALSE;
703
 
      break;
704
 
 
705
 
    case GIMP_GRAY_IMAGE:
706
 
      samplesperpixel = 1;
707
 
      bitspersample   = 8;
708
 
      photometric     = PHOTOMETRIC_MINISBLACK;
709
 
      bytesperrow     = cols;
710
 
      alpha           = FALSE;
711
 
      break;
712
 
 
713
 
    case GIMP_RGBA_IMAGE:
714
 
      predictor       = 2;
715
 
      samplesperpixel = 4;
716
 
      bitspersample   = 8;
717
 
      photometric     = PHOTOMETRIC_RGB;
718
 
      bytesperrow     = cols * 4;
719
 
      alpha           = TRUE;
720
 
      break;
721
 
 
722
 
    case GIMP_GRAYA_IMAGE:
723
 
      samplesperpixel = 2;
724
 
      bitspersample   = 8;
725
 
      photometric     = PHOTOMETRIC_MINISBLACK;
726
 
      bytesperrow     = cols * 2;
727
 
      alpha           = TRUE;
728
 
      break;
729
 
 
730
 
    case GIMP_INDEXED_IMAGE:
731
 
      cmap = gimp_image_get_colormap (image, &colors);
732
 
 
733
 
      if (colors == 2)
734
 
        {
735
 
          is_bw = (memcmp (cmap, bw_map, 6) == 0);
736
 
          photometric = PHOTOMETRIC_MINISWHITE;
737
 
 
738
 
          if (!is_bw)
739
 
            {
740
 
              is_bw = (memcmp (cmap, wb_map, 6) == 0);
741
 
 
742
 
              if (is_bw)
743
 
                invert = FALSE;
744
 
            }
745
 
       }
746
 
 
747
 
      if (is_bw)
748
 
        {
749
 
          bitspersample = 1;
750
 
        }
751
 
      else
752
 
        {
753
 
          bitspersample = 8;
754
 
          photometric   = PHOTOMETRIC_PALETTE;
755
 
 
756
 
          for (i = 0; i < colors; i++)
757
 
            {
758
 
              red[i] = cmap[i * 3 + 0] * 65535 / 255;
759
 
              grn[i] = cmap[i * 3 + 1] * 65535 / 255;
760
 
              blu[i] = cmap[i * 3 + 2] * 65535 / 255;
761
 
            }
762
 
       }
763
 
 
764
 
      samplesperpixel = 1;
765
 
      bytesperrow     = cols;
766
 
      alpha           = FALSE;
767
 
 
768
 
      g_free (cmap);
769
 
      break;
770
 
 
771
 
    case GIMP_INDEXEDA_IMAGE:
772
 
      g_message ("TIFF save cannot handle indexed images with alpha channel.");
773
 
    default:
774
 
      return FALSE;
775
 
    }
776
 
 
777
 
  if (compression == COMPRESSION_CCITTFAX3 ||
778
 
      compression == COMPRESSION_CCITTFAX4)
779
 
    {
780
 
      if (bitspersample != 1 || samplesperpixel != 1)
781
 
        {
782
 
          g_message ("Only monochrome pictures can be compressed with \"CCITT Group 4\" or \"CCITT Group 3\".");
783
 
          return FALSE;
784
 
        }
785
 
    }
786
 
 
787
 
  /* Set TIFF parameters. */
788
 
  TIFFSetField (tif, TIFFTAG_SUBFILETYPE, 0);
789
 
  TIFFSetField (tif, TIFFTAG_IMAGEWIDTH, cols);
790
 
  TIFFSetField (tif, TIFFTAG_IMAGELENGTH, rows);
791
 
  TIFFSetField (tif, TIFFTAG_BITSPERSAMPLE, bitspersample);
792
 
  TIFFSetField (tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
793
 
  TIFFSetField (tif, TIFFTAG_COMPRESSION, compression);
794
 
 
795
 
  if ((compression == COMPRESSION_LZW || compression == COMPRESSION_DEFLATE)
796
 
      && (predictor != 0))
797
 
    {
798
 
      TIFFSetField (tif, TIFFTAG_PREDICTOR, predictor);
799
 
    }
800
 
 
801
 
  if (alpha)
802
 
    {
803
 
      if (tsvals.save_transp_pixels)
804
 
        extra_samples [0] = EXTRASAMPLE_UNASSALPHA;
805
 
      else
806
 
        extra_samples [0] = EXTRASAMPLE_ASSOCALPHA;
807
 
 
808
 
      TIFFSetField (tif, TIFFTAG_EXTRASAMPLES, 1, extra_samples);
809
 
    }
810
 
 
811
 
  TIFFSetField (tif, TIFFTAG_PHOTOMETRIC, photometric);
812
 
  TIFFSetField (tif, TIFFTAG_DOCUMENTNAME, filename);
813
 
  TIFFSetField (tif, TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
814
 
  TIFFSetField (tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
815
 
  /* TIFFSetField( tif, TIFFTAG_STRIPBYTECOUNTS, rows / rowsperstrip ); */
816
 
  TIFFSetField (tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
817
 
 
818
 
  /* resolution fields */
819
 
  {
820
 
    gdouble  xresolution;
821
 
    gdouble  yresolution;
822
 
    gushort  save_unit = RESUNIT_INCH;
823
 
    GimpUnit unit;
824
 
    gfloat   factor;
825
 
 
826
 
    gimp_image_get_resolution (orig_image, &xresolution, &yresolution);
827
 
    unit = gimp_image_get_unit (orig_image);
828
 
    factor = gimp_unit_get_factor (unit);
829
 
 
830
 
    /*  if we have a metric unit, save the resolution as centimeters
831
 
     */
832
 
    if ((ABS (factor - 0.0254) < 1e-5) ||  /* m  */
833
 
        (ABS (factor - 0.254) < 1e-5) ||   /* dm */
834
 
        (ABS (factor - 2.54) < 1e-5) ||    /* cm */
835
 
        (ABS (factor - 25.4) < 1e-5))      /* mm */
836
 
      {
837
 
        save_unit = RESUNIT_CENTIMETER;
838
 
        xresolution /= 2.54;
839
 
        yresolution /= 2.54;
840
 
      }
841
 
 
842
 
    if (xresolution > 1e-5 && yresolution > 1e-5)
843
 
      {
844
 
        TIFFSetField (tif, TIFFTAG_XRESOLUTION, xresolution);
845
 
        TIFFSetField (tif, TIFFTAG_YRESOLUTION, yresolution);
846
 
        TIFFSetField (tif, TIFFTAG_RESOLUTIONUNIT, save_unit);
847
 
      }
848
 
 
849
 
/* TODO: enable in 2.6
850
 
 
851
 
    gint     offset_x, offset_y;
852
 
 
853
 
    gimp_drawable_offsets (layer, &offset_x, &offset_y);
854
 
 
855
 
    if (offset_x || offset_y)
856
 
      {
857
 
        TIFFSetField (tif, TIFFTAG_XPOSITION, offset_x / xresolution);
858
 
        TIFFSetField (tif, TIFFTAG_YPOSITION, offset_y / yresolution);
859
 
      }
860
 
*/
861
 
  }
862
 
 
863
 
  /* The TIFF spec explicitely says ASCII for the image description. */
864
 
  if (image_comment)
865
 
    {
866
 
      const gchar *c = image_comment;
867
 
      gint         len;
868
 
 
869
 
      for (len = strlen (c); len; c++, len--)
870
 
        {
871
 
          if ((guchar) *c > 127)
872
 
            {
873
 
              g_message (_("The TIFF format only supports comments in\n"
874
 
                           "7bit ASCII encoding. No comment is saved."));
875
 
 
876
 
              g_free (image_comment);
877
 
              image_comment = NULL;
878
 
 
879
 
              break;
880
 
            }
881
 
        }
882
 
    }
883
 
 
884
 
  /* do we have a comment?  If so, create a new parasite to hold it,
885
 
   * and attach it to the image. The attach function automatically
886
 
   * detaches a previous incarnation of the parasite. */
887
 
  if (image_comment && *image_comment)
888
 
    {
889
 
      GimpParasite *parasite;
890
 
 
891
 
      TIFFSetField (tif, TIFFTAG_IMAGEDESCRIPTION, image_comment);
892
 
      parasite = gimp_parasite_new ("gimp-comment",
893
 
                                    GIMP_PARASITE_PERSISTENT,
894
 
                                    strlen (image_comment) + 1, image_comment);
895
 
      gimp_image_parasite_attach (orig_image, parasite);
896
 
      gimp_parasite_free (parasite);
897
 
    }
898
 
 
899
 
  /* do we have an ICC profile? If so, write it to the TIFF file */
900
 
#ifdef TIFFTAG_ICCPROFILE
901
 
  {
902
 
    GimpParasite *parasite;
903
 
    uint32        profile_size;
904
 
    const guchar *icc_profile;
905
 
 
906
 
    parasite = gimp_image_parasite_find (orig_image, "icc-profile");
907
 
    if (parasite)
908
 
      {
909
 
        profile_size = gimp_parasite_data_size (parasite);
910
 
        icc_profile = gimp_parasite_data (parasite);
911
 
 
912
 
        TIFFSetField (tif, TIFFTAG_ICCPROFILE, profile_size, icc_profile);
913
 
        gimp_parasite_free (parasite);
914
 
      }
915
 
  }
916
 
#endif
917
 
 
918
 
  /* save path data */
919
 
  save_paths (tif, orig_image);
920
 
 
921
 
  if (!is_bw && drawable_type == GIMP_INDEXED_IMAGE)
922
 
    TIFFSetField (tif, TIFFTAG_COLORMAP, red, grn, blu);
923
 
 
924
 
  /* array to rearrange data */
925
 
  src = g_new (guchar, bytesperrow * tile_height);
926
 
  data = g_new (guchar, bytesperrow);
927
 
 
928
 
  /* Now write the TIFF data. */
929
 
  for (y = 0; y < rows; y = yend)
930
 
    {
931
 
      yend = y + tile_height;
932
 
      yend = MIN (yend, rows);
933
 
 
934
 
      gimp_pixel_rgn_get_rect (&pixel_rgn, src, 0, y, cols, yend - y);
935
 
 
936
 
      for (row = y; row < yend; row++)
937
 
        {
938
 
          t = src + bytesperrow * (row - y);
939
 
 
940
 
          switch (drawable_type)
941
 
            {
942
 
            case GIMP_INDEXED_IMAGE:
943
 
              if (is_bw)
944
 
                {
945
 
                  byte2bit (t, bytesperrow, data, invert);
946
 
                  success = (TIFFWriteScanline (tif, data, row, 0) >= 0);
947
 
                }
948
 
              else
949
 
                {
950
 
                  success = (TIFFWriteScanline (tif, t, row, 0) >= 0);
951
 
                }
952
 
              break;
953
 
 
954
 
            case GIMP_GRAY_IMAGE:
955
 
              success = (TIFFWriteScanline (tif, t, row, 0) >= 0);
956
 
              break;
957
 
 
958
 
            case GIMP_GRAYA_IMAGE:
959
 
              for (col = 0; col < cols*samplesperpixel; col+=samplesperpixel)
960
 
                {
961
 
                  if (tsvals.save_transp_pixels)
962
 
                    {
963
 
                      data[col + 0] = t[col + 0];
964
 
                    }
965
 
                  else
966
 
                    {
967
 
                      /* pre-multiply gray by alpha */
968
 
                      data[col + 0] = (t[col + 0] * t[col + 1]) / 255;
969
 
                    }
970
 
 
971
 
                  data[col + 1] = t[col + 1];  /* alpha channel */
972
 
                }
973
 
 
974
 
              success = (TIFFWriteScanline (tif, data, row, 0) >= 0);
975
 
              break;
976
 
 
977
 
            case GIMP_RGB_IMAGE:
978
 
              success = (TIFFWriteScanline (tif, t, row, 0) >= 0);
979
 
              break;
980
 
 
981
 
            case GIMP_RGBA_IMAGE:
982
 
              for (col = 0; col < cols*samplesperpixel; col+=samplesperpixel)
983
 
                {
984
 
                  if (tsvals.save_transp_pixels)
985
 
                    {
986
 
                      data[col+0] = t[col + 0];
987
 
                      data[col+1] = t[col + 1];
988
 
                      data[col+2] = t[col + 2];
989
 
                    }
990
 
                  else
991
 
                    {
992
 
                      /* pre-multiply rgb by alpha */
993
 
                      data[col+0] = t[col + 0] * t[col + 3] / 255;
994
 
                      data[col+1] = t[col + 1] * t[col + 3] / 255;
995
 
                      data[col+2] = t[col + 2] * t[col + 3] / 255;
996
 
                    }
997
 
 
998
 
                  data[col+3] = t[col + 3];  /* alpha channel */
999
 
                }
1000
 
 
1001
 
              success = (TIFFWriteScanline (tif, data, row, 0) >= 0);
1002
 
              break;
1003
 
 
1004
 
            default:
1005
 
              success = FALSE;
1006
 
              break;
1007
 
            }
1008
 
 
1009
 
          if (!success)
1010
 
            {
1011
 
              g_message ("Failed a scanline write on row %d", row);
1012
 
              return FALSE;
1013
 
            }
1014
 
        }
1015
 
 
1016
 
      if ((row % 32) == 0)
1017
 
        gimp_progress_update ((gdouble) row / (gdouble) rows);
1018
 
    }
1019
 
 
1020
 
  TIFFFlushData (tif);
1021
 
  TIFFClose (tif);
1022
 
  close (fd);
1023
 
 
1024
 
  gimp_progress_update (1.0);
1025
 
 
1026
 
  gimp_drawable_detach (drawable);
1027
 
  g_free (data);
1028
 
 
1029
 
  return TRUE;
1030
 
}
1031
 
 
1032
 
static gboolean
1033
 
save_dialog (gboolean has_alpha,
1034
 
             gboolean is_monochrome)
1035
 
{
1036
 
  GtkWidget *dialog;
1037
 
  GtkWidget *vbox;
1038
 
  GtkWidget *frame;
1039
 
  GtkWidget *hbox;
1040
 
  GtkWidget *label;
1041
 
  GtkWidget *entry;
1042
 
  GtkWidget *toggle;
1043
 
  GtkWidget *g3;
1044
 
  GtkWidget *g4;
1045
 
  gboolean   run;
1046
 
 
1047
 
  dialog = gimp_dialog_new (_("Save as TIFF"), PLUG_IN_BINARY,
1048
 
                            NULL, 0,
1049
 
                            gimp_standard_help_func, SAVE_PROC,
1050
 
 
1051
 
                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1052
 
                            GTK_STOCK_SAVE,   GTK_RESPONSE_OK,
1053
 
 
1054
 
                            NULL);
1055
 
 
1056
 
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1057
 
                                           GTK_RESPONSE_OK,
1058
 
                                           GTK_RESPONSE_CANCEL,
1059
 
                                           -1);
1060
 
 
1061
 
  gimp_window_set_transient (GTK_WINDOW (dialog));
1062
 
 
1063
 
  vbox = gtk_vbox_new (FALSE, 12);
1064
 
  gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
1065
 
  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
1066
 
                      vbox, FALSE, TRUE, 0);
1067
 
 
1068
 
  /*  compression  */
1069
 
  frame = gimp_int_radio_group_new (TRUE, _("Compression"),
1070
 
                                    G_CALLBACK (gimp_radio_button_update),
1071
 
                                    &tsvals.compression, tsvals.compression,
1072
 
 
1073
 
                                    _("_None"),      COMPRESSION_NONE,     NULL,
1074
 
                                    _("_LZW"),       COMPRESSION_LZW,      NULL,
1075
 
                                    _("_Pack Bits"), COMPRESSION_PACKBITS, NULL,
1076
 
                                    _("_Deflate"),   COMPRESSION_DEFLATE,  NULL,
1077
 
                                    _("_JPEG"),      COMPRESSION_JPEG,     NULL,
1078
 
                                    _("CCITT Group _3 fax"), COMPRESSION_CCITTFAX3, &g3,
1079
 
                                    _("CCITT Group _4 fax"), COMPRESSION_CCITTFAX4, &g4,
1080
 
 
1081
 
                                    NULL);
1082
 
 
1083
 
  gtk_widget_set_sensitive (g3, is_monochrome);
1084
 
  gtk_widget_set_sensitive (g4, is_monochrome);
1085
 
 
1086
 
  if (! is_monochrome)
1087
 
    {
1088
 
      if (tsvals.compression == COMPRESSION_CCITTFAX3 ||
1089
 
          tsvals.compression ==  COMPRESSION_CCITTFAX4)
1090
 
        {
1091
 
          gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (g3),
1092
 
                                           COMPRESSION_NONE);
1093
 
        }
1094
 
    }
1095
 
 
1096
 
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
1097
 
  gtk_widget_show (frame);
1098
 
 
1099
 
  /* Keep colors behind alpha mask */
1100
 
  toggle = gtk_check_button_new_with_mnemonic
1101
 
    ( _("Save _color values from transparent pixels"));
1102
 
  gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (toggle),
1103
 
                                has_alpha && tsvals.save_transp_pixels);
1104
 
  gtk_widget_set_sensitive (toggle, has_alpha);
1105
 
  gtk_box_pack_start (GTK_BOX (vbox), toggle, FALSE, FALSE, 0);
1106
 
  gtk_widget_show (toggle);
1107
 
 
1108
 
  g_signal_connect (toggle, "toggled",
1109
 
                    G_CALLBACK (gimp_toggle_button_update),
1110
 
                    &tsvals.save_transp_pixels);
1111
 
 
1112
 
  /* comment entry */
1113
 
  hbox = gtk_hbox_new (FALSE, 6);
1114
 
  gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1115
 
  gtk_widget_show (hbox);
1116
 
 
1117
 
  label = gtk_label_new ( _("Comment:"));
1118
 
  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1119
 
  gtk_widget_show (label);
1120
 
 
1121
 
  entry = gtk_entry_new ();
1122
 
  gtk_widget_show (entry);
1123
 
  gtk_box_pack_start (GTK_BOX (hbox), entry, TRUE, TRUE, 0);
1124
 
  gtk_entry_set_text (GTK_ENTRY (entry), image_comment ? image_comment : "");
1125
 
 
1126
 
  g_signal_connect (entry, "changed",
1127
 
                    G_CALLBACK (comment_entry_callback),
1128
 
                    NULL);
1129
 
 
1130
 
  gtk_widget_show (frame);
1131
 
 
1132
 
  gtk_widget_show (vbox);
1133
 
  gtk_widget_show (dialog);
1134
 
 
1135
 
  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
1136
 
 
1137
 
  gtk_widget_destroy (dialog);
1138
 
 
1139
 
  return run;
1140
 
}
1141
 
 
1142
 
static void
1143
 
comment_entry_callback (GtkWidget *widget,
1144
 
                        gpointer   data)
1145
 
{
1146
 
  const gchar *text = gtk_entry_get_text (GTK_ENTRY (widget));
1147
 
 
1148
 
  g_free (image_comment);
1149
 
  image_comment = g_strdup (text);
1150
 
}
1151
 
 
1152
 
/* Convert n bytes of 0/1 to a line of bits */
1153
 
static void
1154
 
byte2bit (const guchar *byteline,
1155
 
          gint          width,
1156
 
          guchar       *bitline,
1157
 
          gboolean      invert)
1158
 
{
1159
 
  guchar bitval;
1160
 
  guchar rest[8];
1161
 
 
1162
 
  while (width >= 8)
1163
 
    {
1164
 
      bitval = 0;
1165
 
      if (*(byteline++)) bitval |= 0x80;
1166
 
      if (*(byteline++)) bitval |= 0x40;
1167
 
      if (*(byteline++)) bitval |= 0x20;
1168
 
      if (*(byteline++)) bitval |= 0x10;
1169
 
      if (*(byteline++)) bitval |= 0x08;
1170
 
      if (*(byteline++)) bitval |= 0x04;
1171
 
      if (*(byteline++)) bitval |= 0x02;
1172
 
      if (*(byteline++)) bitval |= 0x01;
1173
 
      *(bitline++) = invert ? ~bitval : bitval;
1174
 
      width -= 8;
1175
 
    }
1176
 
  if (width > 0)
1177
 
    {
1178
 
      memset (rest, 0, 8);
1179
 
      memcpy (rest, byteline, width);
1180
 
      bitval = 0;
1181
 
      byteline = rest;
1182
 
      if (*(byteline++)) bitval |= 0x80;
1183
 
      if (*(byteline++)) bitval |= 0x40;
1184
 
      if (*(byteline++)) bitval |= 0x20;
1185
 
      if (*(byteline++)) bitval |= 0x10;
1186
 
      if (*(byteline++)) bitval |= 0x08;
1187
 
      if (*(byteline++)) bitval |= 0x04;
1188
 
      if (*(byteline++)) bitval |= 0x02;
1189
 
      *bitline = invert ? ~bitval & (0xff << (8 - width)) : bitval;
1190
 
    }
1191
 
}