PhysX 3.4, APEX 1.4 patch release @25354359
[PhysX-3.4.git] / Externals / targa / 1 / targa.h
blobf00088cb28040b9d1824bc9b5f455c94c7bdae25
1 /* ---------------------------------------------------------------------------
2 * Truevision Targa Reader/Writer
3 * $Id: targa.h,v 1.7 2003/06/21 09:30:53 emikulic Exp $
5 * Copyright (C) 2001-2003, Emil Mikulic.
7 * Source and binary redistribution of this code, with or without
8 * changes, for free or for profit, is allowed as long as this copyright
9 * notice is kept intact. Modified versions have to be clearly marked
10 * as modified.
12 * This code is provided without any warranty. The copyright holder is
13 * not liable for anything bad that might happen as a result of the
14 * code.
17 * This version modified by NVIDIA Corporation to prevent compilation on
18 * platforms other than Windows.
19 * -------------------------------------------------------------------------*/
21 #ifndef TARGA_H
22 #define TARGA_H
24 #include <stdio.h>
26 #ifndef _MSC_VER
27 # include <inttypes.h>
28 #else /* MSVC */
29 typedef unsigned __int8 uint8_t;
30 typedef unsigned __int16 uint16_t;
31 typedef unsigned __int32 uint32_t;
32 #endif
34 #define BIT(index) (1 << (index))
36 #if defined(__CELLOS_LV2__) || defined(__ghs__) || (defined(_XBOX) && _XBOX_VER == 200)
37 # define htole16(x) ( (((x) & 0x00FF) << 8) | (((x) & 0xFF00) >> 8) )
38 # define letoh16(x) htole16(x)
39 #else /* little endian */
40 # define htole16(x) (x)
41 # define letoh16(x) (x)
42 #endif /* endianness */
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
48 /* Targa image and header fields -------------------------------------------*/
49 typedef struct
51 /* Note that Targa is stored in little-endian order */
52 uint8_t image_id_length;
54 uint8_t color_map_type;
55 /* color map = palette */
56 #define TGA_COLOR_MAP_ABSENT 0
57 #define TGA_COLOR_MAP_PRESENT 1
59 uint8_t image_type;
60 #define TGA_IMAGE_TYPE_NONE 0 /* no image data */
61 #define TGA_IMAGE_TYPE_COLORMAP 1 /* uncompressed, color-mapped */
62 #define TGA_IMAGE_TYPE_BGR 2 /* uncompressed, true-color */
63 #define TGA_IMAGE_TYPE_MONO 3 /* uncompressed, black and white */
64 #define TGA_IMAGE_TYPE_COLORMAP_RLE 9 /* run-length, color-mapped */
65 #define TGA_IMAGE_TYPE_BGR_RLE 10 /* run-length, true-color */
66 #define TGA_IMAGE_TYPE_MONO_RLE 11 /* run-length, black and white */
68 /* color map specification */
69 uint16_t color_map_origin; /* index of first entry */
70 uint16_t color_map_length; /* number of entries included */
71 uint8_t color_map_depth; /* number of bits per entry */
73 /* image specification */
74 uint16_t origin_x;
75 uint16_t origin_y;
76 uint16_t width;
77 uint16_t height;
78 uint8_t pixel_depth;
80 uint8_t image_descriptor;
81 /* bits 0,1,2,3 - attribute bits per pixel
82 * bit 4 - set if image is stored right-to-left
83 * bit 5 - set if image is stored top-to-bottom
84 * bits 6,7 - unused (must be set to zero)
86 #define TGA_ATTRIB_BITS uint8_t(BIT(0)|BIT(1)|BIT(2)|BIT(3))
87 #define TGA_R_TO_L_BIT uint8_t(BIT(4))
88 #define TGA_T_TO_B_BIT uint8_t(BIT(5))
89 #define TGA_UNUSED_BITS uint8_t(BIT(6)|BIT(7))
90 /* Note: right-to-left order is not honored by some Targa readers */
92 uint8_t *image_id;
93 /* The length of this field is given in image_id_length, it's read raw
94 * from the file so it's not not guaranteed to be zero-terminated. If
95 * it's not NULL, it needs to be deallocated. see: tga_free_buffers()
98 uint8_t *color_map_data;
99 /* See the "color map specification" fields above. If not NULL, this
100 * field needs to be deallocated. see: tga_free_buffers()
103 uint8_t *image_data;
104 /* Follows image specification fields (see above) */
106 /* Extension area and developer area are silently ignored. The Targa 2.0
107 * spec says we're not required to read or write them.
110 } tga_image;
114 /* For decoding header bits ------------------------------------------------*/
115 uint8_t tga_get_attribute_bits(const tga_image *tga);
116 int tga_is_right_to_left(const tga_image *tga);
117 int tga_is_top_to_bottom(const tga_image *tga);
118 int tga_is_colormapped(const tga_image *tga);
119 int tga_is_rle(const tga_image *tga);
120 int tga_is_mono(const tga_image *tga);
124 /* Error handling ----------------------------------------------------------*/
125 typedef enum {
126 TGA_NOERR,
127 TGAERR_FOPEN,
128 TGAERR_EOF,
129 TGAERR_WRITE,
130 TGAERR_CMAP_TYPE,
131 TGAERR_IMG_TYPE,
132 TGAERR_NO_IMG,
133 TGAERR_CMAP_MISSING,
134 TGAERR_CMAP_PRESENT,
135 TGAERR_CMAP_LENGTH,
136 TGAERR_CMAP_DEPTH,
137 TGAERR_ZERO_SIZE,
138 TGAERR_PIXEL_DEPTH,
139 TGAERR_NO_MEM,
140 TGAERR_NOT_CMAP,
141 TGAERR_RLE,
142 TGAERR_INDEX_RANGE,
143 TGAERR_MONO
144 } tga_result;
146 const char *tga_error(const tga_result errcode);
148 typedef void TGA_FP;
149 struct tgaFileOperations
151 int (*m_fopen_s)(TGA_FP** stream, const char * filename, const char * mode);
152 size_t (*m_fread)(void* ptr, size_t size, size_t count, TGA_FP* stream);
153 size_t (*m_fwrite)(const void* ptr, size_t size, size_t count, TGA_FP* stream);
154 int (*m_feof)(TGA_FP* stream);
155 int (*m_fclose)(void* stream);
159 /* Load/save ---------------------------------------------------------------*/
160 tga_result tga_read(tga_image *dest, const char *filename, tgaFileOperations* fops = NULL);
161 tga_result tga_read_from_FILE(tga_image *dest, TGA_FP* fp, tgaFileOperations* fops = NULL);
162 tga_result tga_write(const char *filename, const tga_image *src, tgaFileOperations* fops = NULL);
163 tga_result tga_write_to_FILE(TGA_FP *fp, const tga_image *src, tgaFileOperations* fops = NULL);
166 /* Convenient writing functions --------------------------------------------*/
167 tga_result tga_write_mono(const char *filename, uint8_t *image,
168 const uint16_t width, const uint16_t height);
170 tga_result tga_write_mono_rle(const char *filename, uint8_t *image,
171 const uint16_t width, const uint16_t height);
173 tga_result tga_write_bgr(const char *filename, uint8_t *image,
174 const uint16_t width, const uint16_t height, const uint8_t depth);
176 tga_result tga_write_bgr_rle(const char *filename, uint8_t *image,
177 const uint16_t width, const uint16_t height, const uint8_t depth);
179 /* These functions will use tga_swap_red_blue to MODIFY your image data */
180 tga_result tga_write_rgb(const char *filename, uint8_t *image,
181 const uint16_t width, const uint16_t height, const uint8_t depth);
183 tga_result tga_write_rgb_rle(const char *filename, uint8_t *image,
184 const uint16_t width, const uint16_t height, const uint8_t depth);
188 /* Manipulation ------------------------------------------------------------*/
189 tga_result tga_flip_horiz(tga_image *img);
190 tga_result tga_flip_vert(tga_image *img);
191 tga_result tga_color_unmap(tga_image *img);
193 uint8_t *tga_find_pixel(const tga_image *img, uint16_t x, uint16_t y);
194 tga_result tga_unpack_pixel(const uint8_t *src, const uint8_t bits,
195 uint8_t *b, uint8_t *g, uint8_t *r, uint8_t *a);
196 tga_result tga_pack_pixel(uint8_t *dest, const uint8_t bits,
197 const uint8_t b, const uint8_t g, const uint8_t r, const uint8_t a);
199 tga_result tga_desaturate(tga_image *img,
200 const int cr, const int cg, const int cb, const int dv);
201 tga_result tga_desaturate_rec_601_1(tga_image *img);
202 tga_result tga_desaturate_rec_709(tga_image *img);
203 tga_result tga_desaturate_itu(tga_image *img);
204 tga_result tga_desaturate_avg(tga_image *img);
205 tga_result tga_convert_depth(tga_image *img, const uint8_t bits);
206 tga_result tga_swap_red_blue(tga_image *img);
208 void tga_free_buffers(tga_image *img);
212 #ifndef TGA_KEEP_MACROS /* useful for targa.c */
213 # undef htole16
214 # undef letoh16
215 #endif
217 #ifdef __cplusplus
218 } // extern C
219 #endif
221 #endif /* !TARGA_H */
222 /* vim:set tabstop=4 shiftwidth=4 textwidth=78 expandtab: */