1 // ----------------------------------------------------------------------------
2 // MODULE : PTile
3 // LANGUAGE : C
4 // CREATOR : Philippe BOSSUT
5 // CREAT. DATE : Wednesday, March 13, 1996
6 // DESCRIPTION :
7 // SCCSID : @(#)ptile.h 1.7 15:18:40 14 Apr 1997
8 // ----------------------------------------------------------------------------
9 // Copyright (c) 1999 Digital Imaging Group, Inc.
10 // For conditions of distribution and use, see copyright notice
11 // in Flashpix.h
12 // ----------------------------------------------------------------------------
13 #ifndef PTile_h
14 #define PTile_h
15 #ifndef Commun_h
16 #include "common.h"
17 #endif
18 // ----------------------------------------------------------------------------
19
20 // Includes
21 // --------
22 #include <time.h>
23
24 #ifndef SystemIVUE_h
25 #include "ri_sys.h"
26 #endif
27 #ifndef Compresseur_h
28 #include "fpxcompress.h" // for TLC_IdCodec
29 #endif
30
31 #ifndef FPXBaselineIO_h
32 #include "fpxlibio.h"
33 #endif
34
35 #ifdef _WINDOWS
36 #include <winbase.h>
37 #endif
38
39 // Constants
40 // ---------
41
42 // Types Declarations
43 // ------------------
44
45 // Types Definitions
46 // -----------------
47
48 // Classes Declarations
49 // --------------------
50
51 class PHierarchicalImage;
52 class PResolutionLevel;
53 class PTile;
54
55 // Classes Definitions
56 // -------------------
57
58 // PTile class. PTiles keep memory of the pixels themselves.
59 // BE CAREFULL : PResolutionLevel manipulates Arrays of PTile and uses arithmetic of pointers
60 // operations to compute tiles' addresses, consequently, DO NOT ADD SCALAR MEMBERS OR VIRTUAL
61 // FUNCTIONS TO CLASSES INHERITING FROM PTILE. In other words, anything which makes a modification
62 // of the size of PTile's derivate class defeats the arithmetic used in PResolutionLevel and
63 // crashes the application. If you want to add something to a derivate class, you should in fact
64 // add that here in the protected area (I know, it's ugly...). The only thing allowed in derivated
65 // classes is to refurbish some of the virtual methods of the public interface.
66 // Another better solution would be to use a chained list or to modify the code everywhere the pointer
67 // arithmetic is used using a virtual SizeOf() method... To be done in a future version...
68
69 class PTile : public PToolkitObject {
70
71 friend class PHierarchicalImage;
72 friend class PResolutionLevel;
73
74 public:
75 PTile();
76 virtual ~PTile();
77
78 // Memory management: Used by the context to handle the tiles:
79 // -----------------------------------------------------------
80 static Boolean Purge(long* size = NULL, Boolean purgeAll = FALSE); // Release used memory
81 static long GetPurgeableMemory(); // Compute the amount of memory which can be purged
82 static void ClearStaticArrays(); // clear all the static arrays still allocated
83 long AllocatePixels();
84 long AllocateRawPixels();
85 long AllocatePixelMemory( Pixel **memAddress);
86 void FreePixelsBuffer( );
87 void FreeRawPixelsBuffer( );
88 void TouchPixelsBuffer( );
89 void TouchRawPixelsBuffer( );
90 long Free (Boolean forced = FALSE, Boolean freeIncomplete = FALSE); // Free memory allocated to the tile
91
92 // Memory management: Used by others to handle tiles which have pixels in memory:
93 // -----------------------------------------------------------
94 // static long FindOldestTileBuffer( PTile **foundTile, long *isRawPixelsBuffer); PTCH_101 rep
95 static long FindOldestTileBuffer( PTile **foundTile, long *isRawPixelsBuffer, long minSize = 16384); // PTCH_101 new
96 static void FreeAncientBuffers( long numMinutesOld);
97 static void FreeAllRawPixels();
98 static void FlushModifiedTiles();
99
100 // Public interface: Methods used by a PResolutionLevel to access a tile:
101 // ----------------------------------------------------------------------
102 // These methods should not be derived
103
104 // Read pixels methods:
105 FPXStatus ReadRectangleRawData (Pixel* pix, long width, long height, long rowOffset, long x0, long y0);
106 FPXStatus ReadRectangle (Pixel* pix, long width, long height, long rowOffset, long x0, long y0);
107 long InverseAlpha(); // Inverse alpha channel in tile
108
109 // Write pixels methods:
110 FPXStatus WriteTile(); // Advanced tile writing : manage automatic decimation
111 FPXStatus DecimateTile(); // Decimation itself
112 FPXStatus WriteRectangle (Pixel* pix, long width, long height, long rowOffset, long x0, long y0, short plan = -1);
113 FPXStatus Convolution (Pixel* pix, long width, long height, long quadrant);
114
115 // Get information tools
116 Boolean GetInfo (long* width, long* height, Typ_Compression* compr);
117 long GetposFic (); // Give tile position in the file
118 long GetTileSize (); // Give tile size
119 Boolean HasFreshPixels (); // Indicates tile is modified but not yet saved
120
121 // Public interface: Methods implementing the file specific tile description:
122 // --------------------------------------------------------------------------
123 // These methods must be derived in most of the cases
124
125 // Read and write a tile:
126 virtual FPXStatus Read(); // Read pixels & rawPixels in file
127 virtual FPXStatus ReadRawPixels(); // Read rawPixels in file
128 virtual FPXStatus Write(); // Write pixels in file
129
130 // Set information tools
131 virtual void InitializeCreate (PResolutionLevel* father, long width, long height, long id); // Init tile in write mode
132 virtual void InitializeRead (PResolutionLevel* father, long offset, long sizetile, long id, long compression = 0, long nouse = 0); // Init tile in read mode
133
134 // Get compression option
135 virtual long GetCompression ();
136 virtual long GetCompressionSubtype ();
137
138 // Set compression option
139 virtual void SetCompression (long theCompression);
140 virtual void SetCompressionSubtype(long theCompressionSubType);
141
142 // Set quality factor
143 virtual void SetQualityFactor (unsigned char theQualityFactor);
144
145 // Convert the FPX compression code to index of compressor
146 virtual TLC_IdCodec ConvertCompressionOption ();
147
148 protected:
149 // Data of the PTile structure:
150 // ----------------------------
151
152 PResolutionLevel* fatherSubImage; // Point to the father sub-image
153
154 short height; // Height of the tile in pixels
155 short width; // Width of the tile in pixels
156 long freshPixels; // Number of pixels recently written into 'rawPixels'
157 // If freshPixels is non-zero, then the tile in memory
158 // has been written to, but not saved in the file yet.
159 Pixel* rawPixels; // An uncompressed version of data store in file
160 Pixel* pixels; // A copy of 'rawPixels' with colortwist, etc applied
161 FPXBaselineColorSpace rawPixelsSpace; // Color space for the data currently in 'rawPixels'
162 FPXBaselineColorSpace pixelsSpace; // Color space for the data currently in 'pixels'
163 // suitable for user display. Color space always
164 // the same as 'PFileFlashPixIO::usedSpace'
165 time_t pixelsTime; // Last time 'pixels' was accessed
166 time_t rawPixelsTime; // Last time 'rawPixels' was accessed
167
168 short nbChannels; // Number of channels (1 to 4 in Baseline)
169 long dirtyCount; // Check for modification of contrast, colortwist, filtering, or aspect ratio
170 Boolean viewingParamApplied; // True if the viewing parameters have been applied to the cached tile values
171 Boolean pixelsStale; // True if 'pixels' does not reflect changes to 'rawPixels'
172
173 TLC_IdCodec idCodec; // Compressor/Decompressor identifier
174 Boolean decompressorIsMissing; // True if decompressor is missing
175 long compression; // Compression mode used in this image file
176 long compressionSubtype; // Special compression info
177 unsigned char qualityFactor; // Quality factor
178
179 Boolean tileInitialize; // Flag of tile having been initialized
180 long posPixelFic; // Position in the file
181 long tileSize; // Size of the tile in the file
182 int32 identifier; // Tile identifier (i.e. tile number)
183
184 // For fast computation in PTile methods
185 // -------------------------------------
186
187 static unsigned char* invertLUT; // Inversion LUT
188 void AllocInvertTable(); // Allocate and compute the inversion LUT
189
190 // For memory management purposes
191 // ------------------------------
192
193 PTile* previous; // Pointer to the previous tile allocated
194 PTile* next; // Pointer to the next tile allocated
195
196 void Insert(); // Put the tile at the end of the list
197 void Dispose(); // Delete the tile from the list
198 void Lock(); // Disable delete of this tile
199 void UnLock(); // Enable delete of this tile
200 Boolean IsLocked(); // Check if this tile is locked
201
202 void AllocDecompress (long size); // Allocate buffer for decompression
203 static long PurgeDecompress (); // Free decompression buffer
204 void LockDecompress (); // Disable the free of decompression buffer
205 void UnLockDecompress (); // Enable the free of decompression buffer
206
207 static long allocTiles; // Number of allocated tiles
208 static long allocSize; // Size allocated to the tiles
209 static PTile* first; // Oldest tile allocated
210 static PTile* last; // Most recent tile allocated
211 static PTile** locked; // Locked tiles
212 static long indexLocked;
213
214 static Ptr decompressBuffer; // Global buffer for decompression
215 static long decompressSize; // Size of the decompression buffer
216 static Boolean decompressLock; // true if buffer in use
217
218 };
219
220
221
222 // 'inline' Functions
223 // ------------------
224
225 // Get Position of the tile in the file
226
GetposFic()227 inline long PTile::GetposFic() {
228 return (posPixelFic);
229 }
230
GetTileSize()231 inline long PTile::GetTileSize () {
232 return tileSize;
233 }
234
LockDecompress()235 inline void PTile::LockDecompress () {
236 decompressLock = TRUE;
237 }
238
UnLockDecompress()239 inline void PTile::UnLockDecompress () {
240 decompressLock = FALSE;
241 }
242
HasFreshPixels()243 inline Boolean PTile::HasFreshPixels () {
244 return ((freshPixels > 0)? true : false);
245 }
246
TouchPixelsBuffer()247 inline void PTile::TouchPixelsBuffer () {
248 #ifdef _WINDOWS
249 pixelsTime = GetCurrentTime();
250 #else
251 pixelsTime = clock();
252 #endif
253 }
254
TouchRawPixelsBuffer()255 inline void PTile::TouchRawPixelsBuffer () {
256 #ifdef _WINDOWS
257 pixelsTime = GetCurrentTime();
258 #else
259 rawPixelsTime = clock();
260 #endif
261 }
262
263
264 // 'extern' Functions
265 // ------------------
266
267 // 'extern' Variables
268 // ------------------
269
270 // ----------------------------------------------------------------------------
271 #endif // PTile_h
272 // ----------------------------------------------------------------------------
273