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