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)
35 #include "gdk-pixbuf-private.h"
36 #include "gdk-pixbuf-io.h"
40 /* Progressive loading */
42 struct wbmp_progressive_state {
43 ModulePreparedNotifyFunc prepared_func;
44 ModuleUpdatedNotifyFunc updated_func;
47 gboolean need_type : 1;
48 gboolean need_header : 1;
49 gboolean need_width : 1;
50 gboolean need_height : 1;
51 gboolean needmore : 1;
52 gboolean call_progressive_updates : 1;
54 guchar last_buf[16]; /* Just needs to be big enough to hold the largest datum requestable via 'getin' */
58 int width, height, curx, cury;
60 GdkPixbuf *pixbuf; /* Our "target" */
64 gdk_pixbuf__wbmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
65 ModuleUpdatedNotifyFunc updated_func,
66 ModuleFrameDoneNotifyFunc frame_done_func,
67 ModuleAnimationDoneNotifyFunc
68 anim_done_func, gpointer user_data);
70 void gdk_pixbuf__wbmp_image_stop_load(gpointer data);
71 gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data, guchar * buf,
75 /* Shared library entry point --> This should be removed when
76 generic_image_load enters gdk-pixbuf-io. */
77 GdkPixbuf *gdk_pixbuf__wbmp_image_load(FILE * f)
81 struct wbmp_progressive_state *State;
85 State = gdk_pixbuf__wbmp_image_begin_load(NULL, NULL, NULL, NULL, NULL);
87 while (feof(f) == 0) {
88 length = fread(membuf, 1, 4096, f);
90 gdk_pixbuf__wbmp_image_load_increment(State,
95 if (State->pixbuf != NULL)
96 gdk_pixbuf_ref(State->pixbuf);
100 gdk_pixbuf__wbmp_image_stop_load(State);
105 * func - called when we have pixmap created (but no image data)
106 * user_data - passed as arg 1 to func
107 * return context (opaque to user)
111 gdk_pixbuf__wbmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
112 ModuleUpdatedNotifyFunc updated_func,
113 ModuleFrameDoneNotifyFunc frame_done_func,
114 ModuleAnimationDoneNotifyFunc
115 anim_done_func, gpointer user_data)
117 struct wbmp_progressive_state *context;
119 context = g_new0(struct wbmp_progressive_state, 1);
120 context->prepared_func = prepared_func;
121 context->updated_func = updated_func;
122 context->user_data = user_data;
124 context->needmore = context->need_type = context->need_header = context->need_width = context->need_height = TRUE;
125 context->call_progressive_updates = TRUE;
126 context->pixbuf = NULL;
128 return (gpointer) context;
132 * context - returned from image_begin_load
134 * free context, unref gdk_pixbuf
136 void gdk_pixbuf__wbmp_image_stop_load(gpointer data)
138 struct wbmp_progressive_state *context =
139 (struct wbmp_progressive_state *) data;
141 g_return_if_fail(context != NULL);
143 gdk_pixbuf_unref(context->pixbuf);
149 getin(struct wbmp_progressive_state *context, guchar **buf, guint *buf_size, guchar *ptr, int datum_size)
151 int last_num, buf_num;
153 if((context->last_len + *buf_size) < datum_size)
156 /* We know we can pull it out of there */
157 last_num = MIN(datum_size, context->last_len);
158 buf_num = MIN(datum_size-last_num, *buf_size);
159 memcpy(ptr, context->last_buf, last_num);
160 memcpy(ptr+last_num, *buf, buf_num);
162 context->last_len -= last_num;
163 if(context->last_len)
164 memmove(context->last_buf, context->last_buf+last_num, context->last_len);
165 *buf_size -= buf_num;
172 save_rest(struct wbmp_progressive_state *context, const guchar *buf, guint buf_size)
174 if(buf_size > (sizeof(context->last_buf) - context->last_len))
177 memcpy(context->last_buf+context->last_len, buf, buf_size);
178 context->last_len += buf_size;
184 get_mbi(struct wbmp_progressive_state *context, guchar **buf, guint *buf_size, int *val)
193 rv = getin(context, buf, buf_size, intbuf+n, 1);
197 *val |= intbuf[n] & 0x7F;
199 } while(n < sizeof(intbuf) && (intbuf[n-1] & 0x80));
202 if(!rv || !(intbuf[n-1] & 0x80))
204 rv = save_rest(context, intbuf, n);
207 g_error("Couldn't save_rest of intbuf");
215 * context - from image_begin_load
216 * buf - new image data
217 * size - length of new image data
219 * append image data onto inrecrementally built output image
221 gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data, guchar * buf,
224 struct wbmp_progressive_state *context =
225 (struct wbmp_progressive_state *) data;
230 if(context->need_type)
234 bv = getin(context, &buf, &size, &val, 1);
238 context->need_type = FALSE;
241 else if(context->need_header)
245 bv = getin(context, &buf, &size, &val, 1);
248 /* We skip over the extended header - val is unused */
250 context->need_header = FALSE;
253 else if(context->need_width)
255 bv = get_mbi(context, &buf, &size, &context->width);
257 context->need_width = FALSE;
259 else if(context->need_height)
261 bv = get_mbi(context, &buf, &size, &context->height);
264 context->need_height = FALSE;
265 context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, context->width, context->height);
266 if(context->prepared_func)
267 context->prepared_func(context->pixbuf, context->user_data);
270 else if(context->needmore)
273 first_row = context->cury;
274 for( ; context->cury < context->height; context->cury++, context->curx = 0)
276 for( ; context->curx < context->width; context->curx += 8)
281 bv = getin(context, &buf, &size, &byte, 1);
285 ptr = context->pixbuf->pixels + context->pixbuf->rowstride * context->cury + context->curx * 3;
286 for(xoff = 0; xoff < 8; xoff++, ptr += 3)
295 ptr[0] = ptr[1] = ptr[2] = pixval;
299 context->needmore = FALSE;
302 context->updated_func(context->pixbuf, 0, first_row, context->width, context->cury - first_row + 1,
306 bv = FALSE; /* Nothing left to do, stop feeding me data! */
311 return save_rest(context, buf, size);
313 return context->needmore;