]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-wbmp.c
fix some shell typos
[~andy/gtk] / gdk-pixbuf / io-wbmp.c
1 /* GdkPixbuf library - Wireless Bitmap image loader
2  *
3  * Copyright (C) 2000 Red Hat, Inc.
4  *
5  * Authors: Elliot Lee <sopwith@redhat.com
6  *
7  * Based on io-bmp.c
8  *
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.
13  *
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.
18  *
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.
23  */
24
25 /*
26
27 Known bugs:
28         * Since this is based off the libgd implementation, no extended headers implemented (not required for a WAP client)
29 */
30
31 #include <config.h>
32 #include <stdio.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #include <string.h>
37 #include "gdk-pixbuf-private.h"
38 #include "gdk-pixbuf-io.h"
39
40 \f
41
42 /* Progressive loading */
43
44 struct wbmp_progressive_state {
45   ModulePreparedNotifyFunc prepared_func;
46   ModuleUpdatedNotifyFunc updated_func;
47   gpointer user_data;
48
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;
55
56   guchar last_buf[16]; /* Just needs to be big enough to hold the largest datum requestable via 'getin' */
57   guint last_len;
58
59   int type;
60   int width, height, curx, cury;
61
62   GdkPixbuf *pixbuf;    /* Our "target" */
63 };
64
65 static gpointer
66 gdk_pixbuf__wbmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
67                                   ModuleUpdatedNotifyFunc updated_func,
68                                   gpointer user_data,
69                                   GError **error);
70
71 static gboolean gdk_pixbuf__wbmp_image_stop_load(gpointer data, GError **error);
72 static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
73                                                       const guchar * buf,
74                                                       guint size,
75                                                       GError **error);
76
77
78 /* Shared library entry point --> This should be removed when
79    generic_image_load enters gdk-pixbuf-io. */
80 static GdkPixbuf *gdk_pixbuf__wbmp_image_load(FILE * f, GError **error)
81 {
82         size_t length;
83         char membuf[4096];
84         struct wbmp_progressive_state *State;
85
86         GdkPixbuf *pb;
87
88         State = gdk_pixbuf__wbmp_image_begin_load(NULL, NULL, NULL,
89                                                   error);
90
91         if (State == NULL)
92           return NULL;
93         
94         while (feof(f) == 0) {
95                 length = fread(membuf, 1, 4096, f);
96                 if (length > 0)
97                   gdk_pixbuf__wbmp_image_load_increment(State,
98                                                         membuf,
99                                                         length,
100                                                         error);
101
102         }
103         if (State->pixbuf != NULL)
104                 gdk_pixbuf_ref(State->pixbuf);
105
106         pb = State->pixbuf;
107
108         gdk_pixbuf__wbmp_image_stop_load(State, NULL);
109         return pb;
110 }
111
112 /* 
113  * func - called when we have pixmap created (but no image data)
114  * user_data - passed as arg 1 to func
115  * return context (opaque to user)
116  */
117
118 static gpointer
119 gdk_pixbuf__wbmp_image_begin_load(ModulePreparedNotifyFunc prepared_func,
120                                   ModuleUpdatedNotifyFunc updated_func,
121                                   gpointer user_data,
122                                   GError **error)
123 {
124         struct wbmp_progressive_state *context;
125
126         context = g_new0(struct wbmp_progressive_state, 1);
127         context->prepared_func = prepared_func;
128         context->updated_func = updated_func;
129         context->user_data = user_data;
130
131         context->needmore = context->need_type = context->need_header = context->need_width = context->need_height = TRUE;
132         context->call_progressive_updates = TRUE;
133         context->pixbuf = NULL;
134
135         return (gpointer) context;
136 }
137
138 /*
139  * context - returned from image_begin_load
140  *
141  * free context, unref gdk_pixbuf
142  */
143 static gboolean gdk_pixbuf__wbmp_image_stop_load(gpointer data,
144                                                  GError **error)
145 {
146         struct wbmp_progressive_state *context =
147             (struct wbmp_progressive_state *) data;
148
149         /* FIXME this thing needs to report errors if
150          * we have unused image data
151          */
152         
153         g_return_val_if_fail(context != NULL, TRUE);
154         if (context->pixbuf)
155           gdk_pixbuf_unref(context->pixbuf);
156
157         g_free(context);
158
159         return TRUE;
160 }
161
162 static gboolean
163 getin(struct wbmp_progressive_state *context, guchar **buf, guint *buf_size, guchar *ptr, int datum_size)
164 {
165   int last_num, buf_num;
166
167   if((context->last_len + *buf_size) < datum_size)
168     return FALSE;
169
170   /* We know we can pull it out of there */
171   last_num = MIN(datum_size, context->last_len);
172   buf_num = MIN(datum_size-last_num, *buf_size);
173   memcpy(ptr, context->last_buf, last_num);
174   memcpy(ptr+last_num, *buf, buf_num);
175          
176   context->last_len -= last_num;
177   if(context->last_len)
178     memmove(context->last_buf, context->last_buf+last_num, context->last_len);
179   *buf_size -= buf_num;
180   *buf += buf_num;
181
182   return TRUE;
183 }
184
185 static gboolean
186 save_rest(struct wbmp_progressive_state *context, const guchar *buf, guint buf_size)
187 {
188   if(buf_size > (sizeof(context->last_buf) - context->last_len))
189     return FALSE;
190
191   memcpy(context->last_buf+context->last_len, buf, buf_size);
192   context->last_len += buf_size;
193
194   return TRUE;
195 }
196
197 static gboolean
198 get_mbi(struct wbmp_progressive_state *context, guchar **buf, guint *buf_size, int *val)
199 {
200   guchar intbuf[16];
201   int i, n;
202   gboolean rv;
203
204   *val = 0;
205   n = i = 0;
206   do {
207     rv = getin(context, buf, buf_size, intbuf+n, 1);
208     if(!rv)
209       goto out;
210     *val <<= 7;
211     *val |= intbuf[n] & 0x7F;
212     n++;
213   } while(n < sizeof(intbuf) && (intbuf[n-1] & 0x80));
214
215  out:
216   if(!rv || (intbuf[n-1] & 0x80))
217     {
218       rv = save_rest(context, intbuf, n);
219
220       if(!rv)
221         g_error("Couldn't save_rest of intbuf");
222       return FALSE;
223     }
224
225   return TRUE;
226 }
227
228 /*
229  * context - from image_begin_load
230  * buf - new image data
231  * size - length of new image data
232  *
233  * append image data onto inrecrementally built output image
234  */
235 static gboolean gdk_pixbuf__wbmp_image_load_increment(gpointer data,
236                                                       const guchar * buf,
237                                                       guint size, GError **error)
238 {
239         struct wbmp_progressive_state *context =
240             (struct wbmp_progressive_state *) data;
241         gboolean bv = FALSE;
242
243         do
244           {
245             if(context->need_type)
246               {
247                 guchar val;
248
249                 bv = getin(context, &buf, &size, &val, 1);
250                 if(bv)
251                   {
252                     context->type = val;
253                     context->need_type = FALSE;
254                   }
255               }
256             else if(context->need_header)
257               {
258                 guchar val;
259
260                 bv = getin(context, &buf, &size, &val, 1);
261                 if(bv)
262                   {
263                     /* We skip over the extended header - val is unused */
264                     if(!(val & 0x80))
265                       context->need_header = FALSE;
266                   }
267               }
268             else if(context->need_width)
269               {
270                 bv = get_mbi(context, &buf, &size, &context->width);
271                 if(bv)
272                   context->need_width = FALSE;
273               }
274             else if(context->need_height)
275               {
276                 bv = get_mbi(context, &buf, &size, &context->height);
277                 if(bv)
278                   {
279                     context->need_height = FALSE;
280                     context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, context->width, context->height);
281                     g_assert(context->pixbuf);
282
283                     if(context->prepared_func)
284                       context->prepared_func(context->pixbuf, NULL, context->user_data);
285                   }
286               }
287             else if(context->needmore)
288               {
289                 int first_row;
290                 first_row = context->cury;
291                 for( ; context->cury < context->height; context->cury++, context->curx = 0)
292                   {
293                     for( ; context->curx < context->width; context->curx += 8)
294                       {
295                         guchar byte;
296                         guchar *ptr;
297                         int xoff;
298                         bv = getin(context, &buf, &size, &byte, 1);
299                         if(!bv)
300                           goto out;
301
302                         ptr = context->pixbuf->pixels + context->pixbuf->rowstride * context->cury + context->curx * 3;
303                         for(xoff = 7; xoff >= 0; xoff--, ptr += 3)
304                           {
305                             guchar pixval;
306
307                             if(byte & (1<<xoff))
308                               pixval = 0xFF;
309                             else
310                               pixval = 0x0;
311
312                             ptr[0] = ptr[1] = ptr[2] = pixval;
313                           }
314                       }
315                   }
316                 context->needmore = FALSE;
317
318               out:
319                 context->updated_func(context->pixbuf, 0, first_row, context->width, context->cury - first_row + 1,
320                                       context->user_data);
321               }
322             else
323               bv = FALSE; /* Nothing left to do, stop feeding me data! */
324
325           } while(bv);
326
327         if(size)
328           return save_rest(context, buf, size);
329         else
330           return context->needmore;
331 }
332
333 void
334 gdk_pixbuf__wbmp_fill_vtable (GdkPixbufModule *module)
335 {
336   module->load = gdk_pixbuf__wbmp_image_load;
337   module->begin_load = gdk_pixbuf__wbmp_image_begin_load;
338   module->stop_load = gdk_pixbuf__wbmp_image_stop_load;
339   module->load_increment = gdk_pixbuf__wbmp_image_load_increment;
340 }