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
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
17 * This version modified by NVIDIA Corporation to prevent compilation on
18 * platforms other than Windows.
19 * -------------------------------------------------------------------------*/
27 # include <inttypes.h>
29 typedef unsigned __int8
uint8_t;
30 typedef unsigned __int16
uint16_t;
31 typedef unsigned __int32
uint32_t;
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 */
48 /* Targa image and header fields -------------------------------------------*/
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
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 */
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 */
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()
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.
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 ----------------------------------------------------------*/
146 const char *tga_error(const tga_result errcode
);
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 */
221 #endif /* !TARGA_H */
222 /* vim:set tabstop=4 shiftwidth=4 textwidth=78 expandtab: */