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(ModulePreparedNotifyFunc prepared_func,
67 ModuleUpdatedNotifyFunc updated_func,
68 ModuleFrameDoneNotifyFunc frame_done_func,
69 ModuleAnimationDoneNotifyFunc
70 anim_done_func, gpointer user_data,
73 void gdk_pixbuf__wbmp_image_stop_load(gpointer data);
74 gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data, guchar * buf,
79 /* Shared library entry point --> This should be removed when
80 generic_image_load enters gdk-pixbuf-io. */
81 GdkPixbuf *gdk_pixbuf__wbmp_image_load(FILE * f, GError **error)
85 struct wbmp_progressive_state *State;
89 State = gdk_pixbuf__wbmp_image_begin_load(NULL, NULL, NULL, NULL, NULL,
95 while (feof(f) == 0) {
96 length = fread(membuf, 1, 4096, f);
98 gdk_pixbuf__wbmp_image_load_increment(State,
104 if (State->pixbuf != NULL)
105 gdk_pixbuf_ref(State->pixbuf);
109 gdk_pixbuf__wbmp_image_stop_load(State);
114 * func - called when we have pixmap created (but no image data)
115 * user_data - passed as arg 1 to func
116 * return context (opaque to user)
120 gdk_pixbuf__wbmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
121 ModuleUpdatedNotifyFunc updated_func,
122 ModuleFrameDoneNotifyFunc frame_done_func,
123 ModuleAnimationDoneNotifyFunc
124 anim_done_func, gpointer user_data,
127 struct wbmp_progressive_state *context;
129 context = g_new0(struct wbmp_progressive_state, 1);
130 context->prepared_func = prepared_func;
131 context->updated_func = updated_func;
132 context->user_data = user_data;
134 context->needmore = context->need_type = context->need_header = context->need_width = context->need_height = TRUE;
135 context->call_progressive_updates = TRUE;
136 context->pixbuf = NULL;
138 return (gpointer) context;
142 * context - returned from image_begin_load
144 * free context, unref gdk_pixbuf
146 void gdk_pixbuf__wbmp_image_stop_load(gpointer data)
148 struct wbmp_progressive_state *context =
149 (struct wbmp_progressive_state *) data;
151 g_return_if_fail(context != NULL);
153 gdk_pixbuf_unref(context->pixbuf);
159 getin(struct wbmp_progressive_state *context, guchar **buf, guint *buf_size, guchar *ptr, int datum_size)
161 int last_num, buf_num;
163 if((context->last_len + *buf_size) < datum_size)
166 /* We know we can pull it out of there */
167 last_num = MIN(datum_size, context->last_len);
168 buf_num = MIN(datum_size-last_num, *buf_size);
169 memcpy(ptr, context->last_buf, last_num);
170 memcpy(ptr+last_num, *buf, buf_num);
172 context->last_len -= last_num;
173 if(context->last_len)
174 memmove(context->last_buf, context->last_buf+last_num, context->last_len);
175 *buf_size -= buf_num;
182 save_rest(struct wbmp_progressive_state *context, const guchar *buf, guint buf_size)
184 if(buf_size > (sizeof(context->last_buf) - context->last_len))
187 memcpy(context->last_buf+context->last_len, buf, buf_size);
188 context->last_len += buf_size;
194 get_mbi(struct wbmp_progressive_state *context, guchar **buf, guint *buf_size, int *val)
203 rv = getin(context, buf, buf_size, intbuf+n, 1);
207 *val |= intbuf[n] & 0x7F;
209 } while(n < sizeof(intbuf) && (intbuf[n-1] & 0x80));
212 if(!rv || (intbuf[n-1] & 0x80))
214 rv = save_rest(context, intbuf, n);
217 g_error("Couldn't save_rest of intbuf");
225 * context - from image_begin_load
226 * buf - new image data
227 * size - length of new image data
229 * append image data onto inrecrementally built output image
231 gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data, guchar * buf,
232 guint size, GError **error)
234 struct wbmp_progressive_state *context =
235 (struct wbmp_progressive_state *) data;
240 if(context->need_type)
244 bv = getin(context, &buf, &size, &val, 1);
248 context->need_type = FALSE;
251 else if(context->need_header)
255 bv = getin(context, &buf, &size, &val, 1);
258 /* We skip over the extended header - val is unused */
260 context->need_header = FALSE;
263 else if(context->need_width)
265 bv = get_mbi(context, &buf, &size, &context->width);
267 context->need_width = FALSE;
269 else if(context->need_height)
271 bv = get_mbi(context, &buf, &size, &context->height);
274 context->need_height = FALSE;
275 context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, context->width, context->height);
276 g_assert(context->pixbuf);
278 if(context->prepared_func)
279 context->prepared_func(context->pixbuf, context->user_data);
282 else if(context->needmore)
285 first_row = context->cury;
286 for( ; context->cury < context->height; context->cury++, context->curx = 0)
288 for( ; context->curx < context->width; context->curx += 8)
293 bv = getin(context, &buf, &size, &byte, 1);
297 ptr = context->pixbuf->pixels + context->pixbuf->rowstride * context->cury + context->curx * 3;
298 for(xoff = 7; xoff >= 0; xoff--, ptr += 3)
307 ptr[0] = ptr[1] = ptr[2] = pixval;
311 context->needmore = FALSE;
314 context->updated_func(context->pixbuf, 0, first_row, context->width, context->cury - first_row + 1,
318 bv = FALSE; /* Nothing left to do, stop feeding me data! */
323 return save_rest(context, buf, size);
325 return context->needmore;