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#include <stdlib.h>
49
50namespace Imf {
51
52class Header;
53
54
55class Compressor
56{
57  public:
58
59    //---------------------------------------------
60    // Constructor -- hdr is the header of the file
61    // that will be compressed or uncompressed
62    //---------------------------------------------
63
64    Compressor (const Header &hdr);
65
66
67    //-----------
68    // Destructor
69    //-----------
70
71    virtual ~Compressor ();
72
73
74    //----------------------------------------------
75    // Maximum number of scan lines processed by
76    // a single call to compress() and uncompress().
77    //----------------------------------------------
78
79    virtual int		numScanLines () const = 0;
80
81
82    //--------------------------------------------
83    // Format of the pixel data read and written
84    // by the compress() and uncompress() methods.
85    // The default implementation of format()
86    // returns XDR.
87    //--------------------------------------------
88
89    enum Format
90    {
91    NATIVE,		// the machine's native format
92    XDR		// Xdr format
93    };
94
95    virtual Format	format () const;
96
97
98    //----------------------------
99    // Access to the file's header
100    //----------------------------
101
102    const Header &	header () const		{return _header;}
103
104
105    //-------------------------------------------------------------------------
106    // Compress an array of bytes that represents the contents of up to
107    // numScanLines() scan lines:
108    //
109    //	    inPtr		Input buffer (uncompressed data).
110    //
111    //	    inSize		Number of bytes in the input buffer
112    //
113    //	    minY		Minimum y coordinate of the scan lines to
114    //				be compressed
115    //
116    //	    outPtr		Pointer to output buffer
117    //
118    //	    return value	Size of compressed data in output buffer
119    //
120    // Arrangement of uncompressed pixel data in the input buffer:
121    //
122    //	Before calling
123    //
124    //	        compress (buf, size, minY, ...);
125    //
126    //	the InputFile::writePixels() method gathers pixel data from the
127    // 	frame buffer, fb, and places them in buffer buf, like this:
128    //
129    //  char *endOfBuf = buf;
130    //
131    //	for (int y = minY;
132    //	     y <= min (minY + numScanLines() - 1, header().dataWindow().max.y);
133    //	     ++y)
134    //	{
135    //	    for (ChannelList::ConstIterator c = header().channels().begin();
136    //		 c != header().channels().end();
137    //		 ++c)
138    //	    {
139    //		if (modp (y, c.channel().ySampling) != 0)
140    //		    continue;
141    //
142    //		for (int x = header().dataWindow().min.x;
143    //		     x <= header().dataWindow().max.x;
144    //		     ++x)
145    //		{
146    //		    if (modp (x, c.channel().xSampling) != 0)
147    //			continue;
148    //
149    //		    Xdr::write<CharPtrIO> (endOfBuf, fb.pixel (c, x, y));
150    //		}
151    //	    }
152    //	}
153    //
154    //	int size = endOfBuf - buf;
155    //
156    //-------------------------------------------------------------------------
157
158    virtual int		compress (const char *inPtr,
159                  int inSize,
160                  int minY,
161                  const char *&outPtr) = 0;
162
163    virtual int		compressTile (const char *inPtr,
164                      int inSize,
165                      Imath::Box2i range,
166                      const char *&outPtr);
167
168    //-------------------------------------------------------------------------
169    // Uncompress an array of bytes that has been compressed by compress():
170    //
171    //	    inPtr		Input buffer (compressed data).
172    //
173    //	    inSize		Number of bytes in the input buffer
174    //
175    //	    minY		Minimum y coordinate of the scan lines to
176    //				be uncompressed
177    //
178    //	    outPtr		Pointer to output buffer
179    //
180    //	    return value	Size of uncompressed data in output buffer
181    //
182    //-------------------------------------------------------------------------
183
184    virtual int		uncompress (const char *inPtr,
185                    int inSize,
186                    int minY,
187                    const char *&outPtr) = 0;
188
189    virtual int		uncompressTile (const char *inPtr,
190                    int inSize,
191                    Imath::Box2i range,
192                    const char *&outPtr);
193
194  private:
195
196    const Header &	_header;
197};
198
199
200//--------------------------------------
201// Test if c is a valid compression type
202//--------------------------------------
203
204bool		isValidCompression (Compression c);
205
206
207//-----------------------------------------------------------------
208// Construct a Compressor for compression type c:
209//
210//  maxScanLineSize	Maximum number of bytes per uncompressed
211//			scan line.
212//
213//  header		Header of the input or output file whose
214//			pixels will be compressed or uncompressed.
215//
216//  return value	A pointer to a new Compressor object (it
217//			is the caller's responsibility to delete
218//			the object), or 0 (if c is NO_COMPRESSION).
219//
220//-----------------------------------------------------------------
221
222Compressor *	newCompressor (Compression c,
223                   size_t maxScanLineSize,
224                   const Header &hdr);
225
226
227//-----------------------------------------------------------------
228// Construct a Compressor for compression type c for a tiled image:
229//
230//  tileLineSize	Maximum number of bytes per uncompressed
231//			line in a tile.
232//
233//  numTileLines	Maximum number of lines in a tile.
234//
235//  header		Header of the input or output file whose
236//			pixels will be compressed or uncompressed.
237//
238//  return value	A pointer to a new Compressor object (it
239//			is the caller's responsibility to delete
240//			the object), or 0 (if c is NO_COMPRESSION).
241//
242//-----------------------------------------------------------------
243
244Compressor *    newTileCompressor (Compression c,
245                   size_t tileLineSize,
246                   size_t numTileLines,
247                   const Header &hdr);
248
249
250} // namespace Imf
251
252#endif
253