PageRenderTime 69ms CodeModel.GetById 23ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llimage/llimage.h

https://bitbucket.org/lindenlab/viewer-beta/
C++ Header | 334 lines | 183 code | 76 blank | 75 comment | 0 complexity | 6c59b8e63bef761e51dd9215fdeec3c3 MD5 | raw file
  1/** 
  2 * @file llimage.h
  3 * @brief Object for managing images and their textures.
  4 *
  5 * $LicenseInfo:firstyear=2000&license=viewerlgpl$
  6 * Second Life Viewer Source Code
  7 * Copyright (C) 2010, Linden Research, Inc.
  8 * 
  9 * This library is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU Lesser General Public
 11 * License as published by the Free Software Foundation;
 12 * version 2.1 of the License only.
 13 * 
 14 * This library is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 17 * Lesser General Public License for more details.
 18 * 
 19 * You should have received a copy of the GNU Lesser General Public
 20 * License along with this library; if not, write to the Free Software
 21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 22 * 
 23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 24 * $/LicenseInfo$
 25 */
 26
 27#ifndef LL_LLIMAGE_H
 28#define LL_LLIMAGE_H
 29
 30#include "lluuid.h"
 31#include "llstring.h"
 32#include "llthread.h"
 33#include "llmemtype.h"
 34
 35const S32 MIN_IMAGE_MIP =  2; // 4x4, only used for expand/contract power of 2
 36const S32 MAX_IMAGE_MIP = 11; // 2048x2048
 37
 38// *TODO : Use MAX_IMAGE_MIP as max discard level and modify j2c management so that the number 
 39// of levels is read from the header's file, not inferred from its size.
 40const S32 MAX_DISCARD_LEVEL = 5;
 41
 42// JPEG2000 size constraints
 43// Those are declared here as they are germane to other image constraints used in the viewer
 44// and declared right here. Some come from the JPEG2000 spec, some conventions specific to SL.
 45const S32 MAX_DECOMPOSITION_LEVELS = 32;	// Number of decomposition levels cannot exceed 32 according to jpeg2000 spec
 46const S32 MIN_DECOMPOSITION_LEVELS = 5;		// the SL viewer will *crash* trying to decode images with fewer than 5 decomposition levels (unless image is small that is)
 47const S32 MAX_PRECINCT_SIZE = 2048;			// No reason to be bigger than MAX_IMAGE_SIZE 
 48const S32 MIN_PRECINCT_SIZE = 4;			// Can't be smaller than MIN_BLOCK_SIZE
 49const S32 MAX_BLOCK_SIZE = 64;				// Max total block size is 4096, hence 64x64 when using square blocks
 50const S32 MIN_BLOCK_SIZE = 4;				// Min block dim is 4 according to jpeg2000 spec
 51
 52const S32 MIN_IMAGE_SIZE = (1<<MIN_IMAGE_MIP); // 4, only used for expand/contract power of 2
 53const S32 MAX_IMAGE_SIZE = (1<<MAX_IMAGE_MIP); // 2048
 54const S32 MIN_IMAGE_AREA = MIN_IMAGE_SIZE * MIN_IMAGE_SIZE;
 55const S32 MAX_IMAGE_AREA = MAX_IMAGE_SIZE * MAX_IMAGE_SIZE;
 56const S32 MAX_IMAGE_COMPONENTS = 8;
 57const S32 MAX_IMAGE_DATA_SIZE = MAX_IMAGE_AREA * MAX_IMAGE_COMPONENTS; //2048 * 2048 * 8 = 16 MB
 58
 59// Note!  These CANNOT be changed without modifying simulator code
 60// *TODO: change both to 1024 when SIM texture fetching is deprecated
 61const S32 FIRST_PACKET_SIZE = 600;
 62const S32 MAX_IMG_PACKET_SIZE = 1000;
 63
 64// Base classes for images.
 65// There are two major parts for the image:
 66// The compressed representation, and the decompressed representation.
 67
 68class LLImageFormatted;
 69class LLImageRaw;
 70class LLColor4U;
 71class LLPrivateMemoryPool;
 72
 73typedef enum e_image_codec
 74{
 75	IMG_CODEC_INVALID  = 0,
 76	IMG_CODEC_RGB  = 1,
 77	IMG_CODEC_J2C  = 2,
 78	IMG_CODEC_BMP  = 3,
 79	IMG_CODEC_TGA  = 4,
 80	IMG_CODEC_JPEG = 5,
 81	IMG_CODEC_DXT  = 6,
 82	IMG_CODEC_PNG  = 7,
 83	IMG_CODEC_EOF  = 8
 84} EImageCodec;
 85
 86//============================================================================
 87// library initialization class
 88
 89class LLImage
 90{
 91public:
 92	static void initClass();
 93	static void cleanupClass();
 94
 95	static const std::string& getLastError();
 96	static void setLastError(const std::string& message);
 97	
 98protected:
 99	static LLMutex* sMutex;
100	static std::string sLastErrorMessage;
101};
102
103//============================================================================
104// Image base class
105
106class LLImageBase : public LLThreadSafeRefCount
107{
108protected:
109	virtual ~LLImageBase();
110	
111public:
112	LLImageBase();
113
114	enum
115	{
116		TYPE_NORMAL = 0,
117		TYPE_AVATAR_BAKE = 1,
118	};
119
120	virtual void deleteData();
121	virtual U8* allocateData(S32 size = -1);
122	virtual U8* reallocateData(S32 size = -1);
123
124	virtual void dump();
125	virtual void sanityCheck();
126
127	U16 getWidth() const		{ return mWidth; }
128	U16 getHeight() const		{ return mHeight; }
129	S8	getComponents() const	{ return mComponents; }
130	S32 getDataSize() const		{ return mDataSize; }
131
132	const U8 *getData() const	;
133	U8 *getData()				;
134	bool isBufferInvalid() ;
135
136	void setSize(S32 width, S32 height, S32 ncomponents);
137	U8* allocateDataSize(S32 width, S32 height, S32 ncomponents, S32 size = -1); // setSize() + allocateData()
138	void enableOverSize() {mAllowOverSize = true ;}
139	void disableOverSize() {mAllowOverSize = false; }
140
141protected:
142	// special accessor to allow direct setting of mData and mDataSize by LLImageFormatted
143	void setDataAndSize(U8 *data, S32 size) { mData = data; mDataSize = size; }	
144	
145public:
146	static void generateMip(const U8 *indata, U8* mipdata, int width, int height, S32 nchannels);
147	
148	// Function for calculating the download priority for textures
149	// <= 0 priority means that there's no need for more data.
150	static F32 calc_download_priority(F32 virtual_size, F32 visible_area, S32 bytes_sent);
151
152	static EImageCodec getCodecFromExtension(const std::string& exten);
153	
154	static void createPrivatePool() ;
155	static void destroyPrivatePool() ;
156	static LLPrivateMemoryPool* getPrivatePool() {return sPrivatePoolp;}
157
158private:
159	U8 *mData;
160	S32 mDataSize;
161
162	U16 mWidth;
163	U16 mHeight;
164
165	S8 mComponents;
166
167	bool mBadBufferAllocation ;
168	bool mAllowOverSize ;
169
170	static LLPrivateMemoryPool* sPrivatePoolp ;
171public:
172	LLMemType::DeclareMemType& mMemType; // debug
173};
174
175// Raw representation of an image (used for textures, and other uncompressed formats
176class LLImageRaw : public LLImageBase
177{
178protected:
179	/*virtual*/ ~LLImageRaw();
180	
181public:
182	LLImageRaw();
183	LLImageRaw(U16 width, U16 height, S8 components);
184	LLImageRaw(U8 *data, U16 width, U16 height, S8 components);
185	// Construct using createFromFile (used by tools)
186	//LLImageRaw(const std::string& filename, bool j2c_lowest_mip_only = false);
187
188	/*virtual*/ void deleteData();
189	/*virtual*/ U8* allocateData(S32 size = -1);
190	/*virtual*/ U8* reallocateData(S32 size);
191	
192	BOOL resize(U16 width, U16 height, S8 components);
193
194	//U8 * getSubImage(U32 x_pos, U32 y_pos, U32 width, U32 height) const;
195	BOOL setSubImage(U32 x_pos, U32 y_pos, U32 width, U32 height,
196					 const U8 *data, U32 stride = 0, BOOL reverse_y = FALSE);
197
198	void clear(U8 r=0, U8 g=0, U8 b=0, U8 a=255);
199
200	void verticalFlip();
201
202	void expandToPowerOfTwo(S32 max_dim = MAX_IMAGE_SIZE, BOOL scale_image = TRUE);
203	void contractToPowerOfTwo(S32 max_dim = MAX_IMAGE_SIZE, BOOL scale_image = TRUE);
204	void biasedScaleToPowerOfTwo(S32 max_dim = MAX_IMAGE_SIZE);
205	BOOL scale( S32 new_width, S32 new_height, BOOL scale_image = TRUE );
206	//BOOL scaleDownWithoutBlending( S32 new_width, S32 new_height) ;
207
208	// Fill the buffer with a constant color
209	void fill( const LLColor4U& color );
210
211	// Copy operations
212	
213	// Src and dst can be any size.  Src and dst can each have 3 or 4 components.
214	void copy( LLImageRaw* src );
215
216	// Src and dst are same size.  Src and dst have same number of components.
217	void copyUnscaled( LLImageRaw* src );
218	
219	// Src and dst are same size.  Src has 4 components.  Dst has 3 components.
220	void copyUnscaled4onto3( LLImageRaw* src );
221
222	// Src and dst are same size.  Src has 3 components.  Dst has 4 components.
223	void copyUnscaled3onto4( LLImageRaw* src );
224
225	// Src and dst can be any size.  Src and dst have same number of components.
226	void copyScaled( LLImageRaw* src );
227
228	// Src and dst can be any size.  Src has 3 components.  Dst has 4 components.
229	void copyScaled3onto4( LLImageRaw* src );
230
231	// Src and dst can be any size.  Src has 4 components.  Dst has 3 components.
232	void copyScaled4onto3( LLImageRaw* src );
233
234
235	// Composite operations
236
237	// Src and dst can be any size.  Src and dst can each have 3 or 4 components.
238	void composite( LLImageRaw* src );
239
240	// Src and dst can be any size.  Src has 4 components.  Dst has 3 components.
241	void compositeScaled4onto3( LLImageRaw* src );
242
243	// Src and dst are same size.  Src has 4 components.  Dst has 3 components.
244	void compositeUnscaled4onto3( LLImageRaw* src );
245
246protected:
247	// Create an image from a local file (generally used in tools)
248	//bool createFromFile(const std::string& filename, bool j2c_lowest_mip_only = false);
249
250	void copyLineScaled( U8* in, U8* out, S32 in_pixel_len, S32 out_pixel_len, S32 in_pixel_step, S32 out_pixel_step );
251	void compositeRowScaled4onto3( U8* in, U8* out, S32 in_pixel_len, S32 out_pixel_len );
252
253	U8	fastFractionalMult(U8 a,U8 b);
254
255	void setDataAndSize(U8 *data, S32 width, S32 height, S8 components) ;
256
257public:
258	static S32 sGlobalRawMemory;
259	static S32 sRawImageCount;
260};
261
262// Compressed representation of image.
263// Subclass from this class for the different representations (J2C, bmp)
264class LLImageFormatted : public LLImageBase
265{
266public:
267	static LLImageFormatted* createFromType(S8 codec);
268	static LLImageFormatted* createFromExtension(const std::string& instring);	
269
270protected:
271	/*virtual*/ ~LLImageFormatted();
272	
273public:
274	LLImageFormatted(S8 codec);
275
276	// LLImageBase
277	/*virtual*/ void deleteData();
278	/*virtual*/ U8* allocateData(S32 size = -1);
279	/*virtual*/ U8* reallocateData(S32 size);
280	
281	/*virtual*/ void dump();
282	/*virtual*/ void sanityCheck();
283
284	// New methods
285	// subclasses must return a prefered file extension (lowercase without a leading dot)
286	virtual std::string getExtension() = 0;
287	// calcHeaderSize() returns the maximum size of header;
288	//   0 indicates we don't have a header and have to read the entire file
289	virtual S32 calcHeaderSize() { return 0; };
290	// calcDataSize() returns how many bytes to read to load discard_level (including header)
291	virtual S32 calcDataSize(S32 discard_level);
292	// calcDiscardLevelBytes() returns the smallest valid discard level based on the number of input bytes
293	virtual S32 calcDiscardLevelBytes(S32 bytes);
294	// getRawDiscardLevel() by default returns mDiscardLevel, but may be overridden (LLImageJ2C)
295	virtual S8  getRawDiscardLevel() { return mDiscardLevel; }
296	
297	BOOL load(const std::string& filename);
298	BOOL save(const std::string& filename);
299
300	virtual BOOL updateData() = 0; // pure virtual
301 	void setData(U8 *data, S32 size);
302 	void appendData(U8 *data, S32 size);
303
304	// Loads first 4 channels.
305	virtual BOOL decode(LLImageRaw* raw_image, F32 decode_time) = 0;  
306	// Subclasses that can handle more than 4 channels should override this function.
307	virtual BOOL decodeChannels(LLImageRaw* raw_image, F32 decode_time, S32 first_channel, S32 max_channel);
308
309	virtual BOOL encode(const LLImageRaw* raw_image, F32 encode_time) = 0;
310
311	S8 getCodec() const;
312	BOOL isDecoding() const { return mDecoding ? TRUE : FALSE; }
313	BOOL isDecoded()  const { return mDecoded ? TRUE : FALSE; }
314	void setDiscardLevel(S8 discard_level) { mDiscardLevel = discard_level; }
315	S8 getDiscardLevel() const { return mDiscardLevel; }
316
317	// setLastError needs to be deferred for J2C images since it may be called from a DLL
318	virtual void resetLastError();
319	virtual void setLastError(const std::string& message, const std::string& filename = std::string());
320	
321protected:
322	BOOL copyData(U8 *data, S32 size); // calls updateData()
323	
324protected:
325	S8 mCodec;
326	S8 mDecoding;
327	S8 mDecoded;  // unused, but changing LLImage layout requires recompiling static Mac/Linux libs. 2009-01-30 JC
328	S8 mDiscardLevel;
329	
330public:
331	static S32 sGlobalFormattedMemory;
332};
333
334#endif