Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* ---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
 * Truevision Targa Reader/Writer
Toshihiro Shimizu 890ddd
 * $Id: targa.h,v 1.7 2003/06/21 09:30:53 emikulic Exp $
Toshihiro Shimizu 890ddd
 *
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
Toshihiro Shimizu 890ddd
 * changes, for free or for profit, is allowed as long as this copyright
Toshihiro Shimizu 890ddd
 * notice is kept intact.  Modified versions have to be clearly marked
Toshihiro Shimizu 890ddd
 * 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
#ifndef _TARGA_H_
Toshihiro Shimizu 890ddd
#define _TARGA_H_
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#ifndef _MSC_VER
Toshihiro Shimizu 890ddd
#include <inttypes.h></inttypes.h>
Toshihiro Shimizu 890ddd
#else /* MSVC */
Toshihiro Shimizu 890ddd
typedef unsigned __int8 uint8_t;
Toshihiro Shimizu 890ddd
typedef unsigned __int16 uint16_t;
Toshihiro Shimizu 890ddd
typedef unsigned __int32 uint32_t;
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define BIT(index) (1 << (index))
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifdef _BIG_ENDIAN
Toshihiro Shimizu 890ddd
#define htole16(x) ((((x)&0x00FF) << 8) | (((x)&0xFF00) >> 8))
Toshihiro Shimizu 890ddd
#define letoh16(x) htole16(x)
Toshihiro Shimizu 890ddd
#else /* little endian */
Toshihiro Shimizu 890ddd
#define htole16(x) (x)
Toshihiro Shimizu 890ddd
#define letoh16(x) (x)
Toshihiro Shimizu 890ddd
#endif /* endianness */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Targa image and header fields -------------------------------------------*/
Shinya Kitaoka 120a6e
typedef struct {
Shinya Kitaoka 120a6e
  /* Note that Targa is stored in little-endian order */
Shinya Kitaoka 120a6e
  uint8_t image_id_length;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  uint8_t color_map_type;
Toshihiro Shimizu 890ddd
/* color map = palette */
Toshihiro Shimizu 890ddd
#define TGA_COLOR_MAP_ABSENT 0
Toshihiro Shimizu 890ddd
#define TGA_COLOR_MAP_PRESENT 1
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  uint8_t image_type;
Shinya Kitaoka 120a6e
#define TGA_IMAGE_TYPE_NONE 0         /* no image data */
Shinya Kitaoka 120a6e
#define TGA_IMAGE_TYPE_COLORMAP 1     /* uncompressed, color-mapped */
Shinya Kitaoka 120a6e
#define TGA_IMAGE_TYPE_BGR 2          /* uncompressed, true-color */
Shinya Kitaoka 120a6e
#define TGA_IMAGE_TYPE_MONO 3         /* uncompressed, black and white */
Toshihiro Shimizu 890ddd
#define TGA_IMAGE_TYPE_COLORMAP_RLE 9 /* run-length, color-mapped */
Shinya Kitaoka 120a6e
#define TGA_IMAGE_TYPE_BGR_RLE 10     /* run-length, true-color */
Shinya Kitaoka 120a6e
#define TGA_IMAGE_TYPE_MONO_RLE 11    /* run-length, black and white */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* color map specification */
Shinya Kitaoka 120a6e
  uint16_t color_map_origin; /* index of first entry */
Shinya Kitaoka 120a6e
  uint16_t color_map_length; /* number of entries included */
Shinya Kitaoka 120a6e
  uint8_t color_map_depth;   /* number of bits per entry */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* image specification */
Shinya Kitaoka 120a6e
  uint16_t origin_x;
Shinya Kitaoka 120a6e
  uint16_t origin_y;
Shinya Kitaoka 120a6e
  uint16_t width;
Shinya Kitaoka 120a6e
  uint16_t height;
Shinya Kitaoka 120a6e
  uint8_t pixel_depth;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  uint8_t image_descriptor;
Toshihiro Shimizu 890ddd
/* bits 0,1,2,3 - attribute bits per pixel
Toshihiro Shimizu 890ddd
     * bit  4       - set if image is stored right-to-left
Toshihiro Shimizu 890ddd
     * bit  5       - set if image is stored top-to-bottom
Toshihiro Shimizu 890ddd
     * bits 6,7     - unused (must be set to zero)
Toshihiro Shimizu 890ddd
     */
Toshihiro Shimizu 890ddd
#define TGA_ATTRIB_BITS (uint8_t)(BIT(0) | BIT(1) | BIT(2) | BIT(3))
Toshihiro Shimizu 890ddd
#define TGA_R_TO_L_BIT (uint8_t) BIT(4)
Toshihiro Shimizu 890ddd
#define TGA_T_TO_B_BIT (uint8_t) BIT(5)
Toshihiro Shimizu 890ddd
#define TGA_UNUSED_BITS (uint8_t)(BIT(6) | BIT(7))
Shinya Kitaoka 120a6e
  /* Note: right-to-left order is not honored by some Targa readers */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  uint8_t *image_id;
Shinya Kitaoka 120a6e
  /* The length of this field is given in image_id_length, it's read raw
Shinya Kitaoka 120a6e
*from the file so it's not not guaranteed to be zero-terminated.  If
Shinya Kitaoka 120a6e
*it's not NULL, it needs to be deallocated.  see: tga_free_buffers()
Shinya Kitaoka 120a6e
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  uint8_t *color_map_data;
Shinya Kitaoka 120a6e
  /* See the "color map specification" fields above.  If not NULL, this
Shinya Kitaoka 120a6e
*field needs to be deallocated.  see: tga_free_buffers()
Shinya Kitaoka 120a6e
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  uint8_t *image_data;
Shinya Kitaoka 120a6e
  /* Follows image specification fields (see above) */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* Extension area and developer area are silently ignored.  The Targa 2.0
Shinya Kitaoka 120a6e
*spec says we're not required to read or write them.
Shinya Kitaoka 120a6e
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} tga_image;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* For decoding header bits ------------------------------------------------*/
Toshihiro Shimizu 890ddd
uint8_t tga_get_attribute_bits(const tga_image *tga);
Toshihiro Shimizu 890ddd
int tga_is_right_to_left(const tga_image *tga);
Toshihiro Shimizu 890ddd
int tga_is_top_to_bottom(const tga_image *tga);
Toshihiro Shimizu 890ddd
int tga_is_colormapped(const tga_image *tga);
Toshihiro Shimizu 890ddd
int tga_is_rle(const tga_image *tga);
Toshihiro Shimizu 890ddd
int tga_is_mono(const tga_image *tga);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Error handling ----------------------------------------------------------*/
Toshihiro Shimizu 890ddd
typedef enum {
Shinya Kitaoka 120a6e
  TGA_NOERR,
Shinya Kitaoka 120a6e
  TGAERR_FOPEN,
Shinya Kitaoka 120a6e
  TGAERR_EOF,
Shinya Kitaoka 120a6e
  TGAERR_WRITE,
Shinya Kitaoka 120a6e
  TGAERR_CMAP_TYPE,
Shinya Kitaoka 120a6e
  TGAERR_IMG_TYPE,
Shinya Kitaoka 120a6e
  TGAERR_NO_IMG,
Shinya Kitaoka 120a6e
  TGAERR_CMAP_MISSING,
Shinya Kitaoka 120a6e
  TGAERR_CMAP_PRESENT,
Shinya Kitaoka 120a6e
  TGAERR_CMAP_LENGTH,
Shinya Kitaoka 120a6e
  TGAERR_CMAP_DEPTH,
Shinya Kitaoka 120a6e
  TGAERR_ZERO_SIZE,
Shinya Kitaoka 120a6e
  TGAERR_PIXEL_DEPTH,
Shinya Kitaoka 120a6e
  TGAERR_NO_MEM,
Shinya Kitaoka 120a6e
  TGAERR_NOT_CMAP,
Shinya Kitaoka 120a6e
  TGAERR_RLE,
Shinya Kitaoka 120a6e
  TGAERR_INDEX_RANGE,
Shinya Kitaoka 120a6e
  TGAERR_MONO
Toshihiro Shimizu 890ddd
} tga_result;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
const char *tga_error(const tga_result errcode);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Load/save ---------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
tga_result tga_read(tga_image *dest, const char *filename);
Toshihiro Shimizu 890ddd
tga_result tga_read_from_FILE(tga_image *dest, FILE *fp);
Toshihiro Shimizu 890ddd
tga_result tga_write(const char *filename, const tga_image *src);
Toshihiro Shimizu 890ddd
tga_result tga_write_to_FILE(FILE *fp, const tga_image *src);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Convenient writing functions --------------------------------------------*/
Toshihiro Shimizu 890ddd
tga_result tga_write_mono(const char *filename, uint8_t *image,
Shinya Kitaoka 120a6e
                          const uint16_t width, const uint16_t height);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_write_mono_rle(const char *filename, uint8_t *image,
Shinya Kitaoka 120a6e
                              const uint16_t width, const uint16_t height);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_write_bgr(const char *filename, uint8_t *image,
Shinya Kitaoka 120a6e
                         const uint16_t width, const uint16_t height,
Shinya Kitaoka 120a6e
                         const uint8_t depth);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_write_bgr_rle(const char *filename, uint8_t *image,
Shinya Kitaoka 120a6e
                             const uint16_t width, const uint16_t height,
Shinya Kitaoka 120a6e
                             const uint8_t depth);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* These functions will use tga_swap_red_blue to MODIFY your image data */
Toshihiro Shimizu 890ddd
tga_result tga_write_rgb(const char *filename, uint8_t *image,
Shinya Kitaoka 120a6e
                         const uint16_t width, const uint16_t height,
Shinya Kitaoka 120a6e
                         const uint8_t depth);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
tga_result tga_write_rgb_rle(const char *filename, uint8_t *image,
Shinya Kitaoka 120a6e
                             const uint16_t width, const uint16_t height,
Shinya Kitaoka 120a6e
                             const uint8_t depth);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/* Manipulation ------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
tga_result tga_flip_horiz(tga_image *img);
Toshihiro Shimizu 890ddd
tga_result tga_flip_vert(tga_image *img);
Toshihiro Shimizu 890ddd
tga_result tga_color_unmap(tga_image *img);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
uint8_t *tga_find_pixel(const tga_image *img, uint16_t x, uint16_t y);
Shinya Kitaoka 120a6e
tga_result tga_unpack_pixel(const uint8_t *src, const uint8_t bits, uint8_t *b,
Shinya Kitaoka 120a6e
                            uint8_t *g, uint8_t *r, uint8_t *a);
Shinya Kitaoka 120a6e
tga_result tga_pack_pixel(uint8_t *dest, const uint8_t bits, const uint8_t b,
Shinya Kitaoka 120a6e
                          const uint8_t g, const uint8_t r, const uint8_t a);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
tga_result tga_desaturate(tga_image *img, const int cr, const int cg,
Shinya Kitaoka 120a6e
                          const int cb, const int dv);
Toshihiro Shimizu 890ddd
tga_result tga_desaturate_rec_601_1(tga_image *img);
Toshihiro Shimizu 890ddd
tga_result tga_desaturate_rec_709(tga_image *img);
Toshihiro Shimizu 890ddd
tga_result tga_desaturate_itu(tga_image *img);
Toshihiro Shimizu 890ddd
tga_result tga_desaturate_avg(tga_image *img);
Toshihiro Shimizu 890ddd
tga_result tga_convert_depth(tga_image *img, const uint8_t bits);
Toshihiro Shimizu 890ddd
tga_result tga_swap_red_blue(tga_image *img);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void tga_free_buffers(tga_image *img);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TGA_KEEP_MACROS /* useful for targa.c */
Toshihiro Shimizu 890ddd
#undef htole16
Toshihiro Shimizu 890ddd
#undef letoh16
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif /* !_TARGA_H_ */
Toshihiro Shimizu 890ddd
/* vim:set tabstop=4 shiftwidth=4 textwidth=78 expandtab: */