PBRT
/home/felix/UBC/projects/AdaptiveLightfieldSampling/pbrt_v2/src/core/targa.h
00001 /* ---------------------------------------------------------------------------
00002  * Truevision Targa Reader/Writer
00003  * $Id: targa.h,v 1.8 2004/10/09 09:30:26 emikulic Exp $
00004  *
00005  * Copyright (C) 2001-2003 Emil Mikulic.
00006  *
00007  * Source and binary redistribution of this code, with or without
00008  * changes, for free or for profit, is allowed as long as this copyright
00009  * notice is kept intact.  Modified versions have to be clearly marked
00010  * as modified.
00011  *
00012  * This code is provided without any warranty.  The copyright holder is
00013  * not liable for anything bad that might happen as a result of the
00014  * code.
00015  * -------------------------------------------------------------------------*/
00016 
00017 #ifndef _TARGA_H_
00018 #define _TARGA_H_
00019 
00020 #include <stdio.h>
00021 #ifndef _MSC_VER
00022 # include <inttypes.h>
00023 #else /* MSVC */
00024  typedef unsigned __int8  uint8_t;
00025  typedef unsigned __int16 uint16_t;
00026  typedef unsigned __int32 uint32_t;
00027 #endif
00028 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif // __cplusplus
00032 
00033 #define BIT(index) (1 << (index))
00034 
00035 #ifdef _BIG_ENDIAN
00036 # define htole16(x) ( (((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8) )
00037 # define letoh16(x) htole16(x)
00038 #else /* little endian */
00039 # define htole16(x) (x)
00040 # define letoh16(x) (x)
00041 #endif /* endianness */
00042 
00043 
00044 
00045 /* Targa image and header fields -------------------------------------------*/
00046 typedef struct
00047 {
00048     /* Note that Targa is stored in little-endian order */
00049     uint8_t     image_id_length;
00050 
00051     uint8_t     color_map_type;
00052     /* color map = palette */
00053     #define TGA_COLOR_MAP_ABSENT    0
00054     #define TGA_COLOR_MAP_PRESENT   1
00055 
00056     uint8_t     image_type;
00057     #define TGA_IMAGE_TYPE_NONE          0 /* no image data */
00058     #define TGA_IMAGE_TYPE_COLORMAP      1 /* uncompressed, color-mapped */
00059     #define TGA_IMAGE_TYPE_BGR           2 /* uncompressed, true-color */
00060     #define TGA_IMAGE_TYPE_MONO          3 /* uncompressed, black and white */
00061     #define TGA_IMAGE_TYPE_COLORMAP_RLE  9 /* run-length, color-mapped */
00062     #define TGA_IMAGE_TYPE_BGR_RLE      10 /* run-length, true-color */
00063     #define TGA_IMAGE_TYPE_MONO_RLE     11 /* run-length, black and white */
00064 
00065     /* color map specification */
00066     uint16_t    color_map_origin;   /* index of first entry */
00067     uint16_t    color_map_length;   /* number of entries included */
00068     uint8_t     color_map_depth;    /* number of bits per entry */
00069 
00070     /* image specification */
00071     uint16_t    origin_x;
00072     uint16_t    origin_y;
00073     uint16_t    width;
00074     uint16_t    height;
00075     uint8_t     pixel_depth;
00076 
00077     uint8_t     image_descriptor;
00078     /* bits 0,1,2,3 - attribute bits per pixel
00079      * bit  4       - set if image is stored right-to-left
00080      * bit  5       - set if image is stored top-to-bottom
00081      * bits 6,7     - unused (must be set to zero)
00082      */
00083     #define TGA_ATTRIB_BITS (uint8_t)(BIT(0)|BIT(1)|BIT(2)|BIT(3))
00084     #define TGA_R_TO_L_BIT  (uint8_t)BIT(4)
00085     #define TGA_T_TO_B_BIT  (uint8_t)BIT(5)
00086     #define TGA_UNUSED_BITS (uint8_t)(BIT(6)|BIT(7))
00087     /* Note: right-to-left order is not honored by some Targa readers */
00088 
00089     uint8_t *image_id;
00090     /* The length of this field is given in image_id_length, it's read raw
00091      * from the file so it's not not guaranteed to be zero-terminated.  If
00092      * it's not NULL, it needs to be deallocated.  see: tga_free_buffers()
00093      */
00094 
00095     uint8_t *color_map_data;
00096     /* See the "color map specification" fields above.  If not NULL, this
00097      * field needs to be deallocated.  see: tga_free_buffers()
00098      */
00099 
00100     uint8_t *image_data;
00101     /* Follows image specification fields (see above) */
00102 
00103     /* Extension area and developer area are silently ignored.  The Targa 2.0
00104      * spec says we're not required to read or write them.
00105      */
00106 
00107 } tga_image;
00108 
00109 
00110 
00111 /* For decoding header bits ------------------------------------------------*/
00112 uint8_t tga_get_attribute_bits(const tga_image *tga);
00113 int tga_is_right_to_left(const tga_image *tga);
00114 int tga_is_top_to_bottom(const tga_image *tga);
00115 int tga_is_colormapped(const tga_image *tga);
00116 int tga_is_rle(const tga_image *tga);
00117 int tga_is_mono(const tga_image *tga);
00118 
00119 
00120 
00121 /* Error handling ----------------------------------------------------------*/
00122 typedef enum {
00123     TGA_NOERR,
00124     TGAERR_FOPEN,
00125     TGAERR_EOF,
00126     TGAERR_WRITE,
00127     TGAERR_CMAP_TYPE,
00128     TGAERR_IMG_TYPE,
00129     TGAERR_NO_IMG,
00130     TGAERR_CMAP_MISSING,
00131     TGAERR_CMAP_PRESENT,
00132     TGAERR_CMAP_LENGTH,
00133     TGAERR_CMAP_DEPTH,
00134     TGAERR_ZERO_SIZE,
00135     TGAERR_PIXEL_DEPTH,
00136     TGAERR_NO_MEM,
00137     TGAERR_NOT_CMAP,
00138     TGAERR_RLE,
00139     TGAERR_INDEX_RANGE,
00140     TGAERR_MONO
00141 } tga_result;
00142 
00143 const char *tga_error(const tga_result errcode);
00144 
00145 
00146 
00147 /* Load/save ---------------------------------------------------------------*/
00148 tga_result tga_read(tga_image *dest, const char *filename);
00149 tga_result tga_read_from_FILE(tga_image *dest, FILE *fp);
00150 tga_result tga_write(const char *filename, const tga_image *src);
00151 tga_result tga_write_to_FILE(FILE *fp, const tga_image *src);
00152 
00153 
00154 
00155 /* Convenient writing functions --------------------------------------------*/
00156 tga_result tga_write_mono(const char *filename, uint8_t *image,
00157     const uint16_t width, const uint16_t height);
00158 
00159 tga_result tga_write_mono_rle(const char *filename, uint8_t *image,
00160     const uint16_t width, const uint16_t height);
00161 
00162 tga_result tga_write_bgr(const char *filename, uint8_t *image,
00163     const uint16_t width, const uint16_t height, const uint8_t depth);
00164 
00165 tga_result tga_write_bgr_rle(const char *filename, uint8_t *image,
00166     const uint16_t width, const uint16_t height, const uint8_t depth);
00167 
00168 /* These functions will use tga_swap_red_blue to MODIFY your image data */
00169 tga_result tga_write_rgb(const char *filename, uint8_t *image,
00170     const uint16_t width, const uint16_t height, const uint8_t depth);
00171 
00172 tga_result tga_write_rgb_rle(const char *filename, uint8_t *image,
00173     const uint16_t width, const uint16_t height, const uint8_t depth);
00174 
00175 
00176 
00177 /* Manipulation ------------------------------------------------------------*/
00178 tga_result tga_flip_horiz(tga_image *img);
00179 tga_result tga_flip_vert(tga_image *img);
00180 tga_result tga_color_unmap(tga_image *img);
00181 
00182 uint8_t *tga_find_pixel(const tga_image *img, uint16_t x, uint16_t y);
00183 tga_result tga_unpack_pixel(const uint8_t *src, const uint8_t bits,
00184     uint8_t *b, uint8_t *g, uint8_t *r, uint8_t *a);
00185 tga_result tga_pack_pixel(uint8_t *dest, const uint8_t bits,
00186     const uint8_t b, const uint8_t g, const uint8_t r, const uint8_t a);
00187 
00188 tga_result tga_desaturate(tga_image *img,
00189         const int cr, const int cg, const int cb, const int dv);
00190 tga_result tga_desaturate_rec_601_1(tga_image *img);
00191 tga_result tga_desaturate_rec_709(tga_image *img);
00192 tga_result tga_desaturate_itu(tga_image *img);
00193 tga_result tga_desaturate_avg(tga_image *img);
00194 tga_result tga_convert_depth(tga_image *img, const uint8_t bits);
00195 tga_result tga_swap_red_blue(tga_image *img);
00196 
00197 void tga_free_buffers(tga_image *img);
00198 
00199 
00200 
00201 #ifndef TGA_KEEP_MACROS /* useful for targa.c */
00202 # undef htole16
00203 # undef letoh16
00204 #endif
00205 
00206 #ifdef __cplusplus
00207 }
00208 #endif // __cplusplus
00209 
00210 #endif /* !_TARGA_H_ */
00211