Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Truevision Targa Reader/Writer
Toshihiro Shimizu 890ddd
 * Copyright (C) 2001-2003, Emil Mikulic.
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * Source and binary redistribution of this code, with or without changes, for
Toshihiro Shimizu 890ddd
 * free or for profit, is allowed as long as this copyright notice is kept
Toshihiro Shimizu 890ddd
 * intact.  Modified versions must be clearly marked as modified.
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * This code is provided without any warranty.  The copyright holder is
Toshihiro Shimizu 890ddd
 * not liable for anything bad that might happen as a result of the
Toshihiro Shimizu 890ddd
 * code.
Toshihiro Shimizu 890ddd
 * -------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * preso da http://dmr.ath.cx/gfx/targa/
Toshihiro Shimizu 890ddd
 * modificato da gmt
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*@unused@*/ static const char rcsid[] =
Toshihiro Shimizu 890ddd
	"$Id: targa.c,v 1.7 2003/06/21 09:30:53 emikulic Exp $";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define TGA_KEEP_MACROS /* BIT, htole16, letoh16 */
Toshihiro Shimizu 890ddd
#include "targa.h"
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <string.h> /* memcpy, memcmp */</string.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define SANE_DEPTH(x) ((x) == 8 || (x) == 16 || (x) == 24 || (x) == 32)
Toshihiro Shimizu 890ddd
#define UNMAP_DEPTH(x) ((x) == 16 || (x) == 24 || (x) == 32)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static const char tga_id[] =
Toshihiro Shimizu 890ddd
	"\0\0\0\0" /* extension area offset */
Toshihiro Shimizu 890ddd
	"\0\0\0\0" /* developer directory offset */
Toshihiro Shimizu 890ddd
	"TRUEVISION-XFILE.";
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static const size_t tga_id_length = 26; /* tga_id + \0 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* helpers */
Toshihiro Shimizu 890ddd
static tga_result tga_read_rle(tga_image *dest, FILE *fp);
Toshihiro Shimizu 890ddd
static tga_result tga_write_row_RLE(FILE *fp,
Toshihiro Shimizu 890ddd
									const tga_image *src, const uint8_t *row);
Toshihiro Shimizu 890ddd
typedef enum { RAW,
Toshihiro Shimizu 890ddd
			   RLE } packet_type;
Toshihiro Shimizu 890ddd
static packet_type rle_packet_type(const uint8_t *row, const uint16_t pos,
Toshihiro Shimizu 890ddd
								   const uint16_t width, const uint16_t bpp);
Toshihiro Shimizu 890ddd
static uint8_t rle_packet_len(const uint8_t *row, const uint16_t pos,
Toshihiro Shimizu 890ddd
							  const uint16_t width, const uint16_t bpp, const packet_type type);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
uint8_t tga_get_attribute_bits(const tga_image *tga)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tga->image_descriptor & TGA_ATTRIB_BITS;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tga_is_right_to_left(const tga_image *tga)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (tga->image_descriptor & TGA_R_TO_L_BIT) != 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tga_is_top_to_bottom(const tga_image *tga)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (tga->image_descriptor & TGA_T_TO_B_BIT) != 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tga_is_colormapped(const tga_image *tga)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (
Toshihiro Shimizu 890ddd
		tga->image_type == TGA_IMAGE_TYPE_COLORMAP ||
Toshihiro Shimizu 890ddd
		tga->image_type == TGA_IMAGE_TYPE_COLORMAP_RLE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tga_is_rle(const tga_image *tga)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (
Toshihiro Shimizu 890ddd
		tga->image_type == TGA_IMAGE_TYPE_COLORMAP_RLE ||
Toshihiro Shimizu 890ddd
		tga->image_type == TGA_IMAGE_TYPE_BGR_RLE ||
Toshihiro Shimizu 890ddd
		tga->image_type == TGA_IMAGE_TYPE_MONO_RLE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int tga_is_mono(const tga_image *tga)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (
Toshihiro Shimizu 890ddd
		tga->image_type == TGA_IMAGE_TYPE_MONO ||
Toshihiro Shimizu 890ddd
		tga->image_type == TGA_IMAGE_TYPE_MONO_RLE);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Convert the numerical <errcode> into a verbose error string.</errcode>
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * Returns: an error string
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
const char *tga_error(const tga_result errcode)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (errcode) {
Toshihiro Shimizu 890ddd
	case TGA_NOERR:
Toshihiro Shimizu 890ddd
		return "no error";
Toshihiro Shimizu 890ddd
	case TGAERR_FOPEN:
Toshihiro Shimizu 890ddd
		return "error opening file";
Toshihiro Shimizu 890ddd
	case TGAERR_EOF:
Toshihiro Shimizu 890ddd
		return "premature end of file";
Toshihiro Shimizu 890ddd
	case TGAERR_WRITE:
Toshihiro Shimizu 890ddd
		return "error writing to file";
Toshihiro Shimizu 890ddd
	case TGAERR_CMAP_TYPE:
Toshihiro Shimizu 890ddd
		return "invalid color map type";
Toshihiro Shimizu 890ddd
	case TGAERR_IMG_TYPE:
Toshihiro Shimizu 890ddd
		return "invalid image type";
Toshihiro Shimizu 890ddd
	case TGAERR_NO_IMG:
Toshihiro Shimizu 890ddd
		return "no image data included";
Toshihiro Shimizu 890ddd
	case TGAERR_CMAP_MISSING:
Toshihiro Shimizu 890ddd
		return "color-mapped image without color map";
Toshihiro Shimizu 890ddd
	case TGAERR_CMAP_PRESENT:
Toshihiro Shimizu 890ddd
		return "non-color-mapped image with extraneous color map";
Toshihiro Shimizu 890ddd
	case TGAERR_CMAP_LENGTH:
Toshihiro Shimizu 890ddd
		return "color map has zero length";
Toshihiro Shimizu 890ddd
	case TGAERR_CMAP_DEPTH:
Toshihiro Shimizu 890ddd
		return "invalid color map depth";
Toshihiro Shimizu 890ddd
	case TGAERR_ZERO_SIZE:
Toshihiro Shimizu 890ddd
		return "the image dimensions are zero";
Toshihiro Shimizu 890ddd
	case TGAERR_PIXEL_DEPTH:
Toshihiro Shimizu 890ddd
		return "invalid pixel depth";
Toshihiro Shimizu 890ddd
	case TGAERR_NO_MEM:
Toshihiro Shimizu 890ddd
		return "out of memory";
Toshihiro Shimizu 890ddd
	case TGAERR_NOT_CMAP:
Toshihiro Shimizu 890ddd
		return "image is not color mapped";
Toshihiro Shimizu 890ddd
	case TGAERR_RLE:
Toshihiro Shimizu 890ddd
		return "RLE data is corrupt";
Toshihiro Shimizu 890ddd
	case TGAERR_INDEX_RANGE:
Toshihiro Shimizu 890ddd
		return "color map index out of range";
Toshihiro Shimizu 890ddd
	case TGAERR_MONO:
Toshihiro Shimizu 890ddd
		return "image is mono";
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		return "unknown error code";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Read a Targa image from a file named <filename> to <dest>.  This is just a</dest></filename>
Toshihiro Shimizu 890ddd
 * wrapper around tga_read_from_FILE().
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * Returns: TGA_NOERR on success, or a matching TGAERR_* code on failure.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_read(tga_image *dest, const char *filename)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tga_result result;
Toshihiro Shimizu 890ddd
	FILE *fp = fopen(filename, "rb");
Toshihiro Shimizu 890ddd
	if (fp == NULL)
Toshihiro Shimizu 890ddd
		return TGAERR_FOPEN;
Toshihiro Shimizu 890ddd
	result = tga_read_from_FILE(dest, fp);
Toshihiro Shimizu 890ddd
	fclose(fp);
Toshihiro Shimizu 890ddd
	return result;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Read a Targa image from <fp> to <dest>.</dest></fp>
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * Returns: TGA_NOERR on success, or a TGAERR_* code on failure.  In the
Toshihiro Shimizu 890ddd
 *          case of failure, the contents of dest are not guaranteed to be
Toshihiro Shimizu 890ddd
 *          valid.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_read_from_FILE(tga_image *dest, FILE *fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#define BARF(errcode)           \
Toshihiro Shimizu 890ddd
	{                           \
Toshihiro Shimizu 890ddd
		tga_free_buffers(dest); \
Toshihiro Shimizu 890ddd
		return errcode;         \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define READ(destptr, size)               \
Toshihiro Shimizu 890ddd
	if (fread(destptr, size, 1, fp) != 1) \
Toshihiro Shimizu 890ddd
	BARF(TGAERR_EOF)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define READ16(dest)                       \
Toshihiro Shimizu 890ddd
	{                                      \
Toshihiro Shimizu 890ddd
		if (fread(&(dest), 2, 1, fp) != 1) \
Toshihiro Shimizu 890ddd
			BARF(TGAERR_EOF);              \
Toshihiro Shimizu 890ddd
		dest = letoh16(dest);              \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dest->image_id = NULL;
Toshihiro Shimizu 890ddd
	dest->color_map_data = NULL;
Toshihiro Shimizu 890ddd
	dest->image_data = NULL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	READ(&dest->image_id_length, 1);
Toshihiro Shimizu 890ddd
	READ(&dest->color_map_type, 1);
Toshihiro Shimizu 890ddd
	if (dest->color_map_type != TGA_COLOR_MAP_ABSENT &&
Toshihiro Shimizu 890ddd
		dest->color_map_type != TGA_COLOR_MAP_PRESENT)
Toshihiro Shimizu 890ddd
		BARF(TGAERR_CMAP_TYPE);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	READ(&dest->image_type, 1);
Toshihiro Shimizu 890ddd
	if (dest->image_type == TGA_IMAGE_TYPE_NONE)
Toshihiro Shimizu 890ddd
		BARF(TGAERR_NO_IMG);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dest->image_type != TGA_IMAGE_TYPE_COLORMAP &&
Toshihiro Shimizu 890ddd
		dest->image_type != TGA_IMAGE_TYPE_BGR &&
Toshihiro Shimizu 890ddd
		dest->image_type != TGA_IMAGE_TYPE_MONO &&
Toshihiro Shimizu 890ddd
		dest->image_type != TGA_IMAGE_TYPE_COLORMAP_RLE &&
Toshihiro Shimizu 890ddd
		dest->image_type != TGA_IMAGE_TYPE_BGR_RLE &&
Toshihiro Shimizu 890ddd
		dest->image_type != TGA_IMAGE_TYPE_MONO_RLE)
Toshihiro Shimizu 890ddd
		BARF(TGAERR_IMG_TYPE);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (tga_is_colormapped(dest) &&
Toshihiro Shimizu 890ddd
		dest->color_map_type == TGA_COLOR_MAP_ABSENT)
Toshihiro Shimizu 890ddd
		BARF(TGAERR_CMAP_MISSING);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!tga_is_colormapped(dest) &&
Toshihiro Shimizu 890ddd
		dest->color_map_type == TGA_COLOR_MAP_PRESENT)
Toshihiro Shimizu 890ddd
		BARF(TGAERR_CMAP_PRESENT);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	READ16(dest->color_map_origin);
Toshihiro Shimizu 890ddd
	READ16(dest->color_map_length);
Toshihiro Shimizu 890ddd
	READ(&dest->color_map_depth, 1);
Toshihiro Shimizu 890ddd
	if (dest->color_map_type == TGA_COLOR_MAP_PRESENT) {
Toshihiro Shimizu 890ddd
		if (dest->color_map_length == 0)
Toshihiro Shimizu 890ddd
			BARF(TGAERR_CMAP_LENGTH);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!UNMAP_DEPTH(dest->color_map_depth))
Toshihiro Shimizu 890ddd
			BARF(TGAERR_CMAP_DEPTH);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	READ16(dest->origin_x);
Toshihiro Shimizu 890ddd
	READ16(dest->origin_y);
Toshihiro Shimizu 890ddd
	READ16(dest->width);
Toshihiro Shimizu 890ddd
	READ16(dest->height);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dest->width == 0 || dest->height == 0)
Toshihiro Shimizu 890ddd
		BARF(TGAERR_ZERO_SIZE);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	READ(&dest->pixel_depth, 1);
Toshihiro Shimizu 890ddd
	if (!SANE_DEPTH(dest->pixel_depth) ||
Toshihiro Shimizu 890ddd
		(dest->pixel_depth != 8 && tga_is_colormapped(dest)))
Toshihiro Shimizu 890ddd
		BARF(TGAERR_PIXEL_DEPTH);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	READ(&dest->image_descriptor, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dest->image_id_length > 0) {
Toshihiro Shimizu 890ddd
		dest->image_id = (uint8_t *)malloc(dest->image_id_length);
Toshihiro Shimizu 890ddd
		if (dest->image_id == NULL)
Toshihiro Shimizu 890ddd
			BARF(TGAERR_NO_MEM);
Toshihiro Shimizu 890ddd
		READ(dest->image_id, dest->image_id_length);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dest->color_map_type == TGA_COLOR_MAP_PRESENT) {
Toshihiro Shimizu 890ddd
		dest->color_map_data = (uint8_t *)malloc(
Toshihiro Shimizu 890ddd
			(dest->color_map_origin + dest->color_map_length) *
Toshihiro Shimizu 890ddd
			dest->color_map_depth / 8);
Toshihiro Shimizu 890ddd
		if (dest->color_map_data == NULL)
Toshihiro Shimizu 890ddd
			BARF(TGAERR_NO_MEM);
Toshihiro Shimizu 890ddd
		READ(dest->color_map_data +
Toshihiro Shimizu 890ddd
				 (dest->color_map_origin * dest->color_map_depth / 8),
Toshihiro Shimizu 890ddd
			 dest->color_map_length * dest->color_map_depth / 8);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dest->image_data = (uint8_t *)malloc(
Toshihiro Shimizu 890ddd
		dest->width * dest->height * dest->pixel_depth / 8);
Toshihiro Shimizu 890ddd
	if (dest->image_data == NULL)
Toshihiro Shimizu 890ddd
		BARF(TGAERR_NO_MEM);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (tga_is_rle(dest)) {
Toshihiro Shimizu 890ddd
		/* read RLE */
Toshihiro Shimizu 890ddd
		tga_result result = tga_read_rle(dest, fp);
Toshihiro Shimizu 890ddd
		if (result != TGA_NOERR)
Toshihiro Shimizu 890ddd
			BARF(result);
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		/* uncompressed */
Toshihiro Shimizu 890ddd
		READ(dest->image_data,
Toshihiro Shimizu 890ddd
			 dest->width * dest->height * dest->pixel_depth / 8);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
#undef BARF
Toshihiro Shimizu 890ddd
#undef READ
Toshihiro Shimizu 890ddd
#undef READ16
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Helper function for tga_read_from_FILE().  Decompresses RLE image data from
Toshihiro Shimizu 890ddd
 * <fp>.  Assumes <dest> header fields are set correctly.</dest></fp>
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static tga_result tga_read_rle(tga_image *dest, FILE *fp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#define RLE_BIT BIT(7)
Toshihiro Shimizu 890ddd
#define READ(dest, size)               \
Toshihiro Shimizu 890ddd
	if (fread(dest, size, 1, fp) != 1) \
Toshihiro Shimizu 890ddd
	return TGAERR_EOF
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	uint8_t *pos;
Toshihiro Shimizu 890ddd
	uint32_t p_loaded = 0,
Toshihiro Shimizu 890ddd
			 p_expected = dest->width * dest->height;
Toshihiro Shimizu 890ddd
	uint8_t bpp = dest->pixel_depth / 8; /* bytes per pixel */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	pos = dest->image_data;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while ((p_loaded < p_expected) && !feof(fp)) {
Toshihiro Shimizu 890ddd
		uint8_t b;
Toshihiro Shimizu 890ddd
		READ(&b, 1);
Toshihiro Shimizu 890ddd
		if (b & RLE_BIT) {
Toshihiro Shimizu 890ddd
			/* is an RLE packet */
Toshihiro Shimizu 890ddd
			uint8_t count, tmp[4], i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			count = (b & ~RLE_BIT) + 1;
Toshihiro Shimizu 890ddd
			READ(tmp, bpp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			for (i = 0; i < count; i++) {
Toshihiro Shimizu 890ddd
				p_loaded++;
Toshihiro Shimizu 890ddd
				if (p_loaded > p_expected)
Toshihiro Shimizu 890ddd
					return TGAERR_RLE;
Toshihiro Shimizu 890ddd
				memcpy(pos, tmp, bpp);
Toshihiro Shimizu 890ddd
				pos += bpp;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		} else /* RAW packet */
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			uint8_t count;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			count = (b & ~RLE_BIT) + 1;
Toshihiro Shimizu 890ddd
			if (p_loaded + count > p_expected)
Toshihiro Shimizu 890ddd
				return TGAERR_RLE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			p_loaded += count;
Toshihiro Shimizu 890ddd
			READ(pos, bpp * count);
Toshihiro Shimizu 890ddd
			pos += count * bpp;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
#undef RLE_BIT
Toshihiro Shimizu 890ddd
#undef READ
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Write a Targa image to a file named <filename> from <src>.  This is just a</src></filename>
Toshihiro Shimizu 890ddd
 * wrapper around tga_write_to_FILE().
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * Returns: TGA_NOERR on success, or a matching TGAERR_* code on failure.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_write(const char *filename, const tga_image *src)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tga_result result;
Toshihiro Shimizu 890ddd
	FILE *fp = fopen(filename, "wb");
Toshihiro Shimizu 890ddd
	if (fp == NULL)
Toshihiro Shimizu 890ddd
		return TGAERR_FOPEN;
Toshihiro Shimizu 890ddd
	result = tga_write_to_FILE(fp, src);
Toshihiro Shimizu 890ddd
	fclose(fp);
Toshihiro Shimizu 890ddd
	return result;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Write one row of an image to <fp> using RLE.  This is a helper function</fp>
Toshihiro Shimizu 890ddd
 * called from tga_write_to_FILE().  It assumes that <src> has its header</src>
Toshihiro Shimizu 890ddd
 * fields set up correctly.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
#define PIXEL(ofs) (row + (ofs)*bpp)
Toshihiro Shimizu 890ddd
static tga_result tga_write_row_RLE(FILE *fp,
Toshihiro Shimizu 890ddd
									const tga_image *src, const uint8_t *row)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#define WRITE(src, size)               \
Toshihiro Shimizu 890ddd
	if (fwrite(src, size, 1, fp) != 1) \
Toshihiro Shimizu 890ddd
	return TGAERR_WRITE
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	uint16_t pos = 0;
Toshihiro Shimizu 890ddd
	uint16_t bpp = src->pixel_depth / 8;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (pos < src->width) {
Toshihiro Shimizu 890ddd
		packet_type type = rle_packet_type(row, pos, src->width, bpp);
Toshihiro Shimizu 890ddd
		uint8_t len = rle_packet_len(row, pos, src->width, bpp, type);
Toshihiro Shimizu 890ddd
		uint8_t packet_header;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		packet_header = len - 1;
Toshihiro Shimizu 890ddd
		if (type == RLE)
Toshihiro Shimizu 890ddd
			packet_header |= BIT(7);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		WRITE(&packet_header, 1);
Toshihiro Shimizu 890ddd
		if (type == RLE) {
Toshihiro Shimizu 890ddd
			WRITE(PIXEL(pos), bpp);
Toshihiro Shimizu 890ddd
		} else /* type == RAW */
Toshihiro Shimizu 890ddd
		{
Toshihiro Shimizu 890ddd
			WRITE(PIXEL(pos), bpp * len);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		pos += len;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
#undef WRITE
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Determine whether the next packet should be RAW or RLE for maximum
Toshihiro Shimizu 890ddd
 * efficiency.  This is a helper function called from rle_packet_len() and
Toshihiro Shimizu 890ddd
 * tga_write_row_RLE().
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
#define SAME(ofs1, ofs2) (memcmp(PIXEL(ofs1), PIXEL(ofs2), bpp) == 0)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static packet_type rle_packet_type(const uint8_t *row, const uint16_t pos,
Toshihiro Shimizu 890ddd
								   const uint16_t width, const uint16_t bpp)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (pos == width - 1)
Toshihiro Shimizu 890ddd
		return RAW;			/* one pixel */
Toshihiro Shimizu 890ddd
	if (SAME(pos, pos + 1)) /* dupe pixel */
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		if (bpp > 1)
Toshihiro Shimizu 890ddd
			return RLE; /* inefficient for bpp=1 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* three repeats makes the bpp=1 case efficient enough */
Toshihiro Shimizu 890ddd
		if ((pos < width - 2) && SAME(pos + 1, pos + 2))
Toshihiro Shimizu 890ddd
			return RLE;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return RAW;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Find the length of the current RLE packet.  This is a helper function
Toshihiro Shimizu 890ddd
 * called from tga_write_row_RLE().
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static uint8_t rle_packet_len(const uint8_t *row, const uint16_t pos,
Toshihiro Shimizu 890ddd
							  const uint16_t width, const uint16_t bpp, const packet_type type)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	uint8_t len = 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (pos == width - 1)
Toshihiro Shimizu 890ddd
		return 1;
Toshihiro Shimizu 890ddd
	if (pos == width - 2)
Toshihiro Shimizu 890ddd
		return 2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (type == RLE) {
Toshihiro Shimizu 890ddd
		while (pos + len < width) {
Toshihiro Shimizu 890ddd
			if (SAME(pos, pos + len))
Toshihiro Shimizu 890ddd
				len++;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				return len;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (len == 128)
Toshihiro Shimizu 890ddd
				return 128;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else /* type == RAW */
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		while (pos + len < width) {
Toshihiro Shimizu 890ddd
			if (rle_packet_type(row, pos + len, width, bpp) == RAW)
Toshihiro Shimizu 890ddd
				len++;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				return len;
Toshihiro Shimizu 890ddd
			if (len == 128)
Toshihiro Shimizu 890ddd
				return 128;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return len; /* hit end of row (width) */
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
#undef SAME
Toshihiro Shimizu 890ddd
#undef PIXEL
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Writes a Targa image to <fp> from <src>.</src></fp>
Toshihiro Shimizu 890ddd
 *
Toshihiro Shimizu 890ddd
 * Returns: TGA_NOERR on success, or a TGAERR_* code on failure.
Toshihiro Shimizu 890ddd
 *          On failure, the contents of the file are not guaranteed
Toshihiro Shimizu 890ddd
 *          to be valid.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_write_to_FILE(FILE *fp, const tga_image *src)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
#define WRITE(srcptr, size)               \
Toshihiro Shimizu 890ddd
	if (fwrite(srcptr, size, 1, fp) != 1) \
Toshihiro Shimizu 890ddd
	return TGAERR_WRITE
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define WRITE16(src)                       \
Toshihiro Shimizu 890ddd
	{                                      \
Toshihiro Shimizu 890ddd
		uint16_t _temp = htole16(src);     \
Toshihiro Shimizu 890ddd
		if (fwrite(&_temp, 2, 1, fp) != 1) \
Toshihiro Shimizu 890ddd
			return TGAERR_WRITE;           \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	WRITE(&src->image_id_length, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (src->color_map_type != TGA_COLOR_MAP_ABSENT &&
Toshihiro Shimizu 890ddd
		src->color_map_type != TGA_COLOR_MAP_PRESENT)
Toshihiro Shimizu 890ddd
		return TGAERR_CMAP_TYPE;
Toshihiro Shimizu 890ddd
	WRITE(&src->color_map_type, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (src->image_type == TGA_IMAGE_TYPE_NONE)
Toshihiro Shimizu 890ddd
		return TGAERR_NO_IMG;
Toshihiro Shimizu 890ddd
	if (src->image_type != TGA_IMAGE_TYPE_COLORMAP &&
Toshihiro Shimizu 890ddd
		src->image_type != TGA_IMAGE_TYPE_BGR &&
Toshihiro Shimizu 890ddd
		src->image_type != TGA_IMAGE_TYPE_MONO &&
Toshihiro Shimizu 890ddd
		src->image_type != TGA_IMAGE_TYPE_COLORMAP_RLE &&
Toshihiro Shimizu 890ddd
		src->image_type != TGA_IMAGE_TYPE_BGR_RLE &&
Toshihiro Shimizu 890ddd
		src->image_type != TGA_IMAGE_TYPE_MONO_RLE)
Toshihiro Shimizu 890ddd
		return TGAERR_IMG_TYPE;
Toshihiro Shimizu 890ddd
	WRITE(&src->image_type, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (tga_is_colormapped(src) &&
Toshihiro Shimizu 890ddd
		src->color_map_type == TGA_COLOR_MAP_ABSENT)
Toshihiro Shimizu 890ddd
		return TGAERR_CMAP_MISSING;
Toshihiro Shimizu 890ddd
	if (!tga_is_colormapped(src) &&
Toshihiro Shimizu 890ddd
		src->color_map_type == TGA_COLOR_MAP_PRESENT)
Toshihiro Shimizu 890ddd
		return TGAERR_CMAP_PRESENT;
Toshihiro Shimizu 890ddd
	if (src->color_map_type == TGA_COLOR_MAP_PRESENT) {
Toshihiro Shimizu 890ddd
		if (src->color_map_length == 0)
Toshihiro Shimizu 890ddd
			return TGAERR_CMAP_LENGTH;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!UNMAP_DEPTH(src->color_map_depth))
Toshihiro Shimizu 890ddd
			return TGAERR_CMAP_DEPTH;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	WRITE16(src->color_map_origin);
Toshihiro Shimizu 890ddd
	WRITE16(src->color_map_length);
Toshihiro Shimizu 890ddd
	WRITE(&src->color_map_depth, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	WRITE16(src->origin_x);
Toshihiro Shimizu 890ddd
	WRITE16(src->origin_y);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (src->width == 0 || src->height == 0)
Toshihiro Shimizu 890ddd
		return TGAERR_ZERO_SIZE;
Toshihiro Shimizu 890ddd
	WRITE16(src->width);
Toshihiro Shimizu 890ddd
	WRITE16(src->height);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!SANE_DEPTH(src->pixel_depth) ||
Toshihiro Shimizu 890ddd
		(src->pixel_depth != 8 && tga_is_colormapped(src)))
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
	WRITE(&src->pixel_depth, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	WRITE(&src->image_descriptor, 1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (src->image_id_length > 0)
Toshihiro Shimizu 890ddd
		WRITE(&src->image_id, src->image_id_length);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (src->color_map_type == TGA_COLOR_MAP_PRESENT)
Toshihiro Shimizu 890ddd
		WRITE(src->color_map_data +
Toshihiro Shimizu 890ddd
				  (src->color_map_origin * src->color_map_depth / 8),
Toshihiro Shimizu 890ddd
			  src->color_map_length * src->color_map_depth / 8);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (tga_is_rle(src)) {
Toshihiro Shimizu 890ddd
		uint16_t row;
Toshihiro Shimizu 890ddd
		for (row = 0; row < src->height; row++) {
Toshihiro Shimizu 890ddd
			tga_result result = tga_write_row_RLE(fp, src,
Toshihiro Shimizu 890ddd
												  src->image_data + row * src->width * src->pixel_depth / 8);
Toshihiro Shimizu 890ddd
			if (result != TGA_NOERR)
Toshihiro Shimizu 890ddd
				return result;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		/* uncompressed */
Toshihiro Shimizu 890ddd
		WRITE(src->image_data,
Toshihiro Shimizu 890ddd
			  src->width * src->height * src->pixel_depth / 8);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	WRITE(tga_id, tga_id_length);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
#undef WRITE
Toshihiro Shimizu 890ddd
#undef WRITE16
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Convenient writing functions --------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
 * This is just a helper function to initialise the header fields in a
Toshihiro Shimizu 890ddd
 * tga_image struct.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
static void init_tga_image(tga_image *img, uint8_t *image,
Toshihiro Shimizu 890ddd
						   const uint16_t width, const uint16_t height, const uint8_t depth)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	img->image_id_length = 0;
Toshihiro Shimizu 890ddd
	img->color_map_type = TGA_COLOR_MAP_ABSENT;
Toshihiro Shimizu 890ddd
	img->image_type = TGA_IMAGE_TYPE_NONE; /* override this below! */
Toshihiro Shimizu 890ddd
	img->color_map_origin = 0;
Toshihiro Shimizu 890ddd
	img->color_map_length = 0;
Toshihiro Shimizu 890ddd
	img->color_map_depth = 0;
Toshihiro Shimizu 890ddd
	img->origin_x = 0;
Toshihiro Shimizu 890ddd
	img->origin_y = 0;
Toshihiro Shimizu 890ddd
	img->width = width;
Toshihiro Shimizu 890ddd
	img->height = height;
Toshihiro Shimizu 890ddd
	img->pixel_depth = depth;
Toshihiro Shimizu 890ddd
	img->image_descriptor = TGA_T_TO_B_BIT;
Toshihiro Shimizu 890ddd
	img->image_id = NULL;
Toshihiro Shimizu 890ddd
	img->color_map_data = NULL;
Toshihiro Shimizu 890ddd
	img->image_data = image;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_write_mono(const char *filename, uint8_t *image,
Toshihiro Shimizu 890ddd
						  const uint16_t width, const uint16_t height)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tga_image img;
Toshihiro Shimizu 890ddd
	init_tga_image(&img, image, width, height, 8);
Toshihiro Shimizu 890ddd
	img.image_type = TGA_IMAGE_TYPE_MONO;
Toshihiro Shimizu 890ddd
	return tga_write(filename, &img);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_write_mono_rle(const char *filename, uint8_t *image,
Toshihiro Shimizu 890ddd
							  const uint16_t width, const uint16_t height)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tga_image img;
Toshihiro Shimizu 890ddd
	init_tga_image(&img, image, width, height, 8);
Toshihiro Shimizu 890ddd
	img.image_type = TGA_IMAGE_TYPE_MONO_RLE;
Toshihiro Shimizu 890ddd
	return tga_write(filename, &img);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_write_bgr(const char *filename, uint8_t *image,
Toshihiro Shimizu 890ddd
						 const uint16_t width, const uint16_t height, const uint8_t depth)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tga_image img;
Toshihiro Shimizu 890ddd
	init_tga_image(&img, image, width, height, depth);
Toshihiro Shimizu 890ddd
	img.image_type = TGA_IMAGE_TYPE_BGR;
Toshihiro Shimizu 890ddd
	return tga_write(filename, &img);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_write_bgr_rle(const char *filename, uint8_t *image,
Toshihiro Shimizu 890ddd
							 const uint16_t width, const uint16_t height, const uint8_t depth)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tga_image img;
Toshihiro Shimizu 890ddd
	init_tga_image(&img, image, width, height, depth);
Toshihiro Shimizu 890ddd
	img.image_type = TGA_IMAGE_TYPE_BGR_RLE;
Toshihiro Shimizu 890ddd
	return tga_write(filename, &img);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Note: this function will MODIFY  */
Toshihiro Shimizu 890ddd
tga_result tga_write_rgb(const char *filename, uint8_t *image,
Toshihiro Shimizu 890ddd
						 const uint16_t width, const uint16_t height, const uint8_t depth)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tga_image img;
Toshihiro Shimizu 890ddd
	init_tga_image(&img, image, width, height, depth);
Toshihiro Shimizu 890ddd
	img.image_type = TGA_IMAGE_TYPE_BGR;
Toshihiro Shimizu 890ddd
	(void)tga_swap_red_blue(&img);
Toshihiro Shimizu 890ddd
	return tga_write(filename, &img);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Note: this function will MODIFY  */
Toshihiro Shimizu 890ddd
tga_result tga_write_rgb_rle(const char *filename, uint8_t *image,
Toshihiro Shimizu 890ddd
							 const uint16_t width, const uint16_t height, const uint8_t depth)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	tga_image img;
Toshihiro Shimizu 890ddd
	init_tga_image(&img, image, width, height, depth);
Toshihiro Shimizu 890ddd
	img.image_type = TGA_IMAGE_TYPE_BGR_RLE;
Toshihiro Shimizu 890ddd
	(void)tga_swap_red_blue(&img);
Toshihiro Shimizu 890ddd
	return tga_write(filename, &img);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Convenient manipulation functions ---------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Horizontally flip the image in place.  Reverses the right-to-left bit in
Toshihiro Shimizu 890ddd
 * the image descriptor.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_flip_horiz(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	uint16_t row;
Toshihiro Shimizu 890ddd
	size_t bpp;
Toshihiro Shimizu 890ddd
	uint8_t *left, *right;
Toshihiro Shimizu 890ddd
	int r_to_l;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!SANE_DEPTH(img->pixel_depth))
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
	bpp = (size_t)(img->pixel_depth / 8); /* bytes per pixel */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (row = 0; row < img->height; row++) {
Toshihiro Shimizu 890ddd
		left = img->image_data + row * img->width * bpp;
Toshihiro Shimizu 890ddd
		right = left + (img->width - 1) * bpp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* reverse from left to right */
Toshihiro Shimizu 890ddd
		while (left < right) {
Toshihiro Shimizu 890ddd
			uint8_t buffer[4];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* swap */
Toshihiro Shimizu 890ddd
			memcpy(buffer, left, bpp);
Toshihiro Shimizu 890ddd
			memcpy(left, right, bpp);
Toshihiro Shimizu 890ddd
			memcpy(right, buffer, bpp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			left += bpp;
Toshihiro Shimizu 890ddd
			right -= bpp;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* Correct image_descriptor's left-to-right-ness. */
Toshihiro Shimizu 890ddd
	r_to_l = tga_is_right_to_left(img);
Toshihiro Shimizu 890ddd
	img->image_descriptor &= ~TGA_R_TO_L_BIT; /* mask out r-to-l bit */
Toshihiro Shimizu 890ddd
	if (!r_to_l)
Toshihiro Shimizu 890ddd
		/* was l-to-r, need to set r_to_l */
Toshihiro Shimizu 890ddd
		img->image_descriptor |= TGA_R_TO_L_BIT;
Toshihiro Shimizu 890ddd
	/* else bit is already rubbed out */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Vertically flip the image in place.  Reverses the top-to-bottom bit in
Toshihiro Shimizu 890ddd
 * the image descriptor.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_flip_vert(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	uint16_t col;
Toshihiro Shimizu 890ddd
	size_t bpp, line;
Toshihiro Shimizu 890ddd
	uint8_t *top, *bottom;
Toshihiro Shimizu 890ddd
	int t_to_b;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!SANE_DEPTH(img->pixel_depth))
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
	bpp = (size_t)(img->pixel_depth / 8); /* bytes per pixel */
Toshihiro Shimizu 890ddd
	line = bpp * img->width;			  /* bytes per line */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (col = 0; col < img->width; col++) {
Toshihiro Shimizu 890ddd
		top = img->image_data + col * bpp;
Toshihiro Shimizu 890ddd
		bottom = top + (img->height - 1) * line;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* reverse from top to bottom */
Toshihiro Shimizu 890ddd
		while (top < bottom) {
Toshihiro Shimizu 890ddd
			uint8_t buffer[4];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			/* swap */
Toshihiro Shimizu 890ddd
			memcpy(buffer, top, bpp);
Toshihiro Shimizu 890ddd
			memcpy(top, bottom, bpp);
Toshihiro Shimizu 890ddd
			memcpy(bottom, buffer, bpp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			top += line;
Toshihiro Shimizu 890ddd
			bottom -= line;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* Correct image_descriptor's top-to-bottom-ness. */
Toshihiro Shimizu 890ddd
	t_to_b = tga_is_top_to_bottom(img);
Toshihiro Shimizu 890ddd
	img->image_descriptor &= ~TGA_T_TO_B_BIT; /* mask out t-to-b bit */
Toshihiro Shimizu 890ddd
	if (!t_to_b)
Toshihiro Shimizu 890ddd
		/* was b-to-t, need to set t_to_b */
Toshihiro Shimizu 890ddd
		img->image_descriptor |= TGA_T_TO_B_BIT;
Toshihiro Shimizu 890ddd
	/* else bit is already rubbed out */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Convert a color-mapped image to unmapped BGR.  Reallocates image_data to a
Toshihiro Shimizu 890ddd
 * bigger size, then converts the image backwards to avoid using a secondary
Toshihiro Shimizu 890ddd
 * buffer.  Alters the necessary header fields and deallocates the color map.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_color_unmap(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	uint8_t bpp = img->color_map_depth / 8; /* bytes per pixel */
Toshihiro Shimizu 890ddd
	int pos;
Toshihiro Shimizu 890ddd
	void *tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!tga_is_colormapped(img))
Toshihiro Shimizu 890ddd
		return TGAERR_NOT_CMAP;
Toshihiro Shimizu 890ddd
	if (img->pixel_depth != 8)
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
	if (!SANE_DEPTH(img->color_map_depth))
Toshihiro Shimizu 890ddd
		return TGAERR_CMAP_DEPTH;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	tmp = realloc(img->image_data, img->width * img->height * bpp);
Toshihiro Shimizu 890ddd
	if (tmp == NULL)
Toshihiro Shimizu 890ddd
		return TGAERR_NO_MEM;
Toshihiro Shimizu 890ddd
	img->image_data = (uint8_t *)tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (pos = img->width * img->height - 1; pos >= 0; pos--) {
Toshihiro Shimizu 890ddd
		uint8_t c_index = img->image_data[pos];
Toshihiro Shimizu 890ddd
		uint8_t *c_bgr = img->color_map_data + (c_index * bpp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (c_index >= img->color_map_origin + img->color_map_length)
Toshihiro Shimizu 890ddd
			return TGAERR_INDEX_RANGE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		memcpy(img->image_data + (pos * bpp), c_bgr, (size_t)bpp);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* clean up */
Toshihiro Shimizu 890ddd
	img->image_type = TGA_IMAGE_TYPE_BGR;
Toshihiro Shimizu 890ddd
	img->pixel_depth = img->color_map_depth;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	free(img->color_map_data);
Toshihiro Shimizu 890ddd
	img->color_map_data = NULL;
Toshihiro Shimizu 890ddd
	img->color_map_type = TGA_COLOR_MAP_ABSENT;
Toshihiro Shimizu 890ddd
	img->color_map_origin = 0;
Toshihiro Shimizu 890ddd
	img->color_map_length = 0;
Toshihiro Shimizu 890ddd
	img->color_map_depth = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Return a pointer to a given pixel.  Accounts for image orientation (T_TO_B,
Toshihiro Shimizu 890ddd
 * R_TO_L, etc).  Returns NULL if the pixel is out of range.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
uint8_t *tga_find_pixel(const tga_image *img, uint16_t x, uint16_t y)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (x >= img->width || y >= img->height)
Toshihiro Shimizu 890ddd
		return NULL;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!tga_is_top_to_bottom(img))
Toshihiro Shimizu 890ddd
		y = img->height - 1 - y;
Toshihiro Shimizu 890ddd
	if (tga_is_right_to_left(img))
Toshihiro Shimizu 890ddd
		x = img->width - 1 - x;
Toshihiro Shimizu 890ddd
	return img->image_data + (x + y * img->width) * img->pixel_depth / 8;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Unpack the pixel at the src pointer according to bits.  Any of b,g,r,a can
Toshihiro Shimizu 890ddd
 * be set to NULL if not wanted.  Returns TGAERR_PIXEL_DEPTH if a stupid
Toshihiro Shimizu 890ddd
 * number of bits is given.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_unpack_pixel(const uint8_t *src, const uint8_t bits,
Toshihiro Shimizu 890ddd
							uint8_t *b, uint8_t *g, uint8_t *r, uint8_t *a)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (bits) {
Toshihiro Shimizu 890ddd
	case 32:
Toshihiro Shimizu 890ddd
		if (b)
Toshihiro Shimizu 890ddd
			*b = src[0];
Toshihiro Shimizu 890ddd
		if (g)
Toshihiro Shimizu 890ddd
			*g = src[1];
Toshihiro Shimizu 890ddd
		if (r)
Toshihiro Shimizu 890ddd
			*r = src[2];
Toshihiro Shimizu 890ddd
		if (a)
Toshihiro Shimizu 890ddd
			*a = src[3];
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	case 24:
Toshihiro Shimizu 890ddd
		if (b)
Toshihiro Shimizu 890ddd
			*b = src[0];
Toshihiro Shimizu 890ddd
		if (g)
Toshihiro Shimizu 890ddd
			*g = src[1];
Toshihiro Shimizu 890ddd
		if (r)
Toshihiro Shimizu 890ddd
			*r = src[2];
Toshihiro Shimizu 890ddd
		if (a)
Toshihiro Shimizu 890ddd
			*a = 0;
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	case 16: {
Toshihiro Shimizu 890ddd
		uint16_t src16 = (uint16_t)(src[1] << 8) | (uint16_t)src[0];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FIVE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4))
Toshihiro Shimizu 890ddd
		if (b)
Toshihiro Shimizu 890ddd
			*b = ((src16)&FIVE_BITS) << 3;
Toshihiro Shimizu 890ddd
		if (g)
Toshihiro Shimizu 890ddd
			*g = ((src16 >> 5) & FIVE_BITS) << 3;
Toshihiro Shimizu 890ddd
		if (r)
Toshihiro Shimizu 890ddd
			*r = ((src16 >> 10) & FIVE_BITS) << 3;
Toshihiro Shimizu 890ddd
		if (a)
Toshihiro Shimizu 890ddd
			*a = (uint8_t)((src16 & BIT(15)) ? 255 : 0);
Toshihiro Shimizu 890ddd
#undef FIVE_BITS
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	case 8:
Toshihiro Shimizu 890ddd
		if (b)
Toshihiro Shimizu 890ddd
			*b = *src;
Toshihiro Shimizu 890ddd
		if (g)
Toshihiro Shimizu 890ddd
			*g = *src;
Toshihiro Shimizu 890ddd
		if (r)
Toshihiro Shimizu 890ddd
			*r = *src;
Toshihiro Shimizu 890ddd
		if (a)
Toshihiro Shimizu 890ddd
			*a = 0;
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Pack the pixel at the dest pointer according to bits.  Returns
Toshihiro Shimizu 890ddd
 * TGAERR_PIXEL_DEPTH if a stupid number of bits is given.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_pack_pixel(uint8_t *dest, const uint8_t bits,
Toshihiro Shimizu 890ddd
						  const uint8_t b, const uint8_t g, const uint8_t r, const uint8_t a)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	switch (bits) {
Toshihiro Shimizu 890ddd
	case 32:
Toshihiro Shimizu 890ddd
		dest[0] = b;
Toshihiro Shimizu 890ddd
		dest[1] = g;
Toshihiro Shimizu 890ddd
		dest[2] = r;
Toshihiro Shimizu 890ddd
		dest[3] = a;
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	case 24:
Toshihiro Shimizu 890ddd
		dest[0] = b;
Toshihiro Shimizu 890ddd
		dest[1] = g;
Toshihiro Shimizu 890ddd
		dest[2] = r;
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	case 16: {
Toshihiro Shimizu 890ddd
		uint16_t tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define FIVE_BITS (BIT(0) | BIT(1) | BIT(2) | BIT(3) | BIT(4))
Toshihiro Shimizu 890ddd
		tmp = (b >> 3) & FIVE_BITS;
Toshihiro Shimizu 890ddd
		tmp |= ((g >> 3) & FIVE_BITS) << 5;
Toshihiro Shimizu 890ddd
		tmp |= ((r >> 3) & FIVE_BITS) << 10;
Toshihiro Shimizu 890ddd
		if (a > 127)
Toshihiro Shimizu 890ddd
			tmp |= BIT(15);
Toshihiro Shimizu 890ddd
#undef FIVE_BITS
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		dest[0] = (uint8_t)(tmp & 0x00FF);
Toshihiro Shimizu 890ddd
		dest[1] = (uint8_t)((tmp & 0xFF00) >> 8);
Toshihiro Shimizu 890ddd
		break;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	default:
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Desaturate the specified Targa using the specified coefficients:
Toshihiro Shimizu 890ddd
 *      output = ( red * cr + green * cg + blue * cb ) / dv
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_desaturate(tga_image *img, const int cr, const int cg,
Toshihiro Shimizu 890ddd
						  const int cb, const int dv)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	uint8_t bpp = img->pixel_depth / 8; /* bytes per pixel */
Toshihiro Shimizu 890ddd
	uint8_t *dest, *src, *tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (tga_is_mono(img))
Toshihiro Shimizu 890ddd
		return TGAERR_MONO;
Toshihiro Shimizu 890ddd
	if (tga_is_colormapped(img)) {
Toshihiro Shimizu 890ddd
		tga_result result = tga_color_unmap(img);
Toshihiro Shimizu 890ddd
		if (result != TGA_NOERR)
Toshihiro Shimizu 890ddd
			return result;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (!UNMAP_DEPTH(img->pixel_depth))
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dest = img->image_data;
Toshihiro Shimizu 890ddd
	for (src = img->image_data;
Toshihiro Shimizu 890ddd
		 src < img->image_data + img->width * img->height * bpp;
Toshihiro Shimizu 890ddd
		 src += bpp) {
Toshihiro Shimizu 890ddd
		uint8_t b, g, r;
Toshihiro Shimizu 890ddd
		(void)tga_unpack_pixel(src, img->pixel_depth, &b, &g, &r, NULL);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		*dest = (uint8_t)(((int)b * cb +
Toshihiro Shimizu 890ddd
						   (int)g * cg +
Toshihiro Shimizu 890ddd
						   (int)r * cr) /
Toshihiro Shimizu 890ddd
						  dv);
Toshihiro Shimizu 890ddd
		dest++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	/* shrink */
Toshihiro Shimizu 890ddd
	tmp = realloc(img->image_data, img->width * img->height);
Toshihiro Shimizu 890ddd
	if (tmp == NULL)
Toshihiro Shimizu 890ddd
		return TGAERR_NO_MEM;
Toshihiro Shimizu 890ddd
	img->image_data = tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	img->pixel_depth = 8;
Toshihiro Shimizu 890ddd
	img->image_type = TGA_IMAGE_TYPE_MONO;
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_desaturate_rec_601_1(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tga_desaturate(img, 2989, 5866, 1145, 10000);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_desaturate_rec_709(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tga_desaturate(img, 2126, 7152, 722, 10000);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_desaturate_itu(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tga_desaturate(img, 2220, 7067, 713, 10000);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_desaturate_avg(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return tga_desaturate(img, 1, 1, 1, 3);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Convert an image to the given pixel depth. (one of 32, 24, 16)  Avoids
Toshihiro Shimizu 890ddd
 * using a secondary buffer to do the conversion.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_convert_depth(tga_image *img, const uint8_t bits)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	size_t src_size, dest_size;
Toshihiro Shimizu 890ddd
	uint8_t src_bpp, dest_bpp;
Toshihiro Shimizu 890ddd
	uint8_t *src, *dest;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!UNMAP_DEPTH(bits) ||
Toshihiro Shimizu 890ddd
		!SANE_DEPTH(img->pixel_depth))
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (tga_is_colormapped(img)) {
Toshihiro Shimizu 890ddd
		tga_result result = tga_color_unmap(img);
Toshihiro Shimizu 890ddd
		if (result != TGA_NOERR)
Toshihiro Shimizu 890ddd
			return result;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (img->pixel_depth == bits)
Toshihiro Shimizu 890ddd
		return TGA_NOERR; /* no op, no err */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	src_bpp = img->pixel_depth / 8;
Toshihiro Shimizu 890ddd
	dest_bpp = bits / 8;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	src_size = (size_t)(img->width * img->height * src_bpp);
Toshihiro Shimizu 890ddd
	dest_size = (size_t)(img->width * img->height * dest_bpp);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (src_size > dest_size) {
Toshihiro Shimizu 890ddd
		void *tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* convert forwards */
Toshihiro Shimizu 890ddd
		dest = img->image_data;
Toshihiro Shimizu 890ddd
		for (src = img->image_data;
Toshihiro Shimizu 890ddd
			 src < img->image_data + img->width * img->height * src_bpp;
Toshihiro Shimizu 890ddd
			 src += src_bpp) {
Toshihiro Shimizu 890ddd
			uint8_t r, g, b, a;
Toshihiro Shimizu 890ddd
			(void)tga_unpack_pixel(src, img->pixel_depth, &r, &g, &b, &a);
Toshihiro Shimizu 890ddd
			(void)tga_pack_pixel(dest, bits, r, g, b, a);
Toshihiro Shimizu 890ddd
			dest += dest_bpp;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* shrink */
Toshihiro Shimizu 890ddd
		tmp = realloc(img->image_data, img->width * img->height * dest_bpp);
Toshihiro Shimizu 890ddd
		if (tmp == NULL)
Toshihiro Shimizu 890ddd
			return TGAERR_NO_MEM;
Toshihiro Shimizu 890ddd
		img->image_data = tmp;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		/* expand */
Toshihiro Shimizu 890ddd
		void *tmp = realloc(img->image_data,
Toshihiro Shimizu 890ddd
							img->width * img->height * dest_bpp);
Toshihiro Shimizu 890ddd
		if (tmp == NULL)
Toshihiro Shimizu 890ddd
			return TGAERR_NO_MEM;
Toshihiro Shimizu 890ddd
		img->image_data = (uint8_t *)tmp;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/* convert backwards */
Toshihiro Shimizu 890ddd
		dest = img->image_data + (img->width * img->height - 1) * dest_bpp;
Toshihiro Shimizu 890ddd
		for (src = img->image_data + (img->width * img->height - 1) * src_bpp;
Toshihiro Shimizu 890ddd
			 src >= img->image_data;
Toshihiro Shimizu 890ddd
			 src -= src_bpp) {
Toshihiro Shimizu 890ddd
			uint8_t r, g, b, a;
Toshihiro Shimizu 890ddd
			(void)tga_unpack_pixel(src, img->pixel_depth, &r, &g, &b, &a);
Toshihiro Shimizu 890ddd
			(void)tga_pack_pixel(dest, bits, r, g, b, a);
Toshihiro Shimizu 890ddd
			dest -= dest_bpp;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	img->pixel_depth = bits;
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Swap red and blue (RGB becomes BGR and vice verse).  (in-place)
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
tga_result tga_swap_red_blue(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	uint8_t *ptr;
Toshihiro Shimizu 890ddd
	uint8_t bpp = img->pixel_depth / 8;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!UNMAP_DEPTH(img->pixel_depth))
Toshihiro Shimizu 890ddd
		return TGAERR_PIXEL_DEPTH;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (ptr = img->image_data;
Toshihiro Shimizu 890ddd
		 ptr < img->image_data + (img->width * img->height - 1) * bpp;
Toshihiro Shimizu 890ddd
		 ptr += bpp) {
Toshihiro Shimizu 890ddd
		uint8_t r, g, b, a;
Toshihiro Shimizu 890ddd
		(void)tga_unpack_pixel(ptr, img->pixel_depth, &b, &g, &r, &a);
Toshihiro Shimizu 890ddd
		(void)tga_pack_pixel(ptr, img->pixel_depth, r, g, b, a);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return TGA_NOERR;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Free the image_id, color_map_data and image_data buffers of the specified
Toshihiro Shimizu 890ddd
 * tga_image, if they're not already NULL.
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
void tga_free_buffers(tga_image *img)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (img->image_id != NULL) {
Toshihiro Shimizu 890ddd
		free(img->image_id);
Toshihiro Shimizu 890ddd
		img->image_id = NULL;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (img->color_map_data != NULL) {
Toshihiro Shimizu 890ddd
		free(img->color_map_data);
Toshihiro Shimizu 890ddd
		img->color_map_data = NULL;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (img->image_data != NULL) {
Toshihiro Shimizu 890ddd
		free(img->image_data);
Toshihiro Shimizu 890ddd
		img->image_data = NULL;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* vim:set tabstop=4 shiftwidth=4 textwidth=78 expandtab: */