summaryrefslogtreecommitdiff
path: root/gdk-pixbuf/io-tga.c
blob: 5015cacd643863c3b07a35dd88567ec407913180 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/* -*- mode: C; c-file-style: "linux" -*- */
/* 
 * GdkPixbuf library - TGA image loader
 * Copyright (C) 1999 Nicola Girardi <nikke@swlibero.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 * Some NOTES about the TGA loader (2001/06/07, nikke@swlibero.org)
 *
 * - The TGAFooter isn't present in all TGA files.  In fact, there's an older
 *   format specification, still in use, which doesn't cover the TGAFooter.
 *   Actually, most TGA files I have are of the older type.  Anyway I put the 
 *   struct declaration here for completeness.
 *
 * - Error handling was designed to be very paranoid.
 */

#include "config.h"
#include <stdio.h>
#include <string.h>

#include "gdk-pixbuf-private.h"
#include "gdk-pixbuf-buffer-queue-private.h"

#undef DEBUG_TGA

#define TGA_INTERLEAVE_MASK     0xc0
#define TGA_INTERLEAVE_NONE     0x00
#define TGA_INTERLEAVE_2WAY     0x40
#define TGA_INTERLEAVE_4WAY     0x80

#define TGA_ORIGIN_MASK         0x30
#define TGA_ORIGIN_RIGHT        0x10
#define TGA_ORIGIN_UPPER        0x20

enum {
	TGA_TYPE_NODATA = 0,
	TGA_TYPE_PSEUDOCOLOR = 1,
	TGA_TYPE_TRUECOLOR = 2,
	TGA_TYPE_GRAYSCALE = 3,
	TGA_TYPE_RLE_PSEUDOCOLOR = 9,
	TGA_TYPE_RLE_TRUECOLOR = 10,
	TGA_TYPE_RLE_GRAYSCALE = 11
};

#define LE16(p) ((p)[0] + ((p)[1] << 8))

typedef struct _TGAHeader TGAHeader;
typedef struct _TGAFooter TGAFooter;

typedef struct _TGAColor TGAColor;
typedef struct _TGAColormap TGAColormap;

typedef struct _TGAContext TGAContext;

struct _TGAHeader {
	guint8 infolen;
	guint8 has_cmap;
	guint8 type;
	
	guint8 cmap_start[2];
	guint8 cmap_n_colors[2];
	guint8 cmap_bpp;
	
	guint8 x_origin[2];
	guint8 y_origin[2];
	
	guint8 width[2];
	guint8 height[2];
	guint8 bpp;
	
	guint8 flags;
};

struct _TGAFooter {
	guint32 extension_area_offset;
	guint32 developer_directory_offset;

	/* Standard TGA signature, "TRUEVISION-XFILE.\0". */
	union {
		gchar sig_full[18];
		struct {
			gchar sig_chunk[16];
			gchar dot, null;
		} sig_struct;
	} sig;
};

struct _TGAColor {
	guchar r, g, b, a;
};

struct _TGAColormap {
	guint n_colors;
	TGAColor colors[1];
};

typedef gboolean (* TGAProcessFunc) (TGAContext *ctx, GError **error);

struct _TGAContext {
	TGAHeader *hdr;

	TGAColormap *cmap;
	guint cmap_size;

	GdkPixbuf *pbuf;
	int pbuf_x;
	int pbuf_y;
	int pbuf_y_notified;

	GdkPixbufBufferQueue *input;

        TGAProcessFunc process;

	GdkPixbufModuleSizeFunc sfunc;
	GdkPixbufModulePreparedFunc pfunc;
	GdkPixbufModuleUpdatedFunc ufunc;
	gpointer udata;
};

static TGAColormap *
colormap_new (guint n_colors)
{
  TGAColormap *cmap;

  g_assert (n_colors <= G_MAXUINT16);

  cmap = g_try_malloc0 (sizeof (TGAColormap) + (MAX (n_colors, 1) - 1) * sizeof (TGAColor));
  if (cmap == NULL)
    return NULL;

  cmap->n_colors = n_colors;

  return cmap;
}

static const TGAColor *
colormap_get_color (TGAColormap *cmap,
                    guint        id)
{
  static const TGAColor transparent_black = { 0, 0, 0, 0 };

  if (id >= cmap->n_colors)
    return &transparent_black;

  return &cmap->colors[id];
}

static void
colormap_set_color (TGAColormap    *cmap,
                    guint           id,
                    const TGAColor *color)
{
  if (id >= cmap->n_colors)
    return;

  cmap->colors[id] = *color;
}

static void
colormap_free (TGAColormap *cmap)
{
  g_free (cmap);
}

static gboolean
tga_skip_rest_of_image (TGAContext  *ctx,
                        GError     **err)
{
  gdk_pixbuf_buffer_queue_flush (ctx->input, gdk_pixbuf_buffer_queue_get_size (ctx->input));

  return TRUE;
}

static inline void
tga_write_pixel (TGAContext     *ctx,
                 const TGAColor *color)
{
  guint x = (ctx->hdr->flags & TGA_ORIGIN_RIGHT) ? ctx->pbuf->width - ctx->pbuf_x - 1 : ctx->pbuf_x;
  guint y = (ctx->hdr->flags & TGA_ORIGIN_UPPER) ? ctx->pbuf_y : ctx->pbuf->height - ctx->pbuf_y - 1;

  memcpy (ctx->pbuf->pixels + y * ctx->pbuf->rowstride + x * ctx->pbuf->n_channels, color, ctx->pbuf->n_channels);

  ctx->pbuf_x++;
  if (ctx->pbuf_x >= ctx->pbuf->width)
    {
      ctx->pbuf_x = 0;
      ctx->pbuf_y++;
    }
}

static gsize
tga_pixels_remaining (TGAContext *ctx)
{
  return ctx->pbuf->width * (ctx->pbuf->height - ctx->pbuf_y) - ctx->pbuf_x;
}

static gboolean
tga_all_pixels_written (TGAContext *ctx)
{
  return ctx->pbuf_y >= ctx->pbuf->height;
}

static void
tga_emit_update (TGAContext *ctx)
{
  if (!ctx->ufunc)
    return;

  /* We only notify row-by-row for now.
   * I was too lazy to handle line-breaks.
   */
  if (ctx->pbuf_y_notified == ctx->pbuf_y)
    return;

  if (ctx->hdr->flags & TGA_ORIGIN_UPPER)
    (*ctx->ufunc) (ctx->pbuf,
                   0, ctx->pbuf_y_notified,
                   ctx->pbuf->width, ctx->pbuf_y - ctx->pbuf_y_notified,
                   ctx->udata);
  else
    (*ctx->ufunc) (ctx->pbuf,
                   0, ctx->pbuf->height - ctx->pbuf_y,
                   ctx->pbuf->width, ctx->pbuf_y - ctx->pbuf_y_notified,
                   ctx->udata);

  ctx->pbuf_y_notified = ctx->pbuf_y;
}

static gboolean
tga_format_supported (guint type,
                      guint bits_per_pixel)
{
  switch (type)
    {
      case TGA_TYPE_PSEUDOCOLOR:
      case TGA_TYPE_RLE_PSEUDOCOLOR:
        return bits_per_pixel == 8;

      case TGA_TYPE_TRUECOLOR:
      case TGA_TYPE_RLE_TRUECOLOR:
        return bits_per_pixel == 16
            || bits_per_pixel == 24
            || bits_per_pixel == 32;

      case TGA_TYPE_GRAYSCALE:
      case TGA_TYPE_RLE_GRAYSCALE:
        return bits_per_pixel == 8
            || bits_per_pixel == 16;

      default:
        return FALSE;
    }
}

static inline void
tga_read_pixel (TGAContext   *ctx,
                const guchar *data,
                TGAColor     *color)
{
  switch (ctx->hdr->type)
    {
      case TGA_TYPE_PSEUDOCOLOR:
      case TGA_TYPE_RLE_PSEUDOCOLOR:
        *color = *colormap_get_color (ctx->cmap, data[0]);
        break;

      case TGA_TYPE_TRUECOLOR:
      case TGA_TYPE_RLE_TRUECOLOR:
        if (ctx->hdr->bpp == 16)
          {
            guint16 col = data[0] + (data[1] << 8);
            color->r = (col >> 7) & 0xf8;
            color->r |= color->r >> 5;
            color->g = (col >> 2) & 0xf8;
            color->g |= color->g >> 5;
            color->b = col << 3;
            color->b |= color->b >> 5;
            color->a = 255;
          }
        else
          {
            color->b = data[0];
            color->g = data[1];
            color->r = data[2];
            if (ctx->hdr->bpp == 32)
              color->a = data[3];
            else
              color->a = 255;
          }
        break;

      case TGA_TYPE_GRAYSCALE:
      case TGA_TYPE_RLE_GRAYSCALE:
        color->r = color->g = color->b = data[0];
        if (ctx->hdr->bpp == 16)
          color->a = data[1];
        else
          color->a = 255;
        break;

      default:
        g_assert_not_reached ();
    }
}

static gboolean fill_in_context(TGAContext *ctx, GError **err)
{
	gboolean alpha;
	guint w, h;

	g_return_val_if_fail(ctx != NULL, FALSE);

        ctx->cmap_size = ((ctx->hdr->cmap_bpp + 7) >> 3) *
                LE16(ctx->hdr->cmap_n_colors);
        ctx->cmap = colormap_new (LE16(ctx->hdr->cmap_n_colors));
	if (!ctx->cmap) {
		g_set_error_literal(err, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                                    _("Cannot allocate colormap"));
		return FALSE;
	}

	alpha = ((ctx->hdr->bpp == 16) || 
		 (ctx->hdr->bpp == 32) ||
		 (ctx->hdr->has_cmap && (ctx->hdr->cmap_bpp == 32)));

	w = LE16(ctx->hdr->width);
	h = LE16(ctx->hdr->height);

	if (ctx->sfunc) {
		gint wi = w;
		gint hi = h;
		
		(*ctx->sfunc) (&wi, &hi, ctx->udata);
		
		if (wi == 0 || hi == 0) 
			return FALSE;
	}

	ctx->pbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, alpha, 8, w, h);

	if (!ctx->pbuf) {
		g_set_error_literal(err, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                                    _("Cannot allocate new pixbuf"));
		return FALSE;
	}

	return TRUE;
}

static gboolean
tga_load_image (TGAContext  *ctx,
                GError     **err)
{
  TGAColor color;
  GBytes *bytes;
  gsize i, size, bytes_per_pixel;
  const guchar *data;

  bytes_per_pixel = (ctx->hdr->bpp + 7) / 8;
  size = gdk_pixbuf_buffer_queue_get_size (ctx->input) / bytes_per_pixel;
  size = MIN (size, tga_pixels_remaining (ctx));

  bytes = gdk_pixbuf_buffer_queue_pull (ctx->input, size * bytes_per_pixel);
  g_assert (bytes != NULL);

  data = g_bytes_get_data (bytes, NULL);

  for (i = 0; i < size; i++)
    {
      tga_read_pixel (ctx, data, &color);
      tga_write_pixel (ctx, &color);
      data += bytes_per_pixel;
    }

  g_bytes_unref (bytes);

  tga_emit_update (ctx);
  
  if (tga_all_pixels_written (ctx))
    ctx->process = tga_skip_rest_of_image;
  return TRUE;
}

static gboolean
tga_load_rle_image (TGAContext  *ctx,
                    GError     **err)
{
        GBytes *bytes;
	TGAColor color;
	guint rle_num, raw_num;
	const guchar *s;
        guchar tag;
	gsize n, size, bytes_per_pixel;

        bytes_per_pixel = (ctx->hdr->bpp + 7) / 8;
        bytes = gdk_pixbuf_buffer_queue_peek (ctx->input, gdk_pixbuf_buffer_queue_get_size (ctx->input));
	s = g_bytes_get_data (bytes, &size);

	for (n = 0; n < size; ) {
		tag = *s;
		s++, n++;
		if (tag & 0x80) {
			if (n + bytes_per_pixel > size) {
				--n;
                                break;
			} else {
				rle_num = (tag & 0x7f) + 1;
                                tga_read_pixel (ctx, s, &color);
				s += bytes_per_pixel;
				n += bytes_per_pixel;
                                rle_num = MIN (rle_num, tga_pixels_remaining (ctx));
                                for (; rle_num; rle_num--)
                                  {
                                    tga_write_pixel (ctx, &color);
                                  }
	                        if (tga_all_pixels_written (ctx))
                                        break;
			}
		} else {
			raw_num = tag + 1;
			if (n + (raw_num * bytes_per_pixel) > size) {
			        --n;
                                break;
			} else {
                                raw_num = MIN (raw_num, tga_pixels_remaining (ctx));
				for (; raw_num; raw_num--) {
                                        tga_read_pixel (ctx, s, &color);
					s += bytes_per_pixel;
					n += bytes_per_pixel;
                                        tga_write_pixel (ctx, &color);
				}
				
	                        if (tga_all_pixels_written (ctx))
                                        break;
			}
		}
	}

        g_bytes_unref (bytes);
        gdk_pixbuf_buffer_queue_flush (ctx->input, n);

        tga_emit_update (ctx);

	if (tga_all_pixels_written (ctx))
                ctx->process = tga_skip_rest_of_image;
        return TRUE;
}

static gboolean
tga_load_colormap (TGAContext  *ctx,
                   GError     **err)
{
  GBytes *bytes;
  TGAColor color;
  const guchar *p;
  guint i, n_colors;

  if (ctx->hdr->has_cmap)
    {
      bytes = gdk_pixbuf_buffer_queue_pull (ctx->input, ctx->cmap_size);
      if (bytes == NULL)
        return TRUE;

      n_colors = LE16(ctx->hdr->cmap_n_colors);

      p = g_bytes_get_data (bytes, NULL);
      color.a = 255;

      for (i = 0; i < n_colors; i++)
        {
          if ((ctx->hdr->cmap_bpp == 15) || (ctx->hdr->cmap_bpp == 16))
            {
              guint16 col = p[0] + (p[1] << 8);
              color.b = (col >> 7) & 0xf8;
              color.g = (col >> 2) & 0xf8;
              color.r = col << 3;
              p += 2;
            }
          else if ((ctx->hdr->cmap_bpp == 24) || (ctx->hdr->cmap_bpp == 32))
            {
              color.b = *p++;
              color.g = *p++;
              color.r = *p++;
              if (ctx->hdr->cmap_bpp == 32)
                color.a = *p++;
            }
          else
            {
              g_set_error_literal (err, GDK_PIXBUF_ERROR, 
                                   GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                   _("Unexpected bitdepth for colormap entries"));
              g_bytes_unref (bytes);
              return FALSE;
            }
          colormap_set_color (ctx->cmap, i, &color);
        }

      g_bytes_unref (bytes);
    }
  else
    {
      if ((ctx->hdr->type == TGA_TYPE_PSEUDOCOLOR)
          || (ctx->hdr->type == TGA_TYPE_RLE_PSEUDOCOLOR))
        {
          g_set_error_literal (err, GDK_PIXBUF_ERROR, 
                               GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                               _("Pseudocolor image does not contain a colormap"));
          return FALSE;
        }
    }
  
  if ((ctx->hdr->type == TGA_TYPE_RLE_PSEUDOCOLOR)
      || (ctx->hdr->type == TGA_TYPE_RLE_TRUECOLOR)
      || (ctx->hdr->type == TGA_TYPE_RLE_GRAYSCALE))
    ctx->process = tga_load_rle_image;
  else
    ctx->process = tga_load_image;

  return TRUE;
}

static gboolean
tga_read_info (TGAContext  *ctx,
               GError     **err)
{
  if (gdk_pixbuf_buffer_queue_get_size (ctx->input) < ctx->hdr->infolen)
    return TRUE;
  
  gdk_pixbuf_buffer_queue_flush (ctx->input, ctx->hdr->infolen);

  ctx->process = tga_load_colormap;
  return TRUE;
}

static gboolean
tga_load_header (TGAContext  *ctx,
                 GError     **err)
{
  GBytes *bytes;
  
  bytes = gdk_pixbuf_buffer_queue_pull (ctx->input, sizeof (TGAHeader));
  if (bytes == NULL)
    return TRUE;

  ctx->hdr = g_try_malloc (sizeof (TGAHeader));
  if (!ctx->hdr)
    {
      g_set_error_literal (err, GDK_PIXBUF_ERROR,
                           GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                           _("Cannot allocate TGA header memory"));
      return FALSE;
  }
  g_memmove(ctx->hdr, g_bytes_get_data (bytes, NULL), sizeof(TGAHeader));
  g_bytes_unref (bytes);
#ifdef DEBUG_TGA
  g_print ("infolen %d "
           "has_cmap %d "
           "type %d "
           "cmap_start %d "
           "cmap_n_colors %d "
           "cmap_bpp %d "
           "x %d y %d width %d height %d bpp %d "
           "flags %#x",
           ctx->hdr->infolen,
           ctx->hdr->has_cmap,
           ctx->hdr->type,
           LE16(ctx->hdr->cmap_start),
           LE16(ctx->hdr->cmap_n_colors),
           ctx->hdr->cmap_bpp,
           LE16(ctx->hdr->x_origin),
           LE16(ctx->hdr->y_origin),
           LE16(ctx->hdr->width),
           LE16(ctx->hdr->height),
           ctx->hdr->bpp,
           ctx->hdr->flags);
#endif
  if (LE16(ctx->hdr->width) == 0 || 
      LE16(ctx->hdr->height) == 0) {
          g_set_error_literal(err, GDK_PIXBUF_ERROR,
                              GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                              _("TGA image has invalid dimensions"));
          return FALSE;
  }
  if ((ctx->hdr->flags & TGA_INTERLEAVE_MASK) != TGA_INTERLEAVE_NONE) {
          g_set_error_literal(err, GDK_PIXBUF_ERROR, 
                              GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
                              _("TGA image type not supported"));
          return FALSE;
  }
  if (!tga_format_supported (ctx->hdr->type, ctx->hdr->bpp))
    {
      g_set_error_literal(err, GDK_PIXBUF_ERROR,
                          GDK_PIXBUF_ERROR_UNKNOWN_TYPE,
                          _("TGA image type not supported"));
      return FALSE;
    }

  if (!fill_in_context(ctx, err))
          return FALSE;

  if (ctx->pfunc)
          (*ctx->pfunc) (ctx->pbuf, NULL, ctx->udata);

  ctx->process = tga_read_info;
  return TRUE;
}

static gpointer gdk_pixbuf__tga_begin_load(GdkPixbufModuleSizeFunc f0,
                                           GdkPixbufModulePreparedFunc f1,
					   GdkPixbufModuleUpdatedFunc f2,
					   gpointer udata, GError **err)
{
	TGAContext *ctx;

	ctx = g_try_malloc(sizeof(TGAContext));
	if (!ctx) {
		g_set_error_literal(err, GDK_PIXBUF_ERROR, 
                                    GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY,
                                    _("Cannot allocate memory for TGA context struct"));
		return NULL;
	}

	ctx->hdr = NULL;

	ctx->cmap = NULL;
	ctx->cmap_size = 0;

	ctx->pbuf = NULL;
        ctx->pbuf_x = 0;
        ctx->pbuf_y = 0;
        ctx->pbuf_y_notified = 0;

	ctx->input = gdk_pixbuf_buffer_queue_new ();

        ctx->process = tga_load_header;

	ctx->sfunc = f0;
	ctx->pfunc = f1;
	ctx->ufunc = f2;
	ctx->udata = udata;

	return ctx;
}

static gboolean gdk_pixbuf__tga_load_increment(gpointer data,
					       const guchar *buffer,
					       guint size,
					       GError **err)
{
	TGAContext *ctx = (TGAContext*) data;
        TGAProcessFunc process;

	g_return_val_if_fail(buffer != NULL, TRUE);
        gdk_pixbuf_buffer_queue_push (ctx->input, g_bytes_new (buffer, size));

        do
          {
            process = ctx->process;

            if (!process (ctx, err))
              return FALSE;
          }
        while (process != ctx->process);

	return TRUE;
}

static gboolean gdk_pixbuf__tga_stop_load(gpointer data, GError **err)
{
	TGAContext *ctx = (TGAContext *) data;
        gboolean retval = TRUE;

	g_return_val_if_fail (ctx != NULL, FALSE);

        if (ctx->pbuf == NULL || tga_pixels_remaining (ctx))
          {
            g_set_error_literal (err,
                                 GDK_PIXBUF_ERROR,
                                 GDK_PIXBUF_ERROR_CORRUPT_IMAGE,
                                 _("TGA image was truncated or incomplete."));

            retval = FALSE;
          }

	g_free (ctx->hdr);
	if (ctx->cmap)
          colormap_free (ctx->cmap);
	if (ctx->pbuf)
          g_object_unref (ctx->pbuf);
	gdk_pixbuf_buffer_queue_unref (ctx->input);
	g_free (ctx);
	return TRUE;
}

#ifndef INCLUDE_tga
#define MODULE_ENTRY(function) G_MODULE_EXPORT void function
#else
#define MODULE_ENTRY(function) void _gdk_pixbuf__tga_ ## function
#endif

MODULE_ENTRY (fill_vtable) (GdkPixbufModule *module)
{
	module->begin_load = gdk_pixbuf__tga_begin_load;
	module->stop_load = gdk_pixbuf__tga_stop_load;
	module->load_increment = gdk_pixbuf__tga_load_increment;
}

MODULE_ENTRY (fill_info) (GdkPixbufFormat *info)
{
	static const GdkPixbufModulePattern signature[] = {
		{ " \x1\x1", "x  ", 100 },
		{ " \x1\x9", "x  ", 100 },
		{ "  \x2", "xz ",  99 }, /* only 99 since .CUR also matches this */
		{ "  \x3", "xz ", 100 },
		{ "  \xa", "xz ", 100 },
		{ "  \xb", "xz ", 100 },
		{ NULL, NULL, 0 }
	};
	static const gchar *mime_types[] = {
		"image/x-tga",
		NULL
	};
	static const gchar *extensions[] = {
		"tga",
		"targa",
		NULL
	};

	info->name = "tga";
	info->signature = (GdkPixbufModulePattern *) signature;
	info->description = NC_("image format", "Targa");
	info->mime_types = (gchar **) mime_types;
	info->extensions = (gchar **) extensions;
	info->flags = GDK_PIXBUF_FORMAT_THREADSAFE;
	info->license = "LGPL";
}