2021-09-14 11:48:49 +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/>.
|
|
|
|
*/
|
|
|
|
|
2022-02-06 15:41:33 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-09-14 11:48:49 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "gdkjpegprivate.h"
|
|
|
|
|
2022-09-24 03:33:42 +00:00
|
|
|
#include <glib/gi18n-lib.h>
|
2021-09-14 11:48:49 +00:00
|
|
|
#include "gdktexture.h"
|
|
|
|
#include "gdkmemorytextureprivate.h"
|
2021-09-17 01:25:35 +00:00
|
|
|
|
2021-09-18 18:12:39 +00:00
|
|
|
#include "gdkprofilerprivate.h"
|
|
|
|
|
2021-09-14 11:48:49 +00:00
|
|
|
#include <jpeglib.h>
|
|
|
|
#include <jerror.h>
|
|
|
|
#include <setjmp.h>
|
|
|
|
|
|
|
|
/* {{{ Error handling */
|
|
|
|
|
|
|
|
/* No sigsetjmp on Windows */
|
|
|
|
#ifndef HAVE_SIGSETJMP
|
|
|
|
#define sigjmp_buf jmp_buf
|
|
|
|
#define sigsetjmp(jb, x) setjmp(jb)
|
|
|
|
#define siglongjmp longjmp
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct error_handler_data {
|
|
|
|
struct jpeg_error_mgr pub;
|
|
|
|
sigjmp_buf setjmp_buffer;
|
|
|
|
GError **error;
|
|
|
|
};
|
|
|
|
|
2021-09-19 09:00:07 +00:00
|
|
|
G_GNUC_NORETURN static void
|
2021-09-14 11:48:49 +00:00
|
|
|
fatal_error_handler (j_common_ptr cinfo)
|
|
|
|
{
|
|
|
|
struct error_handler_data *errmgr;
|
|
|
|
char buffer[JMSG_LENGTH_MAX];
|
|
|
|
|
|
|
|
errmgr = (struct error_handler_data *) cinfo->err;
|
|
|
|
|
|
|
|
cinfo->err->format_message (cinfo, buffer);
|
|
|
|
|
|
|
|
if (errmgr->error && *errmgr->error == NULL)
|
|
|
|
g_set_error (errmgr->error,
|
|
|
|
GDK_TEXTURE_ERROR,
|
2021-09-17 01:25:35 +00:00
|
|
|
GDK_TEXTURE_ERROR_CORRUPT_IMAGE,
|
|
|
|
_("Error interpreting JPEG image file (%s)"), buffer);
|
2021-09-14 11:48:49 +00:00
|
|
|
|
|
|
|
siglongjmp (errmgr->setjmp_buffer, 1);
|
|
|
|
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
output_message_handler (j_common_ptr cinfo)
|
|
|
|
{
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
2021-09-14 21:40:29 +00:00
|
|
|
/* {{{ Format conversion */
|
|
|
|
|
|
|
|
static void
|
|
|
|
convert_grayscale_to_rgb (guchar *data,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int stride)
|
|
|
|
{
|
|
|
|
gsize x, y;
|
|
|
|
guchar *dest, *src;
|
|
|
|
|
|
|
|
for (y = 0; y < height; y++)
|
|
|
|
{
|
|
|
|
src = data + width;
|
|
|
|
dest = data + 3 * width;
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
dest -= 3;
|
|
|
|
src -= 1;
|
|
|
|
dest[0] = *src;
|
|
|
|
dest[1] = *src;
|
|
|
|
dest[2] = *src;
|
|
|
|
}
|
|
|
|
data += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
convert_cmyk_to_rgba (guchar *data,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int stride)
|
|
|
|
{
|
|
|
|
gsize x, r;
|
|
|
|
guchar *dest;
|
|
|
|
|
|
|
|
for (r = 0; r < height; r++)
|
|
|
|
{
|
|
|
|
dest = data;
|
|
|
|
for (x = 0; x < width; x++)
|
|
|
|
{
|
|
|
|
int c, m, y, k;
|
|
|
|
|
|
|
|
c = dest[0];
|
|
|
|
m = dest[1];
|
|
|
|
y = dest[2];
|
|
|
|
k = dest[3];
|
|
|
|
dest[0] = k * c / 255;
|
|
|
|
dest[1] = k * m / 255;
|
|
|
|
dest[2] = k * y / 255;
|
|
|
|
dest[3] = 255;
|
|
|
|
dest += 4;
|
|
|
|
}
|
|
|
|
data += stride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* }}} */
|
|
|
|
/* {{{ Public API */
|
2021-09-14 11:48:49 +00:00
|
|
|
|
|
|
|
GdkTexture *
|
|
|
|
gdk_load_jpeg (GBytes *input_bytes,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
struct jpeg_decompress_struct info;
|
|
|
|
struct error_handler_data jerr;
|
2021-09-14 21:40:29 +00:00
|
|
|
guint width, height, stride;
|
2021-09-14 11:48:49 +00:00
|
|
|
unsigned char *data;
|
|
|
|
unsigned char *row[1];
|
|
|
|
GBytes *bytes;
|
|
|
|
GdkTexture *texture;
|
2021-09-14 21:40:29 +00:00
|
|
|
GdkMemoryFormat format;
|
2021-09-18 18:12:39 +00:00
|
|
|
G_GNUC_UNUSED guint64 before = GDK_PROFILER_CURRENT_TIME;
|
2021-09-14 11:48:49 +00:00
|
|
|
|
|
|
|
info.err = jpeg_std_error (&jerr.pub);
|
|
|
|
jerr.pub.error_exit = fatal_error_handler;
|
|
|
|
jerr.pub.output_message = output_message_handler;
|
|
|
|
jerr.error = error;
|
|
|
|
|
|
|
|
if (sigsetjmp (jerr.setjmp_buffer, 1))
|
|
|
|
{
|
|
|
|
jpeg_destroy_decompress (&info);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_create_decompress (&info);
|
|
|
|
|
2022-11-01 00:35:15 +00:00
|
|
|
/* Limit to 1GB to avoid OOM with large images */
|
|
|
|
info.mem->max_memory_to_use = 1024 * 1024 * 1024;
|
2022-08-10 17:25:17 +00:00
|
|
|
|
2021-09-14 11:48:49 +00:00
|
|
|
jpeg_mem_src (&info,
|
|
|
|
g_bytes_get_data (input_bytes, NULL),
|
|
|
|
g_bytes_get_size (input_bytes));
|
|
|
|
|
|
|
|
jpeg_read_header (&info, TRUE);
|
|
|
|
jpeg_start_decompress (&info);
|
|
|
|
|
|
|
|
width = info.output_width;
|
|
|
|
height = info.output_height;
|
|
|
|
|
2021-09-14 21:40:29 +00:00
|
|
|
switch ((int)info.out_color_space)
|
|
|
|
{
|
|
|
|
case JCS_GRAYSCALE:
|
|
|
|
case JCS_RGB:
|
|
|
|
stride = 3 * width;
|
|
|
|
data = g_try_malloc_n (stride, height);
|
|
|
|
format = GDK_MEMORY_R8G8B8;
|
|
|
|
break;
|
|
|
|
case JCS_CMYK:
|
|
|
|
stride = 4 * width;
|
|
|
|
data = g_try_malloc_n (stride, height);
|
|
|
|
format = GDK_MEMORY_R8G8B8A8_PREMULTIPLIED;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_set_error (error,
|
2021-09-17 01:25:35 +00:00
|
|
|
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_UNSUPPORTED_CONTENT,
|
|
|
|
_("Unsupported JPEG colorspace (%d)"), info.out_color_space);
|
2021-09-14 21:40:29 +00:00
|
|
|
jpeg_destroy_decompress (&info);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:48:49 +00:00
|
|
|
if (!data)
|
|
|
|
{
|
2021-09-17 01:25:35 +00:00
|
|
|
g_set_error (error,
|
|
|
|
GDK_TEXTURE_ERROR, GDK_TEXTURE_ERROR_TOO_LARGE,
|
|
|
|
_("Not enough memory for image size %ux%u"), width, height);
|
2021-09-14 11:48:49 +00:00
|
|
|
jpeg_destroy_decompress (&info);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (info.output_scanline < info.output_height)
|
|
|
|
{
|
2021-09-14 21:40:29 +00:00
|
|
|
row[0] = (unsigned char *)(&data[stride * info.output_scanline]);
|
2021-09-14 11:48:49 +00:00
|
|
|
jpeg_read_scanlines (&info, row, 1);
|
|
|
|
}
|
|
|
|
|
2021-09-14 21:40:29 +00:00
|
|
|
switch ((int)info.out_color_space)
|
|
|
|
{
|
|
|
|
case JCS_GRAYSCALE:
|
|
|
|
convert_grayscale_to_rgb (data, width, height, stride);
|
|
|
|
format = GDK_MEMORY_R8G8B8;
|
|
|
|
break;
|
|
|
|
case JCS_RGB:
|
|
|
|
break;
|
|
|
|
case JCS_CMYK:
|
|
|
|
convert_cmyk_to_rgba (data, width, height, stride);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_assert_not_reached ();
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:48:49 +00:00
|
|
|
jpeg_finish_decompress (&info);
|
|
|
|
jpeg_destroy_decompress (&info);
|
|
|
|
|
2021-09-14 21:40:29 +00:00
|
|
|
bytes = g_bytes_new_take (data, stride * height);
|
2021-09-14 11:48:49 +00:00
|
|
|
|
|
|
|
texture = gdk_memory_texture_new (width, height,
|
2021-09-14 21:40:29 +00:00
|
|
|
format,
|
|
|
|
bytes, stride);
|
2021-09-14 11:48:49 +00:00
|
|
|
|
|
|
|
g_bytes_unref (bytes);
|
|
|
|
|
2021-09-18 18:12:39 +00:00
|
|
|
gdk_profiler_end_mark (before, "jpeg load", NULL);
|
|
|
|
|
2021-09-14 11:48:49 +00:00
|
|
|
return texture;
|
|
|
|
}
|
|
|
|
|
2021-09-14 21:40:29 +00:00
|
|
|
GBytes *
|
|
|
|
gdk_save_jpeg (GdkTexture *texture)
|
|
|
|
{
|
|
|
|
struct jpeg_compress_struct info;
|
|
|
|
struct error_handler_data jerr;
|
|
|
|
struct jpeg_error_mgr err;
|
2021-10-17 20:15:53 +00:00
|
|
|
guchar *data;
|
2021-09-14 21:40:29 +00:00
|
|
|
gulong size = 0;
|
|
|
|
guchar *input = NULL;
|
2021-10-17 20:15:53 +00:00
|
|
|
GdkMemoryTexture *memtex = NULL;
|
|
|
|
const guchar *texdata;
|
|
|
|
gsize texstride;
|
2021-09-14 21:40:29 +00:00
|
|
|
guchar *row;
|
2021-10-17 20:15:53 +00:00
|
|
|
int width, height;
|
2021-09-14 21:40:29 +00:00
|
|
|
|
|
|
|
width = gdk_texture_get_width (texture);
|
|
|
|
height = gdk_texture_get_height (texture);
|
|
|
|
|
|
|
|
info.err = jpeg_std_error (&jerr.pub);
|
|
|
|
jerr.pub.error_exit = fatal_error_handler;
|
|
|
|
jerr.pub.output_message = output_message_handler;
|
|
|
|
jerr.error = NULL;
|
|
|
|
|
|
|
|
if (sigsetjmp (jerr.setjmp_buffer, 1))
|
|
|
|
{
|
|
|
|
free (data);
|
|
|
|
g_free (input);
|
|
|
|
jpeg_destroy_compress (&info);
|
2021-10-17 20:15:53 +00:00
|
|
|
g_clear_object (&memtex);
|
2021-09-14 21:40:29 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
info.err = jpeg_std_error (&err);
|
|
|
|
jpeg_create_compress (&info);
|
2022-08-10 17:25:17 +00:00
|
|
|
|
2021-09-14 21:40:29 +00:00
|
|
|
info.image_width = width;
|
|
|
|
info.image_height = height;
|
|
|
|
info.input_components = 3;
|
|
|
|
info.in_color_space = JCS_RGB;
|
|
|
|
|
|
|
|
jpeg_set_defaults (&info);
|
|
|
|
jpeg_set_quality (&info, 75, TRUE);
|
|
|
|
|
2022-08-22 14:48:00 +00:00
|
|
|
info.mem->max_memory_to_use = 300 * 1024 * 1024;
|
2022-08-10 17:25:17 +00:00
|
|
|
|
2021-09-14 21:40:29 +00:00
|
|
|
jpeg_mem_dest (&info, &data, &size);
|
|
|
|
|
2021-10-17 20:15:53 +00:00
|
|
|
memtex = gdk_memory_texture_from_texture (texture,
|
|
|
|
GDK_MEMORY_R8G8B8);
|
|
|
|
texdata = gdk_memory_texture_get_data (memtex);
|
|
|
|
texstride = gdk_memory_texture_get_stride (memtex);
|
2021-09-14 21:40:29 +00:00
|
|
|
|
|
|
|
jpeg_start_compress (&info, TRUE);
|
|
|
|
|
|
|
|
while (info.next_scanline < info.image_height)
|
|
|
|
{
|
2021-10-17 20:15:53 +00:00
|
|
|
row = (guchar *) texdata + info.next_scanline * texstride;
|
2021-09-14 21:40:29 +00:00
|
|
|
jpeg_write_scanlines (&info, &row, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
jpeg_finish_compress (&info);
|
|
|
|
|
2021-10-17 20:15:53 +00:00
|
|
|
g_object_unref (memtex);
|
2021-09-14 21:40:29 +00:00
|
|
|
g_free (input);
|
|
|
|
jpeg_destroy_compress (&info);
|
|
|
|
|
|
|
|
return g_bytes_new_with_free_func (data, size, (GDestroyNotify) free, NULL);
|
|
|
|
}
|
|
|
|
|
2021-09-14 11:48:49 +00:00
|
|
|
/* }}} */
|
|
|
|
|
|
|
|
/* vim:set foldmethod=marker expandtab: */
|