1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
*
* f-jpeg-utils.c: Utility functions for JPEG files.
*
* Copyright (C) 2001 Red Hat Inc.
* Copyright (C) 2001 The Free Software Foundation, Inc.
* Copyright (C) 2003 Ettore Perazzoli
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Authors: Alexander Larsson <alexl@redhat.com>
* Ettore Perazzoli <ettore@perazzoli.org>
* Paolo Bacchilega <paolo.bacch@tin.it>
*/
#include <config.h>
#include "f-jpeg-utils.h"
#include <setjmp.h>
#include <stdio.h>
/* libjpeg likes to define these symbols, that we already define in config.h. */
#undef HAVE_STDDEF_H
#undef HAVE_STDLIB_H
/* HAVE_LIBEXIF is required by jpeg-data.h. */
#define HAVE_LIBEXIF
#include "libjpegtran/jpeg-data.h"
#include "libjpegtran/jpegtran.h"
#include <glib.h>
#include <gio/gio.h>
#include <libexif/exif-data.h>
#include <libexif/exif-content.h>
#include <libexif/exif-entry.h>
#define BUFFER_SIZE 8192
/* FIME these strings should actually be translated */
#define _(x) x
typedef struct {
struct jpeg_source_mgr pub; /* public fields */
GInputStream *stream;
JOCTET buffer[BUFFER_SIZE];
} Source;
typedef struct {
struct jpeg_error_mgr pub;
jmp_buf setjmp_buffer;
} ErrorHandlerData;
static void
fatal_error_handler (j_common_ptr cinfo)
{
ErrorHandlerData *data;
data = (ErrorHandlerData *) cinfo->err;
longjmp (data->setjmp_buffer, 1);
}
static void
output_message_handler (j_common_ptr cinfo)
{
/* If we don't supply this handler, libjpeg reports errors
* directly to stderr.
*/
}
static void
init_source (j_decompress_ptr cinfo)
{
}
static gboolean
fill_input_buffer (j_decompress_ptr cinfo)
{
Source *src;
gssize nbytes;
GError *err = NULL;
src = (Source *) cinfo->src;
nbytes = g_input_stream_read (src->stream,
src->buffer,
G_N_ELEMENTS (src->buffer),
NULL,
&err);
if (err != NULL || nbytes == 0) {
/* return a fake EOI marker so we will eventually terminate */
src->buffer[0] = (JOCTET) 0xFF;
src->buffer[1] = (JOCTET) JPEG_EOI;
nbytes = 2;
}
src->pub.next_input_byte = src->buffer;
src->pub.bytes_in_buffer = nbytes;
return TRUE;
}
static void
skip_input_data (j_decompress_ptr cinfo, long num_bytes)
{
Source *src;
src = (Source *) cinfo->src;
if (num_bytes > 0) {
while (num_bytes > (long) src->pub.bytes_in_buffer) {
num_bytes -= (long) src->pub.bytes_in_buffer;
fill_input_buffer (cinfo);
}
src->pub.next_input_byte += (size_t) num_bytes;
src->pub.bytes_in_buffer -= (size_t) num_bytes;
}
}
static void
term_source (j_decompress_ptr cinfo)
{
}
static void
gio_src (j_decompress_ptr cinfo, GInputStream *stream)
{
Source *src;
if (cinfo->src == NULL) { /* first time for this JPEG object? */
cinfo->src = &(g_new (Source, 1))->pub;
}
src = (Source *) cinfo->src;
src->pub.init_source = init_source;
src->pub.fill_input_buffer = fill_input_buffer;
src->pub.skip_input_data = skip_input_data;
src->pub.resync_to_restart = jpeg_resync_to_restart; /* use default method */
src->pub.term_source = term_source;
src->stream = stream;
src->pub.bytes_in_buffer = 0; /* forces fill_input_buffer on first read */
src->pub.next_input_byte = NULL; /* until buffer loaded */
}
static void
gio_src_free (j_decompress_ptr cinfo)
{
g_free (cinfo->src);
}
static int
calculate_divisor (int width,
int height,
int target_width,
int target_height)
{
if (width/8 > target_width && height/8 > target_height) {
return 8;
}
if (width/4 > target_width && height/4 > target_height) {
return 4;
}
if (width/2 > target_width && height/2 > target_height) {
return 2;
}
return 1;
}
static void
free_buffer (guchar *pixels, gpointer data)
{
g_free (pixels);
}
static GdkPixbuf *
do_load_internal (const char *path,
int target_width, int target_height,
int *original_width_return, int *original_height_return)
{
struct jpeg_decompress_struct cinfo;
ErrorHandlerData jerr;
unsigned char *lines[1];
guchar * volatile buffer;
guchar * volatile pixels;
guchar *ptr;
GFile *uri;
GFileInputStream *input_stream;
GError *err = NULL;
unsigned int i;
g_return_val_if_fail (g_path_is_absolute (path), NULL);
if (original_width_return != NULL)
*original_width_return = 0;
if (original_height_return != NULL)
*original_height_return = 0;
uri = g_file_new_for_path (path);
input_stream = g_file_read (uri, NULL, &err);
g_object_unref (uri);
if (err != NULL)
return NULL;
cinfo.err = jpeg_std_error (&jerr.pub);
jerr.pub.error_exit = fatal_error_handler;
jerr.pub.output_message = output_message_handler;
buffer = NULL;
pixels = NULL;
if (setjmp (jerr.setjmp_buffer)) {
/* Handle a JPEG error. */
jpeg_destroy_decompress (&cinfo);
g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, NULL);
g_free (buffer);
g_free (pixels);
return NULL;
}
jpeg_create_decompress (&cinfo);
gio_src (&cinfo, G_INPUT_STREAM (input_stream));
jpeg_read_header (&cinfo, TRUE);
if (target_width != 0 && target_height != 0) {
cinfo.scale_num = 1;
cinfo.scale_denom = calculate_divisor (cinfo.image_width,
cinfo.image_height,
target_width,
target_height);
cinfo.dct_method = JDCT_FASTEST;
cinfo.do_fancy_upsampling = FALSE;
jpeg_start_decompress (&cinfo);
pixels = g_malloc (cinfo.output_width * cinfo.output_height * 3);
ptr = pixels;
if (cinfo.num_components == 1) {
/* Allocate extra buffer for grayscale data */
buffer = g_malloc (cinfo.output_width);
lines[0] = buffer;
} else {
lines[0] = pixels;
}
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_scanlines (&cinfo, lines, 1);
if (cinfo.num_components == 1) {
/* Convert grayscale to rgb */
for (i = 0; i < cinfo.output_width; i++) {
ptr[i*3] = buffer[i];
ptr[i*3+1] = buffer[i];
ptr[i*3+2] = buffer[i];
}
ptr += cinfo.output_width * 3;
} else {
lines[0] += cinfo.output_width * 3;
}
}
g_free (buffer);
buffer = NULL;
jpeg_finish_decompress (&cinfo);
}
jpeg_destroy_decompress (&cinfo);
gio_src_free (&cinfo);
g_input_stream_close (G_INPUT_STREAM (input_stream), NULL, NULL);
if (original_width_return != NULL)
*original_width_return = cinfo.image_width;
if (original_height_return != NULL)
*original_height_return = cinfo.image_height;
if (target_width == 0 || target_height == 0)
return NULL;
return gdk_pixbuf_new_from_data (pixels, GDK_COLORSPACE_RGB, FALSE, 8,
cinfo.output_width,
cinfo.output_height,
cinfo.output_width * 3,
free_buffer, NULL);
}
/* Public API. */
//GdkPixbuf *
//f_load_scaled_jpeg (const char *path,
// int target_width,
// int target_height,
// int *original_width_return,
// int *original_height_return)
//{
// return do_load_internal (path, target_width, target_height, original_width_return, original_height_return);
//}
/* FIXME: Error reporting in this function sucks... */
void
f_get_jpeg_size (const char *path,
int *width_return,
int *height_return)
{
do_load_internal (path, 0, 0, width_return, height_return);
}
void
f_save_jpeg_exif (const char *filename, ExifData *exif_data)
{
JPEGData *jdata;
g_warning ("exif = %p", exif_data);
jdata = jpeg_data_new_from_file (filename);
if (jdata == NULL) {
g_warning ("unable to parse jpeg file");
return;
}
if (exif_data == NULL) {
g_warning ("missing exif data");
}
jpeg_data_set_exif_data (jdata, exif_data);
jpeg_data_save_file (jdata, filename);
jpeg_data_unref (jdata);
}
/* Implementation of non-lossy JPEG file transformations, based on GThumb code
by Paolo Bacchilega. */
static gboolean
swap_fields (ExifContent *content,
ExifTag tag1,
ExifTag tag2)
{
ExifEntry *entry1;
ExifEntry *entry2;
unsigned char *data;
unsigned int size;
entry1 = exif_content_get_entry (content, tag1);
if (entry1 == NULL)
return FALSE;
entry2 = exif_content_get_entry (content, tag2);
if (entry2 == NULL)
return FALSE;
data = entry1->data;
size = entry1->size;
entry1->data = entry2->data;
entry1->size = entry2->size;
entry2->data = data;
entry2->size = size;
return TRUE;
}
static void
swap_xy_exif_fields (const char *filename)
{
JPEGData *jdata;
ExifData *edata;
unsigned int i;
jdata = jpeg_data_new_from_file (filename);
if (jdata == NULL)
return;
edata = jpeg_data_get_exif_data (jdata);
if (edata == NULL) {
jpeg_data_unref (jdata);
return;
}
for (i = 0; i < EXIF_IFD_COUNT; i++) {
ExifContent *content = edata->ifd[i];
if ((content == NULL) || (content->count == 0))
continue;
swap_fields (content,
EXIF_TAG_RELATED_IMAGE_WIDTH,
EXIF_TAG_RELATED_IMAGE_LENGTH);
swap_fields (content,
EXIF_TAG_IMAGE_WIDTH,
EXIF_TAG_IMAGE_LENGTH);
swap_fields (content,
EXIF_TAG_PIXEL_X_DIMENSION,
EXIF_TAG_PIXEL_Y_DIMENSION);
swap_fields (content,
EXIF_TAG_X_RESOLUTION,
EXIF_TAG_Y_RESOLUTION);
swap_fields (content,
EXIF_TAG_FOCAL_PLANE_X_RESOLUTION,
EXIF_TAG_FOCAL_PLANE_Y_RESOLUTION);
}
jpeg_data_save_file (jdata, filename);
exif_data_unref (edata);
jpeg_data_unref (jdata);
}
gboolean
f_transform_jpeg (const char *source_path,
const char *destination_path,
FJpegTransform transform,
char **error_message_return)
{
JXFORM_CODE jpegtran_transform;
*error_message_return = NULL;
/* Since the error reporting of jpegtran sucks, check at least that the source
file exists. */
if (! g_file_test (source_path, G_FILE_TEST_EXISTS)) {
if (error_message_return != NULL)
*error_message_return = g_strdup (_("File not found"));
return FALSE;
}
switch (transform) {
case F_JPEG_TRANSFORM_ROTATE_90:
jpegtran_transform = JXFORM_ROT_90;
break;
case F_JPEG_TRANSFORM_ROTATE_180:
jpegtran_transform = JXFORM_ROT_180;
break;
case F_JPEG_TRANSFORM_ROTATE_270:
jpegtran_transform = JXFORM_ROT_270;
break;
case F_JPEG_TRANSFORM_FLIP_H:
jpegtran_transform = JXFORM_FLIP_H;
break;
case F_JPEG_TRANSFORM_FLIP_V:
jpegtran_transform = JXFORM_FLIP_V;
break;
default:
g_warning ("%s(): unknown transform type %d", G_STRFUNC, transform);
if (error_message_return != NULL)
*error_message_return = g_strdup_printf (_("Unknown transform type %d"), transform);
return FALSE;
}
/* FIXME: Need to improve the error reporting here. */
/* Also note how the jpegtran() args are not const-safe. */
if (jpegtran ((char *) source_path, (char *) destination_path, jpegtran_transform) != 0) {
if (error_message_return != NULL)
*error_message_return = g_strdup (_("Operation failed"));
return FALSE;
}
/* FIXME: ...And here we have no error reporting at all. Yuck. */
if (transform == F_JPEG_TRANSFORM_ROTATE_270
|| transform == F_JPEG_TRANSFORM_ROTATE_90)
swap_xy_exif_fields (destination_path);
return TRUE;
}
|