]> Pileus Git - ~andy/gtk/blob - gdk-pixbuf/io-ras.c
Updated Norwegian translation. Updated some.
[~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                                  ModuleFrameDoneNotifyFunc frame_done_func,
100                                  ModuleAnimationDoneNotifyFunc anim_done_func,
101                                  gpointer user_data,
102                                  GError **error);
103 static gboolean gdk_pixbuf__ras_image_stop_load(gpointer data, GError **error);
104 static gboolean gdk_pixbuf__ras_image_load_increment(gpointer data,
105                                                      const guchar * buf, guint size,
106                                                      GError **error);
107
108
109
110 /* Shared library entry point */
111 static GdkPixbuf *gdk_pixbuf__ras_image_load(FILE * f, GError **error)
112 {
113         guchar *membuf;
114         size_t length;
115         struct ras_progressive_state *State;
116         
117         GdkPixbuf *pb;
118         
119         State = gdk_pixbuf__ras_image_begin_load(NULL, NULL, NULL,
120                                                  NULL, NULL, error);
121         
122         membuf = g_malloc(4096);
123         
124         g_assert(membuf != NULL);
125         
126         while (feof(f) == 0) {
127                 length = fread(membuf, 1, 4096, f);
128                 if (!gdk_pixbuf__ras_image_load_increment(State, membuf, length,
129                                                           error)) {
130                         gdk_pixbuf__ras_image_stop_load (State, NULL);
131                         return NULL;
132                 }
133         }
134         g_free(membuf);
135         if (State->pixbuf != NULL)
136                 gdk_pixbuf_ref(State->pixbuf);
137
138         pb = State->pixbuf;
139
140         gdk_pixbuf__ras_image_stop_load(State, NULL);
141         return pb;
142 }
143
144 static void RAS2State(struct rasterfile *RAS,
145                       struct ras_progressive_state *State)
146 {
147         State->Header.width = GUINT32_FROM_BE(RAS->width);
148         State->Header.height = GUINT32_FROM_BE(RAS->height);
149         State->Header.depth = GUINT32_FROM_BE(RAS->depth);
150         State->Header.type = GUINT32_FROM_BE(RAS->type);
151         State->Header.maptype = GUINT32_FROM_BE(RAS->maptype);
152         State->Header.maplength = GUINT32_FROM_BE(RAS->maplength);
153
154         g_assert(State->Header.maplength <= 768);       /* Otherwise, we are in trouble */
155
156         State->RasType = State->Header.depth;   /* This may be less trivial someday */
157         State->HeaderSize = 32 + State->Header.maplength;
158
159         if (State->RasType == 32)
160                 State->LineWidth = State->Header.width * 4;
161         if (State->RasType == 24)
162                 State->LineWidth = State->Header.width * 3;
163         if (State->RasType == 8)
164                 State->LineWidth = State->Header.width * 1;
165         if (State->RasType == 1) {
166                 State->LineWidth = State->Header.width / 8;
167                 if ((State->Header.width & 7) != 0)
168                         State->LineWidth++;
169         }
170
171         /* Now padd the line to be a multiple of 16 bits */
172         if ((State->LineWidth & 1) != 0)
173                 State->LineWidth++;
174
175         if (State->LineBuf == NULL)
176                 State->LineBuf = g_malloc(State->LineWidth);
177
178         g_assert(State->LineBuf != NULL);
179
180
181         if (State->pixbuf == NULL) {
182                 if (State->RasType == 32)
183                         State->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE,
184                                                        8,
185                                                        (gint)
186                                                        State->Header.width,
187                                                        (gint)
188                                                        State->Header.
189                                                        height);
190                 else
191                         State->pixbuf =
192                             gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
193                                            (gint) State->Header.width,
194                                            (gint) State->Header.height);
195                 if (State->prepared_func != NULL)
196                         /* Notify the client that we are ready to go */
197                         (*State->prepared_func) (State->pixbuf,
198                                                  State->user_data);
199
200         }
201         
202         if ((State->Header.maplength==0)&&(State->RasType==1)) {
203                 State->HeaderBuf[32] = 255;
204                 State->HeaderBuf[33] = 0;
205                 State->HeaderBuf[34] = 255;
206                 State->HeaderBuf[35] = 0;
207                 State->HeaderBuf[36] = 255;
208                 State->HeaderBuf[37] = 0;
209         }
210
211 }
212
213 /* 
214  * func - called when we have pixmap created (but no image data)
215  * user_data - passed as arg 1 to func
216  * return context (opaque to user)
217  */
218
219 static gpointer
220 gdk_pixbuf__ras_image_begin_load(ModulePreparedNotifyFunc prepared_func,
221                                  ModuleUpdatedNotifyFunc updated_func,
222                                  ModuleFrameDoneNotifyFunc frame_done_func,
223                                  ModuleAnimationDoneNotifyFunc anim_done_func,
224                                  gpointer user_data,
225                                  GError **error)
226 {
227         struct ras_progressive_state *context;
228
229         context = g_new0(struct ras_progressive_state, 1);
230         context->prepared_func = prepared_func;
231         context->updated_func = updated_func;
232         context->user_data = user_data;
233
234         context->HeaderSize = 32;
235         context->HeaderBuf = g_malloc(32 + 768);        /* 32 for rasheader,
236                                                            768 for the colormap */
237         context->HeaderDone = 0;
238
239         context->LineWidth = 0;
240         context->LineBuf = NULL;
241         context->LineDone = 0;
242         context->Lines = 0;
243
244         context->RasType = 0;
245
246         memset(&context->Header, 0, sizeof(struct rasterfile));
247
248
249         context->pixbuf = NULL;
250
251
252         return (gpointer) context;
253 }
254
255 /*
256  * context - returned from image_begin_load
257  *
258  * free context, unref gdk_pixbuf
259  */
260 static gboolean
261 gdk_pixbuf__ras_image_stop_load(gpointer data, GError **error)
262 {
263         struct ras_progressive_state *context =
264             (struct ras_progressive_state *) data;
265
266         /* FIXME this thing needs to report errors if
267          * we have unused image data
268          */
269
270         g_return_val_if_fail(context != NULL, TRUE);
271
272         if (context->LineBuf != NULL)
273                 g_free(context->LineBuf);
274         if (context->HeaderBuf != NULL)
275                 g_free(context->HeaderBuf);
276
277         if (context->pixbuf)
278                 gdk_pixbuf_unref(context->pixbuf);
279
280         g_free(context);
281
282         return TRUE;
283 }
284
285 /* 
286  OneLine is called when enough data is received to process 1 line 
287  of pixels 
288  */
289
290 static void OneLine32(struct ras_progressive_state *context)
291 {
292         gint X;
293         guchar *Pixels;
294
295         X = 0;
296         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
297         while (X < context->Header.width) {
298                 /* The joys of having a BGR byteorder */
299                 Pixels[X * 4 + 0] = context->LineBuf[X * 4 + 2];
300                 Pixels[X * 4 + 1] = context->LineBuf[X * 4 + 1];
301                 Pixels[X * 4 + 2] = context->LineBuf[X * 4 + 0];
302                 Pixels[X * 4 + 3] = context->LineBuf[X * 4 + 3];
303                 X++;
304         }
305 }
306
307 static void OneLine24(struct ras_progressive_state *context)
308 {
309         gint X;
310         guchar *Pixels;
311
312         X = 0;
313         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
314         while (X < context->Header.width) {
315                 /* The joys of having a BGR byteorder */
316                 Pixels[X * 3 + 0] = context->LineBuf[X * 3 + 2];
317                 Pixels[X * 3 + 1] = context->LineBuf[X * 3 + 1];
318                 Pixels[X * 3 + 2] = context->LineBuf[X * 3 + 0];
319                 X++;
320         }
321
322 }
323
324 static void OneLine8(struct ras_progressive_state *context)
325 {
326         gint X;
327         guchar *Pixels;
328
329         X = 0;
330         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
331         while (X < context->Header.width) {
332                 /* The joys of having a BGR byteorder */
333                 Pixels[X * 3 + 0] =
334                     context->HeaderBuf[context->LineBuf[X] + 32];
335                 Pixels[X * 3 + 1] =
336                     context->HeaderBuf[context->LineBuf[X] + 256 + 32];
337                 Pixels[X * 3 + 2] =
338                     context->HeaderBuf[context->LineBuf[X] + 512 + 32];
339                 X++;
340         }
341 }
342
343 static void OneLine1(struct ras_progressive_state *context)
344 {
345         gint X;
346         guchar *Pixels;
347
348         X = 0;
349         Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
350         while (X < context->Header.width) {
351                 int Bit;
352                 
353                 Bit = (context->LineBuf[X/8])>>(7-(X&7));
354                 Bit = Bit & 1;
355                 /* The joys of having a BGR byteorder */
356                 Pixels[X * 3 + 0] =
357                     context->HeaderBuf[Bit + 32];
358                 Pixels[X * 3 + 1] =
359                     context->HeaderBuf[Bit + 2 + 32];
360                 Pixels[X * 3 + 2] =
361                     context->HeaderBuf[Bit + 4 + 32];
362                 X++;
363         }
364 }
365
366
367 static void OneLine(struct ras_progressive_state *context)
368 {
369         if (context->RasType == 32)
370                 OneLine32(context);
371         if (context->RasType == 24)
372                 OneLine24(context);
373         if (context->RasType == 8)
374                 OneLine8(context);
375         if (context->RasType == 1)
376                 OneLine1(context);
377
378         context->LineDone = 0;
379         if (context->Lines > context->Header.height)
380                 return;
381         context->Lines++;
382
383         if (context->updated_func != NULL) {
384                 (*context->updated_func) (context->pixbuf,
385                                           0,
386                                           context->Lines,
387                                           context->Header.width,
388                                           context->Header.height,
389                                           context->user_data);
390
391         }
392 }
393
394 /*
395  * context - from image_begin_load
396  * buf - new image data
397  * size - length of new image data
398  *
399  * append image data onto inrecrementally built output image
400  */
401 static gboolean
402 gdk_pixbuf__ras_image_load_increment(gpointer data,
403                                      const guchar * buf, guint size,
404                                      GError **error)
405 {
406         struct ras_progressive_state *context =
407             (struct ras_progressive_state *) data;
408
409         gint BytesToCopy;
410
411         while (size > 0) {
412                 if (context->HeaderDone < context->HeaderSize) {        /* We still 
413                                                                            have headerbytes to do */
414                         BytesToCopy =
415                             context->HeaderSize - context->HeaderDone;
416                         if (BytesToCopy > size)
417                                 BytesToCopy = size;
418
419                         memmove(context->HeaderBuf + context->HeaderDone,
420                                buf, BytesToCopy);
421
422                         size -= BytesToCopy;
423                         buf += BytesToCopy;
424                         context->HeaderDone += BytesToCopy;
425
426                 } else {
427                         /* Pixeldata only */
428                         BytesToCopy =
429                             context->LineWidth - context->LineDone;
430                         if (BytesToCopy > size)
431                                 BytesToCopy = size;
432
433                         if (BytesToCopy > 0) {
434                                 memmove(context->LineBuf +
435                                        context->LineDone, buf,
436                                        BytesToCopy);
437
438                                 size -= BytesToCopy;
439                                 buf += BytesToCopy;
440                                 context->LineDone += BytesToCopy;
441                         }
442                         if ((context->LineDone >= context->LineWidth) &&
443                             (context->LineWidth > 0))
444                                 OneLine(context);
445
446
447                 }
448
449                 if (context->HeaderDone >= 32)
450                         RAS2State((struct rasterfile *) context->HeaderBuf,
451                                   context);
452
453
454         }
455
456         return TRUE;
457 }
458
459 void
460 gdk_pixbuf__ras_fill_vtable (GdkPixbufModule *module)
461 {
462   module->load = gdk_pixbuf__ras_image_load;
463   module->begin_load = gdk_pixbuf__ras_image_begin_load;
464   module->stop_load = gdk_pixbuf__ras_image_stop_load;
465   module->load_increment = gdk_pixbuf__ras_image_load_increment;
466 }