]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-ras.c
Catch invalid frame dimensions.
[~andy/gtk] / gdk-pixbuf / io-ras.c
1 /* GdkPixbuf library - SUNRAS image loader
2  *
3  * Copyright (C) 1999 The Free Software Foundation
4  *
5  * Authors: Arjan van de Ven <arjan@fenrus.demon.nl>
6  *          Federico Mena-Quintero <federico@gimp.org>
7  *
8  * Based on io-gif.c, io-tiff.c and io-png.c
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  */
25
26 /*
27
28 Known bugs:
29         * Compressed rasterfiles don't work yet
30
31 */
32
33 #include <config.h>
34 #include <stdio.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #include <string.h>
39 #include "gdk-pixbuf-private.h"
40 #include "gdk-pixbuf-io.h"
41
42 \f
43
44 /* 
45    Header structure for sunras files.
46    All values are in big-endian order on disk
47    
48    Note: Every scanline is padded to be a multiple of 16 bits
49  */
50
51 struct rasterfile {
52         guint magic;
53         guint width;
54         guint height;
55         guint depth;
56         guint length;
57         guint type;
58         guint maptype;
59         guint maplength;
60 };
61
62 /* 
63         This does a byte-order swap. Does glib have something like
64         be32_to_cpu() ??
65 */
66
67 /* Progressive loading */
68
69 struct ras_progressive_state {
70         ModulePreparedNotifyFunc prepared_func;
71         ModuleUpdatedNotifyFunc updated_func;
72         gpointer user_data;
73
74         gint HeaderSize;        /* The size of the header-part (incl colormap) */
75         guchar *HeaderBuf;      /* The buffer for the header (incl colormap) */
76         gint HeaderDone;        /* The nr of bytes actually in HeaderBuf */
77
78         gint LineWidth;         /* The width of a line in bytes */
79         guchar *LineBuf;        /* Buffer for 1 line */
80         gint LineDone;          /* # of bytes in LineBuf */
81         gint Lines;             /* # of finished lines */
82
83         gint RasType;           /*  32 = BGRA
84                                    24 = BGR
85                                    8 = 8 bit colormapped
86                                    1  = 1 bit bitonal 
87                                  */
88
89
90         struct rasterfile Header;       /* Decoded (BE->CPU) header */
91
92
93         GdkPixbuf *pixbuf;      /* Our "target" */
94 };
95
96 static gpointer
97 gdk_pixbuf__ras_image_begin_load(ModulePreparedNotifyFunc prepared_func,
98                                  ModuleUpdatedNotifyFunc updated_func,
99                                  gpointer user_data,
100                                  GError **error);
101 static gboolean gdk_pixbuf__ras_image_stop_load(gpointer data, GError **error);
102 static gboolean gdk_pixbuf__ras_image_load_increment(gpointer data,
103                                                      const guchar * buf, guint size,
104                                                      GError **error);
105
106
107
108 /* Shared library entry point */
109 static GdkPixbuf *gdk_pixbuf__ras_image_load(FILE * f, GError **error)
110 {
111         guchar *membuf;
112         size_t length;
113         struct ras_progressive_state *State;
114         
115         GdkPixbuf *pb;
116         
117         State = gdk_pixbuf__ras_image_begin_load(NULL, NULL, NULL, error);
118         
119         membuf = g_malloc(4096);
120         
121         g_assert(membuf != NULL);
122         
123         while (feof(f) == 0) {
124                 length = fread(membuf, 1, 4096, f);
125                 if (!gdk_pixbuf__ras_image_load_increment(State, membuf, length,
126                                                           error)) {
127                         gdk_pixbuf__ras_image_stop_load (State, NULL);
128                         return NULL;
129                 }
130         }
131         g_free(membuf);
132         if (State->pixbuf != NULL)
133                 g_object_ref(State->pixbuf);
134
135         pb = State->pixbuf;
136
137         gdk_pixbuf__ras_image_stop_load(State, NULL);
138         return pb;
139 }
140
141 static gboolean RAS2State(struct rasterfile *RAS,
142                           struct ras_progressive_state *State,
143                           GError **error)
144 {
145         State->Header.width = GUINT32_FROM_BE(RAS->width);
146         State->Header.height = GUINT32_FROM_BE(RAS->height);
147         State->Header.depth = GUINT32_FROM_BE(RAS->depth);
148         State->Header.type = GUINT32_FROM_BE(RAS->type);
149         State->Header.maptype = GUINT32_FROM_BE(RAS->maptype);
150         State->Header.maplength = GUINT32_FROM_BE(RAS->maplength);
151
152         if ((gint)State->Header.width <= 0 ||
153             (gint)State->Header.height <= 0 || 
154             State->Header.maplength > 768) {
155                 g_set_error (error,
156                              GDK_PIXBUF_ERROR,
157                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
158                              _("RAS image has bogus header data")); 
159                 return FALSE;
160         }
161
162         State->RasType = State->Header.depth;   /* This may be less trivial someday */
163         State->HeaderSize = 32 + State->Header.maplength;
164
165         if (State->RasType == 32)
166                 State->LineWidth = State->Header.width * 4;
167         else if (State->RasType == 24)
168                 State->LineWidth = State->Header.width * 3;
169         else if (State->RasType == 8)
170                 State->LineWidth = State->Header.width * 1;
171         else if (State->RasType == 1) {
172                 State->LineWidth = State->Header.width / 8;
173                 if ((State->Header.width & 7) != 0)
174                         State->LineWidth++;
175         }
176         else {
177                 g_set_error (error,
178                              GDK_PIXBUF_ERROR,
179                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
180                              _("RAS image has unknown type")); 
181                 return FALSE;
182         }
183
184         /* Now pad the line to be a multiple of 16 bits */
185         if ((State->LineWidth & 1) != 0)
186                 State->LineWidth++;
187
188         if (!State->LineBuf) {
189                 State->LineBuf = g_try_malloc (State->LineWidth);
190
191                 if (!State->LineBuf) {
192                         g_set_error (error,
193                                      GDK_PIXBUF_ERROR,
194                                      GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
195                                      _("Not enough memory to load RAS image")); 
196                         return FALSE;
197                 }
198         }
199
200
201         if (!State->pixbuf) {
202                 if (State->RasType == 32)
203                         State->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8,
204                                                         (gint) State->Header.width,
205                                                         (gint) State->Header.height);
206                 else
207                         State->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
208                                                         (gint) State->Header.width,
209                                                         (gint) State->Header.height);
210                 
211                 if (!State->pixbuf) {
212                         g_set_error (error,
213                                      GDK_PIXBUF_ERROR,
214                                      GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
215                                      _("Not enough memory to load RAS image"));
216                         return FALSE;
217                 }
218
219                 if (State->prepared_func != NULL)
220                         /* Notify the client that we are ready to go */
221                         (*State->prepared_func) (State->pixbuf,
222                                                  NULL,
223                                                  State->user_data);
224
225         }
226         
227         if ((State->Header.maplength == 0) && (State->RasType == 1)) {
228                 State->HeaderBuf[32] = 255;
229                 State->HeaderBuf[33] = 0;
230                 State->HeaderBuf[34] = 255;
231                 State->HeaderBuf[35] = 0;
232                 State->HeaderBuf[36] = 255;
233                 State->HeaderBuf[37] = 0;
234         }
235
236         return TRUE;
237 }
238
239 /* 
240  * func - called when we have pixmap created (but no image data)
241  * user_data - passed as arg 1 to func
242  * return context (opaque to user)
243  */
244
245 static gpointer
246 gdk_pixbuf__ras_image_begin_load(ModulePreparedNotifyFunc prepared_func,
247                                  ModuleUpdatedNotifyFunc updated_func,
248                                  gpointer user_data,
249                                  GError **error)
250 {
251         struct ras_progressive_state *context;
252
253         context = g_new0(struct ras_progressive_state, 1);
254         context->prepared_func = prepared_func;
255         context->updated_func = updated_func;
256         context->user_data = user_data;
257
258         context->HeaderSize = 32;
259         context->HeaderBuf = g_malloc(32 + 768);        /* 32 for rasheader,
260                                                            768 for the colormap */
261         context->HeaderDone = 0;
262
263         context->LineWidth = 0;
264         context->LineBuf = NULL;
265         context->LineDone = 0;
266         context->Lines = 0;
267
268         context->RasType = 0;
269
270         memset(&context->Header, 0, sizeof(struct rasterfile));
271
272
273         context->pixbuf = NULL;
274
275
276         return (gpointer) context;
277 }
278
279 /*
280  * context - returned from image_begin_load
281  *
282  * free context, unref gdk_pixbuf
283  */
284 static gboolean
285 gdk_pixbuf__ras_image_stop_load(gpointer data, GError **error)
286 {
287         struct ras_progressive_state *context =
288             (struct ras_progressive_state *) data;
289
290         /* FIXME this thing needs to report errors if
291          * we have unused image data
292          */
293
294         g_return_val_if_fail(context != NULL, TRUE);
295
296         if (context->LineBuf != NULL)
297                 g_free(context->LineBuf);
298         if (context->HeaderBuf != NULL)
299                 g_free(context->HeaderBuf);
300
301         if (context->pixbuf)
302                 g_object_unref(context->pixbuf);
303
304         g_free(context);
305
306         return TRUE;
307 }
308
309 /* 
310  OneLine is called when enough data is received to process 1 line 
311  of pixels 
312  */
313
314 static void OneLine32(struct ras_progressive_state *context)
315 {
316         gint X;
317         guchar *Pixels;
318
319         X = 0;
320         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
321         while (X < context->Header.width) {
322                 /* The joys of having a BGR byteorder */
323                 Pixels[X * 4 + 0] = context->LineBuf[X * 4 + 2];
324                 Pixels[X * 4 + 1] = context->LineBuf[X * 4 + 1];
325                 Pixels[X * 4 + 2] = context->LineBuf[X * 4 + 0];
326                 Pixels[X * 4 + 3] = context->LineBuf[X * 4 + 3];
327                 X++;
328         }
329 }
330
331 static void OneLine24(struct ras_progressive_state *context)
332 {
333         gint X;
334         guchar *Pixels;
335
336         X = 0;
337         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
338         while (X < context->Header.width) {
339                 /* The joys of having a BGR byteorder */
340                 Pixels[X * 3 + 0] = context->LineBuf[X * 3 + 2];
341                 Pixels[X * 3 + 1] = context->LineBuf[X * 3 + 1];
342                 Pixels[X * 3 + 2] = context->LineBuf[X * 3 + 0];
343                 X++;
344         }
345
346 }
347
348 static void OneLine8(struct ras_progressive_state *context)
349 {
350         gint X;
351         guchar *Pixels;
352
353         X = 0;
354         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
355         while (X < context->Header.width) {
356                 /* The joys of having a BGR byteorder */
357                 Pixels[X * 3 + 0] =
358                     context->HeaderBuf[context->LineBuf[X] + 32];
359                 Pixels[X * 3 + 1] =
360                     context->HeaderBuf[context->LineBuf[X] + 256 + 32];
361                 Pixels[X * 3 + 2] =
362                     context->HeaderBuf[context->LineBuf[X] + 512 + 32];
363                 X++;
364         }
365 }
366
367 static void OneLine1(struct ras_progressive_state *context)
368 {
369         gint X;
370         guchar *Pixels;
371
372         X = 0;
373         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
374         while (X < context->Header.width) {
375                 int Bit;
376                 
377                 Bit = (context->LineBuf[X/8])>>(7-(X&7));
378                 Bit = Bit & 1;
379                 /* The joys of having a BGR byteorder */
380                 Pixels[X * 3 + 0] =
381                     context->HeaderBuf[Bit + 32];
382                 Pixels[X * 3 + 1] =
383                     context->HeaderBuf[Bit + 2 + 32];
384                 Pixels[X * 3 + 2] =
385                     context->HeaderBuf[Bit + 4 + 32];
386                 X++;
387         }
388 }
389
390
391 static void OneLine(struct ras_progressive_state *context)
392 {
393         context->LineDone = 0;
394         if (context->Lines >= context->Header.height)
395                 return;
396         if (context->RasType == 32)
397                 OneLine32(context);
398         if (context->RasType == 24)
399                 OneLine24(context);
400         if (context->RasType == 8)
401                 OneLine8(context);
402         if (context->RasType == 1)
403                 OneLine1(context);
404
405         context->LineDone = 0;
406         context->Lines++;
407
408         if (context->updated_func != NULL) {
409                 (*context->updated_func) (context->pixbuf,
410                                           0,
411                                           context->Lines,
412                                           context->Header.width,
413                                           context->Header.height,
414                                           context->user_data);
415
416         }
417 }
418
419 /*
420  * context - from image_begin_load
421  * buf - new image data
422  * size - length of new image data
423  *
424  * append image data onto incrementally built output image
425  */
426 static gboolean
427 gdk_pixbuf__ras_image_load_increment(gpointer data,
428                                      const guchar * buf, guint size,
429                                      GError **error)
430 {
431         struct ras_progressive_state *context =
432             (struct ras_progressive_state *) data;
433
434         gint BytesToCopy;
435
436         while (size > 0) {
437                 if (context->HeaderDone < context->HeaderSize) {        /* We still 
438                                                                            have headerbytes to do */
439                         BytesToCopy =
440                             context->HeaderSize - context->HeaderDone;
441                         if (BytesToCopy > size)
442                                 BytesToCopy = size;
443
444                         memmove(context->HeaderBuf + context->HeaderDone,
445                                buf, BytesToCopy);
446
447                         size -= BytesToCopy;
448                         buf += BytesToCopy;
449                         context->HeaderDone += BytesToCopy;
450
451                 } else {
452                         /* Pixeldata only */
453                         BytesToCopy =
454                             context->LineWidth - context->LineDone;
455                         if (BytesToCopy > size)
456                                 BytesToCopy = size;
457
458                         if (BytesToCopy > 0) {
459                                 memmove(context->LineBuf +
460                                        context->LineDone, buf,
461                                        BytesToCopy);
462
463                                 size -= BytesToCopy;
464                                 buf += BytesToCopy;
465                                 context->LineDone += BytesToCopy;
466                         }
467                         if ((context->LineDone >= context->LineWidth) &&
468                             (context->LineWidth > 0))
469                                 OneLine(context);
470
471
472                 }
473
474                 if (context->HeaderDone >= 32)
475                         if (!RAS2State((struct rasterfile *) context->HeaderBuf,
476                                        context, error)) {
477                                 return FALSE;
478                         }
479
480
481         }
482
483         return TRUE;
484 }
485
486 void
487 gdk_pixbuf__ras_fill_vtable (GdkPixbufModule *module)
488 {
489   module->load = gdk_pixbuf__ras_image_load;
490   module->begin_load = gdk_pixbuf__ras_image_begin_load;
491   module->stop_load = gdk_pixbuf__ras_image_stop_load;
492   module->load_increment = gdk_pixbuf__ras_image_load_increment;
493 }
494