actually set the variable here
[swfdec.git] / swfdec / swfdec_image_decoder.c
blob30ef283d0b95451d2c77e3f6bbb251316eaac267
1 /* Swfdec
2 * Copyright (C) 2007 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include "swfdec_image_decoder.h"
25 #include "swfdec_debug.h"
26 #include "swfdec_image.h"
28 G_DEFINE_TYPE (SwfdecImageDecoder, swfdec_image_decoder, SWFDEC_TYPE_DECODER)
30 static void
31 swfdec_image_decoder_dispose (GObject *object)
33 SwfdecImageDecoder *image = SWFDEC_IMAGE_DECODER (object);
35 if (image->queue) {
36 swfdec_buffer_queue_unref (image->queue);
37 image->queue = NULL;
40 if (image->image) {
41 g_object_unref (image->image);
42 image->image = NULL;
45 G_OBJECT_CLASS (swfdec_image_decoder_parent_class)->dispose (object);
48 static SwfdecStatus
49 swfdec_image_decoder_parse (SwfdecDecoder *dec, SwfdecBuffer *buffer)
51 SwfdecImageDecoder *image = SWFDEC_IMAGE_DECODER (dec);
53 if (image->queue == NULL)
54 image->queue = swfdec_buffer_queue_new ();
55 swfdec_buffer_queue_push (image->queue, buffer);
56 dec->bytes_loaded += buffer->length;
57 if (dec->bytes_loaded > dec->bytes_total)
58 dec->bytes_total = dec->bytes_loaded;
59 return 0;
62 /* FIXME: move this to swfdec_image API? */
63 static gboolean
64 swfdec_image_get_size (SwfdecImage *image, guint *w, guint *h)
66 cairo_surface_t *surface;
68 surface = swfdec_image_create_surface (image, NULL);
69 if (surface == NULL)
70 return FALSE;
72 if (w)
73 *w = cairo_image_surface_get_width (surface);
74 if (h)
75 *h = cairo_image_surface_get_width (surface);
77 cairo_surface_destroy (surface);
79 return TRUE;
82 static SwfdecStatus
83 swfdec_image_decoder_eof (SwfdecDecoder *dec)
85 SwfdecImageDecoder *image = SWFDEC_IMAGE_DECODER (dec);
86 SwfdecBuffer *buffer;
87 guint depth;
89 if (image->queue == NULL)
90 return 0;
91 depth = swfdec_buffer_queue_get_depth (image->queue);
92 if (depth == 0) {
93 swfdec_buffer_queue_unref (image->queue);
94 image->queue = NULL;
95 return SWFDEC_STATUS_ERROR;
97 buffer = swfdec_buffer_queue_pull (image->queue,
98 swfdec_buffer_queue_get_depth (image->queue));
99 swfdec_buffer_queue_unref (image->queue);
100 image->queue = NULL;
101 image->image = swfdec_image_new (buffer);
102 if (!swfdec_image_get_size (image->image, &dec->width, &dec->height))
103 return SWFDEC_STATUS_ERROR;
104 dec->frames_loaded = 1;
105 dec->frames_total = 1;
106 if (image->image->type == SWFDEC_IMAGE_TYPE_JPEG)
107 dec->data_type = SWFDEC_LOADER_DATA_JPEG;
108 else if (image->image->type == SWFDEC_IMAGE_TYPE_PNG)
109 dec->data_type = SWFDEC_LOADER_DATA_PNG;
112 return SWFDEC_STATUS_INIT | SWFDEC_STATUS_IMAGE;
115 static void
116 swfdec_image_decoder_class_init (SwfdecImageDecoderClass *class)
118 GObjectClass *object_class = G_OBJECT_CLASS (class);
119 SwfdecDecoderClass *decoder_class = SWFDEC_DECODER_CLASS (class);
121 object_class->dispose = swfdec_image_decoder_dispose;
123 decoder_class->parse = swfdec_image_decoder_parse;
124 decoder_class->eof = swfdec_image_decoder_eof;
127 static void
128 swfdec_image_decoder_init (SwfdecImageDecoder *s)