1
/* tiff saving for GIMP
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
23
* tifftopnm.c - converts a Tagged Image File to a portable anymap
25
* Derived by Jef Poskanzer from tif2ras.c, which is:
27
* Copyright (c) 1990 by Sun Microsystems, Inc.
29
* Author: Patrick J. Naughton
30
* naughton@wind.sun.com
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.
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.
50
#include <sys/types.h>
57
#include <glib/gstdio.h>
59
#include <libgimpbase/gimpwin32-io.h>
68
#include <libgimp/gimp.h>
69
#include <libgimp/gimpui.h>
71
#include "libgimp/stdplugins-intl.h"
74
#define SAVE_PROC "file-tiff-save"
75
#define SAVE2_PROC "file-tiff-save2"
76
#define PLUG_IN_BINARY "tiff-save"
83
gboolean save_transp_pixels;
89
GimpDrawable *drawable;
90
GimpPixelRgn pixel_rgn;
95
/* Declare some local functions.
97
static void query (void);
98
static void run (const gchar *name,
100
const GimpParam *param,
102
GimpParam **return_vals);
104
static gboolean image_is_monochrome (gint32 image);
106
static gboolean save_paths (TIFF *tif,
108
static gboolean save_image (const gchar *filename,
113
static gboolean save_dialog (gboolean has_alpha,
114
gboolean is_monochrome);
116
static void comment_entry_callback (GtkWidget *widget,
119
static void byte2bit (const guchar *byteline,
124
static void tiff_warning (const gchar *module,
127
static void tiff_error (const gchar *module,
131
const GimpPlugInInfo PLUG_IN_INFO =
133
NULL, /* init_proc */
134
NULL, /* quit_proc */
135
query, /* query_proc */
139
static TiffSaveVals tsvals =
141
COMPRESSION_NONE, /* compression */
142
TRUE, /* alpha handling */
145
static gchar *image_comment = NULL;
146
static GimpRunMode run_mode = GIMP_RUN_INTERACTIVE;
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) }" }
162
static const GimpParamDef save_args_old[] =
167
static const GimpParamDef save_args[] =
170
{ GIMP_PDB_INT32, "save-transp-pixels", "Keep the color data masked by an alpha channel intact" }
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",
182
"RGB*, GRAY*, INDEXED",
184
G_N_ELEMENTS (save_args_old), 0,
185
save_args_old, NULL);
187
gimp_register_file_handler_mime (SAVE_PROC, "image/tiff");
188
gimp_register_save_handler (SAVE_PROC, "tif,tiff", "");
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",
199
"RGB*, GRAY*, INDEXED",
201
G_N_ELEMENTS (save_args), 0,
204
gimp_register_file_handler_mime (SAVE2_PROC, "image/tiff");
208
run (const gchar *name,
210
const GimpParam *param,
212
GimpParam **return_vals)
214
static GimpParam values[2];
215
GimpPDBStatusType status = GIMP_PDB_SUCCESS;
216
GimpParasite *parasite;
220
GimpExportReturn export = GIMP_EXPORT_CANCEL;
222
run_mode = param[0].data.d_int32;
227
*return_vals = values;
229
values[0].type = GIMP_PDB_STATUS;
230
values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
232
TIFFSetWarningHandler (tiff_warning);
233
TIFFSetErrorHandler (tiff_error);
235
if ((strcmp (name, SAVE_PROC) == 0) ||
236
(strcmp (name, SAVE2_PROC) == 0))
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;
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. */
245
/* eventually export the image */
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)
258
values[0].data.d_status = GIMP_PDB_CANCEL;
266
parasite = gimp_image_parasite_find (orig_image, "gimp-comment");
269
image_comment = g_strndup (gimp_parasite_data (parasite),
270
gimp_parasite_data_size (parasite));
271
gimp_parasite_free (parasite);
276
case GIMP_RUN_INTERACTIVE:
277
/* Possibly retrieve data */
278
gimp_get_data (SAVE_PROC, &tsvals);
280
parasite = gimp_image_parasite_find (orig_image, "tiff-save-options");
283
const TiffSaveVals *pvals = gimp_parasite_data (parasite);
285
tsvals.compression = pvals->compression;
286
tsvals.save_transp_pixels = pvals->save_transp_pixels;
288
gimp_parasite_free (parasite);
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;
296
case GIMP_RUN_NONINTERACTIVE:
297
/* Make sure all the arguments are there! */
298
if (nparams == 6 || nparams == 7)
300
switch (param[5].data.d_int32)
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;
313
tsvals.save_transp_pixels = param[6].data.d_int32;
315
tsvals.save_transp_pixels = TRUE;
319
status = GIMP_PDB_CALLING_ERROR;
323
case GIMP_RUN_WITH_LAST_VALS:
324
/* Possibly retrieve data */
325
gimp_get_data (SAVE_PROC, &tsvals);
327
parasite = gimp_image_parasite_find (orig_image, "tiff-save-options");
330
const TiffSaveVals *pvals = gimp_parasite_data (parasite);
332
tsvals.compression = pvals->compression;
333
tsvals.save_transp_pixels = pvals->save_transp_pixels;
335
gimp_parasite_free (parasite);
342
if (status == GIMP_PDB_SUCCESS)
344
if (save_image (param[3].data.d_string, image, drawable, orig_image))
346
/* Store mvals data */
347
gimp_set_data (SAVE_PROC, &tsvals, sizeof (TiffSaveVals));
351
status = GIMP_PDB_EXECUTION_ERROR;
355
if (export == GIMP_EXPORT_EXPORT)
356
gimp_image_delete (image);
360
status = GIMP_PDB_CALLING_ERROR;
363
values[0].data.d_status = status;
367
tiff_warning (const gchar *module,
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"))
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)
382
/* for older versions of libtiff? */
383
else if (! strcmp (fmt, "unknown field with tag %d (0x%x) ignored"))
385
G_VA_COPY (ap_test, ap);
386
if (va_arg (ap_test, int) >= 32768)
390
g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, fmt, ap);
394
tiff_error (const gchar *module,
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"))
402
g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, fmt, ap);
406
image_is_monochrome (gint32 image)
410
gboolean monochrome = FALSE;
412
g_return_val_if_fail (image != -1, FALSE);
414
colors = gimp_image_get_colormap (image, &num_colors);
420
const guchar bw_map[] = { 0, 0, 0, 255, 255, 255 };
421
const guchar wb_map[] = { 255, 255, 255, 0, 0, 0 };
423
if (memcmp (colors, bw_map, 6) == 0 ||
424
memcmp (colors, wb_map, 6) == 0)
438
double_to_psd_fixed (gdouble value, gchar *target)
443
frac = modf (value, &in);
450
i = (gint) CLAMP (in, -16, 15);
451
f = CLAMP ((gint) (frac * 0xFFFFFF), 0, 0xFFFFFF);
453
target[0] = i & 0xFF;
454
target[1] = (f >> 16) & 0xFF;
455
target[2] = (f >> 8) & 0xFF;
456
target[3] = f & 0xFF;
461
save_paths (TIFF *tif,
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;
470
width = gimp_image_width (image);
471
height = gimp_image_height (image);
472
vectors = gimp_image_get_vectors (image, &num_vectors);
474
if (num_vectors <= 0)
477
ps_tag = g_string_new ("");
479
/* Only up to 1000 paths supported */
480
for (v = 0; v < MIN (num_vectors, 1000); v++)
483
gchar *name, *nameend;
486
gchar pointrecord[26] = { 0, };
490
data = g_string_new ("8BIM");
491
g_string_append_c (data, id / 256);
492
g_string_append_c (data, id % 256);
495
* - use iso8859-1 if possible
496
* - otherwise use UTF-8, prepended with \xef\xbb\xbf (Byte-Order-Mark)
498
name = gimp_vectors_get_name (vectors[v]);
499
tmpname = g_convert (name, -1, "iso8859-1", "utf-8", NULL, &len, &err);
501
if (tmpname && err == NULL)
503
g_string_append_c (data, MIN (len, 255));
504
g_string_append_len (data, tmpname, MIN (len, 255));
509
/* conversion failed, we fall back to UTF-8 */
510
len = g_utf8_strlen (name, 255 - 3); /* need three marker-bytes */
512
nameend = g_utf8_offset_to_pointer (name, len);
513
len = nameend - name; /* in bytes */
514
g_assert (len + 3 <= 255);
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);
524
if (data->len % 2) /* padding to even size */
525
g_string_append_c (data, 0);
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 */
532
pointrecord[1] = 6; /* fill rule record */
533
g_string_append_len (data, pointrecord, 26);
535
strokes = gimp_vectors_get_strokes (vectors[v], &num_strokes);
537
for (s = 0; s < num_strokes; s++)
539
GimpVectorsStrokeType type;
545
type = gimp_vectors_stroke_get_points (vectors[v], strokes[s],
546
&num_points, &points, &closed);
548
if (type != GIMP_VECTORS_STROKE_TYPE_BEZIER ||
549
num_points > 65535 ||
552
g_printerr ("tiff-save: unsupported stroke type: "
553
"%d (%d points)\n", type, num_points);
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);
563
for (p = 0; p < num_points; p += 6)
565
pointrecord[1] = closed ? 2 : 5;
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);
574
g_string_append_len (data, pointrecord, 26);
580
/* fix up the length */
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;
588
g_string_append_len (ps_tag, data->str, data->len);
589
g_string_free (data, TRUE);
593
TIFFSetField (tif, TIFFTAG_PHOTOSHOP, ps_tag->len, ps_tag->str);
594
g_string_free (ps_tag, TRUE);
603
** pnmtotiff.c - converts a portable anymap to a Tagged Image File
605
** Derived by Jef Poskanzer from ras2tif.c, which is:
607
** Copyright (c) 1990 by Sun Microsystems, Inc.
609
** Author: Patrick J. Naughton
610
** naughton@wind.sun.com
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.
620
save_image (const gchar *filename,
623
gint32 orig_image) /* the export function might have created a duplicate */
629
gint cols, col, rows, row, i;
632
gushort extra_samples[1];
636
gshort samplesperpixel;
637
gshort bitspersample;
639
guchar *t, *src, *data;
643
GimpDrawable *drawable;
644
GimpImageType drawable_type;
645
GimpPixelRgn pixel_rgn;
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 };
654
compression = tsvals.compression;
656
/* Disabled because this isn't in older releases of libtiff, and it
657
wasn't helping much anyway */
659
if (TIFFFindCODEC((uint16) compression) == NULL)
660
compression = COMPRESSION_NONE; /* CODEC not available */
664
tile_height = gimp_tile_height ();
665
rowsperstrip = tile_height;
667
fd = g_open (filename, O_CREAT | O_TRUNC | O_WRONLY | _O_BINARY, 0644);
671
g_message (_("Could not open '%s' for writing: %s"),
672
gimp_filename_to_utf8 (filename), g_strerror (errno));
676
tif = TIFFFdOpen (fd, filename, "w");
678
TIFFSetWarningHandler (tiff_warning);
679
TIFFSetErrorHandler (tiff_error);
681
gimp_progress_init_printf (_("Saving '%s'"),
682
gimp_filename_to_utf8 (filename));
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);
689
cols = drawable->width;
690
rows = drawable->height;
692
gimp_tile_cache_ntiles (1 + drawable->width / gimp_tile_width ());
694
switch (drawable_type)
700
photometric = PHOTOMETRIC_RGB;
701
bytesperrow = cols * 3;
705
case GIMP_GRAY_IMAGE:
708
photometric = PHOTOMETRIC_MINISBLACK;
713
case GIMP_RGBA_IMAGE:
717
photometric = PHOTOMETRIC_RGB;
718
bytesperrow = cols * 4;
722
case GIMP_GRAYA_IMAGE:
725
photometric = PHOTOMETRIC_MINISBLACK;
726
bytesperrow = cols * 2;
730
case GIMP_INDEXED_IMAGE:
731
cmap = gimp_image_get_colormap (image, &colors);
735
is_bw = (memcmp (cmap, bw_map, 6) == 0);
736
photometric = PHOTOMETRIC_MINISWHITE;
740
is_bw = (memcmp (cmap, wb_map, 6) == 0);
754
photometric = PHOTOMETRIC_PALETTE;
756
for (i = 0; i < colors; i++)
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;
771
case GIMP_INDEXEDA_IMAGE:
772
g_message ("TIFF save cannot handle indexed images with alpha channel.");
777
if (compression == COMPRESSION_CCITTFAX3 ||
778
compression == COMPRESSION_CCITTFAX4)
780
if (bitspersample != 1 || samplesperpixel != 1)
782
g_message ("Only monochrome pictures can be compressed with \"CCITT Group 4\" or \"CCITT Group 3\".");
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);
795
if ((compression == COMPRESSION_LZW || compression == COMPRESSION_DEFLATE)
798
TIFFSetField (tif, TIFFTAG_PREDICTOR, predictor);
803
if (tsvals.save_transp_pixels)
804
extra_samples [0] = EXTRASAMPLE_UNASSALPHA;
806
extra_samples [0] = EXTRASAMPLE_ASSOCALPHA;
808
TIFFSetField (tif, TIFFTAG_EXTRASAMPLES, 1, extra_samples);
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);
818
/* resolution fields */
822
gushort save_unit = RESUNIT_INCH;
826
gimp_image_get_resolution (orig_image, &xresolution, &yresolution);
827
unit = gimp_image_get_unit (orig_image);
828
factor = gimp_unit_get_factor (unit);
830
/* if we have a metric unit, save the resolution as centimeters
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 */
837
save_unit = RESUNIT_CENTIMETER;
842
if (xresolution > 1e-5 && yresolution > 1e-5)
844
TIFFSetField (tif, TIFFTAG_XRESOLUTION, xresolution);
845
TIFFSetField (tif, TIFFTAG_YRESOLUTION, yresolution);
846
TIFFSetField (tif, TIFFTAG_RESOLUTIONUNIT, save_unit);
849
/* TODO: enable in 2.6
851
gint offset_x, offset_y;
853
gimp_drawable_offsets (layer, &offset_x, &offset_y);
855
if (offset_x || offset_y)
857
TIFFSetField (tif, TIFFTAG_XPOSITION, offset_x / xresolution);
858
TIFFSetField (tif, TIFFTAG_YPOSITION, offset_y / yresolution);
863
/* The TIFF spec explicitely says ASCII for the image description. */
866
const gchar *c = image_comment;
869
for (len = strlen (c); len; c++, len--)
871
if ((guchar) *c > 127)
873
g_message (_("The TIFF format only supports comments in\n"
874
"7bit ASCII encoding. No comment is saved."));
876
g_free (image_comment);
877
image_comment = NULL;
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)
889
GimpParasite *parasite;
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);
899
/* do we have an ICC profile? If so, write it to the TIFF file */
900
#ifdef TIFFTAG_ICCPROFILE
902
GimpParasite *parasite;
904
const guchar *icc_profile;
906
parasite = gimp_image_parasite_find (orig_image, "icc-profile");
909
profile_size = gimp_parasite_data_size (parasite);
910
icc_profile = gimp_parasite_data (parasite);
912
TIFFSetField (tif, TIFFTAG_ICCPROFILE, profile_size, icc_profile);
913
gimp_parasite_free (parasite);
919
save_paths (tif, orig_image);
921
if (!is_bw && drawable_type == GIMP_INDEXED_IMAGE)
922
TIFFSetField (tif, TIFFTAG_COLORMAP, red, grn, blu);
924
/* array to rearrange data */
925
src = g_new (guchar, bytesperrow * tile_height);
926
data = g_new (guchar, bytesperrow);
928
/* Now write the TIFF data. */
929
for (y = 0; y < rows; y = yend)
931
yend = y + tile_height;
932
yend = MIN (yend, rows);
934
gimp_pixel_rgn_get_rect (&pixel_rgn, src, 0, y, cols, yend - y);
936
for (row = y; row < yend; row++)
938
t = src + bytesperrow * (row - y);
940
switch (drawable_type)
942
case GIMP_INDEXED_IMAGE:
945
byte2bit (t, bytesperrow, data, invert);
946
success = (TIFFWriteScanline (tif, data, row, 0) >= 0);
950
success = (TIFFWriteScanline (tif, t, row, 0) >= 0);
954
case GIMP_GRAY_IMAGE:
955
success = (TIFFWriteScanline (tif, t, row, 0) >= 0);
958
case GIMP_GRAYA_IMAGE:
959
for (col = 0; col < cols*samplesperpixel; col+=samplesperpixel)
961
if (tsvals.save_transp_pixels)
963
data[col + 0] = t[col + 0];
967
/* pre-multiply gray by alpha */
968
data[col + 0] = (t[col + 0] * t[col + 1]) / 255;
971
data[col + 1] = t[col + 1]; /* alpha channel */
974
success = (TIFFWriteScanline (tif, data, row, 0) >= 0);
978
success = (TIFFWriteScanline (tif, t, row, 0) >= 0);
981
case GIMP_RGBA_IMAGE:
982
for (col = 0; col < cols*samplesperpixel; col+=samplesperpixel)
984
if (tsvals.save_transp_pixels)
986
data[col+0] = t[col + 0];
987
data[col+1] = t[col + 1];
988
data[col+2] = t[col + 2];
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;
998
data[col+3] = t[col + 3]; /* alpha channel */
1001
success = (TIFFWriteScanline (tif, data, row, 0) >= 0);
1011
g_message ("Failed a scanline write on row %d", row);
1016
if ((row % 32) == 0)
1017
gimp_progress_update ((gdouble) row / (gdouble) rows);
1020
TIFFFlushData (tif);
1024
gimp_progress_update (1.0);
1026
gimp_drawable_detach (drawable);
1033
save_dialog (gboolean has_alpha,
1034
gboolean is_monochrome)
1047
dialog = gimp_dialog_new (_("Save as TIFF"), PLUG_IN_BINARY,
1049
gimp_standard_help_func, SAVE_PROC,
1051
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
1052
GTK_STOCK_SAVE, GTK_RESPONSE_OK,
1056
gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
1058
GTK_RESPONSE_CANCEL,
1061
gimp_window_set_transient (GTK_WINDOW (dialog));
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);
1069
frame = gimp_int_radio_group_new (TRUE, _("Compression"),
1070
G_CALLBACK (gimp_radio_button_update),
1071
&tsvals.compression, tsvals.compression,
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,
1083
gtk_widget_set_sensitive (g3, is_monochrome);
1084
gtk_widget_set_sensitive (g4, is_monochrome);
1086
if (! is_monochrome)
1088
if (tsvals.compression == COMPRESSION_CCITTFAX3 ||
1089
tsvals.compression == COMPRESSION_CCITTFAX4)
1091
gimp_int_radio_group_set_active (GTK_RADIO_BUTTON (g3),
1096
gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
1097
gtk_widget_show (frame);
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);
1108
g_signal_connect (toggle, "toggled",
1109
G_CALLBACK (gimp_toggle_button_update),
1110
&tsvals.save_transp_pixels);
1113
hbox = gtk_hbox_new (FALSE, 6);
1114
gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
1115
gtk_widget_show (hbox);
1117
label = gtk_label_new ( _("Comment:"));
1118
gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1119
gtk_widget_show (label);
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 : "");
1126
g_signal_connect (entry, "changed",
1127
G_CALLBACK (comment_entry_callback),
1130
gtk_widget_show (frame);
1132
gtk_widget_show (vbox);
1133
gtk_widget_show (dialog);
1135
run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
1137
gtk_widget_destroy (dialog);
1143
comment_entry_callback (GtkWidget *widget,
1146
const gchar *text = gtk_entry_get_text (GTK_ENTRY (widget));
1148
g_free (image_comment);
1149
image_comment = g_strdup (text);
1152
/* Convert n bytes of 0/1 to a line of bits */
1154
byte2bit (const guchar *byteline,
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;
1178
memset (rest, 0, 8);
1179
memcpy (rest, byteline, width);
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;