2 * GdkPixbuf library - PCX image loader
4 * Copyright (C) 2003 Josh A. Beam
6 * Authors: Josh A. Beam <josh@joshbeam.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
27 #include "gdk-pixbuf-private.h"
28 #include "gdk-pixbuf-io.h"
32 #define PCX_TASK_DONE 0
33 #define PCX_TASK_LOAD_HEADER 1
34 #define PCX_TASK_LOAD_DATA 2
35 #define PCX_TASK_LOAD_PALETTE 3
62 GdkPixbufModuleSizeFunc size_func;
63 GdkPixbufModuleUpdatedFunc updated_func;
64 GdkPixbufModulePreparedFunc prepared_func;
69 gboolean header_loaded;
70 struct pcx_header *header;
86 * set context's image information based on the header
89 fill_pcx_context(struct pcx_context *context)
91 struct pcx_header *header = context->header;
93 context->bpp = header->bitsperpixel;
94 context->width = header->xmax - header->xmin + 1;
95 context->height = header->ymax - header->ymin + 1;
96 context->num_planes = header->colorplanes;
97 context->bytesperline = header->bytesperline;
99 if(header->version == 5 && context->bpp == 8 && context->num_planes == 3)
104 free_pcx_context(struct pcx_context *context, gboolean unref_pixbuf)
107 g_free(context->header);
109 g_free(context->buf);
110 if(unref_pixbuf && context->pixbuf)
111 g_object_unref(context->pixbuf);
113 g_free(context->line);
115 g_free(context->p_data);
121 * read each plane of a single scanline. store_planes is
122 * the number of planes that can be stored in the planes array.
123 * data is the pointer to the block of memory to read
124 * from, size is the length of that data, and line_bytes
125 * is where the number of bytes read will be stored.
128 read_scanline_data(guchar *data, guint size, guchar *planes[],
129 guint store_planes, guint num_planes, guint bytesperline,
137 for(p = 0; p < num_planes; p++) {
138 for(i = 0; i < bytesperline;) { /* i incremented when line byte set */
143 if(byte >> 6 == 0x3) {
144 count = byte & ~(0x3 << 6);
154 for(j = 0; j < count; j++) {
156 planes[p][i++] = byte;
160 if(i >= bytesperline) {
162 if(p >= num_planes) {
172 *line_bytes = d; /* number of bytes read for scanline */
177 gdk_pixbuf__pcx_begin_load(GdkPixbufModuleSizeFunc size_func,
178 GdkPixbufModulePreparedFunc prepared_func,
179 GdkPixbufModuleUpdatedFunc updated_func,
180 gpointer user_data, GError **error)
182 struct pcx_context *context;
184 context = g_new0(struct pcx_context, 1);
188 context->header = g_try_malloc(sizeof(struct pcx_header));
189 if(!context->header) {
191 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for header"));
195 context->size_func = size_func;
196 context->updated_func = updated_func;
197 context->prepared_func = prepared_func;
198 context->user_data = user_data;
200 context->current_task = PCX_TASK_LOAD_HEADER;
202 context->buf = g_try_malloc(sizeof(guchar) * 512);
204 g_free(context->header);
206 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for context buffer"));
209 context->buf_size = 512;
211 context->header_loaded = FALSE;
217 pcx_resize_context_buf(struct pcx_context *context, guint size)
221 new_buf = g_try_realloc(context->buf, size);
225 context->buf = new_buf;
226 context->buf_size = size;
231 * remove a number of bytes (specified by size) from the
232 * beginning of a context's buf
235 pcx_chop_context_buf(struct pcx_context *context, guint size)
239 if(size > context->buf_pos)
246 for(i = 0, j = size; j < context->buf_pos; i++, j++)
247 context->buf[i] = context->buf[j];
249 context->buf_pos -= size;
255 read_pixel_1(guchar *data, guint offset)
262 retval = data[offset] >> 7;
264 bit_offset = offset % 8;
265 offset -= bit_offset;
267 retval = (data[offset] >> (7 - bit_offset)) & 0x1;
274 read_pixel_4(guchar *data, guint offset)
280 retval = data[offset] >> 4;
284 retval = data[offset] & 0xf;
291 pcx_increment_load_data_1(struct pcx_context *context)
298 if(context->num_planes == 4) {
299 planes[0] = context->line;
300 planes[1] = planes[0] + context->bytesperline;
301 planes[2] = planes[1] + context->bytesperline;
302 planes[3] = planes[2] + context->bytesperline;
304 } else if(context->num_planes == 3) {
305 planes[0] = context->line;
306 planes[1] = planes[0] + context->bytesperline;
307 planes[2] = planes[1] + context->bytesperline;
309 } else if(context->num_planes == 2) {
310 planes[0] = context->line;
311 planes[1] = planes[0] + context->bytesperline;
313 } else if(context->num_planes == 1) {
314 planes[0] = context->line;
320 while(read_scanline_data(context->buf, context->buf_pos, planes, store_planes, context->num_planes, context->bytesperline, &line_bytes)) {
321 pcx_chop_context_buf(context, line_bytes);
323 for(i = 0; i < context->width; i++) {
326 if(context->num_planes == 4) {
327 p = read_pixel_1(planes[3], i);
329 p |= read_pixel_1(planes[2], i);
331 p |= read_pixel_1(planes[1], i);
333 p |= read_pixel_1(planes[0], i);
334 } else if(context->num_planes == 3) {
335 p = read_pixel_1(planes[2], i);
337 p |= read_pixel_1(planes[1], i);
339 p |= read_pixel_1(planes[0], i);
340 } else if(context->num_planes == 2) {
341 p = read_pixel_1(planes[1], i);
343 p |= read_pixel_1(planes[0], i);
344 } else if(context->num_planes == 1) {
345 p = read_pixel_1(planes[0], i);
350 context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[p * 3 + 0];
351 context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[p * 3 + 1];
352 context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[p * 3 + 2];
355 if(context->updated_func)
356 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
358 context->current_line++;
360 if(context->current_line == context->height) {
361 context->current_task = PCX_TASK_DONE;
370 pcx_increment_load_data_2(struct pcx_context *context)
377 planes[0] = context->line;
379 while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
380 pcx_chop_context_buf(context, line_bytes);
382 for(i = 0; i < context->width; i++) {
383 shift = 6 - 2 * (i % 4);
384 h = (planes[0][i / 4] >> shift) & 0x3;
385 context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[h * 3 + 0];
386 context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[h * 3 + 1];
387 context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[h * 3 + 2];
390 if(context->updated_func)
391 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
393 context->current_line++;
395 if(context->current_line == context->height) {
396 context->current_task = PCX_TASK_DONE;
405 pcx_increment_load_data_4(struct pcx_context *context)
411 planes[0] = context->line;
413 while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
414 pcx_chop_context_buf(context, line_bytes);
416 for(i = 0; i < context->width; i++) {
419 p = read_pixel_4(planes[0], i) & 0xf;
420 context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[p * 3 + 0];
421 context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[p * 3 + 1];
422 context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[p * 3 + 2];
425 if(context->updated_func)
426 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
428 context->current_line++;
430 if(context->current_line == context->height) {
431 context->current_task = PCX_TASK_DONE;
440 * for loading 8-bit pcx images, we keep a buffer containing
441 * each pixel's palette number; once we've loaded each scanline,
442 * we wait for loading to stop and call pcx_load_palette_8,
443 * which finds the palette at the end of the pcx data and sets the
447 pcx_increment_load_data_8(struct pcx_context *context)
453 planes[0] = context->line;
455 while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
456 pcx_chop_context_buf(context, line_bytes);
458 for(i = 0; i < context->width; i++)
459 context->p_data[context->current_line * context->width + i + 0] = planes[0][i];
461 context->current_line++;
463 if(context->current_line == context->height) {
464 context->current_task = PCX_TASK_LOAD_PALETTE;
473 * read the palette and set the RGB data
476 pcx_load_palette_8(struct pcx_context *context)
480 if(context->current_line < context->height)
483 if(context->buf_pos >= 769) {
484 guchar *palette = context->buf + (context->buf_pos - 769);
486 if(palette[0] == 12) {
488 for(i = 0; i < context->height; i++) {
489 for(j = 0; j < context->width; j++) {
490 context->data[i * context->rowstride + j * 3 + 0] = palette[(context->p_data[i * context->width + j]) * 3 + 0];
491 context->data[i * context->rowstride + j * 3 + 1] = palette[(context->p_data[i * context->width + j]) * 3 + 1];
492 context->data[i * context->rowstride + j * 3 + 2] = palette[(context->p_data[i * context->width + j]) * 3 + 2];
495 if(context->updated_func)
496 context->updated_func(context->pixbuf, 0, i, context->width, 1, context->user_data);
500 g_print("read palette\n");
503 context->current_task = PCX_TASK_DONE;
507 g_print("this ain't a palette\n");
517 * in 24-bit images, each scanline has three color planes
518 * for red, green, and blue, respectively.
521 pcx_increment_load_data_24(struct pcx_context *context)
527 planes[0] = context->line;
528 planes[1] = planes[0] + context->bytesperline;
529 planes[2] = planes[1] + context->bytesperline;
531 while(read_scanline_data(context->buf, context->buf_pos, planes, 3, context->num_planes, context->bytesperline, &line_bytes)) {
532 pcx_chop_context_buf(context, line_bytes);
534 for(i = 0; i < context->width; i++) {
535 context->data[context->current_line * context->rowstride + i * 3 + 0] = planes[0][i];
536 context->data[context->current_line * context->rowstride + i * 3 + 1] = planes[1][i];
537 context->data[context->current_line * context->rowstride + i * 3 + 2] = planes[2][i];
540 if(context->updated_func)
541 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
543 context->current_line++;
545 if(context->current_line == context->height) {
546 context->current_task = PCX_TASK_DONE;
555 gdk_pixbuf__pcx_load_increment(gpointer data, const guchar *buf, guint size,
558 struct pcx_context *context = (struct pcx_context *)data;
559 struct pcx_header *header;
561 gboolean retval = TRUE;
563 /* if context's buf isn't large enough to hold its current data plus the passed buf, increase its size */
564 if(context->buf_pos + size > context->buf_size) {
565 if(!pcx_resize_context_buf(context, sizeof(guchar) * (context->buf_pos + size))) {
566 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for context buffer"));
571 for(i = 0; i < size; i++)
572 context->buf[context->buf_pos++] = buf[i];
574 if(context->current_task == PCX_TASK_LOAD_HEADER) {
575 if(!context->header_loaded && context->buf_pos > sizeof(struct pcx_header)) { /* set header */
578 memcpy(context->header, context->buf, sizeof(struct pcx_header));
579 pcx_chop_context_buf(context, sizeof(struct pcx_header));
580 header = context->header;
582 /* convert the multi-byte header variables that will be used */
583 header->xmin = GINT16_FROM_LE(header->xmin);
584 header->ymin = GINT16_FROM_LE(header->ymin);
585 header->xmax = GINT16_FROM_LE(header->xmax);
586 header->ymax = GINT16_FROM_LE(header->ymax);
587 header->bytesperline = GUINT16_FROM_LE(header->bytesperline);
590 g_print ("Manufacturer %d\n"
596 header->manufacturer, header->version,
597 header->encoding, header->bitsperpixel,
598 header->colorplanes, header->palettetype);
601 context->header_loaded = TRUE;
602 fill_pcx_context(context);
604 width = context->width;
605 height = context->height;
606 if(width <= 0 || height <= 0) {
607 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, _("Image has invalid width and/or height"));
610 if (context->size_func)
612 (*context->size_func) (&width, &height, context->user_data);
613 if (width == 0 || height == 0)
617 switch(context->bpp) {
619 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported bpp"));
623 if(context->num_planes < 1 || context->num_planes > 4) {
624 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported number of %d-bit planes"), 1);
631 if(context->num_planes != 1) {
632 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported number of %d-bit planes"), context->bpp);
637 break; /* context's bpp is set to 24 if there are three 8-bit planes */
641 g_print("io-pcx: header loaded\n");
642 g_print("bpp: %u\n", context->bpp);
643 g_print("dimensions: %ux%u\n", context->width, context->height);
646 context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, context->width, context->height);
647 if(!context->pixbuf) {
648 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't create new pixbuf"));
651 context->data = gdk_pixbuf_get_pixels(context->pixbuf);
652 context->rowstride = gdk_pixbuf_get_rowstride(context->pixbuf);
654 context->line = g_try_malloc(sizeof(guchar) * context->bytesperline * context->num_planes);
656 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for line data"));
660 if(context->bpp == 8) {
661 context->p_data = g_try_malloc(sizeof(guchar) * context->width * context->height);
662 if(!context->p_data) {
663 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for paletted data"));
668 if(context->prepared_func)
669 context->prepared_func(context->pixbuf, NULL, context->user_data);
671 context->current_task = PCX_TASK_LOAD_DATA;
677 if(context->current_task == PCX_TASK_LOAD_DATA) {
678 switch(context->bpp) {
680 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported bpp"));
684 retval = pcx_increment_load_data_1(context);
687 retval = pcx_increment_load_data_2(context);
690 retval = pcx_increment_load_data_4(context);
693 retval = pcx_increment_load_data_8(context);
696 retval = pcx_increment_load_data_24(context);
705 gdk_pixbuf__pcx_stop_load(gpointer data, GError **error)
707 struct pcx_context *context = (struct pcx_context *)data;
709 if(context->current_line != context->height) {
710 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("Didn't get all lines of PCX image"));
711 free_pcx_context(context, FALSE);
715 if(context->current_task == PCX_TASK_LOAD_PALETTE) {
716 if(!pcx_load_palette_8(context)) {
717 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("No palette found at end of PCX data"));
718 free_pcx_context(context, FALSE);
723 free_pcx_context(context, FALSE);
729 MODULE_ENTRY (pcx, fill_vtable) (GdkPixbufModule *module)
731 module->begin_load = gdk_pixbuf__pcx_begin_load;
732 module->stop_load = gdk_pixbuf__pcx_stop_load;
733 module->load_increment = gdk_pixbuf__pcx_load_increment;
737 MODULE_ENTRY (pcx, fill_info) (GdkPixbufFormat *info)
739 static GdkPixbufModulePattern signature[] = {
740 { "\x0a \x01", NULL, 100 },
741 { "\x0a\x02\x01", NULL, 100 },
742 { "\x0a\x03\x01", NULL, 100 },
743 { "\x0a\x04\x01", NULL, 100 },
744 { "\x0a\x05\x01", NULL, 100 },
747 static gchar *mime_types[] = {
751 static gchar *extensions[] = {
757 info->signature = signature;
758 info->description = N_("The PCX image format");
759 info->mime_types = mime_types;
760 info->extensions = extensions;
761 info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
762 info->license = "LGPL";