+/*
+=item tga_header_unpack(header, headbuf)
+
+Unpacks the header structure into from buffer and stores
+in the header structure.
+
+ header - header structure
+ headbuf - buffer to unpack from
+
+=cut
+*/
+
+static
+void
+tga_header_unpack(tga_header *header, unsigned char headbuf[18]) {
+ header->idlength = headbuf[0];
+ header->colourmaptype = headbuf[1];
+ header->datatypecode = headbuf[2];
+ header->colourmaporigin = (headbuf[4] << 8) + headbuf[3];
+ header->colourmaplength = (headbuf[6] << 8) + headbuf[5];
+ header->colourmapdepth = headbuf[7];
+ header->x_origin = (headbuf[9] << 8) + headbuf[8];
+ header->y_origin = (headbuf[11] << 8) + headbuf[10];
+ header->width = (headbuf[13] << 8) + headbuf[12];
+ header->height = (headbuf[15] << 8) + headbuf[14];
+ header->bitsperpixel = headbuf[16];
+ header->imagedescriptor = headbuf[17];
+}
+
+
+
+int
+tga_header_verify(unsigned char headbuf[18]) {
+ tga_header header;
+ tga_header_unpack(&header, headbuf);
+ switch (header.datatypecode) {
+ default:
+ printf("bad typecode!\n");
+ return 0;
+ case 0:
+ case 1: /* Uncompressed, color-mapped images */
+ case 2: /* Uncompressed, rgb images */
+ case 3: /* Uncompressed, grayscale images */
+ case 9: /* Compressed, color-mapped images */
+ case 10: /* Compressed, rgb images */
+ case 11: /* Compressed, grayscale images */
+ break;
+ }
+
+ switch (header.colourmaptype) {
+ default:
+ printf("bad colourmaptype!\n");
+ return 0;
+ case 0:
+ case 1:
+ break;
+ }
+
+ return 1;
+}
+
+
+/*
+=item tga_header_pack(header, headbuf)
+
+Packs header structure into buffer for writing.
+
+ header - header structure
+ headbuf - buffer to pack into
+
+=cut
+*/
+
+static
+void
+tga_header_pack(tga_header *header, unsigned char headbuf[18]) {
+ headbuf[0] = header->idlength;
+ headbuf[1] = header->colourmaptype;
+ headbuf[2] = header->datatypecode;
+ headbuf[3] = header->colourmaporigin & 0xff;
+ headbuf[4] = header->colourmaporigin >> 8;
+ headbuf[5] = header->colourmaplength & 0xff;
+ headbuf[6] = header->colourmaplength >> 8;
+ headbuf[7] = header->colourmapdepth;
+ headbuf[8] = header->x_origin & 0xff;
+ headbuf[9] = header->x_origin >> 8;
+ headbuf[10] = header->y_origin & 0xff;
+ headbuf[11] = header->y_origin >> 8;
+ headbuf[12] = header->width & 0xff;
+ headbuf[13] = header->width >> 8;
+ headbuf[14] = header->height & 0xff;
+ headbuf[15] = header->height >> 8;
+ headbuf[16] = header->bitsperpixel;
+ headbuf[17] = header->imagedescriptor;
+}
+
+
+/*
+=item tga_source_read(s, buf, pixels)
+
+Reads pixel number of pixels from source s into buffer buf. Takes
+care of decompressing the stream if needed.