]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-ras.c
[quartz] Delete the typedef of GdkDevicePrivate
[~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_literal (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_literal (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_literal (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_literal (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_literal (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         g_free(context->LineBuf);
285         g_free(context->HeaderBuf);
286
287         if (context->pixbuf)
288                 g_object_unref(context->pixbuf);
289
290         g_free(context);
291
292         return TRUE;
293 }
294
295 /* 
296  OneLine is called when enough data is received to process 1 line 
297  of pixels 
298  */
299
300 static void OneLine32(struct ras_progressive_state *context)
301 {
302         gint X;
303         guchar *Pixels;
304
305         X = 0;
306         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
307         while (X < context->Header.width) {
308                 /* The joys of having a BGR byteorder */
309                 Pixels[X * 4 + 0] = context->LineBuf[X * 4 + 2];
310                 Pixels[X * 4 + 1] = context->LineBuf[X * 4 + 1];
311                 Pixels[X * 4 + 2] = context->LineBuf[X * 4 + 0];
312                 Pixels[X * 4 + 3] = context->LineBuf[X * 4 + 3];
313                 X++;
314         }
315 }
316
317 static void OneLine24(struct ras_progressive_state *context)
318 {
319         gint X;
320         guchar *Pixels;
321
322         X = 0;
323         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
324         while (X < context->Header.width) {
325                 /* The joys of having a BGR byteorder */
326                 Pixels[X * 3 + 0] = context->LineBuf[X * 3 + 2];
327                 Pixels[X * 3 + 1] = context->LineBuf[X * 3 + 1];
328                 Pixels[X * 3 + 2] = context->LineBuf[X * 3 + 0];
329                 X++;
330         }
331
332 }
333
334 static void OneLine8(struct ras_progressive_state *context)
335 {
336         gint X;
337         guchar *Pixels;
338         int offset = context->Header.maplength / 3;
339
340         X = 0;
341         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
342         while (X < context->Header.width) {
343                 /* The joys of having a BGR byteorder */
344                 Pixels[X * 3 + 0] =
345                     context->HeaderBuf[context->LineBuf[X] + 32];
346                 Pixels[X * 3 + 1] =
347                     context->HeaderBuf[context->LineBuf[X] + offset + 32];
348                 Pixels[X * 3 + 2] =
349                     context->HeaderBuf[context->LineBuf[X] + 2*offset + 32];
350                 X++;
351         }
352 }
353
354 static void OneLine1(struct ras_progressive_state *context)
355 {
356         gint X;
357         guchar *Pixels;
358
359         X = 0;
360         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
361         while (X < context->Header.width) {
362                 int Bit;
363                 
364                 Bit = (context->LineBuf[X/8])>>(7-(X&7));
365                 Bit = Bit & 1;
366                 /* The joys of having a BGR byteorder */
367                 Pixels[X * 3 + 0] =
368                     context->HeaderBuf[Bit + 32];
369                 Pixels[X * 3 + 1] =
370                     context->HeaderBuf[Bit + 2 + 32];
371                 Pixels[X * 3 + 2] =
372                     context->HeaderBuf[Bit + 4 + 32];
373                 X++;
374         }
375 }
376
377
378 static void OneLine(struct ras_progressive_state *context)
379 {
380         context->LineDone = 0;
381         if (context->Lines >= context->Header.height)
382                 return;
383         if (context->RasType == 32)
384                 OneLine32(context);
385         if (context->RasType == 24)
386                 OneLine24(context);
387         if (context->RasType == 8)
388                 OneLine8(context);
389         if (context->RasType == 1)
390                 OneLine1(context);
391
392         context->LineDone = 0;
393         context->Lines++;
394
395         if (context->updated_func != NULL) {
396                 (*context->updated_func) (context->pixbuf,
397                                           0,
398                                           context->Lines,
399                                           context->Header.width,
400                                           1,
401                                           context->user_data);
402
403         }
404 }
405
406 static gboolean
407 DoCompressed (struct ras_progressive_state *context,
408               const guchar * buf, guint size,
409               GError **error)
410 {
411         int i;
412
413         for (i = 0; i < size; i++) {
414                 switch (context->DecoderState) {
415                     case 0:
416                             if (buf[i] == 0x80)
417                                     context->DecoderState = 1;
418                             else
419                                     context->LineBuf[context->LineDone++] = buf[i];
420                             break;
421                     case 1:
422                             if (buf[i] == 0) {
423                                     context->LineBuf[context->LineDone++] = 0x80;
424                                     context->DecoderState = 0;
425                             }
426                             else
427                                     context->DecoderState = buf[i] + 1;
428                             break;
429                     default:
430                             for (; context->DecoderState; context->DecoderState--) {
431                                     context->LineBuf[context->LineDone++] = buf[i];
432                                     if ((context->LineDone >= context->LineWidth) && (context->LineWidth > 0))
433                                             OneLine(context);
434                             }
435                 }
436                 if ((context->LineDone >= context->LineWidth) && (context->LineWidth > 0))
437                         OneLine(context);
438         }
439         return TRUE;
440 }
441
442 /*
443  * context - from image_begin_load
444  * buf - new image data
445  * size - length of new image data
446  *
447  * append image data onto incrementally built output image
448  */
449 static gboolean
450 gdk_pixbuf__ras_image_load_increment(gpointer data,
451                                      const guchar * buf, guint size,
452                                      GError **error)
453 {
454         struct ras_progressive_state *context =
455             (struct ras_progressive_state *) data;
456
457         gint BytesToCopy;
458
459         while (size > 0) {
460                 if (context->HeaderDone < context->HeaderSize) {        /* We still 
461                                                                            have headerbytes to do */
462                         BytesToCopy =
463                             context->HeaderSize - context->HeaderDone;
464                         if (BytesToCopy > size)
465                                 BytesToCopy = size;
466
467                         memmove(context->HeaderBuf + context->HeaderDone,
468                                buf, BytesToCopy);
469
470                         size -= BytesToCopy;
471                         buf += BytesToCopy;
472                         context->HeaderDone += BytesToCopy;
473
474                 } else if (context->Header.type == 2) {
475                         if (!DoCompressed (context, buf, size, error)) {
476                                 return FALSE;
477                         }
478                         size = 0;
479                 }
480                 else {
481                         BytesToCopy =
482                             context->LineWidth - context->LineDone;
483                         if (BytesToCopy > size)
484                                 BytesToCopy = size;
485
486                         if (BytesToCopy > 0) {
487                                 memmove(context->LineBuf +
488                                        context->LineDone, buf,
489                                        BytesToCopy);
490
491                                 size -= BytesToCopy;
492                                 buf += BytesToCopy;
493                                 context->LineDone += BytesToCopy;
494                         }
495                         if ((context->LineDone >= context->LineWidth) &&
496                             (context->LineWidth > 0))
497                                 OneLine(context);
498
499
500                 }
501
502                 if (context->HeaderDone >= 32)
503                         if (!RAS2State((struct rasterfile *) context->HeaderBuf,
504                                        context, error)) {
505                                 return FALSE;
506                         }
507
508
509         }
510
511         return TRUE;
512 }
513
514 #ifndef INCLUDE_ras
515 #define MODULE_ENTRY(function) G_MODULE_EXPORT void function
516 #else
517 #define MODULE_ENTRY(function) void _gdk_pixbuf__ras_ ## function
518 #endif
519
520 MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
521 {
522         module->begin_load = gdk_pixbuf__ras_image_begin_load;
523         module->stop_load = gdk_pixbuf__ras_image_stop_load;
524         module->load_increment = gdk_pixbuf__ras_image_load_increment;
525 }
526
527 MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
528 {
529         static GdkPixbufModulePattern signature[] = {
530                 { "\x59\xa6\x6a\x95", NULL, 100 },
531                 { NULL, NULL, 0 }
532         };
533         static gchar * mime_types[] = {
534                 "image/x-cmu-raster",
535                 "image/x-sun-raster",
536                 NULL
537         };
538         static gchar * extensions[] = {
539                 "ras",
540                 NULL
541         };
542
543         info->name = "ras";
544         info->signature = signature;
545         info->description = N_("The Sun raster image format");
546         info->mime_types = mime_types;
547         info->extensions = extensions;
548         info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
549         info->license = "LGPL";
550 }
551