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