1 /* GdkPixbuf library - Wireless Bitmap image loader
3 * Copyright (C) 2000 Red Hat, Inc.
5 * Authors: Elliot Lee <sopwith@redhat.com
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
28 * Since this is based off the libgd implementation, no extended headers implemented (not required for a WAP client)
37 #include "gdk-pixbuf-private.h"
38 #include "gdk-pixbuf-io.h"
42 /* Progressive loading */
44 struct wbmp_progressive_state {
45 ModulePreparedNotifyFunc prepared_func;
46 ModuleUpdatedNotifyFunc updated_func;
49 gboolean need_type : 1;
50 gboolean need_header : 1;
51 gboolean need_width : 1;
52 gboolean need_height : 1;
53 gboolean needmore : 1;
54 gboolean call_progressive_updates : 1;
56 guchar last_buf[16]; /* Just needs to be big enough to hold the largest datum requestable via 'getin' */
60 int width, height, curx, cury;
62 GdkPixbuf *pixbuf; /* Our "target" */
66 gdk_pixbuf__wbmp_image_begin_load(ModuleSizeFunc size_func,
67 ModulePreparedNotifyFunc prepared_func,
68 ModuleUpdatedNotifyFunc updated_func,
72 static gboolean gdk_pixbuf__wbmp_image_stop_load(gpointer data, GError **error);
73 static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
79 * func - called when we have pixmap created (but no image data)
80 * user_data - passed as arg 1 to func
81 * return context (opaque to user)
85 gdk_pixbuf__wbmp_image_begin_load(ModuleSizeFunc size_func,
86 ModulePreparedNotifyFunc prepared_func,
87 ModuleUpdatedNotifyFunc updated_func,
91 struct wbmp_progressive_state *context;
93 context = g_new0(struct wbmp_progressive_state, 1);
94 context->prepared_func = prepared_func;
95 context->updated_func = updated_func;
96 context->user_data = user_data;
98 context->needmore = context->need_type = context->need_header = context->need_width = context->need_height = TRUE;
99 context->call_progressive_updates = TRUE;
100 context->pixbuf = NULL;
102 return (gpointer) context;
106 * context - returned from image_begin_load
108 * free context, unref gdk_pixbuf
110 static gboolean gdk_pixbuf__wbmp_image_stop_load(gpointer data,
113 struct wbmp_progressive_state *context =
114 (struct wbmp_progressive_state *) data;
116 /* FIXME this thing needs to report errors if
117 * we have unused image data
120 g_return_val_if_fail(context != NULL, TRUE);
122 g_object_unref(context->pixbuf);
130 getin(struct wbmp_progressive_state *context, const guchar **buf, guint *buf_size, guchar *ptr, int datum_size)
132 int last_num, buf_num;
134 if((context->last_len + *buf_size) < datum_size)
137 /* We know we can pull it out of there */
138 last_num = MIN(datum_size, context->last_len);
139 buf_num = MIN(datum_size-last_num, *buf_size);
140 memcpy(ptr, context->last_buf, last_num);
141 memcpy(ptr+last_num, *buf, buf_num);
143 context->last_len -= last_num;
144 if(context->last_len)
145 memmove(context->last_buf, context->last_buf+last_num, context->last_len);
146 *buf_size -= buf_num;
153 save_rest(struct wbmp_progressive_state *context, const guchar *buf, guint buf_size)
155 if(buf_size > (sizeof(context->last_buf) - context->last_len))
158 memcpy(context->last_buf+context->last_len, buf, buf_size);
159 context->last_len += buf_size;
165 get_mbi(struct wbmp_progressive_state *context, const guchar **buf, guint *buf_size, int *val)
174 rv = getin(context, buf, buf_size, intbuf+n, 1);
178 *val |= intbuf[n] & 0x7F;
180 } while(n < sizeof(intbuf) && (intbuf[n-1] & 0x80));
183 if(!rv || (intbuf[n-1] & 0x80))
185 rv = save_rest(context, intbuf, n);
188 g_error("Couldn't save_rest of intbuf");
196 * context - from image_begin_load
197 * buf - new image data
198 * size - length of new image data
200 * append image data onto inrecrementally built output image
202 static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
204 guint size, GError **error)
206 struct wbmp_progressive_state *context =
207 (struct wbmp_progressive_state *) data;
212 if(context->need_type)
216 bv = getin(context, &buf, &size, &val, 1);
220 context->need_type = FALSE;
223 else if(context->need_header)
227 bv = getin(context, &buf, &size, &val, 1);
230 /* We skip over the extended header - val is unused */
232 context->need_header = FALSE;
235 else if(context->need_width)
237 bv = get_mbi(context, &buf, &size, &context->width);
239 context->need_width = FALSE;
241 if (context->width <= 0) {
244 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
245 _("Image has zero width"));
252 else if(context->need_height)
254 bv = get_mbi(context, &buf, &size, &context->height);
257 context->need_height = FALSE;
259 if (context->height <= 0) {
262 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
263 _("Image has zero height"));
268 context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, context->width, context->height);
270 if (!context->pixbuf) {
273 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
274 _("Not enough memory to load image"));
279 if(context->prepared_func)
280 context->prepared_func(context->pixbuf, NULL, context->user_data);
283 else if(context->needmore)
286 first_row = context->cury;
287 for( ; context->cury < context->height; context->cury++, context->curx = 0)
289 for( ; context->curx < context->width; context->curx += 8)
294 bv = getin(context, &buf, &size, &byte, 1);
298 ptr = context->pixbuf->pixels + context->pixbuf->rowstride * context->cury + context->curx * 3;
299 for(xoff = 7; xoff >= 0; xoff--, ptr += 3)
303 if (context->curx + (7 - xoff) == context->width)
311 ptr[0] = ptr[1] = ptr[2] = pixval;
315 context->needmore = FALSE;
318 if(context->updated_func)
319 context->updated_func(context->pixbuf, 0, first_row, context->width, context->cury - first_row + 1,
323 bv = FALSE; /* Nothing left to do, stop feeding me data! */
328 bv = save_rest(context, buf, size);
332 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
333 _("Couldn't save the rest"));
342 gdk_pixbuf__wbmp_fill_vtable (GdkPixbufModule *module)
344 module->begin_load = gdk_pixbuf__wbmp_image_begin_load;
345 module->stop_load = gdk_pixbuf__wbmp_image_stop_load;
346 module->load_increment = gdk_pixbuf__wbmp_image_load_increment;