2021-09-10 16:47:14 +00:00
|
|
|
/* GDK - The GIMP Drawing Kit
|
|
|
|
* Copyright (C) 2021 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gdktiffprivate.h"
|
|
|
|
|
2022-09-24 03:33:42 +00:00
|
|
|
#include <glib/gi18n-lib.h>
|
2021-09-22 00:01:41 +00:00
|
|
|
#include "gdkmemoryformatprivate.h"
|
2021-09-17 01:25:35 +00:00
|
|
|
#include "gdkmemorytextureprivate.h"
|
2021-09-18 18:12:39 +00:00
|
|
|
#include "gdkprofilerprivate.h"
|
2021-09-10 16:47:14 +00:00
|
|
|
#include "gdktexture.h"
|
|
|
|
#include "gdktextureprivate.h"
|
2021-09-17 01:25:35 +00:00
|
|
|
|
2021-09-10 16:47:14 +00:00
|
|
|
#include <tiffio.h>
|
|
|
|
|
|
|
|
/* Our main interest in tiff as an image format is that it is
|
|
|
|
* flexible enough to save all our texture formats without
|
|
|
|
* lossy conversions.
|
|
|
|
*
|
|
|
|
* The loader isn't meant to be a very versatile. It just aims
|
|
|
|
* to load the subset that we're saving ourselves. For anything
|
|
|
|
* else, we fall back to TIFFRGBAImage, which is the same api
|
|
|
|
* that gdk-pixbuf uses.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* {{{ IO handling */
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
GBytes **out_bytes;
|
|
|
|
gchar *data;
|
|
|
|
gsize size;
|
|
|
|
gsize position;
|
|
|
|
} TiffIO;
|
|
|
|
|
|
|
|
static void
|
|
|
|
tiff_io_warning (const char *module,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap) G_GNUC_PRINTF(2, 0);
|
|
|
|
static void
|
|
|
|
tiff_io_warning (const char *module,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
tiff_io_error (const char *module,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap) G_GNUC_PRINTF(2, 0);
|
|
|
|
static void
|
|
|
|
tiff_io_error (const char *module,
|
|
|
|
const char *fmt,
|
|
|
|
va_list ap)
|
|
|
|
{
|
|
|
|
g_logv (G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
static tsize_t
|
|
|
|
tiff_io_no_read (thandle_t handle,
|
|
|
|
tdata_t buffer,
|
|
|
|
tsize_t size)
|
|
|
|
{
|
|
|
|
return (tsize_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static tsize_t
|
|
|
|
tiff_io_read (thandle_t handle,
|
|
|
|
tdata_t buffer,
|
|
|
|
tsize_t size)
|
|
|
|
{
|
|
|
|
TiffIO *io = (TiffIO *) handle;
|
|
|
|
gsize read;
|
|
|
|
|
|
|
|
if (io->position >= io->size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
read = MIN (size, io->size - io->position);
|
|
|
|
|
|
|
|
memcpy (buffer, io->data + io->position, read);
|
|
|
|
io->position += read;
|
|
|
|
|
|
|
|
return (tsize_t) read;
|
|
|
|
}
|
|
|
|
|
|
|
|
static tsize_t
|
|
|
|
tiff_io_no_write (thandle_t handle,
|
|
|
|
tdata_t buffer,
|
|
|
|
tsize_t size)
|
|
|
|
{
|
|
|
|
return (tsize_t) -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static tsize_t
|
|
|
|
tiff_io_write (thandle_t handle,
|
|
|
|
tdata_t buffer,
|
|
|
|
tsize_t size)
|
|
|
|
{
|
|
|
|
TiffIO *io = (TiffIO *) handle;
|
|
|
|
|
|
|
|
if (io->position > io->size ||
|
|
|
|
io->size - io->position < size)
|
|
|
|
{
|
|
|
|
io->size = io->position + size;
|
|
|
|
io->data = g_realloc (io->data, io->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy (io->data + io->position, buffer, size);
|
|
|
|
io->position += size;
|
|
|
|
|
|
|
|
return (tsize_t) size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static toff_t
|
|
|
|
tiff_io_seek (thandle_t handle,
|
|
|
|
toff_t offset,
|
|
|
|
int whence)
|
|
|
|
{
|
|
|
|
TiffIO *io = (TiffIO *) handle;
|
|
|
|
|
|
|
|
switch (whence)
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
return -1;
|
|
|
|
case SEEK_SET:
|
|
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
|
|
offset += io->position;
|
|
|
|
break;
|
|
|
|
case SEEK_END:
|
|
|
|
offset += io->size;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (offset < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
io->position = offset;
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tiff_io_close (thandle_t handle)
|
|
|
|
{
|
|
|
|
TiffIO *io = (TiffIO *) handle;
|
|
|
|
|
|
|
|
if (io->out_bytes)
|
|
|
|
*io->out_bytes = g_bytes_new_take (io->data, io->size);
|
|
|
|
|
|
|
|
g_free (io);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static toff_t
|
|
|
|
tiff_io_get_file_size (thandle_t handle)
|
|
|
|
{
|
|
|
|
TiffIO *io = (TiffIO *) handle;
|
|
|
|
|
|
|
|
return io->size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TIFF *
|
|
|
|
tiff_open_read (GBytes *bytes)
|
|
|
|
{
|
|
|
|
TiffIO *io;
|
|
|
|
|
|
|
|
TIFFSetWarningHandler ((TIFFErrorHandler) tiff_io_warning);
|
|
|
|
TIFFSetErrorHandler ((TIFFErrorHandler) tiff_io_error);
|
|
|
|
|
|
|
|
io = g_new0 (TiffIO, 1);
|
|
|
|
|
|
|
|
io->data = (char *) g_bytes_get_data (bytes, &io->size);
|
|
|
|
|
|
|
|
return TIFFClientOpen ("GTK-read", "r",
|
|
|
|
(thandle_t) io,
|
|
|
|
tiff_io_read,
|
|
|
|
tiff_io_no_write,
|
|
|
|
tiff_io_seek,
|
|
|
|
tiff_io_close,
|
|
|
|
tiff_io_get_file_size,
|
|
|
|
NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static TIFF *
|
|
|
|
tiff_open_write (GBytes **result)
|
|
|
|
{
|
|
|
|
TiffIO *io;
|
|
|
|
|
|
|
|
TIFFSetWarningHandler ((TIFFErrorHandler) tiff_io_warning);
|
|
|
|
TIFFSetErrorHandler ((TIFFErrorHandler) tiff_io_error);
|
|
|
|
|
|
|
|
io = g_new0 (TiffIO, 1);
|
|
|
|
|
|
|
|
io->out_bytes = result;
|
|
|
|
|
|
|
|
return TIFFClientOpen ("GTK-write", "w",
|
|
|
|
(thandle_t) io,
|
|
|
|
tiff_io_no_read,
|
|
|
|
tiff_io_write,
|
|
|
|
tiff_io_seek,
|
|
|
|
tiff_io_close,
|
|
|
|
tiff_io_get_file_size,
|
|
|
|
NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ Public API */
|
|
|
|
|
2021-10-17 12:27:03 +00:00
|
|
|
typedef struct _FormatData FormatData;
|
|
|
|
struct _FormatData {
|
2021-09-10 16:47:14 +00:00
|
|
|
GdkMemoryFormat format;
|
|
|
|
guint16 bits_per_sample;
|
|
|
|
guint16 samples_per_pixel;
|
|
|
|
guint16 sample_format;
|
2021-10-17 12:27:03 +00:00
|
|
|
guint16 alpha_samples;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const FormatData format_data[] = {
|
|
|
|
[GDK_MEMORY_B8G8R8A8_PREMULTIPLIED] = { GDK_MEMORY_R8G8B8A8_PREMULTIPLIED, 8, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_ASSOCALPHA },
|
|
|
|
[GDK_MEMORY_A8R8G8B8_PREMULTIPLIED] = { GDK_MEMORY_R8G8B8A8_PREMULTIPLIED, 8, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_ASSOCALPHA },
|
|
|
|
[GDK_MEMORY_R8G8B8A8_PREMULTIPLIED] = { GDK_MEMORY_R8G8B8A8_PREMULTIPLIED, 8, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_ASSOCALPHA },
|
|
|
|
[GDK_MEMORY_B8G8R8A8] = { GDK_MEMORY_R8G8B8A8, 8, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_UNASSALPHA },
|
|
|
|
[GDK_MEMORY_A8R8G8B8] = { GDK_MEMORY_R8G8B8A8, 8, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_UNASSALPHA },
|
|
|
|
[GDK_MEMORY_R8G8B8A8] = { GDK_MEMORY_R8G8B8A8, 8, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_UNASSALPHA },
|
|
|
|
[GDK_MEMORY_A8B8G8R8] = { GDK_MEMORY_R8G8B8A8, 8, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_UNASSALPHA },
|
2021-12-13 22:51:24 +00:00
|
|
|
[GDK_MEMORY_R8G8B8] = { GDK_MEMORY_R8G8B8, 8, 3, SAMPLEFORMAT_UINT, 0 },
|
2021-10-17 12:27:03 +00:00
|
|
|
[GDK_MEMORY_B8G8R8] = { GDK_MEMORY_R8G8B8, 8, 3, SAMPLEFORMAT_UINT, 0 },
|
|
|
|
[GDK_MEMORY_R16G16B16] = { GDK_MEMORY_R16G16B16, 16, 3, SAMPLEFORMAT_UINT, 0 },
|
|
|
|
[GDK_MEMORY_R16G16B16A16_PREMULTIPLIED] = { GDK_MEMORY_R16G16B16A16_PREMULTIPLIED, 16, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_ASSOCALPHA },
|
|
|
|
[GDK_MEMORY_R16G16B16A16] = { GDK_MEMORY_R16G16B16A16, 16, 4, SAMPLEFORMAT_UINT, EXTRASAMPLE_UNASSALPHA },
|
|
|
|
[GDK_MEMORY_R16G16B16_FLOAT] = { GDK_MEMORY_R16G16B16_FLOAT, 16, 3, SAMPLEFORMAT_IEEEFP, 0 },
|
|
|
|
[GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED] = { GDK_MEMORY_R16G16B16A16_FLOAT_PREMULTIPLIED, 16, 4, SAMPLEFORMAT_IEEEFP, EXTRASAMPLE_ASSOCALPHA },
|
|
|
|
[GDK_MEMORY_R16G16B16A16_FLOAT] = { GDK_MEMORY_R16G16B16A16_FLOAT, 16, 4, SAMPLEFORMAT_IEEEFP, EXTRASAMPLE_UNASSALPHA },
|
|
|
|
[GDK_MEMORY_R32G32B32_FLOAT] = { GDK_MEMORY_R32G32B32_FLOAT, 32, 3, SAMPLEFORMAT_IEEEFP, 0 },
|
|
|
|
[GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED] = { GDK_MEMORY_R32G32B32A32_FLOAT_PREMULTIPLIED, 32, 4, SAMPLEFORMAT_IEEEFP, EXTRASAMPLE_ASSOCALPHA },
|
|
|
|
[GDK_MEMORY_R32G32B32A32_FLOAT] = { GDK_MEMORY_R32G32B32A32_FLOAT, 32, 4, SAMPLEFORMAT_IEEEFP, EXTRASAMPLE_UNASSALPHA },
|
2021-09-10 16:47:14 +00:00
|
|
|
};
|
|
|
|
|
2021-10-17 12:27:03 +00:00
|
|
|
/* if this fails, somebody forgot to add formats above */
|
|
|
|
G_STATIC_ASSERT (G_N_ELEMENTS (format_data) == GDK_MEMORY_N_FORMATS);
|
|
|
|
|
2021-09-10 16:47:14 +00:00
|
|
|
GBytes *
|
|
|
|
gdk_save_tiff (GdkTexture *texture)
|
|
|
|
{
|
|
|
|
TIFF *tif;
|
|
|
|
int width, height, stride;
|
|
|
|
const guchar *line;
|
|
|
|
const guchar *data;
|
|
|
|
GBytes *result = NULL;
|
2021-10-17 12:27:03 +00:00
|
|
|
GdkMemoryTexture *memtex;
|
2021-09-10 16:47:14 +00:00
|
|
|
GdkMemoryFormat format;
|
2021-10-17 12:27:03 +00:00
|
|
|
const FormatData *fdata = NULL;
|
2021-09-10 16:47:14 +00:00
|
|
|
|
|
|
|
tif = tiff_open_write (&result);
|
|
|
|
|
|
|
|
width = gdk_texture_get_width (texture);
|
|
|
|
height = gdk_texture_get_height (texture);
|
2021-10-07 04:19:41 +00:00
|
|
|
format = gdk_texture_get_format (texture);
|
2021-10-17 12:27:03 +00:00
|
|
|
fdata = &format_data[format];
|
2021-09-10 16:47:14 +00:00
|
|
|
|
2021-10-17 12:27:03 +00:00
|
|
|
if (fdata == NULL)
|
|
|
|
fdata = &format_data[0];
|
2021-09-10 16:47:14 +00:00
|
|
|
|
|
|
|
TIFFSetField (tif, TIFFTAG_SOFTWARE, "GTK");
|
|
|
|
TIFFSetField (tif, TIFFTAG_IMAGEWIDTH, width);
|
|
|
|
TIFFSetField (tif, TIFFTAG_IMAGELENGTH, height);
|
2021-10-17 12:27:03 +00:00
|
|
|
TIFFSetField (tif, TIFFTAG_BITSPERSAMPLE, fdata->bits_per_sample);
|
|
|
|
TIFFSetField (tif, TIFFTAG_SAMPLESPERPIXEL, fdata->samples_per_pixel);
|
|
|
|
TIFFSetField (tif, TIFFTAG_SAMPLEFORMAT, fdata->sample_format);
|
2021-09-10 16:47:14 +00:00
|
|
|
TIFFSetField (tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
|
|
|
|
TIFFSetField (tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
|
2021-10-17 12:27:03 +00:00
|
|
|
if (fdata->alpha_samples)
|
|
|
|
TIFFSetField (tif, TIFFTAG_EXTRASAMPLES, 1, &fdata->alpha_samples);
|
2021-09-10 16:47:14 +00:00
|
|
|
|
|
|
|
TIFFSetField (tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
|
|
|
|
TIFFSetField (tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
|
|
|
|
|
2021-10-17 12:27:03 +00:00
|
|
|
memtex = gdk_memory_texture_from_texture (texture, fdata->format);
|
|
|
|
data = gdk_memory_texture_get_data (memtex);
|
|
|
|
stride = gdk_memory_texture_get_stride (memtex);
|
|
|
|
|
2021-09-10 16:47:14 +00:00
|
|
|
line = (const guchar *)data;
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
if (TIFFWriteScanline (tif, (void *)line, y, 0) == -1)
|
|
|
|
{
|
|
|
|
TIFFClose (tif);
|
2021-10-17 12:27:03 +00:00
|
|
|
g_object_unref (memtex);
|
2021-09-10 16:47:14 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
line += stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
TIFFFlushData (tif);
|
|
|
|
TIFFClose (tif);
|
|
|
|
|
|
|
|
g_assert (result);
|
|
|
|
|
2021-10-17 12:27:03 +00:00
|
|
|
g_object_unref (memtex);
|
2021-09-10 16:47:14 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GdkTexture *
|
|
|
|
load_fallback (TIFF *tif,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
int width, height;
|
|
|
|
guchar *data;
|
|
|
|
GBytes *bytes;
|
|
|
|
GdkTexture *texture;
|
|
|
|
|
|
|
|
TIFFGetField (tif, TIFFTAG_IMAGEWIDTH, &width);
|
|
|
|
TIFFGetField (tif, TIFFTAG_IMAGELENGTH, &height);
|
|
|
|
|
|
|
|
data = g_malloc (width * height * 4);
|
|
|
|
|
2021-09-18 07:00:16 +00:00
|
|
|
if (!TIFFReadRGBAImageOriented (tif, width, height, (guint32 *)data, ORIENTATION_TOPLEFT, 1))
|
2021-09-10 16:47:14 +00:00
|
|
|
{
|
|
|
|
g_set_error_literal (error,
|
|
|
|
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
|
2021-09-17 01:25:35 +00:00
|
|
|
_("Failed to load RGB data from TIFF file"));
|
2021-09-10 16:47:14 +00:00
|
|
|
g_free (data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes = g_bytes_new_take (data, width * height * 4);
|
|
|
|
|
|
|
|
texture = gdk_memory_texture_new (width, height,
|
|
|
|
GDK_MEMORY_R8G8B8A8_PREMULTIPLIED,
|
|
|
|
bytes,
|
|
|
|
width * 4);
|
|
|
|
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
GdkTexture *
|
|
|
|
gdk_load_tiff (GBytes *input_bytes,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
TIFF *tif;
|
|
|
|
guint16 samples_per_pixel;
|
|
|
|
guint16 bits_per_sample;
|
|
|
|
guint16 photometric;
|
|
|
|
guint16 planarconfig;
|
|
|
|
guint16 sample_format;
|
|
|
|
guint16 orientation;
|
|
|
|
guint32 width, height;
|
2021-10-17 12:27:03 +00:00
|
|
|
guint16 alpha_samples;
|
2021-09-10 16:47:14 +00:00
|
|
|
GdkMemoryFormat format;
|
|
|
|
guchar *data, *line;
|
|
|
|
gsize stride;
|
|
|
|
int bpp;
|
|
|
|
GBytes *bytes;
|
|
|
|
GdkTexture *texture;
|
2021-09-18 18:12:39 +00:00
|
|
|
G_GNUC_UNUSED gint64 before = GDK_PROFILER_CURRENT_TIME;
|
2021-09-10 16:47:14 +00:00
|
|
|
|
|
|
|
tif = tiff_open_read (input_bytes);
|
2021-12-13 22:51:24 +00:00
|
|
|
if (!tif)
|
|
|
|
{
|
|
|
|
g_set_error_literal (error,
|
|
|
|
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
|
|
|
|
_("Could not load TIFF data"));
|
|
|
|
return NULL;
|
|
|
|
}
|
2021-09-10 16:47:14 +00:00
|
|
|
|
|
|
|
TIFFSetDirectory (tif, 0);
|
|
|
|
|
|
|
|
TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);
|
|
|
|
TIFFGetFieldDefaulted (tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
|
|
|
|
TIFFGetFieldDefaulted (tif, TIFFTAG_SAMPLEFORMAT, &sample_format);
|
|
|
|
TIFFGetFieldDefaulted (tif, TIFFTAG_PHOTOMETRIC, &photometric);
|
|
|
|
TIFFGetFieldDefaulted (tif, TIFFTAG_PLANARCONFIG, &planarconfig);
|
|
|
|
TIFFGetFieldDefaulted (tif, TIFFTAG_ORIENTATION, &orientation);
|
|
|
|
TIFFGetFieldDefaulted (tif, TIFFTAG_IMAGEWIDTH, &width);
|
|
|
|
TIFFGetFieldDefaulted (tif, TIFFTAG_IMAGELENGTH, &height);
|
|
|
|
|
|
|
|
if (samples_per_pixel == 4)
|
|
|
|
{
|
|
|
|
guint16 extra;
|
|
|
|
guint16 *extra_types;
|
|
|
|
|
2021-10-22 18:45:27 +00:00
|
|
|
if (TIFFGetField (tif, TIFFTAG_EXTRASAMPLES, &extra, &extra_types))
|
|
|
|
alpha_samples = extra_types[0];
|
|
|
|
else
|
2021-10-17 12:27:03 +00:00
|
|
|
alpha_samples = 0;
|
2021-09-10 16:47:14 +00:00
|
|
|
|
2021-10-17 12:27:03 +00:00
|
|
|
if (alpha_samples != 0 && alpha_samples != EXTRASAMPLE_ASSOCALPHA && alpha_samples != EXTRASAMPLE_UNASSALPHA)
|
2021-09-10 16:47:14 +00:00
|
|
|
{
|
|
|
|
texture = load_fallback (tif, error);
|
|
|
|
TIFFClose (tif);
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
}
|
2021-10-17 12:27:03 +00:00
|
|
|
else
|
|
|
|
alpha_samples = 0;
|
2021-09-10 16:47:14 +00:00
|
|
|
|
2021-10-17 12:27:03 +00:00
|
|
|
for (format = 0; format < G_N_ELEMENTS (format_data); format++)
|
2021-09-10 16:47:14 +00:00
|
|
|
{
|
2021-10-17 12:27:03 +00:00
|
|
|
/* not a native format */
|
|
|
|
if (format_data[format].format != format)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (format_data[format].sample_format == sample_format &&
|
|
|
|
format_data[format].bits_per_sample == bits_per_sample &&
|
|
|
|
format_data[format].samples_per_pixel == samples_per_pixel &&
|
|
|
|
format_data[format].alpha_samples == alpha_samples)
|
2021-09-10 16:47:14 +00:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-17 12:27:03 +00:00
|
|
|
if (format == G_N_ELEMENTS(format_data) ||
|
2021-09-10 16:47:14 +00:00
|
|
|
photometric != PHOTOMETRIC_RGB ||
|
|
|
|
planarconfig != PLANARCONFIG_CONTIG ||
|
|
|
|
TIFFIsTiled (tif) ||
|
|
|
|
orientation != ORIENTATION_TOPLEFT)
|
|
|
|
{
|
|
|
|
texture = load_fallback (tif, error);
|
|
|
|
TIFFClose (tif);
|
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
stride = width * gdk_memory_format_bytes_per_pixel (format);
|
|
|
|
|
|
|
|
g_assert (TIFFScanlineSize (tif) == stride);
|
|
|
|
|
|
|
|
data = g_try_malloc_n (height, stride);
|
|
|
|
if (!data)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
2021-09-17 01:25:35 +00:00
|
|
|
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_TOO_LARGE,
|
|
|
|
_("Not enough memory for image size %ux%u"), width, height);
|
2021-09-10 16:47:14 +00:00
|
|
|
TIFFClose (tif);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
line = data;
|
|
|
|
for (int y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
if (TIFFReadScanline (tif, line, y, 0) == -1)
|
|
|
|
{
|
|
|
|
g_set_error (error,
|
|
|
|
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
|
2021-09-17 01:25:35 +00:00
|
|
|
_("Reading data failed at row %d"), y);
|
2021-09-10 16:47:14 +00:00
|
|
|
TIFFClose (tif);
|
|
|
|
g_free (data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
line += stride;
|
|
|
|
}
|
|
|
|
|
|
|
|
bpp = gdk_memory_format_bytes_per_pixel (format);
|
|
|
|
bytes = g_bytes_new_take (data, width * height * bpp);
|
|
|
|
|
|
|
|
texture = gdk_memory_texture_new (width, height,
|
|
|
|
format,
|
|
|
|
bytes, width * bpp);
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
|
|
|
TIFFClose (tif);
|
|
|
|
|
2021-09-18 18:12:39 +00:00
|
|
|
if (GDK_PROFILER_IS_RUNNING)
|
|
|
|
{
|
|
|
|
gint64 end = GDK_PROFILER_CURRENT_TIME;
|
|
|
|
if (end - before > 500000)
|
|
|
|
gdk_profiler_add_mark (before, end - before, "tiff load", NULL);
|
|
|
|
}
|
|
|
|
|
2021-09-10 16:47:14 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* vim:set foldmethod=marker expandtab: */
|