1/*****************************************************************************/
2// SGITranslator
3// Written by Stephan A��mus <stippi@yellowbites.com>
4// derived from GIMP SGI plugin by Michael Sweet
5//
6// SGIImage.h
7//
8// SGI image file format library routines.
9//
10// Formed into a class SGIImage, adopted to Be API and modified to use
11// BPositionIO, optimizations for buffered reading.
12//
13//
14// Copyright (c) 2003 Haiku Project
15// Portions Copyright 1997-1998 Michael Sweet (mike@easysw.com)
16//
17// Permission is hereby granted, free of charge, to any person obtaining a
18// copy of this software and associated documentation files (the "Software"),
19// to deal in the Software without restriction, including without limitation
20// the rights to use, copy, modify, merge, publish, distribute, sublicense,
21// and/or sell copies of the Software, and to permit persons to whom the
22// Software is furnished to do so, subject to the following conditions:
23//
24// The above copyright notice and this permission notice shall be included
25// in all copies or substantial portions of the Software.
26//
27// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
28// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
30// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
32// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
33// DEALINGS IN THE SOFTWARE.
34/*****************************************************************************/
35
36/*
37 * "$Id: SGIImage.h 14449 2005-10-20 12:15:56Z stippi $"
38 *
39 * Revision History:
40 *
41 *	 $Log: SGIImage.h,v $
42 *	 Revision 1.2  2004/02/03 00:52:18  mwilber
43 *	 Removed GPL text as permission was obtained from Michael Sweet to allow this derivative work to be distributed under the MIT License.
44 *
45 *	 Revision 1.1  2004/02/02 23:55:38  mwilber
46 *	 Initial check in for Stephan Assmus' SGITranslator
47 *
48 *	 Revision 1.5	1998/05/17 16:01:33	mike
49 *	 Added <unistd.h> header file.
50 *
51 *	 Revision 1.4	1998/04/23	17:40:49	mike
52 *	 Updated to support 16-bit <unsigned> image data.
53 *
54 *	 Revision 1.3	1998/02/05	17:10:58	mike
55 *	 Added sgiOpenFile() function for opening an existing file pointer.
56 *
57 *	 Revision 1.2	1997/06/18	00:55:28	mike
58 *	 Updated to hold length table when writing.
59 *	 Updated to hold current length when doing ARLE.
60 *
61 *	 Revision 1.1	1997/06/15	03:37:19	mike
62 *	 Initial revision
63 */
64
65#ifndef SGI_IMAGE_H
66#define SGI_IMAGE_H
67
68#include <DataIO.h>
69#include <InterfaceDefs.h>
70
71#define SGI_MAGIC		474	// magic number in image file
72
73#define SGI_READ		0	// read from an SGI image file
74#define SGI_WRITE		1	// write to an SGI image file
75
76#define SGI_COMP_NONE	0	// no compression
77#define SGI_COMP_RLE	1	// run-length encoding
78#define SGI_COMP_ARLE	2	// agressive run-length encoding
79
80extern const char kSGICopyright[];
81
82class SGIImage {
83 public:
84								SGIImage();
85	virtual						~SGIImage();
86
87			// not really necessary, SetTo() will return an error anyways
88			status_t			InitCheck() const;
89
90			// first version -> read from an existing sgi image in stream
91			status_t			SetTo(BPositionIO* stream);
92			// second version -> set up a stream for writing an sgi image;
93			// when SetTo() returns, the image header will have been written
94			// already
95			status_t			SetTo(BPositionIO* stream,
96									  uint16 width, uint16 height,
97									  uint16 channels, uint32 bytesPerChannel,
98									  uint32 compression);
99			// has to be called if writing, writes final information to the stream
100			status_t			Unset();
101
102			// access to each row of image data
103			status_t			ReadRow(void* row, int32 lineNum, int32 channel);
104			// write one row of image data
105			// right now, could be used to modify an image in place, but only
106			// if dealing with uncompressed data, compressed data is currently
107			// not supported
108			status_t			WriteRow(void* row, int32 lineNum, int32 channel);
109
110			// access to the attributes of the sgi image
111			uint16				Width() const
112									{ return fWidth; }
113			uint16				Height() const
114									{ return fHeight; }
115			uint32				BytesPerChannel() const
116									{ return fBytesPerChannel; }
117			uint32				CountChannels() const
118									{ return fChannelCount; }
119
120 private:
121			int32				_ReadLong() const;
122			int16				_ReadShort() const;
123			int8				_ReadChar() const;
124			status_t			_WriteLong(int32 n) const;
125			status_t			_WriteShort(uint16 n) const;
126			status_t			_WriteChar(int8 n) const;
127
128			ssize_t				_ReadRLE8(uint8* row, int32 numPixels) const;
129			ssize_t				_ReadRLE8(uint8* row, uint8* rleBuffer, int32 numPixels) const;
130			ssize_t				_ReadRLE16(uint16* row, int32 numPixels) const;
131			ssize_t				_ReadRLE16(uint16* row, uint16* rleBuffer, int32 numPixels) const;
132			ssize_t				_WriteRLE8(uint8* row, int32 numPixels) const;
133			ssize_t				_WriteRLE16(uint16* row, int32 numPixels) const;
134
135
136	BPositionIO*				fStream;
137
138	uint32						fMode;				// reading or writing
139	uint32						fBytesPerChannel;
140	uint32						fCompression;
141
142	uint16						fWidth;				// in number of pixels
143	uint16						fHeight;			// in number of pixels
144	uint16						fChannelCount;
145
146	off_t						fFirstRowOffset;	// offset into stream
147	off_t						fNextRowOffset;		// offset into stream
148
149	int32**						fOffsetTable;		// offset table for compression
150	int32**						fLengthTable;		// length table for compression
151
152	uint16*						fARLERow;			// advanced RLE compression buffer
153	int32						fARLEOffset;		// advanced RLE buffer offset
154	int32						fARLELength;		// advanced RLE buffer length
155};
156
157#endif // SGI_IMAGE_H
158
159