1/*****************************************************************************/
2// TGATranslator
3// Written by Michael Wilber, Haiku Translation Kit Team
4//
5// TGATranslator.h
6//
7// This BTranslator based object is for opening and writing TGA files.
8//
9//
10// Copyright (c) 2002  Haiku, Inc.
11//
12// Permission is hereby granted, free of charge, to any person obtaining a
13// copy of this software and associated documentation files (the "Software"),
14// to deal in the Software without restriction, including without limitation
15// the rights to use, copy, modify, merge, publish, distribute, sublicense,
16// and/or sell copies of the Software, and to permit persons to whom the
17// Software is furnished to do so, subject to the following conditions:
18//
19// The above copyright notice and this permission notice shall be included
20// in all copies or substantial portions of the Software.
21//
22// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28// DEALINGS IN THE SOFTWARE.
29/*****************************************************************************/
30
31#ifndef TGA_TRANSLATOR_H
32#define TGA_TRANSLATOR_H
33
34#include <Translator.h>
35#include <TranslatorFormats.h>
36#include <TranslationDefs.h>
37#include <GraphicsDefs.h>
38#include <InterfaceDefs.h>
39#include <DataIO.h>
40#include <ByteOrder.h>
41#include "BaseTranslator.h"
42
43#define TGA_TRANSLATOR_VERSION B_TRANSLATION_MAKE_VERSION(1,0,0)
44#define TGA_IN_QUALITY 0.7
45#define TGA_IN_CAPABILITY 0.8
46#define TGA_OUT_QUALITY 0.7
47#define TGA_OUT_CAPABILITY 0.6
48
49#define BBT_IN_QUALITY 0.7
50#define BBT_IN_CAPABILITY 0.6
51#define BBT_OUT_QUALITY 0.6
52#define BBT_OUT_CAPABILITY 0.8
53
54// TGA Translator Settings
55#define TGA_SETTING_RLE "tga /rle"
56#define TGA_SETTING_IGNORE_ALPHA "tga /ignore_alpha"
57
58// TGA files are stored in the Intel byte order :)
59struct TGAFileHeader {
60	uint8 idlength;
61		// Number of bytes in the Image ID field
62	uint8 colormaptype;
63		// 0	Has NO color-map (palette)
64		// 1	Has color-map (palette)
65	uint8 imagetype;
66		// 0	No Image Data Included
67		// 1	Uncompressed, Color-mapped image
68		// 2	Uncompressed, True-color image
69		// 3	Uncompressed, Black-and-white (Greyscale?) image
70		// 9	Run-length encoded, Color-mapped image
71		// 10	Run-length encoded, True-color image
72		// 11	Run-length encoded, Black-and-white (Greyscale?) image
73};
74
75#define TGA_NO_COLORMAP			0
76#define TGA_COLORMAP			1
77
78#define TGA_NO_IMAGE_DATA		0
79
80#define TGA_NOCOMP_COLORMAP		1
81#define TGA_NOCOMP_TRUECOLOR	2
82#define TGA_NOCOMP_BW			3
83#define TGA_RLE_COLORMAP		9
84#define TGA_RLE_TRUECOLOR		10
85#define TGA_RLE_BW				11
86
87// Information about the color map (palette). These bytes are
88// always present, but are zero if no color map is present
89struct TGAColorMapSpec {
90	uint16 firstentry;		// first useful entry in the color map
91	uint16 length;			// number of color map entries
92	uint8 entrysize;		// number of bits per entry
93};
94
95struct TGAImageSpec {
96	uint16 xorigin;
97	uint16 yorigin;
98	uint16 width;
99	uint16 height;
100	uint8 depth;
101		// pixel depth includes alpha bits!
102	uint8 descriptor;
103		// bits 3-0: number of attribute bits per pixel
104		// bits 5&4: order pixels are drawn to the screen
105			// Screen Dest of 		Image Origin
106			// first pixel			bit 5	bit 4
107			//////////////////////////////////////////
108			// bottom left			0		0
109			// bottom right			0		1
110			// top left				1		0
111			// top right			1		1
112		// bits 7&6: must be zero
113};
114
115#define TGA_ORIGIN_VERT_BIT	0x20
116#define TGA_ORIGIN_BOTTOM	0
117#define TGA_ORIGIN_TOP		1
118
119#define TGA_ORIGIN_HORZ_BIT	0x10
120#define TGA_ORIGIN_LEFT		0
121#define TGA_ORIGIN_RIGHT	1
122
123#define TGA_DESC_BITS76		0xc0
124#define TGA_DESC_ALPHABITS	0x0f
125
126#define TGA_RLE_PACKET_TYPE_BIT 0x80
127
128#define TGA_HEADERS_SIZE 18
129
130#define TGA_STREAM_BUFFER_SIZE 1024
131
132class TGATranslator : public BaseTranslator {
133public:
134	TGATranslator();
135
136	virtual status_t DerivedIdentify(BPositionIO *inSource,
137		const translation_format *inFormat, BMessage *ioExtension,
138		translator_info *outInfo, uint32 outType);
139
140	virtual status_t DerivedTranslate(BPositionIO *inSource,
141		const translator_info *inInfo, BMessage *ioExtension,
142		uint32 outType, BPositionIO *outDestination, int32 baseType);
143
144	virtual BView *NewConfigView(TranslatorSettings *settings);
145
146protected:
147	virtual ~TGATranslator();
148		// this is protected because the object is deleted by the
149		// Release() function instead of being deleted directly by
150		// the user
151
152private:
153	uint8 tga_alphabits(TGAFileHeader &filehead, TGAColorMapSpec &mapspec,
154		TGAImageSpec &imagespec);
155
156	status_t translate_from_bits(BPositionIO *inSource, uint32 outType,
157		BPositionIO *outDestination);
158
159	status_t translate_from_tganm_to_bits(BPositionIO *inSource,
160		BPositionIO *outDestination, TGAFileHeader &filehead,
161		TGAColorMapSpec &mapspec, TGAImageSpec &imagespec);
162
163	status_t translate_from_tganmrle_to_bits(BPositionIO *inSource,
164		BPositionIO *outDestination, TGAFileHeader &filehead,
165		TGAColorMapSpec &mapspec, TGAImageSpec &imagespec);
166
167	status_t translate_from_tgamrle_to_bits(BPositionIO *inSource,
168		BPositionIO *outDestination, TGAFileHeader &filehead,
169		TGAColorMapSpec &mapspec, TGAImageSpec &imagespec, uint8 *pmap);
170
171	status_t translate_from_tga(BPositionIO *inSource, uint32 outType,
172		BPositionIO *outDestination);
173
174};
175
176#endif // #ifndef TGA_TRANSLATOR_H
177
178