|
Packit |
a4058c |
/* JPEG 2000 loader
|
|
Packit |
a4058c |
*
|
|
Packit |
a4058c |
* Copyright (c) 2007 Bastien Nocera <hadess@hadess.net>
|
|
Packit |
a4058c |
* Inspired by work by Ben Karel <web+moz@eschew.org>
|
|
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 |
#include "config.h"
|
|
Packit |
a4058c |
#include <stdlib.h>
|
|
Packit |
a4058c |
#include <string.h>
|
|
Packit |
a4058c |
#include <errno.h>
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#include "gdk-pixbuf-private.h"
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#include <jasper/jasper.h>
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
G_MODULE_EXPORT void fill_vtable (GdkPixbufModule * module);
|
|
Packit |
a4058c |
G_MODULE_EXPORT void fill_info (GdkPixbufFormat * info);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
struct jasper_context {
|
|
Packit |
a4058c |
GdkPixbuf *pixbuf;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
GdkPixbufModuleSizeFunc size_func;
|
|
Packit |
a4058c |
GdkPixbufModuleUpdatedFunc updated_func;
|
|
Packit |
a4058c |
GdkPixbufModulePreparedFunc prepared_func;
|
|
Packit |
a4058c |
gpointer user_data;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
jas_stream_t *stream;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
int width, height;
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static void
|
|
Packit |
a4058c |
free_jasper_context (struct jasper_context *context)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
if (!context)
|
|
Packit |
a4058c |
return;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context->stream) {
|
|
Packit |
a4058c |
jas_stream_close (context->stream);
|
|
Packit |
a4058c |
context->stream = NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
g_free (context);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gpointer
|
|
Packit |
a4058c |
jasper_image_begin_load (GdkPixbufModuleSizeFunc size_func,
|
|
Packit |
a4058c |
GdkPixbufModulePreparedFunc prepared_func,
|
|
Packit |
a4058c |
GdkPixbufModuleUpdatedFunc updated_func,
|
|
Packit |
a4058c |
gpointer user_data, GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
struct jasper_context *context;
|
|
Packit |
a4058c |
jas_stream_t *stream;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
jas_init ();
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
stream = jas_stream_memopen (NULL, -1);
|
|
Packit |
a4058c |
if (!stream) {
|
|
Packit |
a4058c |
g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
|
|
Packit |
a4058c |
_("Couldn't allocate memory for stream"));
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context = g_new0 (struct jasper_context, 1);
|
|
Packit |
a4058c |
if (!context)
|
|
Packit |
a4058c |
return NULL;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context->size_func = size_func;
|
|
Packit |
a4058c |
context->updated_func = updated_func;
|
|
Packit |
a4058c |
context->prepared_func = prepared_func;
|
|
Packit |
a4058c |
context->user_data = user_data;
|
|
Packit |
a4058c |
context->width = context->height = -1;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context->stream = stream;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return context;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static const char *
|
|
Packit |
a4058c |
colourspace_to_str (int c)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
switch (c) {
|
|
Packit |
a4058c |
case JAS_CLRSPC_FAM_UNKNOWN:
|
|
Packit |
a4058c |
return "Unknown";
|
|
Packit |
a4058c |
case JAS_CLRSPC_FAM_XYZ:
|
|
Packit |
a4058c |
return "XYZ";
|
|
Packit |
a4058c |
case JAS_CLRSPC_FAM_LAB:
|
|
Packit |
a4058c |
return "LAB";
|
|
Packit |
a4058c |
case JAS_CLRSPC_FAM_GRAY:
|
|
Packit |
a4058c |
return "GRAY";
|
|
Packit |
a4058c |
case JAS_CLRSPC_FAM_RGB:
|
|
Packit |
a4058c |
return "RGB";
|
|
Packit |
a4058c |
case JAS_CLRSPC_FAM_YCBCR:
|
|
Packit |
a4058c |
return "YCbCr";
|
|
Packit |
a4058c |
default:
|
|
Packit |
a4058c |
return "Invalid";
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
jasper_image_try_load (struct jasper_context *context, GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
jas_image_t *raw_image, *image;
|
|
Packit |
a4058c |
int num_components, colourspace_family;
|
|
Packit |
a4058c |
int i, rowstride, shift;
|
|
Packit |
a4058c |
guchar *pixels;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
raw_image = jas_image_decode (context->stream, -1, 0);
|
|
Packit |
a4058c |
if (!raw_image) {
|
|
Packit |
a4058c |
g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
Packit |
a4058c |
_("Couldn't decode image"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context->width == -1 && context->height == -1) {
|
|
Packit |
a4058c |
int width, height;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context->width = width = jas_image_cmptwidth (raw_image, 0);
|
|
Packit |
a4058c |
context->height = height = jas_image_cmptheight (raw_image, 0);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (context->size_func) {
|
|
Packit |
a4058c |
(*context->size_func) (&width, &height, context->user_data);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (width == 0 || height == 0) {
|
|
Packit |
a4058c |
jas_image_destroy(raw_image);
|
|
Packit |
a4058c |
g_set_error_literal (error,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR,
|
|
Packit |
a4058c |
GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
|
|
Packit |
a4058c |
_("Transformed JPEG2000 has zero width or height"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* We only know how to handle grayscale and RGB images */
|
|
Packit |
a4058c |
num_components = jas_image_numcmpts (raw_image);
|
|
Packit |
a4058c |
colourspace_family = jas_clrspc_fam (jas_image_clrspc (raw_image));
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if ((num_components != 3 && num_components != 4 && num_components != 1) ||
|
|
Packit |
a4058c |
(colourspace_family != JAS_CLRSPC_FAM_RGB && colourspace_family != JAS_CLRSPC_FAM_GRAY)) {
|
|
Packit |
a4058c |
jas_image_destroy (raw_image);
|
|
Packit |
a4058c |
g_debug ("Unsupported colourspace %s (num components: %d)",
|
|
Packit |
a4058c |
colourspace_to_str (colourspace_family), num_components);
|
|
Packit |
a4058c |
g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
|
|
Packit |
a4058c |
_("Image type currently not supported"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Apply the colour profile to the image, creating a new one */
|
|
Packit |
a4058c |
if (jas_image_clrspc (raw_image) != JAS_CLRSPC_SRGB) {
|
|
Packit |
a4058c |
jas_cmprof_t *profile;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
profile = jas_cmprof_createfromclrspc (JAS_CLRSPC_SRGB);
|
|
Packit |
a4058c |
if (!profile) {
|
|
Packit |
a4058c |
jas_image_destroy (raw_image);
|
|
Packit |
a4058c |
g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
|
|
Packit |
a4058c |
_("Couldn't allocate memory for color profile"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
image = jas_image_chclrspc (raw_image, profile, JAS_CMXFORM_INTENT_PER);
|
|
Packit |
a4058c |
if (!image) {
|
|
Packit |
a4058c |
jas_image_destroy (raw_image);
|
|
Packit |
a4058c |
g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
|
|
Packit |
a4058c |
_("Couldn't allocate memory for color profile"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
image = raw_image;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (!context->pixbuf) {
|
|
Packit |
a4058c |
int bits_per_sample;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Unfortunately, gdk-pixbuf doesn't support 16 bpp images
|
|
Packit |
a4058c |
* bits_per_sample = jas_image_cmptprec (image, 0);
|
|
Packit |
a4058c |
if (bits_per_sample < 8)
|
|
Packit |
a4058c |
bits_per_sample = 8;
|
|
Packit |
a4058c |
else if (bits_per_sample > 8)
|
|
Packit |
a4058c |
bits_per_sample = 16;
|
|
Packit |
a4058c |
*/
|
|
Packit |
a4058c |
bits_per_sample = 8;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
context->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
|
|
Packit |
a4058c |
FALSE, bits_per_sample,
|
|
Packit |
a4058c |
context->width, context->height);
|
|
Packit |
a4058c |
if (context->pixbuf == NULL) {
|
|
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 JPEG 2000 file"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
if (context->prepared_func)
|
|
Packit |
a4058c |
context->prepared_func (context->pixbuf, NULL, context->user_data);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* We calculate how much we should shift the pixel
|
|
Packit |
a4058c |
* data by to make it fit into our pixbuf */
|
|
Packit |
a4058c |
shift = MAX (jas_image_cmptprec (image, 0) - gdk_pixbuf_get_bits_per_sample (context->pixbuf), 0);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* Loop over the 3 colourspaces */
|
|
Packit |
a4058c |
rowstride = gdk_pixbuf_get_rowstride (context->pixbuf);
|
|
Packit |
a4058c |
pixels = gdk_pixbuf_get_pixels (context->pixbuf);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
for (i = 0; i < num_components; i++) {
|
|
Packit |
a4058c |
jas_matrix_t *matrix;
|
|
Packit |
a4058c |
int j;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
matrix = jas_matrix_create (context->height, context->width);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
/* in libjasper, R is 0, G is 1, etc. we're lucky :)
|
|
Packit |
a4058c |
* but we need to handle the "opacity" channel ourselves */
|
|
Packit |
a4058c |
if (i != 4) {
|
|
Packit |
a4058c |
jas_image_readcmpt (image, i, 0, 0, context->width, context->height, matrix);
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
jas_image_readcmpt (image, JAS_IMAGE_CT_OPACITY, 0, 0, context->width, context->height, matrix);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
for (j = 0; j < context->height; j++) {
|
|
Packit |
a4058c |
int k;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
for (k = 0; k < context->width; k++) {
|
|
Packit |
a4058c |
if (num_components == 3 || num_components == 4) {
|
|
Packit |
a4058c |
pixels[j * rowstride + k * 3 + i] = jas_matrix_get (matrix, j, k) >> shift;
|
|
Packit |
a4058c |
} else {
|
|
Packit |
a4058c |
pixels[j * rowstride + k * 3] =
|
|
Packit |
a4058c |
pixels[j * rowstride + k * 3 + 1] =
|
|
Packit |
a4058c |
pixels[j * rowstride + k * 3 + 2] = jas_matrix_get (matrix, j, k) >> shift;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
/* Update once per line for the last component, otherwise
|
|
Packit |
a4058c |
* we might contain garbage */
|
|
Packit |
a4058c |
if (context->updated_func && (i == num_components - 1) && k != 0) {
|
|
Packit |
a4058c |
context->updated_func (context->pixbuf, 0, j, k, 1, context->user_data);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
jas_matrix_destroy (matrix);
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (image != raw_image)
|
|
Packit |
a4058c |
jas_image_destroy (image);
|
|
Packit |
a4058c |
jas_image_destroy (raw_image);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
jasper_image_stop_load (gpointer data, GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
struct jasper_context *context = (struct jasper_context *) data;
|
|
Packit |
a4058c |
gboolean ret;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
jas_stream_rewind (context->stream);
|
|
Packit |
a4058c |
ret = jasper_image_try_load (context, error);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
free_jasper_context (context);
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return ret;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
static gboolean
|
|
Packit |
a4058c |
jasper_image_load_increment (gpointer data, const guchar *buf, guint size, GError **error)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
struct jasper_context *context = (struct jasper_context *) data;
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
if (jas_stream_write (context->stream, buf, size) < 0) {
|
|
Packit |
a4058c |
g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
|
|
Packit |
a4058c |
_("Couldn't allocate memory to buffer image data"));
|
|
Packit |
a4058c |
return FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
return TRUE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
#ifndef INCLUDE_jasper
|
|
Packit |
a4058c |
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
|
|
Packit |
a4058c |
#else
|
|
Packit |
a4058c |
#define MODULE_ENTRY(function) void _gdk_pixbuf__jasper_ ## function
|
|
Packit |
a4058c |
#endif
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
MODULE_ENTRY (fill_vtable) (GdkPixbufModule * module)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
module->begin_load = jasper_image_begin_load;
|
|
Packit |
a4058c |
module->stop_load = jasper_image_stop_load;
|
|
Packit |
a4058c |
module->load_increment = jasper_image_load_increment;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
MODULE_ENTRY (fill_info) (GdkPixbufFormat * info)
|
|
Packit |
a4058c |
{
|
|
Packit |
a4058c |
static const GdkPixbufModulePattern signature[] = {
|
|
Packit |
a4058c |
{ " jP", "!!!! ", 100 }, /* file begins with 'jP' at offset 4 */
|
|
Packit |
a4058c |
{ "\xff\x4f\xff\x51\x00", NULL, 100 }, /* file starts with FF 4F FF 51 00 */
|
|
Packit |
a4058c |
{ NULL, NULL, 0 }
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
static const gchar *mime_types[] = {
|
|
Packit |
a4058c |
"image/jp2",
|
|
Packit |
a4058c |
"image/jpeg2000",
|
|
Packit |
a4058c |
"image/jpx",
|
|
Packit |
a4058c |
NULL
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
static const gchar *extensions[] = {
|
|
Packit |
a4058c |
"jp2",
|
|
Packit |
a4058c |
"jpc",
|
|
Packit |
a4058c |
"jpx",
|
|
Packit |
a4058c |
"j2k",
|
|
Packit |
a4058c |
"jpf",
|
|
Packit |
a4058c |
NULL
|
|
Packit |
a4058c |
};
|
|
Packit |
a4058c |
|
|
Packit |
a4058c |
info->name = "jpeg2000";
|
|
Packit |
a4058c |
info->signature = (GdkPixbufModulePattern *) signature;
|
|
Packit |
a4058c |
info->description = NC_("image format", "JPEG 2000");
|
|
Packit |
a4058c |
info->mime_types = (gchar **) mime_types;
|
|
Packit |
a4058c |
info->extensions = (gchar **) extensions;
|
|
Packit |
a4058c |
info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
|
|
Packit |
a4058c |
info->license = "LGPL";
|
|
Packit |
a4058c |
info->disabled = FALSE;
|
|
Packit |
a4058c |
}
|
|
Packit |
a4058c |
|