io-qtif.c 20.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* -*- mode: C; c-file-style: "linux" -*- */
/* GdkPixbuf library - QTIF image loader
 *
 * This module extracts image data from QTIF format and uses
 * other GDK pixbuf modules to decode the image data.
 *
 * Copyright (C) 2008 Kevin Peng
 *
 * Authors: Kevin Peng <kevin@zycomtech.com>
 *
 * 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
22
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
23 24 25 26 27
 */


#include "config.h"
#include <errno.h>
28
#include <libintl.h>
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>
#include "gdk-pixbuf.h"
#include "gdk-pixbuf-private.h"

/***
 * Definitions
 */
/* Read buffer size */
#define READ_BUFFER_SIZE    8192

/* Only allow atom of size up to 10MB. */
#define ATOM_SIZE_MAX       100000000

/* Aborts after going to through this many atoms. */
#define QTIF_ATOM_COUNT_MAX 10u

/* QTIF static image data tag "idat". */
#define QTIF_TAG_IDATA      0x69646174u


/***
 * Types
 */
/* QTIF State */
typedef enum {
    STATE_READY,
    STATE_DATA,
    STATE_OTHER
} QTIFState;

/* QTIF Atom Header */
typedef struct {
    guint32 length;
    guint32 tag;
} QtHeader;

/* QTIF loader context */
typedef struct {
    GdkPixbufLoader *loader;
    gpointer        user_data;
    QTIFState       state;
    guint32         run_length;
    gint            atom_count;

    guchar          header_buffer[sizeof(QtHeader)];

    GdkPixbufModuleSizeFunc     size_func;
    GdkPixbufModulePreparedFunc prepare_func;
    GdkPixbufModuleUpdatedFunc  update_func;
    gint            cb_prepare_count;
    gint            cb_update_count;
} QTIFContext;

/***
 * Local function prototypes
 */
static GdkPixbuf *gdk_pixbuf__qtif_image_load (FILE *f, GError **error);
static gpointer gdk_pixbuf__qtif_image_begin_load (GdkPixbufModuleSizeFunc size_func,
                                                   GdkPixbufModulePreparedFunc prepare_func,
                                                   GdkPixbufModuleUpdatedFunc update_func,
                                                   gpointer user_data,
                                                   GError **error);
static gboolean gdk_pixbuf__qtif_image_stop_load (gpointer context, GError **error);
static gboolean gdk_pixbuf__qtif_image_load_increment(gpointer context,
                                                      const guchar *buf, guint size,
                                                      GError **error);
static gboolean gdk_pixbuf__qtif_image_create_loader (QTIFContext *context, GError **error);
static gboolean gdk_pixbuf__qtif_image_free_loader (QTIFContext *context, GError **error);

static void gdk_pixbuf__qtif_cb_size_prepared(GdkPixbufLoader *loader,
                                              gint width,
                                              gint height,
                                              gpointer user_data);
static void gdk_pixbuf__qtif_cb_area_prepared(GdkPixbufLoader *loader, gpointer user_data);
static void gdk_pixbuf__qtif_cb_area_updated(GdkPixbufLoader *loader,
                                             gint x,
                                             gint y,
                                             gint width,
                                             gint height,
                                             gpointer user_data);

/***
 * Function definitions.
 */

/* Load QTIF from a file handler. */
static GdkPixbuf *gdk_pixbuf__qtif_image_load (FILE *f, GError **error)
{
    guint count;

    if(f == NULL)
    {
        g_set_error_literal (error, GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_BAD_OPTION,
                             _("Input file descriptor is NULL."));
        return NULL;
    }

    for(count = QTIF_ATOM_COUNT_MAX; count != 0u; count--)
    {
        QtHeader hdr;
        size_t rd;

        /* Read QtHeader. */
        rd = fread(&hdr, 1, sizeof(QtHeader), f);
        if(rd != sizeof(QtHeader))
        {
            g_set_error_literal(error, GDK_PIXBUF_ERROR,
                                GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                _("Failed to read QTIF header"));
            return NULL;
        }

        hdr.length = GUINT32_FROM_BE(hdr.length) - sizeof(QtHeader);
        if(hdr.length > ATOM_SIZE_MAX)
        {
            g_set_error(error, GDK_PIXBUF_ERROR,
                        GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Matthias Clasen's avatar
Matthias Clasen committed
150 151 152 153
                        ngettext (  "QTIF atom size too large (%d byte)",
                                    "QTIF atom size too large (%d bytes)",
                                    hdr.length),
                        hdr.length);
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
            return NULL;
        }

        switch(GUINT32_FROM_BE(hdr.tag))
        {
        case QTIF_TAG_IDATA: /* "idat" data atom. */
            {
                /* Load image using GdkPixbufLoader. */
                guchar *buf;
                GdkPixbufLoader *loader;
                GdkPixbuf *pixbuf = NULL;
                GError *tmp = NULL;

                /* Allocate read buffer. */
                buf = g_try_malloc(READ_BUFFER_SIZE);
                if(buf == NULL)
                {
                    g_set_error(error, GDK_PIXBUF_ERROR,
                                GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Matthias Clasen's avatar
Matthias Clasen committed
173 174 175 176 177
                                ngettext ( "Failed to allocate %d byte for file read buffer",
                                           "Failed to allocate %d bytes for file read buffer",
                                           READ_BUFFER_SIZE
                                ),
                                READ_BUFFER_SIZE);
178 179 180 181 182 183 184 185 186
                    return NULL;
                }

                /* Create GdkPixbufLoader. */
                loader = gdk_pixbuf_loader_new();
                if(loader == NULL)
                {
                    g_set_error(error, GDK_PIXBUF_ERROR,
                                GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Matthias Clasen's avatar
Matthias Clasen committed
187 188 189 190
                                ngettext (  "QTIF atom size too large (%d byte)",
                                            "QTIF atom size too large (%d bytes)",
                                            hdr.length),
                                hdr.length);
191 192 193 194 195 196
                    goto clean_up;
                }

                /* Read atom data. */
                while(hdr.length != 0u)
                {
197
                    if(fread(buf, 1, rd, f) != rd)
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
                    {
                        g_set_error(error, GDK_PIXBUF_ERROR,
                                    GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                    _("File error when reading QTIF atom: %s"), g_strerror(errno));
                        break;
                    }

                    if(!gdk_pixbuf_loader_write(loader, buf, rd, &tmp))
                    {
                        g_propagate_error (error, tmp);
                        break;
                    }
                    hdr.length -= rd;
                }

clean_up:
                /* Release loader */
                if(loader != NULL)
                {
                    gdk_pixbuf_loader_close(loader, NULL);
                    pixbuf = gdk_pixbuf_loader_get_pixbuf(loader);
                    if(pixbuf != NULL)
                    {
                        g_object_ref(pixbuf);
                    }
                    g_object_unref(loader);
                }
                if(buf != NULL)
                {
                    g_free(buf);
                }
                return pixbuf;
            }

        default:
            /* Skip any other types of atom. */
            if(!fseek(f, hdr.length, SEEK_CUR))
            {
                g_set_error(error, GDK_PIXBUF_ERROR,
                            GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
Matthias Clasen's avatar
Matthias Clasen committed
238 239 240 241
                            ngettext (  "Failed to skip the next %d byte with seek().",
                                        "Failed to skip the next %d bytes with seek().",
                                        hdr.length),
                            hdr.length);
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
                return NULL;
            }
            break;
        }
    }
    return NULL;
}

/* Incremental load begin. */
static gpointer gdk_pixbuf__qtif_image_begin_load (GdkPixbufModuleSizeFunc size_func,
                                                   GdkPixbufModulePreparedFunc prepare_func,
                                                   GdkPixbufModuleUpdatedFunc update_func,
                                                   gpointer user_data,
                                                   GError **error)
{
    QTIFContext *context;

    /* Create context struct. */
    context = g_new0(QTIFContext, 1);
    if(context == NULL)
    {
        g_set_error_literal (error, GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
Matthias Clasen's avatar
Matthias Clasen committed
265
                             _("Failed to allocate QTIF context structure."));
266 267 268 269 270 271 272 273 274 275 276 277 278
        return NULL;
    }

    /* Fill context parameters. */
    context->loader = NULL;
    context->user_data = user_data;
    context->state = STATE_READY;
    context->run_length = 0u;
    context->atom_count = QTIF_ATOM_COUNT_MAX;
    context->size_func = size_func;
    context->prepare_func = prepare_func;
    context->update_func = update_func;

279
    return context;
280 281 282 283 284 285
}

/* Incremental load clean up. */
static gboolean gdk_pixbuf__qtif_image_stop_load (gpointer data, GError **error)
{
    QTIFContext *context = (QTIFContext *)data;
286
    gboolean ret = TRUE;
287 288 289 290 291 292 293 294 295 296 297 298

    if(context->loader != NULL)
    {
        GError *tmp = NULL;

        ret = gdk_pixbuf__qtif_image_free_loader(context, &tmp);
        if(!ret)
        {
            g_propagate_error (error, tmp);
        }
    }
    g_free(context);
299 300

    return ret;
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
}

/* Create a new GdkPixbufLoader and connect to its signals. */
static gboolean gdk_pixbuf__qtif_image_create_loader (QTIFContext *context, GError **error)
{
    GError *tmp = NULL;

    if(context == NULL)
    {
        return FALSE;
    }

    /* Free existing loader. */
    if(context->loader != NULL)
    {
        gdk_pixbuf__qtif_image_free_loader(context, &tmp);
    }

    /* Create GdkPixbufLoader object. */
    context->loader = gdk_pixbuf_loader_new();
    if(context->loader == NULL)
    {
        g_set_error_literal (error, GDK_PIXBUF_ERROR,
                             GDK_PIXBUF_ERROR_FAILED,
                             _("Failed to create GdkPixbufLoader object."));
        return FALSE;
    }

    /* Connect signals. */
    context->cb_prepare_count = 0;
    context->cb_update_count = 0;
    if(context->size_func != NULL)
    {
        g_signal_connect(context->loader, "size-prepared",
                         G_CALLBACK(gdk_pixbuf__qtif_cb_size_prepared),
                         context);
    }
    if(context->prepare_func != NULL)
    {
        g_signal_connect(context->loader, "area-prepared",
                         G_CALLBACK(gdk_pixbuf__qtif_cb_area_prepared),
                         context);
    }
    if(context->update_func != NULL)
    {
        g_signal_connect(context->loader, "area-updated",
                         G_CALLBACK(gdk_pixbuf__qtif_cb_area_updated),
                         context);
    }
    return TRUE;
}

/* Free the GdkPixbufLoader and perform callback if haven't done so. */
static gboolean gdk_pixbuf__qtif_image_free_loader (QTIFContext *context, GError **error)
{
    GdkPixbuf *pixbuf;
    GError *tmp = NULL;
    gboolean ret;

    if((context == NULL) || (context->loader == NULL))
    {
        return FALSE;
    }

    /* Close GdkPixbufLoader. */
    ret = gdk_pixbuf_loader_close(context->loader, &tmp);
    if(!ret)
    {
        g_propagate_error (error, tmp);
    }


    /* Get GdkPixbuf from GdkPixbufLoader. */
    pixbuf = gdk_pixbuf_loader_get_pixbuf(context->loader);
    if(pixbuf != NULL)
    {
        g_object_ref(pixbuf);
    }

    /* Free GdkPixbufLoader. */
    g_object_ref(context->loader);
    context->loader = NULL;

    if(pixbuf != NULL)
    {
        /* Callback functions should be called for at least once. */
        if((context->prepare_func != NULL) && (context->cb_prepare_count == 0))
        {
            (context->prepare_func)(pixbuf, NULL, context->user_data);
        }

        if((context->update_func != NULL) && (context->cb_update_count == 0))
        {
            gint width;
            gint height;

            width = gdk_pixbuf_get_width(pixbuf);
            height = gdk_pixbuf_get_height(pixbuf);
            (context->update_func)(pixbuf, 0, 0, width, height, context->user_data);
        }

        /* Free GdkPixbuf (callback function should ref it). */
        g_object_ref(pixbuf);
    }

    return ret;
}


/* Incrementally load the next chunk of data. */
static gboolean gdk_pixbuf__qtif_image_load_increment (gpointer data,
                                                       const guchar *buf, guint size,
                                                       GError **error)
{
    QTIFContext *context = (QTIFContext *)data;
    GError *tmp = NULL;
    gboolean ret = TRUE; /* Return TRUE for insufficient data. */

    while(ret && (size != 0u))
    {
        switch(context->state)
        {
        case STATE_READY:
            /* Abort if we have seen too mant atoms. */
            if(context->atom_count == 0u)
            {
                g_set_error_literal (error, GDK_PIXBUF_ERROR,
                                     GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                     _("Failed to find an image data atom."));
                return FALSE;
            }
            context->atom_count--;

            /* Copy to header buffer in context, in case supplied data is not enough. */
            while(context->run_length < sizeof(QtHeader))
            {
                context->header_buffer[context->run_length] = *buf;
                context->run_length++;
                buf++;
                size--;
            }

            /* Parse buffer as QT header. */
            if(context->run_length == sizeof(QtHeader))
            {
                QtHeader *hdr = (QtHeader *)context->header_buffer;
                context->run_length = GUINT32_FROM_BE(hdr->length) - sizeof(QtHeader);

                /* Atom max size check. */
                if(context->run_length > ATOM_SIZE_MAX)
                {
                    g_set_error(error, GDK_PIXBUF_ERROR,
Matthias Clasen's avatar
Matthias Clasen committed
453 454 455 456 457
                                       GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                       ngettext (  "QTIF atom size too large (%d byte)",
                                                   "QTIF atom size too large (%d bytes)",
                                                    hdr->length),
                                       hdr->length);
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 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
                    return FALSE;
                }

                /* Set state according to atom type. */
                if(GUINT32_FROM_BE(hdr->tag) == QTIF_TAG_IDATA)
                {
                    GError *tmp = NULL;

                    context->state = STATE_DATA;

                    /* Create GdkPixbufLoader for this image data. */
                    ret = gdk_pixbuf__qtif_image_create_loader(context, &tmp);
                    if(!ret)
                    {
                        g_propagate_error (error, tmp);
                    }
                }
                else
                {
                    context->state = STATE_OTHER;
                }
            }
            break;

        default: /* Both STATE_DATA and STATE_OTHER will come here. */
            /* Check for atom boundary. */
            if(context->run_length > size)
            {
                /* Supply image data to GdkPixbufLoader if in STATE_DATA. */
                if(context->state == STATE_DATA)
                {
                    tmp = NULL;
                    ret = gdk_pixbuf_loader_write(context->loader, buf, size, &tmp);
                    if(!ret && (error != NULL) && (*error == NULL))
                    {
                        g_propagate_error (error, tmp);
                    }
                }
                context->run_length -= size;
                size = 0u;
            }
            else
            {
                /* Supply image data to GdkPixbufLoader if in STATE_DATA. */
                if(context->state == STATE_DATA)
                {
                    gboolean r;

                    /* Here we should have concluded a complete image atom. */
                    tmp = NULL;
                    ret = gdk_pixbuf_loader_write(context->loader, buf, context->run_length, &tmp);
                    if(!ret && (error != NULL) && (*error == NULL))
                    {
                        g_propagate_error (error, tmp);
                    }

                    /* Free GdkPixbufLoader and handle callback. */
                    tmp = NULL;
                    r = gdk_pixbuf__qtif_image_free_loader(context, &tmp);
                    if(!r)
                    {
                        if((error != NULL) && (*error == NULL))
                        {
                            g_propagate_error (error, tmp);
                        }
                        ret = FALSE;
                    }
                }
                buf = &buf[context->run_length];
                size -= context->run_length;
                context->run_length = 0u;
                context->state = STATE_READY;
            }
            break;
        }
    }

    return ret;
}

/* Event handlers */
static void gdk_pixbuf__qtif_cb_size_prepared(GdkPixbufLoader *loader,
                                              gint width,
                                              gint height,
                                              gpointer user_data)
{
    QTIFContext *context = (QTIFContext *)user_data;
    if((context != NULL) && (context->size_func != NULL))
    {
        (context->size_func)(&width, &height, context->user_data);
        context->cb_prepare_count++;
    }
}

static void gdk_pixbuf__qtif_cb_area_prepared(GdkPixbufLoader *loader, gpointer user_data)
{
    QTIFContext *context = (QTIFContext *)user_data;
    if((loader != NULL) && (context != NULL) && (context->prepare_func != NULL))
    {
        GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(context->loader);
        (context->prepare_func)(pixbuf, NULL, context->user_data);
        context->cb_update_count++;
    }
}

static void gdk_pixbuf__qtif_cb_area_updated(GdkPixbufLoader *loader,
                                             gint x,
                                             gint y,
                                             gint width,
                                             gint height,
                                             gpointer user_data)
{
    QTIFContext *context = (QTIFContext *)user_data;
    if((loader != NULL) && (context != NULL) && (context->update_func != NULL))
    {
        GdkPixbuf *pixbuf = gdk_pixbuf_loader_get_pixbuf(context->loader);
        (context->update_func)(pixbuf, x, y, width, height, context->user_data);
    }
}


#ifndef INCLUDE_qtif
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
#else
#define MODULE_ENTRY(function) void _gdk_pixbuf__qtif_ ## function
#endif

MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
{
    module->load = gdk_pixbuf__qtif_image_load;
    module->begin_load = gdk_pixbuf__qtif_image_begin_load;
    module->stop_load = gdk_pixbuf__qtif_image_stop_load;
    module->load_increment = gdk_pixbuf__qtif_image_load_increment;
}

MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
{
595
    static const GdkPixbufModulePattern signature[] = {
596 597 598 599
        { "abcdidsc", "xxxx    ", 100 },
        { "abcdidat", "xxxx    ", 100 },
        { NULL, NULL, 0 }
    };
600
    static const gchar *mime_types[] = {
601 602 603 604
        "image/x-quicktime",
        "image/qtif",
        NULL
    };
605
    static const gchar *extensions[] = {
606 607 608 609 610 611
        "qtif",
        "qif",
        NULL
    };

    info->name = "qtif";
612
    info->signature = (GdkPixbufModulePattern *) signature;
613
    info->description = N_("The QTIF image format");
614 615
    info->mime_types = (gchar **) mime_types;
    info->extensions = (gchar **) extensions;
616 617 618 619
    info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
    info->license = "LGPL";
}