|
Packit |
a4058c |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
|
|
Packit |
a4058c |
/* GdkPixbuf library - TIFF image loader
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* Copyright (C) 1999 Mark Crichton
|
|
Packit |
a4058c |
* Copyright (C) 1999 The Free Software Foundation
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* Authors: Mark Crichton <crichton@gimp.org>
|
|
Packit |
a4058c |
* Federico Mena-Quintero <federico@gimp.org>
|
|
Packit |
a4058c |
* Jonathan Blandford <jrb@redhat.com>
|
|
Packit |
a4058c |
* S�ren Sandmann <sandmann@daimi.au.dk>
|
|
Packit |
a4058c |
* Christian Dywan <christian@lanedo.com>
|
|
Packit |
a4058c |
* Mukund Sivaraman <muks@banu.com>
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* This library is free software; you can redistribute it and/or
|
|
Packit |
a4058c |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit |
a4058c |
* License as published by the Free Software Foundation; either
|
|
Packit |
a4058c |
* version 2 of the License, or (at your option) any later version.
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* This library is distributed in the hope that it will be useful,
|
|
Packit |
a4058c |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
a4058c |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
a4058c |
* Lesser General Public License for more details.
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit |
a4058c |
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Following code (almost) blatantly ripped from Imlib */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#include "config.h"
|
|
Packit |
a4058c |
#include <stdlib.h>
|
|
Packit |
a4058c |
#include <string.h>
|
|
Packit |
a4058c |
#ifdef HAVE_UNISTD_H
|
|
Packit |
a4058c |
#include <unistd.h>
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
#include <tiffio.h>
|
|
Packit |
a4058c |
#include <errno.h>
|
|
Packit |
a4058c |
#include "gdk-pixbuf-private.h"
|
|
Packit |
a4058c |
#include "fallback-c89.c"
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#ifdef G_OS_WIN32
|
|
Packit |
a4058c |
#include <fcntl.h>
|
|
Packit |
a4058c |
#include <io.h>
|
|
Packit |
a4058c |
#define lseek(a,b,c) _lseek(a,b,c)
|
|
Packit |
a4058c |
#define O_RDWR _O_RDWR
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
typedef struct _TiffContext TiffContext;
|
|
Packit |
a4058c |
struct _TiffContext
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
GdkPixbufModuleSizeFunc size_func;
|
|
Packit |
a4058c |
GdkPixbufModulePreparedFunc prepare_func;
|
|
Packit |
a4058c |
GdkPixbufModuleUpdatedFunc update_func;
|
|
Packit |
a4058c |
gpointer user_data;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
guchar *buffer;
|
|
Packit |
a4058c |
guint allocated;
|
|
Packit |
a4058c |
guint used;
|
|
Packit |
a4058c |
guint pos;
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
tiff_warning_handler (const char *mod, const char *fmt, va_list ap)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
/* Don't print anything; we should not be dumping junk to
|
|
Packit |
a4058c |
* stderr, since that may be bad for some apps.
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
tiff_set_handlers (void)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TIFFSetErrorHandler (tiff_warning_handler);
|
|
Packit |
a4058c |
TIFFSetWarningHandler (tiff_warning_handler);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void free_buffer (guchar *pixels, gpointer data)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
g_free (pixels);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static GdkPixbuf *
|
|
Packit |
a4058c |
tiff_image_parse (TIFF *tiff, TiffContext *context, GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
guchar *pixels = NULL;
|
|
Packit |
a4058c |
gint width, height, rowstride, bytes;
|
|
Packit |
a4058c |
GdkPixbuf *pixbuf;
|
|
Packit |
a4058c |
guint16 bits_per_sample = 0;
|
|
Packit |
a4058c |
uint16 orientation = 0;
|
|
Packit |
a4058c |
uint16 transform = 0;
|
|
Packit |
a4058c |
uint16 codec;
|
|
Packit |
a4058c |
gchar *icc_profile_base64;
|
|
Packit |
a4058c |
const gchar *icc_profile;
|
|
Packit |
a4058c |
guint icc_profile_size;
|
|
Packit |
a4058c |
uint16 resolution_unit;
|
|
Packit |
a4058c |
gchar *density_str;
|
|
Packit |
a4058c |
gint retval;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* We're called with the lock held. */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!TIFFGetField (tiff, TIFFTAG_IMAGEWIDTH, &width)) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("Could not get image width (bad TIFF file)"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!TIFFGetField (tiff, TIFFTAG_IMAGELENGTH, &height)) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("Could not get image height (bad TIFF file)"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (width <= 0 || height <= 0) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
Packit |
a4058c |
_("Width or height of TIFF image is zero"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (width > G_MAXINT / 4) { /* overflow */
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
Packit |
a4058c |
_("Dimensions of TIFF image too large"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
rowstride = width * 4;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (height > G_MAXINT / rowstride) { /* overflow */
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
Packit |
a4058c |
_("Dimensions of TIFF image too large"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
bytes = height * rowstride;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context && context->size_func) {
|
|
Packit |
a4058c |
gint w = width;
|
|
Packit |
a4058c |
gint h = height;
|
|
Packit |
a4058c |
(* context->size_func) (&w, &h, context->user_data);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* This is a signal that this function is being called
|
|
Packit |
a4058c |
to support gdk_pixbuf_get_file_info, so we can stop
|
|
Packit |
a4058c |
parsing the tiff file at this point. It is not an
|
|
Packit |
a4058c |
error condition. */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (w == 0 || h == 0)
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pixels = g_try_malloc (bytes);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!pixels) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
|
|
Packit |
a4058c |
_("Insufficient memory to open TIFF file"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pixbuf = gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, TRUE, 8,
|
|
Packit |
a4058c |
width, height, rowstride,
|
|
Packit |
a4058c |
free_buffer, NULL);
|
|
Packit |
a4058c |
if (!pixbuf) {
|
|
Packit |
a4058c |
g_free (pixels);
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
|
|
Packit |
a4058c |
_("Insufficient memory to open TIFF file"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Save the bits per sample as an option since pixbufs are
|
|
Packit |
a4058c |
expected to be always 8 bits per sample. */
|
|
Packit |
a4058c |
TIFFGetField (tiff, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
|
|
Packit |
a4058c |
if (bits_per_sample > 0) {
|
|
Packit |
a4058c |
gchar str[5];
|
|
Packit |
a4058c |
g_snprintf (str, sizeof (str), "%d", bits_per_sample);
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "bits-per-sample", str);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Set the "orientation" key associated with this image. libtiff
|
|
Packit |
a4058c |
orientation handling is odd, so further processing is required
|
|
Packit |
a4058c |
by higher-level functions based on this tag. If the embedded
|
|
Packit |
a4058c |
orientation tag is 1-4, libtiff flips/mirrors the image as
|
|
Packit |
a4058c |
required, and no client processing is required - so we report
|
|
Packit |
a4058c |
no orientation. Orientations 5-8 require rotations which would
|
|
Packit |
a4058c |
swap the width and height of the image. libtiff does not do this.
|
|
Packit |
a4058c |
Instead it interprets orientations 5-8 the same as 1-4.
|
|
Packit |
a4058c |
See http://bugzilla.remotesensing.org/show_bug.cgi?id=1548.
|
|
Packit |
a4058c |
To correct for this, the client must apply the transform normally
|
|
Packit |
a4058c |
used for orientation 5 to both orientations 5 and 7, and apply
|
|
Packit |
a4058c |
the transform normally used for orientation 7 for both
|
|
Packit |
a4058c |
orientations 6 and 8. Then everythings works out OK! */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFGetField (tiff, TIFFTAG_ORIENTATION, &orientation);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
switch (orientation) {
|
|
Packit |
a4058c |
case 5:
|
|
Packit |
a4058c |
case 7:
|
|
Packit |
a4058c |
transform = 5;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
case 6:
|
|
Packit |
a4058c |
case 8:
|
|
Packit |
a4058c |
transform = 7;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
default:
|
|
Packit |
a4058c |
transform = 0;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (transform > 0 ) {
|
|
Packit |
a4058c |
gchar str[5];
|
|
Packit |
a4058c |
g_snprintf (str, sizeof (str), "%d", transform);
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "orientation", str);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFGetField (tiff, TIFFTAG_COMPRESSION, &codec);
|
|
Packit |
a4058c |
if (codec > 0) {
|
|
Packit |
a4058c |
gchar str[5];
|
|
Packit |
a4058c |
g_snprintf (str, sizeof (str), "%d", codec);
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "compression", str);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Extract embedded ICC profile */
|
|
Packit |
a4058c |
retval = TIFFGetField (tiff, TIFFTAG_ICCPROFILE, &icc_profile_size, &icc_profile);
|
|
Packit |
a4058c |
if (retval == 1) {
|
|
Packit |
a4058c |
icc_profile_base64 = g_base64_encode ((const guchar *) icc_profile, icc_profile_size);
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "icc-profile", icc_profile_base64);
|
|
Packit |
a4058c |
g_free (icc_profile_base64);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
retval = TIFFGetField (tiff, TIFFTAG_RESOLUTIONUNIT, &resolution_unit);
|
|
Packit |
a4058c |
if (retval == 1) {
|
|
Packit |
a4058c |
float x_resolution = 0, y_resolution = 0;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFGetField (tiff, TIFFTAG_XRESOLUTION, &x_resolution);
|
|
Packit |
a4058c |
TIFFGetField (tiff, TIFFTAG_YRESOLUTION, &y_resolution);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
switch (resolution_unit) {
|
|
Packit |
a4058c |
case RESUNIT_INCH:
|
|
Packit |
a4058c |
density_str = g_strdup_printf ("%d", (int) round (x_resolution));
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "x-dpi", density_str);
|
|
Packit |
a4058c |
g_free (density_str);
|
|
Packit |
a4058c |
density_str = g_strdup_printf ("%d", (int) round (y_resolution));
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "y-dpi", density_str);
|
|
Packit |
a4058c |
g_free (density_str);
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
case RESUNIT_CENTIMETER:
|
|
Packit |
a4058c |
density_str = g_strdup_printf ("%d", DPCM_TO_DPI (x_resolution));
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "x-dpi", density_str);
|
|
Packit |
a4058c |
g_free (density_str);
|
|
Packit |
a4058c |
density_str = g_strdup_printf ("%d", DPCM_TO_DPI (y_resolution));
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "y-dpi", density_str);
|
|
Packit |
a4058c |
g_free (density_str);
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context && context->prepare_func)
|
|
Packit |
a4058c |
(* context->prepare_func) (pixbuf, NULL, context->user_data);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!TIFFReadRGBAImageOriented (tiff, width, height, (uint32 *)pixels, ORIENTATION_TOPLEFT, 1)) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("Failed to load RGB data from TIFF file"));
|
|
Packit |
a4058c |
g_object_unref (pixbuf);
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Flag multi-page documents, because this loader only handles the
|
|
Packit |
a4058c |
first page. The client app may wish to warn the user. */
|
|
Packit |
a4058c |
if (TIFFReadDirectory (tiff))
|
|
Packit |
a4058c |
gdk_pixbuf_set_option (pixbuf, "multipage", "yes");
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#if G_BYTE_ORDER == G_BIG_ENDIAN
|
|
Packit |
a4058c |
/* Turns out that the packing used by TIFFRGBAImage depends on
|
|
Packit |
a4058c |
* the host byte order...
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
while (pixels < pixbuf->pixels + bytes) {
|
|
Packit |
a4058c |
uint32 pixel = *(uint32 *)pixels;
|
|
Packit |
a4058c |
int r = TIFFGetR(pixel);
|
|
Packit |
a4058c |
int g = TIFFGetG(pixel);
|
|
Packit |
a4058c |
int b = TIFFGetB(pixel);
|
|
Packit |
a4058c |
int a = TIFFGetA(pixel);
|
|
Packit |
a4058c |
*pixels++ = r;
|
|
Packit |
a4058c |
*pixels++ = g;
|
|
Packit |
a4058c |
*pixels++ = b;
|
|
Packit |
a4058c |
*pixels++ = a;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context && context->update_func)
|
|
Packit |
a4058c |
(* context->update_func) (pixbuf, 0, 0, width, height, context->user_data);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return pixbuf;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Static loader */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static GdkPixbuf *
|
|
Packit |
a4058c |
gdk_pixbuf__tiff_image_load (FILE *f, GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TIFF *tiff;
|
|
Packit |
a4058c |
int fd;
|
|
Packit |
a4058c |
GdkPixbuf *pixbuf;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
g_return_val_if_fail (f != NULL, NULL);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
tiff_set_handlers ();
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
fd = fileno (f);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* On OSF, apparently fseek() works in some on-demand way, so
|
|
Packit |
a4058c |
* the fseek gdk_pixbuf_new_from_file() doesn't work here
|
|
Packit |
a4058c |
* since we are using the raw file descriptor. So, we call lseek() on the fd
|
|
Packit |
a4058c |
* before using it. (#60840)
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
lseek (fd, 0, SEEK_SET);
|
|
Packit |
a4058c |
tiff = TIFFFdOpen (fd, "libpixbuf-tiff", "r");
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!tiff) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
Packit |
a4058c |
_("Failed to open TIFF image"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pixbuf = tiff_image_parse (tiff, NULL, error);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFClose (tiff);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return pixbuf;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Progressive loader */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gpointer
|
|
Packit |
a4058c |
gdk_pixbuf__tiff_image_begin_load (GdkPixbufModuleSizeFunc size_func,
|
|
Packit |
a4058c |
GdkPixbufModulePreparedFunc prepare_func,
|
|
Packit |
a4058c |
GdkPixbufModuleUpdatedFunc update_func,
|
|
Packit |
a4058c |
gpointer user_data,
|
|
Packit |
a4058c |
GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffContext *context;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context = g_new0 (TiffContext, 1);
|
|
Packit |
a4058c |
context->size_func = size_func;
|
|
Packit |
a4058c |
context->prepare_func = prepare_func;
|
|
Packit |
a4058c |
context->update_func = update_func;
|
|
Packit |
a4058c |
context->user_data = user_data;
|
|
Packit |
a4058c |
context->buffer = NULL;
|
|
Packit |
a4058c |
context->allocated = 0;
|
|
Packit |
a4058c |
context->used = 0;
|
|
Packit |
a4058c |
context->pos = 0;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return context;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static tsize_t
|
|
Packit |
a4058c |
tiff_load_read (thandle_t handle, tdata_t buf, tsize_t size)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffContext *context = (TiffContext *)handle;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context->pos + size > context->used)
|
|
Packit |
a4058c |
return 0;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
memcpy (buf, context->buffer + context->pos, size);
|
|
Packit |
a4058c |
context->pos += size;
|
|
Packit |
a4058c |
return size;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static tsize_t
|
|
Packit |
a4058c |
tiff_load_write (thandle_t handle, tdata_t buf, tsize_t size)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
return -1;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static toff_t
|
|
Packit |
a4058c |
tiff_load_seek (thandle_t handle, toff_t offset, int whence)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffContext *context = (TiffContext *)handle;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
switch (whence) {
|
|
Packit |
a4058c |
case SEEK_SET:
|
|
Packit |
a4058c |
if (offset > context->used)
|
|
Packit |
a4058c |
return -1;
|
|
Packit |
a4058c |
context->pos = offset;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
case SEEK_CUR:
|
|
Packit |
a4058c |
if (offset + context->pos >= context->used)
|
|
Packit |
a4058c |
return -1;
|
|
Packit |
a4058c |
context->pos += offset;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
case SEEK_END:
|
|
Packit |
a4058c |
if (offset + context->used > context->used)
|
|
Packit |
a4058c |
return -1;
|
|
Packit |
a4058c |
context->pos = context->used + offset;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
default:
|
|
Packit |
a4058c |
return -1;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
return context->pos;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static int
|
|
Packit |
a4058c |
tiff_load_close (thandle_t context)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
return 0;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static toff_t
|
|
Packit |
a4058c |
tiff_load_size (thandle_t handle)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffContext *context = (TiffContext *)handle;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return context->used;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static int
|
|
Packit |
a4058c |
tiff_load_map_file (thandle_t handle, tdata_t *buf, toff_t *size)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffContext *context = (TiffContext *)handle;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
*buf = context->buffer;
|
|
Packit |
a4058c |
*size = context->used;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return 0;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
tiff_load_unmap_file (thandle_t handle, tdata_t data, toff_t offset)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
gdk_pixbuf__tiff_image_stop_load (gpointer data,
|
|
Packit |
a4058c |
GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffContext *context = data;
|
|
Packit |
a4058c |
TIFF *tiff;
|
|
Packit |
a4058c |
gboolean retval = FALSE;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
g_return_val_if_fail (data != NULL, FALSE);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
tiff_set_handlers ();
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
tiff = TIFFClientOpen ("libtiff-pixbuf", "r", data,
|
|
Packit |
a4058c |
tiff_load_read, tiff_load_write,
|
|
Packit |
a4058c |
tiff_load_seek, tiff_load_close,
|
|
Packit |
a4058c |
tiff_load_size,
|
|
Packit |
a4058c |
tiff_load_map_file, tiff_load_unmap_file);
|
|
Packit |
a4058c |
if (!tiff) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("Failed to load TIFF image"));
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
GdkPixbuf *pixbuf;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pixbuf = tiff_image_parse (tiff, context, error);
|
|
Packit |
a4058c |
retval = (pixbuf != NULL);
|
|
Packit |
a4058c |
g_clear_object (&pixbuf);
|
|
Packit |
a4058c |
/* tiff_image_parse() can return NULL on success in a particular case */
|
|
Packit |
a4058c |
if (!retval && error && !*error) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("Failed to load TIFF image"));
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (tiff)
|
|
Packit |
a4058c |
TIFFClose (tiff);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
g_free (context->buffer);
|
|
Packit |
a4058c |
g_free (context);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return retval;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
make_available_at_least (TiffContext *context, guint needed)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
guchar *new_buffer = NULL;
|
|
Packit |
a4058c |
guint need_alloc;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
need_alloc = context->used + needed;
|
|
Packit |
a4058c |
if (need_alloc > context->allocated) {
|
|
Packit |
a4058c |
guint new_size = 1;
|
|
Packit |
a4058c |
while (new_size < need_alloc)
|
|
Packit |
a4058c |
new_size *= 2;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
new_buffer = g_try_realloc (context->buffer, new_size);
|
|
Packit |
a4058c |
if (new_buffer) {
|
|
Packit |
a4058c |
context->buffer = new_buffer;
|
|
Packit |
a4058c |
context->allocated = new_size;
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
gdk_pixbuf__tiff_image_load_increment (gpointer data, const guchar *buf,
|
|
Packit |
a4058c |
guint size, GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffContext *context = (TiffContext *) data;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
g_return_val_if_fail (data != NULL, FALSE);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
tiff_set_handlers ();
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!make_available_at_least (context, size)) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
|
|
Packit |
a4058c |
_("Insufficient memory to open TIFF file"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
memcpy (context->buffer + context->used, buf, size);
|
|
Packit |
a4058c |
context->used += size;
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
typedef struct {
|
|
Packit |
a4058c |
gchar *buffer;
|
|
Packit |
a4058c |
guint allocated;
|
|
Packit |
a4058c |
guint used;
|
|
Packit |
a4058c |
guint pos;
|
|
Packit |
a4058c |
} TiffSaveContext;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static tsize_t
|
|
Packit |
a4058c |
tiff_save_read (thandle_t handle, tdata_t buf, tsize_t size)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
return -1;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static tsize_t
|
|
Packit |
a4058c |
tiff_save_write (thandle_t handle, tdata_t buf, tsize_t size)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffSaveContext *context = (TiffSaveContext *)handle;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Modify buffer length */
|
|
Packit |
a4058c |
if (context->pos + size > context->used)
|
|
Packit |
a4058c |
context->used = context->pos + size;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Realloc */
|
|
Packit |
a4058c |
if (context->used > context->allocated) {
|
|
Packit |
a4058c |
context->buffer = g_realloc (context->buffer, context->pos + size);
|
|
Packit |
a4058c |
context->allocated = context->used;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Now copy the data */
|
|
Packit |
a4058c |
memcpy (context->buffer + context->pos, buf, size);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Update pos */
|
|
Packit |
a4058c |
context->pos += size;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return size;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static toff_t
|
|
Packit |
a4058c |
tiff_save_seek (thandle_t handle, toff_t offset, int whence)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffSaveContext *context = (TiffSaveContext *)handle;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
switch (whence) {
|
|
Packit |
a4058c |
case SEEK_SET:
|
|
Packit |
a4058c |
context->pos = offset;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
case SEEK_CUR:
|
|
Packit |
a4058c |
context->pos += offset;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
case SEEK_END:
|
|
Packit |
a4058c |
context->pos = context->used + offset;
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
default:
|
|
Packit |
a4058c |
return -1;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
return context->pos;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static int
|
|
Packit |
a4058c |
tiff_save_close (thandle_t context)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
return 0;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static toff_t
|
|
Packit |
a4058c |
tiff_save_size (thandle_t handle)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
return -1;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static TiffSaveContext *
|
|
Packit |
a4058c |
create_save_context (void)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TiffSaveContext *context;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context = g_new (TiffSaveContext, 1);
|
|
Packit |
a4058c |
context->buffer = NULL;
|
|
Packit |
a4058c |
context->allocated = 0;
|
|
Packit |
a4058c |
context->used = 0;
|
|
Packit |
a4058c |
context->pos = 0;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return context;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
free_save_context (TiffSaveContext *context)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
g_free (context->buffer);
|
|
Packit |
a4058c |
g_free (context);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
copy_gray_row (gint *dest,
|
|
Packit |
a4058c |
guchar *src,
|
|
Packit |
a4058c |
gint width,
|
|
Packit |
a4058c |
gboolean has_alpha)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
gint i;
|
|
Packit |
a4058c |
guchar *p;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
p = src;
|
|
Packit |
a4058c |
for (i = 0; i < width; i++) {
|
|
Packit |
a4058c |
int pr, pg, pb, pv;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
pr = *p++;
|
|
Packit |
a4058c |
pg = *p++;
|
|
Packit |
a4058c |
pb = *p++;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (has_alpha) {
|
|
Packit |
a4058c |
int pa = *p++;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Premul alpha to simulate it */
|
|
Packit |
a4058c |
if (pa > 0) {
|
|
Packit |
a4058c |
pr = pr * pa / 255;
|
|
Packit |
a4058c |
pg = pg * pa / 255;
|
|
Packit |
a4058c |
pb = pb * pa / 255;
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
pr = pg = pb = 0;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Calculate value MAX(MAX(r,g),b) */
|
|
Packit |
a4058c |
pv = pr > pg ? pr : pg;
|
|
Packit |
a4058c |
pv = pv > pb ? pv : pb;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
*dest++ = pv;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
gdk_pixbuf__tiff_image_save_to_callback (GdkPixbufSaveFunc save_func,
|
|
Packit |
a4058c |
gpointer user_data,
|
|
Packit |
a4058c |
GdkPixbuf *pixbuf,
|
|
Packit |
a4058c |
gchar **keys,
|
|
Packit |
a4058c |
gchar **values,
|
|
Packit |
a4058c |
GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
TIFF *tiff;
|
|
Packit |
a4058c |
gint width, height, rowstride;
|
|
Packit |
a4058c |
const gchar *bits_per_sample = NULL;
|
|
Packit |
a4058c |
long bps;
|
|
Packit |
a4058c |
const gchar *compression = NULL;
|
|
Packit |
a4058c |
guchar *pixels;
|
|
Packit |
a4058c |
gboolean has_alpha;
|
|
Packit |
a4058c |
gushort alpha_samples[1] = { EXTRASAMPLE_UNASSALPHA };
|
|
Packit |
a4058c |
int y;
|
|
Packit |
a4058c |
TiffSaveContext *context;
|
|
Packit |
a4058c |
gboolean retval;
|
|
Packit |
a4058c |
const gchar *icc_profile = NULL;
|
|
Packit |
a4058c |
const gchar *x_dpi = NULL;
|
|
Packit |
a4058c |
const gchar *y_dpi = NULL;
|
|
Packit |
a4058c |
guint16 codec;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
tiff_set_handlers ();
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context = create_save_context ();
|
|
Packit |
a4058c |
tiff = TIFFClientOpen ("libtiff-pixbuf", "w", context,
|
|
Packit |
a4058c |
tiff_save_read, tiff_save_write,
|
|
Packit |
a4058c |
tiff_save_seek, tiff_save_close,
|
|
Packit |
a4058c |
tiff_save_size,
|
|
Packit |
a4058c |
NULL, NULL);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!tiff) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("Failed to save TIFF image"));
|
|
Packit |
a4058c |
free_save_context (context);
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
|
|
Packit |
a4058c |
pixels = gdk_pixbuf_get_pixels (pixbuf);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
height = gdk_pixbuf_get_height (pixbuf);
|
|
Packit |
a4058c |
width = gdk_pixbuf_get_width (pixbuf);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Guaranteed by the caller. */
|
|
Packit |
a4058c |
g_assert (width >= 0);
|
|
Packit |
a4058c |
g_assert (height >= 0);
|
|
Packit |
a4058c |
g_assert (rowstride >= 0);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_IMAGEWIDTH, width);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_IMAGELENGTH, height);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* libtiff supports a number of 'codecs' such as:
|
|
Packit |
a4058c |
1 None, 2 Huffman, 5 LZW, 7 JPEG, 8 Deflate, see tiff.h */
|
|
Packit |
a4058c |
if (keys && *keys && values && *values) {
|
|
Packit |
a4058c |
guint i = 0;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
while (keys[i]) {
|
|
Packit |
a4058c |
if (g_str_equal (keys[i], "bits-per-sample"))
|
|
Packit |
a4058c |
bits_per_sample = values[i];
|
|
Packit |
a4058c |
else if (g_str_equal (keys[i], "compression"))
|
|
Packit |
a4058c |
compression = values[i];
|
|
Packit |
a4058c |
else if (g_str_equal (keys[i], "icc-profile"))
|
|
Packit |
a4058c |
icc_profile = values[i];
|
|
Packit |
a4058c |
else if (g_str_equal (keys[i], "x-dpi"))
|
|
Packit |
a4058c |
x_dpi = values[i];
|
|
Packit |
a4058c |
else if (g_str_equal (keys[i], "y-dpi"))
|
|
Packit |
a4058c |
y_dpi = values[i];
|
|
Packit |
a4058c |
i++;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Use 8 bits per sample by default, if none was recorded or
|
|
Packit |
a4058c |
specified. */
|
|
Packit |
a4058c |
if (!bits_per_sample)
|
|
Packit |
a4058c |
bits_per_sample = "8";
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Use DEFLATE compression (8) by default, if none was recorded
|
|
Packit |
a4058c |
or specified. */
|
|
Packit |
a4058c |
if (!compression)
|
|
Packit |
a4058c |
compression = "8";
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* libtiff supports a number of 'codecs' such as:
|
|
Packit |
a4058c |
1 None, 2 Huffman, 5 LZW, 7 JPEG, 8 Deflate, see tiff.h */
|
|
Packit |
a4058c |
codec = strtol (compression, NULL, 0);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (TIFFIsCODECConfigured (codec))
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_COMPRESSION, codec);
|
|
Packit |
a4058c |
else {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("TIFF compression doesn't refer to a valid codec."));
|
|
Packit |
a4058c |
retval = FALSE;
|
|
Packit |
a4058c |
goto cleanup;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* We support 1-bit or 8-bit saving */
|
|
Packit |
a4058c |
bps = atol (bits_per_sample);
|
|
Packit |
a4058c |
if (bps == 1) {
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 1);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, 1);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4);
|
|
Packit |
a4058c |
} else if (bps == 8) {
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_BITSPERSAMPLE, 8);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_SAMPLESPERPIXEL, has_alpha ? 4 : 3);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (has_alpha)
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_EXTRASAMPLES, 1, alpha_samples);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (icc_profile != NULL) {
|
|
Packit |
a4058c |
guchar *icc_profile_buf;
|
|
Packit |
a4058c |
gsize icc_profile_size;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* decode from base64 */
|
|
Packit |
a4058c |
icc_profile_buf = g_base64_decode (icc_profile, &icc_profile_size);
|
|
Packit |
a4058c |
if (icc_profile_size < 127) {
|
|
Packit |
a4058c |
g_set_error (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_BAD_OPTION,
|
|
Packit |
a4058c |
_("Color profile has invalid length %d."),
|
|
Packit |
a4058c |
(gint) icc_profile_size);
|
|
Packit |
a4058c |
retval = FALSE;
|
|
Packit |
a4058c |
g_free (icc_profile_buf);
|
|
Packit |
a4058c |
goto cleanup;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_ICCPROFILE, icc_profile_size, icc_profile_buf);
|
|
Packit |
a4058c |
g_free (icc_profile_buf);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
/* The passed bits-per-sample is not supported. */
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("TIFF bits-per-sample doesn't contain a supported value."));
|
|
Packit |
a4058c |
retval = FALSE;
|
|
Packit |
a4058c |
goto cleanup;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_ROWSPERSTRIP, height);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (bps == 1) {
|
|
Packit |
a4058c |
guchar *mono_row;
|
|
Packit |
a4058c |
gint *dith_row_1, *dith_row_2, *dith_row_tmp;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
dith_row_1 = g_new (gint, width);
|
|
Packit |
a4058c |
dith_row_2 = g_new (gint, width);
|
|
Packit |
a4058c |
mono_row = g_malloc ((width + 7) / 8);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
copy_gray_row (dith_row_1, pixels, width, has_alpha);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
for (y = 0; y < height; y++) {
|
|
Packit |
a4058c |
guint x;
|
|
Packit |
a4058c |
gint *p;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
memset (mono_row, 0, (width + 7) / 8);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (y > 0) {
|
|
Packit |
a4058c |
dith_row_tmp = dith_row_1;
|
|
Packit |
a4058c |
dith_row_1 = dith_row_2;
|
|
Packit |
a4058c |
dith_row_2 = dith_row_tmp;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (y < (height - 1))
|
|
Packit |
a4058c |
copy_gray_row (dith_row_2, pixels + ((y + 1) * rowstride), width, has_alpha);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
p = dith_row_1;
|
|
Packit |
a4058c |
for (x = 0; x < width; x++) {
|
|
Packit |
a4058c |
gint p_old, p_new, quant_error;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Apply Floyd-Steinberg dithering */
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
p_old = *p++;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (p_old > 127)
|
|
Packit |
a4058c |
p_new = 255;
|
|
Packit |
a4058c |
else
|
|
Packit |
a4058c |
p_new = 0;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
quant_error = p_old - p_new;
|
|
Packit |
a4058c |
if (x < (width - 1))
|
|
Packit |
a4058c |
dith_row_1[x + 1] += 7 * quant_error / 16;
|
|
Packit |
a4058c |
if (y < (height - 1)) {
|
|
Packit |
a4058c |
if (x > 0)
|
|
Packit |
a4058c |
dith_row_2[x - 1] += 3 * quant_error / 16;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
dith_row_2[x] += 5 * quant_error / 16;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (x < (width - 1))
|
|
Packit |
a4058c |
dith_row_2[x + 1] += quant_error / 16;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (p_new > 127)
|
|
Packit |
a4058c |
mono_row[x / 8] |= (0x1 << (7 - (x % 8)));
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (TIFFWriteScanline (tiff, mono_row, y, 0) == -1)
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
g_free (mono_row);
|
|
Packit |
a4058c |
g_free (dith_row_1);
|
|
Packit |
a4058c |
g_free (dith_row_2);
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
for (y = 0; y < height; y++) {
|
|
Packit |
a4058c |
if (TIFFWriteScanline (tiff, pixels + y * rowstride, y, 0) == -1)
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (y < height) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("Failed to write TIFF data"));
|
|
Packit |
a4058c |
TIFFClose (tiff);
|
|
Packit |
a4058c |
retval = FALSE;
|
|
Packit |
a4058c |
goto cleanup;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (x_dpi != NULL && y_dpi != NULL) {
|
|
Packit |
a4058c |
char *endptr = NULL;
|
|
Packit |
a4058c |
uint16 resolution_unit = RESUNIT_INCH;
|
|
Packit |
a4058c |
float x_dpi_value, y_dpi_value;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
x_dpi_value = strtol (x_dpi, &endptr, 10);
|
|
Packit |
a4058c |
if (x_dpi[0] != '\0' && *endptr != '\0')
|
|
Packit |
a4058c |
x_dpi_value = -1;
|
|
Packit |
a4058c |
if (x_dpi_value <= 0) {
|
|
Packit |
a4058c |
g_set_error (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_BAD_OPTION,
|
|
Packit |
a4058c |
_("TIFF x-dpi must be greater than zero; value '%s' is not allowed."),
|
|
Packit |
a4058c |
x_dpi);
|
|
Packit |
a4058c |
retval = FALSE;
|
|
Packit |
a4058c |
goto cleanup;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
y_dpi_value = strtol (y_dpi, &endptr, 10);
|
|
Packit |
a4058c |
if (y_dpi[0] != '\0' && *endptr != '\0')
|
|
Packit |
a4058c |
y_dpi_value = -1;
|
|
Packit |
a4058c |
if (y_dpi_value <= 0) {
|
|
Packit |
a4058c |
g_set_error (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_BAD_OPTION,
|
|
Packit |
a4058c |
_("TIFF y-dpi must be greater than zero; value '%s' is not allowed."),
|
|
Packit |
a4058c |
y_dpi);
|
|
Packit |
a4058c |
retval = FALSE;
|
|
Packit |
a4058c |
goto cleanup;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_RESOLUTIONUNIT, resolution_unit);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_XRESOLUTION, x_dpi_value);
|
|
Packit |
a4058c |
TIFFSetField (tiff, TIFFTAG_YRESOLUTION, y_dpi_value);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
TIFFClose (tiff);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Now call the callback */
|
|
Packit |
a4058c |
retval = save_func (context->buffer, context->used, error, user_data);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
cleanup:
|
|
Packit |
a4058c |
free_save_context (context);
|
|
Packit |
a4058c |
return retval;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
save_to_file_cb (const gchar *buf,
|
|
Packit |
a4058c |
gsize count,
|
|
Packit |
a4058c |
GError **error,
|
|
Packit |
a4058c |
gpointer data)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
gint bytes;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
while (count > 0) {
|
|
Packit |
a4058c |
bytes = fwrite (buf, sizeof (gchar), count, (FILE *) data);
|
|
Packit |
a4058c |
if (bytes <= 0)
|
|
Packit |
a4058c |
break;
|
|
Packit |
a4058c |
count -= bytes;
|
|
Packit |
a4058c |
buf += bytes;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (count) {
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_FAILED,
|
|
Packit |
a4058c |
_("Couldn't write to TIFF file"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
gdk_pixbuf__tiff_image_save (FILE *f,
|
|
Packit |
a4058c |
GdkPixbuf *pixbuf,
|
|
Packit |
a4058c |
gchar **keys,
|
|
Packit |
a4058c |
gchar **values,
|
|
Packit |
a4058c |
GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
return gdk_pixbuf__tiff_image_save_to_callback (save_to_file_cb,
|
|
Packit |
a4058c |
f, pixbuf, keys,
|
|
Packit |
a4058c |
values, error);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
gdk_pixbuf__tiff_is_save_option_supported (const gchar *option_key)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
if (g_strcmp0 (option_key, "bits-per-sample") == 0 ||
|
|
Packit |
a4058c |
g_strcmp0 (option_key, "compression") == 0 ||
|
|
Packit |
a4058c |
g_strcmp0 (option_key, "icc-profile") == 0 ||
|
|
Packit |
a4058c |
g_strcmp0 (option_key, "x-dpi") == 0 ||
|
|
Packit |
a4058c |
g_strcmp0 (option_key, "y-dpi") == 0)
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#ifndef INCLUDE_tiff
|
|
Packit |
a4058c |
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
|
|
Packit |
a4058c |
#else
|
|
Packit |
a4058c |
#define MODULE_ENTRY(function) void _gdk_pixbuf__tiff_ ## function
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
module->load = gdk_pixbuf__tiff_image_load;
|
|
Packit |
a4058c |
module->begin_load = gdk_pixbuf__tiff_image_begin_load;
|
|
Packit |
a4058c |
module->stop_load = gdk_pixbuf__tiff_image_stop_load;
|
|
Packit |
a4058c |
module->load_increment = gdk_pixbuf__tiff_image_load_increment;
|
|
Packit |
a4058c |
module->save = gdk_pixbuf__tiff_image_save;
|
|
Packit |
a4058c |
module->save_to_callback = gdk_pixbuf__tiff_image_save_to_callback;
|
|
Packit |
a4058c |
module->is_save_option_supported = gdk_pixbuf__tiff_is_save_option_supported;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
static const GdkPixbufModulePattern signature[] = {
|
|
Packit |
a4058c |
{ "MM \x2a", " z ", 100 },
|
|
Packit |
a4058c |
{ "II\x2a ", " z", 100 },
|
|
Packit |
a4058c |
{ "II* \020 CR\002 ", " z zzz z", 0 },
|
|
Packit |
a4058c |
{ NULL, NULL, 0 }
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
static const gchar *mime_types[] = {
|
|
Packit |
a4058c |
"image/tiff",
|
|
Packit |
a4058c |
NULL
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
static const gchar *extensions[] = {
|
|
Packit |
a4058c |
"tiff",
|
|
Packit |
a4058c |
"tif",
|
|
Packit |
a4058c |
NULL
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
info->name = "tiff";
|
|
Packit |
a4058c |
info->signature = (GdkPixbufModulePattern *) signature;
|
|
Packit |
a4058c |
info->description = NC_("image format", "TIFF");
|
|
Packit |
a4058c |
info->mime_types = (gchar **) mime_types;
|
|
Packit |
a4058c |
info->extensions = (gchar **) extensions;
|
|
Packit |
a4058c |
info->flags = GDK_PIXBUF_FORMAT_WRITABLE | GDK_PIXBUF_FORMAT_THREADSAFE;
|
|
Packit |
a4058c |
info->license = "LGPL";
|
|
Packit |
a4058c |
}
|