1 /* GdkPixbuf library - SUNRAS image loader
3 * Copyright (C) 1999 The Free Software Foundation
5 * Authors: Arjan van de Ven <arjan@fenrus.demon.nl>
6 * Federico Mena-Quintero <federico@gimp.org>
8 * Based on io-gif.c, io-tiff.c and io-png.c
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.
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.
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.
29 * Compressed rasterfiles don't work yet
39 #include "gdk-pixbuf-private.h"
40 #include "gdk-pixbuf-io.h"
45 Header structure for sunras files.
46 All values are in big-endian order on disk
48 Note: Every scanline is padded to be a multiple of 16 bits
63 This does a byte-order swap. Does glib have something like
67 /* Progressive loading */
69 struct ras_progressive_state {
70 ModulePreparedNotifyFunc prepared_func;
71 ModuleUpdatedNotifyFunc updated_func;
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 */
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 */
83 gint RasType; /* 32 = BGRA
90 struct rasterfile Header; /* Decoded (BE->CPU) header */
93 GdkPixbuf *pixbuf; /* Our "target" */
97 gdk_pixbuf__ras_image_begin_load(ModulePreparedNotifyFunc prepared_func,
98 ModuleUpdatedNotifyFunc updated_func,
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,
108 /* Shared library entry point */
109 static GdkPixbuf *gdk_pixbuf__ras_image_load(FILE * f, GError **error)
113 struct ras_progressive_state *State;
117 State = gdk_pixbuf__ras_image_begin_load(NULL, NULL, NULL, error);
119 membuf = g_malloc(4096);
121 g_assert(membuf != NULL);
123 while (feof(f) == 0) {
124 length = fread(membuf, 1, 4096, f);
125 if (!gdk_pixbuf__ras_image_load_increment(State, membuf, length,
127 gdk_pixbuf__ras_image_stop_load (State, NULL);
132 if (State->pixbuf != NULL)
133 g_object_ref(State->pixbuf);
137 gdk_pixbuf__ras_image_stop_load(State, NULL);
141 static gboolean RAS2State(struct rasterfile *RAS,
142 struct ras_progressive_state *State,
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);
152 if ((gint)State->Header.width <= 0 ||
153 (gint)State->Header.height <= 0 ||
154 State->Header.maplength > 768) {
157 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
158 _("RAS image has bogus header data"));
162 State->RasType = State->Header.depth; /* This may be less trivial someday */
163 State->HeaderSize = 32 + State->Header.maplength;
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)
179 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
180 _("RAS image has unknown type"));
184 /* Now pad the line to be a multiple of 16 bits */
185 if ((State->LineWidth & 1) != 0)
188 if (!State->LineBuf) {
189 State->LineBuf = g_try_malloc (State->LineWidth);
191 if (!State->LineBuf) {
194 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
195 _("Not enough memory to load RAS image"));
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);
207 State->pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8,
208 (gint) State->Header.width,
209 (gint) State->Header.height);
211 if (!State->pixbuf) {
214 GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
215 _("Not enough memory to load RAS image"));
219 if (State->prepared_func != NULL)
220 /* Notify the client that we are ready to go */
221 (*State->prepared_func) (State->pixbuf,
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;
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)
246 gdk_pixbuf__ras_image_begin_load(ModulePreparedNotifyFunc prepared_func,
247 ModuleUpdatedNotifyFunc updated_func,
251 struct ras_progressive_state *context;
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;
258 context->HeaderSize = 32;
259 context->HeaderBuf = g_malloc(32 + 768); /* 32 for rasheader,
260 768 for the colormap */
261 context->HeaderDone = 0;
263 context->LineWidth = 0;
264 context->LineBuf = NULL;
265 context->LineDone = 0;
268 context->RasType = 0;
270 memset(&context->Header, 0, sizeof(struct rasterfile));
273 context->pixbuf = NULL;
276 return (gpointer) context;
280 * context - returned from image_begin_load
282 * free context, unref gdk_pixbuf
285 gdk_pixbuf__ras_image_stop_load(gpointer data, GError **error)
287 struct ras_progressive_state *context =
288 (struct ras_progressive_state *) data;
290 /* FIXME this thing needs to report errors if
291 * we have unused image data
294 g_return_val_if_fail(context != NULL, TRUE);
296 if (context->LineBuf != NULL)
297 g_free(context->LineBuf);
298 if (context->HeaderBuf != NULL)
299 g_free(context->HeaderBuf);
302 g_object_unref(context->pixbuf);
310 OneLine is called when enough data is received to process 1 line
314 static void OneLine32(struct ras_progressive_state *context)
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];
331 static void OneLine24(struct ras_progressive_state *context)
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];
348 static void OneLine8(struct ras_progressive_state *context)
354 Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
355 while (X < context->Header.width) {
356 /* The joys of having a BGR byteorder */
358 context->HeaderBuf[context->LineBuf[X] + 32];
360 context->HeaderBuf[context->LineBuf[X] + 256 + 32];
362 context->HeaderBuf[context->LineBuf[X] + 512 + 32];
367 static void OneLine1(struct ras_progressive_state *context)
373 Pixels = context->pixbuf->pixels + context->pixbuf->rowstride * context->Lines;
374 while (X < context->Header.width) {
377 Bit = (context->LineBuf[X/8])>>(7-(X&7));
379 /* The joys of having a BGR byteorder */
381 context->HeaderBuf[Bit + 32];
383 context->HeaderBuf[Bit + 2 + 32];
385 context->HeaderBuf[Bit + 4 + 32];
391 static void OneLine(struct ras_progressive_state *context)
393 context->LineDone = 0;
394 if (context->Lines >= context->Header.height)
396 if (context->RasType == 32)
398 if (context->RasType == 24)
400 if (context->RasType == 8)
402 if (context->RasType == 1)
405 context->LineDone = 0;
408 if (context->updated_func != NULL) {
409 (*context->updated_func) (context->pixbuf,
412 context->Header.width,
413 context->Header.height,
420 * context - from image_begin_load
421 * buf - new image data
422 * size - length of new image data
424 * append image data onto incrementally built output image
427 gdk_pixbuf__ras_image_load_increment(gpointer data,
428 const guchar * buf, guint size,
431 struct ras_progressive_state *context =
432 (struct ras_progressive_state *) data;
437 if (context->HeaderDone < context->HeaderSize) { /* We still
438 have headerbytes to do */
440 context->HeaderSize - context->HeaderDone;
441 if (BytesToCopy > size)
444 memmove(context->HeaderBuf + context->HeaderDone,
449 context->HeaderDone += BytesToCopy;
454 context->LineWidth - context->LineDone;
455 if (BytesToCopy > size)
458 if (BytesToCopy > 0) {
459 memmove(context->LineBuf +
460 context->LineDone, buf,
465 context->LineDone += BytesToCopy;
467 if ((context->LineDone >= context->LineWidth) &&
468 (context->LineWidth > 0))
474 if (context->HeaderDone >= 32)
475 if (!RAS2State((struct rasterfile *) context->HeaderBuf,
487 gdk_pixbuf__ras_fill_vtable (GdkPixbufModule *module)
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;