1///////////////////////////////////////////////////////////////////////////
2//
3// Copyright (c) 2002, Industrial Light & Magic, a division of Lucas
4// Digital Ltd. LLC
5//
6// All rights reserved.
7//
8// Redistribution and use in source and binary forms, with or without
9// modification, are permitted provided that the following conditions are
10// met:
11// *       Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// *       Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following disclaimer
15// in the documentation and/or other materials provided with the
16// distribution.
17// *       Neither the name of Industrial Light & Magic nor the names of
18// its contributors may be used to endorse or promote products derived
19// from this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32//
33///////////////////////////////////////////////////////////////////////////
34
35
36
37#ifndef INCLUDED_IMF_COMPRESSOR_H
38#define INCLUDED_IMF_COMPRESSOR_H
39
40//-----------------------------------------------------------------------------
41//
42//	class Compressor
43//
44//-----------------------------------------------------------------------------
45
46#include <ImfCompression.h>
47#include "ImathBox.h"
48
49namespace Imf {
50
51class Header;
52
53
54class Compressor
55{
56  public:
57
58    //---------------------------------------------
59    // Constructor -- hdr is the header of the file
60    // that will be compressed or uncompressed
61    //---------------------------------------------
62
63    Compressor (const Header &hdr);
64
65
66    //-----------
67    // Destructor
68    //-----------
69
70    virtual ~Compressor ();
71
72
73    //----------------------------------------------
74    // Maximum number of scan lines processed by
75    // a single call to compress() and uncompress().
76    //----------------------------------------------
77
78    virtual int		numScanLines () const = 0;
79
80
81    //--------------------------------------------
82    // Format of the pixel data read and written
83    // by the compress() and uncompress() methods.
84    // The default implementation of format()
85    // returns XDR.
86    //--------------------------------------------
87
88    enum Format
89    {
90	NATIVE,		// the machine's native format
91	XDR		// Xdr format
92    };
93
94    virtual Format	format () const;
95
96
97    //----------------------------
98    // Access to the file's header
99    //----------------------------
100
101    const Header &	header () const		{return _header;}
102
103
104    //-------------------------------------------------------------------------
105    // Compress an array of bytes that represents the contents of up to
106    // numScanLines() scan lines:
107    //
108    //	    inPtr		Input buffer (uncompressed data).
109    //
110    //	    inSize		Number of bytes in the input buffer
111    //
112    //	    minY		Minimum y coordinate of the scan lines to
113    //				be compressed
114    //
115    //	    outPtr		Pointer to output buffer
116    //
117    //	    return value	Size of compressed data in output buffer
118    //
119    // Arrangement of uncompressed pixel data in the input buffer:
120    //
121    //	Before calling
122    //
123    //	        compress (buf, size, minY, ...);
124    //
125    //	the InputFile::writePixels() method gathers pixel data from the
126    // 	frame buffer, fb, and places them in buffer buf, like this:
127    //
128    //  char *endOfBuf = buf;
129    //
130    //	for (int y = minY;
131    //	     y <= min (minY + numScanLines() - 1, header().dataWindow().max.y);
132    //	     ++y)
133    //	{
134    //	    for (ChannelList::ConstIterator c = header().channels().begin();
135    //		 c != header().channels().end();
136    //		 ++c)
137    //	    {
138    //		if (modp (y, c.channel().ySampling) != 0)
139    //		    continue;
140    //
141    //		for (int x = header().dataWindow().min.x;
142    //		     x <= header().dataWindow().max.x;
143    //		     ++x)
144    //		{
145    //		    if (modp (x, c.channel().xSampling) != 0)
146    //			continue;
147    //
148    //		    Xdr::write<CharPtrIO> (endOfBuf, fb.pixel (c, x, y));
149    //		}
150    //	    }
151    //	}
152    //
153    //	int size = endOfBuf - buf;
154    //
155    //-------------------------------------------------------------------------
156
157    virtual int		compress (const char *inPtr,
158				  int inSize,
159				  int minY,
160				  const char *&outPtr) = 0;
161
162    virtual int		compressTile (const char *inPtr,
163				      int inSize,
164				      Imath::Box2i range,
165				      const char *&outPtr);
166
167    //-------------------------------------------------------------------------
168    // Uncompress an array of bytes that has been compressed by compress():
169    //
170    //	    inPtr		Input buffer (compressed data).
171    //
172    //	    inSize		Number of bytes in the input buffer
173    //
174    //	    minY		Minimum y coordinate of the scan lines to
175    //				be uncompressed
176    //
177    //	    outPtr		Pointer to output buffer
178    //
179    //	    return value	Size of uncompressed data in output buffer
180    //
181    //-------------------------------------------------------------------------
182
183    virtual int		uncompress (const char *inPtr,
184				    int inSize,
185				    int minY,
186				    const char *&outPtr) = 0;
187
188    virtual int		uncompressTile (const char *inPtr,
189					int inSize,
190					Imath::Box2i range,
191					const char *&outPtr);
192
193  private:
194
195    const Header &	_header;
196};
197
198
199//--------------------------------------
200// Test if c is a valid compression type
201//--------------------------------------
202
203bool		isValidCompression (Compression c);
204
205
206//-----------------------------------------------------------------
207// Construct a Compressor for compression type c:
208//
209//  maxScanLineSize	Maximum number of bytes per uncompressed
210//			scan line.
211//
212//  header		Header of the input or output file whose
213//			pixels will be compressed or uncompressed.
214//
215//  return value	A pointer to a new Compressor object (it
216//			is the caller's responsibility to delete
217//			the object), or 0 (if c is NO_COMPRESSION).
218//
219//-----------------------------------------------------------------
220
221Compressor *	newCompressor (Compression c,
222			       int maxScanLineSize,
223			       const Header &hdr);
224
225
226//-----------------------------------------------------------------
227// Construct a Compressor for compression type c for a tiled image:
228//
229//  tileLineSize	Maximum number of bytes per uncompressed
230//			line in a tile.
231//
232//  numTileLines	Maximum number of lines in a tile.
233//
234//  header		Header of the input or output file whose
235//			pixels will be compressed or uncompressed.
236//
237//  return value	A pointer to a new Compressor object (it
238//			is the caller's responsibility to delete
239//			the object), or 0 (if c is NO_COMPRESSION).
240//
241//-----------------------------------------------------------------
242
243Compressor *    newTileCompressor (Compression c,
244				   int tileLineSize,
245				   int numTileLines,
246				   const Header &hdr);
247
248
249} // namespace Imf
250
251#endif
252