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.
26 #include "gdk-pixbuf-private.h"
27 #include "gdk-pixbuf-io.h"
31 #define PCX_TASK_DONE 0
32 #define PCX_TASK_LOAD_HEADER 1
33 #define PCX_TASK_LOAD_DATA 2
34 #define PCX_TASK_LOAD_PALETTE 3
61 GdkPixbufModuleSizeFunc size_func;
62 GdkPixbufModuleUpdatedFunc updated_func;
63 GdkPixbufModulePreparedFunc prepared_func;
68 gboolean header_loaded;
69 struct pcx_header *header;
85 * set context's image information based on the header
88 fill_pcx_context(struct pcx_context *context)
90 struct pcx_header *header = context->header;
92 context->bpp = header->bitsperpixel;
93 context->width = header->xmax - header->xmin + 1;
94 context->height = header->ymax - header->ymin + 1;
95 context->num_planes = header->colorplanes;
96 context->bytesperline = header->bytesperline;
98 if(header->version == 5 && context->bpp == 8 && context->num_planes == 3)
103 free_pcx_context(struct pcx_context *context, gboolean unref_pixbuf)
106 g_free(context->header);
108 g_free(context->buf);
109 if(unref_pixbuf && context->pixbuf)
110 g_object_unref(context->pixbuf);
112 g_free(context->line);
114 g_free(context->p_data);
120 * read each plane of a single scanline. store_planes is
121 * the number of planes that can be stored in the planes array.
122 * data is the pointer to the block of memory to read
123 * from, size is the length of that data, and line_bytes
124 * is where the number of bytes read will be stored.
127 read_scanline_data(guchar *data, guint size, guchar *planes[],
128 guint store_planes, guint num_planes, guint bytesperline,
136 for(p = 0; p < num_planes; p++) {
137 for(i = 0; i < bytesperline;) { /* i incremented when line byte set */
142 if(byte >> 6 == 0x3) {
143 count = byte & ~(0x3 << 6);
153 for(j = 0; j < count; j++) {
155 planes[p][i++] = byte;
159 if(i >= bytesperline) {
161 if(p >= num_planes) {
171 *line_bytes = d; /* number of bytes read for scanline */
176 gdk_pixbuf__pcx_begin_load(GdkPixbufModuleSizeFunc size_func,
177 GdkPixbufModulePreparedFunc prepared_func,
178 GdkPixbufModuleUpdatedFunc updated_func,
179 gpointer user_data, GError **error)
181 struct pcx_context *context;
183 context = g_new0(struct pcx_context, 1);
187 context->header = g_try_malloc(sizeof(struct pcx_header));
188 if(!context->header) {
190 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for header"));
194 context->size_func = size_func;
195 context->updated_func = updated_func;
196 context->prepared_func = prepared_func;
197 context->user_data = user_data;
199 context->current_task = PCX_TASK_LOAD_HEADER;
201 context->buf = g_try_malloc(sizeof(guchar) * 512);
203 g_free(context->header);
205 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for context buffer"));
208 context->buf_size = 512;
210 context->header_loaded = FALSE;
216 pcx_resize_context_buf(struct pcx_context *context, guint size)
220 new_buf = g_try_realloc(context->buf, size);
224 context->buf = new_buf;
225 context->buf_size = size;
230 * remove a number of bytes (specified by size) from the
231 * beginning of a context's buf
234 pcx_chop_context_buf(struct pcx_context *context, guint size)
238 if(size > context->buf_pos)
245 for(i = 0, j = size; j < context->buf_pos; i++, j++)
246 context->buf[i] = context->buf[j];
248 context->buf_pos -= size;
254 read_pixel_1(guchar *data, guint offset)
261 retval = data[offset] >> 7;
263 bit_offset = offset % 8;
264 offset -= bit_offset;
266 retval = (data[offset] >> (7 - bit_offset)) & 0x1;
273 read_pixel_4(guchar *data, guint offset)
279 retval = data[offset] >> 4;
283 retval = data[offset] & 0xf;
290 pcx_increment_load_data_1(struct pcx_context *context)
297 if(context->num_planes == 4) {
298 planes[0] = context->line;
299 planes[1] = planes[0] + context->bytesperline;
300 planes[2] = planes[1] + context->bytesperline;
301 planes[3] = planes[2] + context->bytesperline;
303 } else if(context->num_planes == 3) {
304 planes[0] = context->line;
305 planes[1] = planes[0] + context->bytesperline;
306 planes[2] = planes[1] + context->bytesperline;
308 } else if(context->num_planes == 2) {
309 planes[0] = context->line;
310 planes[1] = planes[0] + context->bytesperline;
312 } else if(context->num_planes == 1) {
313 planes[0] = context->line;
319 while(read_scanline_data(context->buf, context->buf_pos, planes, store_planes, context->num_planes, context->bytesperline, &line_bytes)) {
320 pcx_chop_context_buf(context, line_bytes);
322 for(i = 0; i < context->width; i++) {
325 if(context->num_planes == 4) {
326 p = read_pixel_1(planes[3], i);
328 p |= read_pixel_1(planes[2], i);
330 p |= read_pixel_1(planes[1], i);
332 p |= read_pixel_1(planes[0], i);
333 } else if(context->num_planes == 3) {
334 p = read_pixel_1(planes[2], i);
336 p |= read_pixel_1(planes[1], i);
338 p |= read_pixel_1(planes[0], i);
339 } else if(context->num_planes == 2) {
340 p = read_pixel_1(planes[1], i);
342 p |= read_pixel_1(planes[0], i);
343 } else if(context->num_planes == 1) {
344 p = read_pixel_1(planes[0], i);
349 context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[p * 3 + 0];
350 context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[p * 3 + 1];
351 context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[p * 3 + 2];
354 if(context->updated_func)
355 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
357 context->current_line++;
359 if(context->current_line == context->height) {
360 context->current_task = PCX_TASK_DONE;
369 pcx_increment_load_data_2(struct pcx_context *context)
376 planes[0] = context->line;
378 while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
379 pcx_chop_context_buf(context, line_bytes);
381 for(i = 0; i < context->width; i++) {
382 shift = 6 - 2 * (i % 4);
383 h = (planes[0][i / 4] >> shift) & 0x3;
384 context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[h * 3 + 0];
385 context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[h * 3 + 1];
386 context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[h * 3 + 2];
389 if(context->updated_func)
390 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
392 context->current_line++;
394 if(context->current_line == context->height) {
395 context->current_task = PCX_TASK_DONE;
404 pcx_increment_load_data_4(struct pcx_context *context)
410 planes[0] = context->line;
412 while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
413 pcx_chop_context_buf(context, line_bytes);
415 for(i = 0; i < context->width; i++) {
418 p = read_pixel_4(planes[0], i) & 0xf;
419 context->data[context->current_line * context->rowstride + i * 3 + 0] = context->header->palette[p * 3 + 0];
420 context->data[context->current_line * context->rowstride + i * 3 + 1] = context->header->palette[p * 3 + 1];
421 context->data[context->current_line * context->rowstride + i * 3 + 2] = context->header->palette[p * 3 + 2];
424 if(context->updated_func)
425 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
427 context->current_line++;
429 if(context->current_line == context->height) {
430 context->current_task = PCX_TASK_DONE;
439 * for loading 8-bit pcx images, we keep a buffer containing
440 * each pixel's palette number; once we've loaded each scanline,
441 * we wait for loading to stop and call pcx_load_palette_8,
442 * which finds the palette at the end of the pcx data and sets the
446 pcx_increment_load_data_8(struct pcx_context *context)
452 planes[0] = context->line;
454 while(read_scanline_data(context->buf, context->buf_pos, planes, 1, context->num_planes, context->bytesperline, &line_bytes)) {
455 pcx_chop_context_buf(context, line_bytes);
457 for(i = 0; i < context->width; i++)
458 context->p_data[context->current_line * context->width + i + 0] = planes[0][i];
460 context->current_line++;
462 if(context->current_line == context->height) {
463 context->current_task = PCX_TASK_LOAD_PALETTE;
472 * read the palette and set the RGB data
475 pcx_load_palette_8(struct pcx_context *context)
479 if(context->current_line < context->height)
482 if(context->buf_pos >= 769) {
483 guchar *palette = context->buf + (context->buf_pos - 769);
485 if(palette[0] == 12) {
487 for(i = 0; i < context->height; i++) {
488 for(j = 0; j < context->width; j++) {
489 context->data[i * context->rowstride + j * 3 + 0] = palette[(context->p_data[i * context->width + j]) * 3 + 0];
490 context->data[i * context->rowstride + j * 3 + 1] = palette[(context->p_data[i * context->width + j]) * 3 + 1];
491 context->data[i * context->rowstride + j * 3 + 2] = palette[(context->p_data[i * context->width + j]) * 3 + 2];
494 if(context->updated_func)
495 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
499 g_print("read palette\n");
502 context->current_task = PCX_TASK_DONE;
506 g_print("this ain't a palette\n");
516 * in 24-bit images, each scanline has three color planes
517 * for red, green, and blue, respectively.
520 pcx_increment_load_data_24(struct pcx_context *context)
526 planes[0] = context->line;
527 planes[1] = planes[0] + context->bytesperline;
528 planes[2] = planes[1] + context->bytesperline;
530 while(read_scanline_data(context->buf, context->buf_pos, planes, 3, context->num_planes, context->bytesperline, &line_bytes)) {
531 pcx_chop_context_buf(context, line_bytes);
533 for(i = 0; i < context->width; i++) {
534 context->data[context->current_line * context->rowstride + i * 3 + 0] = planes[0][i];
535 context->data[context->current_line * context->rowstride + i * 3 + 1] = planes[1][i];
536 context->data[context->current_line * context->rowstride + i * 3 + 2] = planes[2][i];
539 if(context->updated_func)
540 context->updated_func(context->pixbuf, 0, context->current_line, context->width, 1, context->user_data);
542 context->current_line++;
544 if(context->current_line == context->height) {
545 context->current_task = PCX_TASK_DONE;
554 gdk_pixbuf__pcx_load_increment(gpointer data, const guchar *buf, guint size,
557 struct pcx_context *context = (struct pcx_context *)data;
558 struct pcx_header *header;
560 gboolean retval = TRUE;
562 /* if context's buf isn't large enough to hold its current data plus the passed buf, increase its size */
563 if(context->buf_pos + size > context->buf_size) {
564 if(!pcx_resize_context_buf(context, sizeof(guchar) * (context->buf_pos + size))) {
565 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for context buffer"));
570 for(i = 0; i < size; i++)
571 context->buf[context->buf_pos++] = buf[i];
573 if(context->current_task == PCX_TASK_LOAD_HEADER) {
574 if(!context->header_loaded && context->buf_pos > sizeof(struct pcx_header)) { /* set header */
577 memcpy(context->header, context->buf, sizeof(struct pcx_header));
578 pcx_chop_context_buf(context, sizeof(struct pcx_header));
579 header = context->header;
581 /* convert the multi-byte header variables that will be used */
582 header->xmin = GINT16_FROM_LE(header->xmin);
583 header->ymin = GINT16_FROM_LE(header->ymin);
584 header->xmax = GINT16_FROM_LE(header->xmax);
585 header->ymax = GINT16_FROM_LE(header->ymax);
586 header->bytesperline = GUINT16_FROM_LE(header->bytesperline);
589 g_print ("Manufacturer %d\n"
595 header->manufacturer, header->version,
596 header->encoding, header->bitsperpixel,
597 header->colorplanes, header->palettetype);
600 context->header_loaded = TRUE;
601 fill_pcx_context(context);
603 width = context->width;
604 height = context->height;
605 if(width <= 0 || height <= 0) {
606 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_CORRUPT_IMAGE, _("Image has invalid width and/or height"));
609 if(context->size_func)
610 context->size_func(&width, &height, context->user_data);
612 switch(context->bpp) {
614 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported bpp"));
618 if(context->num_planes < 1 || context->num_planes > 4) {
619 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported number of %d-bit planes"), 1);
626 if(context->num_planes != 1) {
627 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported number of %d-bit planes"), context->bpp);
632 break; /* context's bpp is set to 24 if there are three 8-bit planes */
636 g_print("io-pcx: header loaded\n");
637 g_print("bpp: %u\n", context->bpp);
638 g_print("dimensions: %ux%u\n", context->width, context->height);
641 context->pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, context->width, context->height);
642 if(!context->pixbuf) {
643 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't create new pixbuf"));
646 context->data = gdk_pixbuf_get_pixels(context->pixbuf);
647 context->rowstride = gdk_pixbuf_get_rowstride(context->pixbuf);
649 context->line = g_try_malloc(sizeof(guchar) * context->bytesperline * context->num_planes);
651 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for line data"));
655 if(context->bpp == 8) {
656 context->p_data = g_try_malloc(sizeof(guchar) * context->width * context->height);
657 if(!context->p_data) {
658 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, _("Couldn't allocate memory for paletted data"));
663 if(context->prepared_func)
664 context->prepared_func(context->pixbuf, NULL, context->user_data);
666 context->current_task = PCX_TASK_LOAD_DATA;
672 if(context->current_task == PCX_TASK_LOAD_DATA) {
673 switch(context->bpp) {
675 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_UNKNOWN_TYPE, _("Image has unsupported bpp"));
679 retval = pcx_increment_load_data_1(context);
682 retval = pcx_increment_load_data_2(context);
685 retval = pcx_increment_load_data_4(context);
688 retval = pcx_increment_load_data_8(context);
691 retval = pcx_increment_load_data_24(context);
700 gdk_pixbuf__pcx_stop_load(gpointer data, GError **error)
702 struct pcx_context *context = (struct pcx_context *)data;
704 if(context->current_line != context->height) {
705 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("Didn't get all lines of PCX image"));
706 free_pcx_context(context, FALSE);
710 if(context->current_task == PCX_TASK_LOAD_PALETTE) {
711 if(!pcx_load_palette_8(context)) {
712 g_set_error(error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_FAILED, _("No palette found at end of PCX data"));
713 free_pcx_context(context, FALSE);
718 free_pcx_context(context, FALSE);
724 MODULE_ENTRY (pcx, fill_vtable) (GdkPixbufModule *module)
726 module->begin_load = gdk_pixbuf__pcx_begin_load;
727 module->stop_load = gdk_pixbuf__pcx_stop_load;
728 module->load_increment = gdk_pixbuf__pcx_load_increment;
732 MODULE_ENTRY (pcx, fill_info) (GdkPixbufFormat *info)
734 static GdkPixbufModulePattern signature[] = {
735 { "\x0a \x01", NULL, 100 },
736 { "\x0a\x02\x01", NULL, 100 },
737 { "\x0a\x03\x01", NULL, 100 },
738 { "\x0a\x04\x01", NULL, 100 },
739 { "\x0a\x05\x01", NULL, 100 },
742 static gchar *mime_types[] = {
746 static gchar *extensions[] = {
752 info->signature = signature;
753 info->description = N_("The PCX image format");
754 info->mime_types = mime_types;
755 info->extensions = extensions;