GL Studio API
image.h
Go to the documentation of this file.
1 /*! \file
2  \brief The Image class. All textures are converted internally into Images.
3 
4  \par Copyright Information
5 
6  Copyright (c) 2015 by The DiSTI Corporation.<br>
7  11301 Corporate Blvd; Suite 100<br>
8  Orlando, Florida 32817<br>
9  USA<br>
10  <br>
11  All rights reserved.<br>
12 
13  This Software contains proprietary trade secrets of DiSTI and may not be
14 reproduced, in whole or part, in any form, or by any means of electronic,
15 mechanical, or otherwise, without the written permission of DiSTI. Said
16 permission may be derived through the purchase of applicable DiSTI product
17 licenses which detail the distribution rights of this content and any
18 Derivative Works based on this or other copyrighted DiSTI Software.
19 
20  NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21 AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22 PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23 AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24 IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25 PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26 
27  LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28 IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29 INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30 DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31 INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32 INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33 OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34 EXCEED FIVE DOLLARS (US$5.00).
35 
36  The aforementioned terms and restrictions are governed by the laws of the
37 State of Florida and the United States of America.
38 
39 */
40 
41 #ifndef _IMAGE_LIB_H
42 #define _IMAGE_LIB_H
43 
44 #include "gls_include.h"
45 #include <stdio.h>
46 #include "util.h"
47 #include "list.h"
48 #include "file_path_class.h"
49 #include "IFontImage.h"
50 
51 namespace disti
52 {
53 
54 class Mutex;
55 const int IMG_TRANSPARENT = 0; /**< Alpha value for a transparent pixel */
56 const int IMG_NON_TRANSPARENT = 255; /**< Alpha value for a non-transparent pixel */
57 const int MAX_MIP_MAP_IMAGES = 16; /**< Maximum number of mipmapping levels */
58 
59 typedef enum
60 {
61  GLS_CODEC_RAW, /**< Image data is not compressed */
62  GLS_CODEC_LZ77, /**< Image data is compressed with LZ77 (Z-Lib) */
63  GLS_CODEC_JPEG /**< Image data is compressed with lossy JPEG */
65 
66 typedef enum
67 {
68  GLS_NPOT_AUTO, /**< Allow Non Power Of Two textures if able */
69  GLS_NPOT_ENABLED, /**< Always allow Non Power Of Two textures */
70  GLS_NPOT_DISABLED /**< Never allow Non Power Of Two textures */
72 
73 /** The glsInlineImage structure. This structure is generated and contains information needed to
74  * create reconstruct an inline generated image.
75  */
77 {
78  typedef const unsigned char* const _BufferType;
79  unsigned int _width; /**< Width of image in pixels */
80  unsigned int _height; /**< Height of image in pixels */
81  unsigned int _components; /**< Number of components in image */
82  unsigned int _pixel_format; /**< GL_RGB, GL_RGBA ... */
83  glsImageCodec _codec; /**< CODEC used to compress image */
84  unsigned long _crc; /**< CRC of image data (compressed or uncompressed?) */
85  bool _gl_texture_compression; /**< Value of GlTextureCompression */
86  unsigned long _image_data_size; /**< Size of RGB data (compressed) */
87  unsigned long _alpha_data_size; /**< Size of Alpha data (compressed). Zero if no alpha channel present */
88  unsigned int _line_length; /**< Length of each line of inline data, in bytes */
89  _BufferType* _rgb_buffer; /**< Pointer to the static compressed rgb buffer for the image */
90  _BufferType* _alpha_buffer; /**< Pointer to the static compressed alpha layer for the image */
91 
92  glsInlineImage(unsigned int width,
93  unsigned int height,
94  unsigned int components,
95  unsigned int pixel_format,
96  glsImageCodec codec,
97  unsigned long crc,
98  bool gl_tex_compress,
99  unsigned long image_data_size,
100  unsigned long alpha_data_size,
101  unsigned int line_length,
102  const unsigned char* const* rgb_buffer,
103  const unsigned char* const* alpha_buffer):
104  _width(width),
105  _height(height),
106  _components(components),
107  _pixel_format(pixel_format),
108  _codec(codec),
109  _crc(crc),
110  _gl_texture_compression(gl_tex_compress),
111  _image_data_size(image_data_size),
112  _alpha_data_size(alpha_data_size),
113  _line_length(line_length),
114  _rgb_buffer(rgb_buffer),
115  _alpha_buffer(alpha_buffer)
116  {
117  }
118 };
119 
120 /** Enable/Disable global Non Power Of Two capability. This will override the
121  * texture's setting from the editor.
122  * \note NPOT detection is not currently supported. (GLS-5940)
123  * \param state The state to set it to. Will enable, disable, or apply auto detection
124  for if NPOT support is available.
125  */
126 GLS_EXPORT void SetNPOTState( ImageNPOTMode state );
127 
128 /** Determines if Non Power Of Two textures are supported.
129  * \return GLS_NPOT_ENABLED if NPOT textures are supported, GLS_NPOT_DISABLED otherwise.
130  */
132 
133 /** By default, Image instances release their texture data in main memory after loading it to the GPU.
134  * This flag controls that default behavior for new Image instances. Set to false if you want all
135  * Images to keep a copy of their texture data loaded in main memory. (Note: This can dramatically
136  * increase memory usage.)
137  *
138  * \note The default applies only to Image instances created after this flag is set. To set or get
139  * the value behavior for a particular Image instance, call Image::SetReleaseImageDataEnabled()
140  * or Image::IsReleaseImageDataEnabled(), respectively.
141  *
142  * \note Default value: true
143  *
144  * \param value The new default value
145  *
146  * \sa disti::Image::SetReleaseImageDataEnabled(), disti::Image::IsReleaseImageDataEnabled()
147  */
148 GLS_EXPORT void SetReleaseImageDataDefault( bool enable );
149 
150 /** Returns the current value for the flag controlling the default behavior for new Image instances.
151  * See SetReleaseImageDataDefault() for more details.
152  * \sa disti::SetReleaseImageDataDefault( bool )
153  */
154 GLS_EXPORT bool GetReleaseImageDataDefault( void );
155 
156 
157 /**
158  The Image class. Implements loading 2D image files.
159 */
160 class Image
161  : virtual public IFontImage
162 {
163 protected:
164  unsigned int _textureHandle; /**< The texture handle of the image. */
165  int _pixelFormat; /**< Pixel format for the image */
166  int _pixelSize; /**< Size of a pixel in bytes */
167  int _errorFound; /**< Error found reading image file flag. */
168  char *_errorMessage; /**< Error message in string format. */
169  int _width; /**< Actual width of image in pixels. */
170  int _height; /**< Actual height of image in pixels. */
171  int _texWidth; /**< Texture width (power of 2) of the image in pixels. */
172  int _texHeight; /**< Texture height (power of 2) of the image in pixels. */
173 
174  float _texCoord[2]; /**< Texture coordinates of upper right corner
175  * of image. This will be (1.0,1.0) for a
176  * square power of two image */
177 
178  unsigned char *_rgbBuf; /**< Pointer to the image data for this image. */
179 
180  bool _mipMap; /**< True if this image should use mip mapping */
181  unsigned char *_mmBuf[MAX_MIP_MAP_IMAGES]; /**< Pointers to the smaller versions of _rgbBuf */
182 
183  unsigned long _crcValue; /**< CRC value used to determine if two images are the same */
184 
185  int _numUsers; /**< Count of number of objects that reference this one */
186 
187  bool _replaceImageFlag; /**< Indicates the images has been changed and needs to be re-bound */
188 
189  bool _releaseImageData; /**< true to allow image data for image to be unloaded from main memory else false */
190 
191  static bool _glTextureCompressionSupported; /**< Indicates if GL Texture compression is supported on the
192  Machine that this program is running on. */
193 
194  bool _glTextureCompression; /**< Indicates if GL Texture compression is enabled for this image */
195 
196  glsInlineImage* _staticInlineImage; /**< Inline image data for this image, if applicable. */
197 
198  bool _allowImageSharing; /**< If false, causes Image::CrcValue() to always return 0, disabling duplicate checking. */
199 
200  bool _allowNPOT; /**< If true, this texture is not scaled to power of two. */
201 
202 #ifdef GLES
203  unsigned int _stateManagerHandle; /**< Handle used by the OpenGL ES state manager to quickly find this texture */
204 #endif
205 
206  /** Convert Luminance Alpha texture to Alpha texture.
207  * Used on OpenGL ES target to speedup texturing for fonts
208  */
210 
211 public:
212 
213  static GLS_EXPORT bool _globalMipMapEnabled; /**< Determines the default value for _mipMap, and does not
214  * do mip mapping if false */
215 
216  /** All images get added to the InstanceList */
217  static GLS_EXPORT List_c& InstanceList();
218 
219  /** Use this Mutex when accessing the InstanceList */
220  static GLS_EXPORT disti::Mutex& InstanceListMutex();
221 
222  /** Empties the InstanceList
223  * This is done when loading objects which will be drawn in different
224  * OpenGL contexts.
225  */
226  static GLS_EXPORT void EmptyInstanceList();
227 
228  /** Determines whether this image will release its image data from main memory when it is not needed.
229  *
230  * When enabled (true), the the Image will release the main memory copy of the data when it is not
231  * needed in order to save memory. This flag is enabled by default and should be disabled in situations
232  * such as when the ImageData for this Image cannot be re-created from permanent storage.
233  *
234  * \note The default value is determined by disti::GetReleaseImageDataDefault().
235  *
236  * \param enable Whether release of image data is enabled.
237  *
238  * \sa IsReleaseImageDataEnabled()
239  * \sa disti::SetReleaseImageDataDefault()
240  *
241  * \note The static methods SetUnloadImageDataEnabled() and IsUnloadImageDataEnabled() from Image and Image::InstanceListEntry
242  * were removed to disambiguate between the per-instance setting and the global setting. Code that was calling
243  * Image::SetUnloadImageDataEnabled() to set the default flag for all instances should now call disti::SetReleaseImageDataDefault().
244  * Code that was calling Image::InstanceListEntry::SetUnloadImageDataEnabled() to set the flag for only one instance
245  * should call this method.
246  */
247  GLS_EXPORT void SetReleaseImageDataEnabled( bool enable );
248 
249  /** Returnswhether release of image data is enabled.
250  *
251  * \sa SetReleaseImageDataEnabled()
252  * \sa disti::GetReleaseImageDataDefault()
253  *
254  * \note The static methods SetUnloadImageDataEnabled() and IsUnloadImageDataEnabled() from Image and Image::InstanceListEntry
255  * were removed to disambiguate between the per-instance setting and the global setting. Code that was calling
256  * Image::IsUnloadImageDataEnabled() to get the default flag for all instances should now call disti::GetReleaseImageDataDefault().
257  * Code that was calling Image::InstanceListEntry::IsUnloadImageDataEnabled() to get the flag for only one instance
258  * should call this method.
259  */
260  GLS_EXPORT bool IsReleaseImageDataEnabled();
261 
262 #ifdef GLES
263  /** see IFontImage */
264  virtual unsigned int StateManagerHandle() const { return _stateManagerHandle; }
265 
266  /** see IFontImage */
267  virtual void StateManagerHandle( unsigned int handle ) { _stateManagerHandle = handle; }
268 #endif
269 
270  /** These are options that are possibly used during loading of the image. */
272  {
273  public:
274  LoadOptions() : _reverseAlpha(FALSE)
275  {
276  }
277 
278  bool _reverseAlpha; /** For compatibility with images with the alpha channel reversed */
279 
280  bool operator==( const LoadOptions &rhs ) const
281  {
282  return( _reverseAlpha == rhs._reverseAlpha );
283  }
284  }; // class LoadOptions
285 
286  /** an entry in the Image::InstanceList */
288  {
289  public:
290  /** holds absolute file path, file size, mod time and image load options */
291  class FileID
292  {
293  public:
294  /** default ctor - no file */
295  GLS_EXPORT FileID( void );
296 
297  /** ctor
298  * \param path path for file ( can be relative or absolute, will ultimately be stored in the FileID as an absolute path )
299  * \param loadOptions image loading options for file
300  */
301  GLS_EXPORT FileID( const FilePathClass &path, const LoadOptions &loadOptions );
302 
303  /** copy ctor
304  * \param src FileID to copy
305  */
306  GLS_EXPORT FileID( const FileID &src );
307 
308  /** get the absolute path for the file
309  * \return the absolute path for the file
310  */
311  GLS_EXPORT const FilePathClass& GetPath( void ) const;
312 
313  /** get the image load options for the file
314  * \return the image load options for the file
315  */
316  GLS_EXPORT const LoadOptions& GetLoadOptions( void ) const;
317 
318  /** get the file size for the file
319  * \return the file size for the file
320  */
321  GLS_EXPORT off_t GetFileSize( void ) const;
322 
323  /** get the modification time for the file
324  * \return the modification time for the file
325  */
326  GLS_EXPORT time_t GetModTime( void ) const;
327 
328  /** compare two FileID's checking for same absolute path and load options
329  * \note file size and mod time are not compared here, just absolute path and load options
330  * \param rhs FileID to compare
331  * \return true if equal absolute paths and load options
332  */
333  GLS_EXPORT bool operator==( const FileID &rhs ) const;
334 
335  /** assigment operator
336  * \param rhs
337  */
338  GLS_EXPORT FileID& operator=( const FileID &rhs );
339 
340  /** get the absolute path for the given path
341  * \param imgPath path in question
342  * \return the absolute path for the given path
343  */
344  static FilePathClass GetAbsoluteFilePath( const FilePathClass &imgPath );
345 
346  protected:
347  FilePathClass _path; /**< absolute path for image file */
348  LoadOptions _loadOptions; /**< load options associated with image */
349  off_t _fileSize; /**< size in bytes of image file */
350  time_t _modTime; /**< file mod time for image file */
351  }; // class FileID
352 
353  /** array of FileID */
355 
356  /** ctor
357  * \param img image for list entry
358  * \param imgPath [optional, defaults to ""] optional path to image file associated with image
359  * \param imgLoadOptions [optional] optional load options associated with image
360  */
361  GLS_EXPORT InstanceListEntry( Image *img, const FilePathClass &imgPath = FilePathClass(), const LoadOptions &imgLoadOptions = LoadOptions() );
362 
363  /** dtor -- NOTE: image associated with entry is not destroyed */
364  GLS_EXPORT ~InstanceListEntry();
365 
366  /** get the image associated with the entry
367  * \return the image associated with the entry
368  */
369  GLS_EXPORT Image* GetImage( void );
370 
371  /** get the image files associated with the entry
372  * \return the image files associated with the entry
373  */
374  GLS_EXPORT FileIDArray& GetAssociatedFiles( void );
375 
376  /** determine if the given fileID is associated with this entry
377  * \param fileID file ID in question
378  * \param foundFileID [out] receives associated file ID if the given fileID is associated with this entry
379  * \return true if the given fileID is associated with this entry else false
380  */
381  GLS_EXPORT bool FileIsAssociatedWithImage( const FileID &fileID, FileID &foundFileID );
382 
383  /** associate the given fileID with the image in this entry
384  * \param fileID file ID in question
385  */
386  GLS_EXPORT void AssociateFileWithImage( const FileID &fileID );
387 
388  /** disassociate the given fileID with the image in this entry
389  * \param fileID file ID in question
390  */
391  GLS_EXPORT void DisassociateFileWithImage( const FileID &fileID );
392 
393  /** Attempt to reload the associated image from an associated image file. Associated image will
394  * be marked as no longer being a candidate for unloading.
395  * NOTE: Image::InstanceListMutex() cannot be locked when calling this method
396  * \return true if reload succeeded else false
397  * \pre Image::InstanceListMutex() is not locked
398  */
399  GLS_EXPORT bool ReloadImageFromFile( void );
400 
401  protected:
402  Image *_img; /**< image associated with entry */
403  FileIDArray _associatedFiles; /**< files associated with image else empty array */
404 
405  private:
406  // disallow
407  InstanceListEntry( const InstanceListEntry &src );
408  InstanceListEntry operator=( const InstanceListEntry &rhs );
409  }; // class InstanceListEntry
410 
411 protected:
412  /** Add this image to the Image Sharing Instance List */
413  GLS_EXPORT void AddSelfToInstanceList();
414 
415  /** Set the Image _texWidth, _texHeight, _texWidth, _texCoord[0],
416  * and _texCoord[1]. Width and height must be set before calling
417  * this function. */
418  virtual GLS_EXPORT void SetTexWidthHeight(void);
419 
420  /** Allocate and initialize the pixel buffer memory for this image
421  * _texWidth, _textHeight and _pixelSize must be initialized prior to
422  * calling! _rgbBuf is set to the initialized memory.
423  * \param zeroOut Whether or not to clear the memory to zero on allocation.
424  * \return True if allocated. false on failure.
425  */
426  virtual GLS_EXPORT bool AllocatePixelMemory(bool zeroOut = true);
427 
428  /** Allocate memory for Mip-Map images
429  * \param zeroOut Whether or not to clear the memory to zero on allocation.
430  */
431  virtual GLS_EXPORT bool AllocateMipMapMemory(bool zeroOut = true);
432 
433  /** Calculate mip-map images from the main image */
434  virtual GLS_EXPORT void CalculateMipMapImages(bool reCalc = false);
435 
436  /** Helper routine used by constructor. Allocates pixel memory and sets the
437  * texture width, height and pixel size
438  * \param width Width of the image
439  * \param height Height of the image
440  * \param pixelSize The size in bytes of each pixel
441  */
442  virtual GLS_EXPORT void Initialize(int width,int height,int pixelSize);
443 
444 public:
445 
446  /** \return The state of any error found reading image file flag. */
447  virtual GLS_EXPORT int ErrorFound(void);
448 
449  /** Sets the error message.
450  * \param errorMessage String containing error message.
451  */
452  virtual GLS_EXPORT void SetError(char *errorMessage);
453 
454  /** Returns the error message found during reading image file. */
455  virtual GLS_EXPORT char *GetError(void);
456 
457  /** Draws the image at the current raster position. */
458  virtual GLS_EXPORT void Draw(void);
459 
460  /** Draws the image at the x,y raster position specified.
461  * \param x X position to draw the image
462  * \param y Y position to draw the image
463  */
464  virtual GLS_EXPORT void Draw(int x,int y);
465 
466  /** \return A new image that is a duplicate of this image or NULL if duplication is not possible.
467  */
468  virtual GLS_EXPORT Image* Clone();
469 
470  /** \return A pointer to the 2D image data */
471  virtual GLS_EXPORT unsigned char *ImageData(void);
472 
473  /** \return the X position of the texture coordinate used for the image. */
474  virtual GLS_EXPORT float TextureCoordX(void);
475 
476  /** \return the Y position of the texture coordinate used for the image. */
477  virtual GLS_EXPORT float TextureCoordY(void);
478 
479  /** \return the texture width of the image file. This is the power
480  * of 2 size greater than or equal to the size of the image.
481  */
482  virtual GLS_EXPORT int TextureWidth(void);
483 
484  /** \return the texture height of the image file. This is the power
485  * of 2 size greater than or equal the size of the image.
486  */
487  virtual GLS_EXPORT int TextureHeight(void);
488 
489  /** \return the memory size of the image in bytes */
490  virtual GLS_EXPORT int Size(void);
491 
492  /** \return the width of the image. */
493  virtual GLS_EXPORT int Width(void);
494 
495  /** \return the height of the image. */
496  virtual GLS_EXPORT int Height(void);
497 
498  /** \return true if the internal texture handle is valid */
499  virtual GLS_EXPORT bool TextureHandleValid();
500 
501  /** \return the OpenGL texture handle of this texture instance */
502  virtual GLS_EXPORT unsigned int TextureHandle() const { return _textureHandle; }
503 
504  /** Returns the pixel format for this image
505  * \return The internal pixel format for this image
506  */
507  virtual GLS_EXPORT int PixelFormat(void);
508 
509  /** Sets the pixel format for this image and also the pixel size as
510  * determined from the format. Eg. GL_RGBA has a pixel size of 4.
511  * \param format The new internal pixel format for this image
512  */
513  virtual GLS_EXPORT void PixelFormat(int format);
514 
515  /** \return The OpenGL enumerator for the pixel format for this
516  * image as a string. Used for inline texture generation
517  */
518  virtual GLS_EXPORT char *PixelFormatString(void);
519 
520  /** \return The size of a pixel in this image in bytes */
521  virtual GLS_EXPORT int PixelSize(void);
522 
523  /** Returns a pointer to the memory location corresponding to the given X,Y
524  * position within the image
525  * \param x X raster position
526  * \param y Y raster position
527  */
528  virtual GLS_EXPORT unsigned char *GetRasterPosition(int x,int y);
529 
530  /** Set the value of a secific pixel in the rbg buffer.
531  * \param x X position of the pixel
532  * \param y Y position of the pixel
533  * \param color Color of the pixel
534  */
535  virtual GLS_EXPORT void SetPixel(int x,int y,float color[]);
536 
537  /** Takes the image and creates a 2D Decal texture map. (Not Implemented)*/
538  virtual GLS_EXPORT void SetAsCurrentTexture(void);
539 
540  /** Create a new texture object using Image object data. */
541  virtual GLS_EXPORT void AllocateTextureBinding(void);
542 
543  /** Deletes the OpenGL texture object for this Image. */
544  virtual GLS_EXPORT void DeallocateTextureBinding(void);
545 
546  /** Make this Image object the active texture. */
547  virtual GLS_EXPORT void BindTexture(void);
548 
549  /** Create an Image object with the specified image data and image size.
550  * \param data Pointer to the image data
551  * \param width Width of the image
552  * \param height Height of the image
553  * \param pixelFormat The format of each pixel, eg. GL_RGBA
554  */
555  GLS_EXPORT Image(const unsigned char* const data,int width,int height,int pixelFormat);
556 
557  /** Create an Image object with the specified image size.
558  * \param width Width of the image
559  * \param height Height of the image
560  * \param pixelSize The size in bytes of each pixel
561  * \param allowNPOT If the image is allowed to be non-power of two (NPOT)
562  */
563  GLS_EXPORT Image(int width,int height,int pixelSize, bool allowNPOT = false);
564 
565  /** Create an Image from an inline image (GL Studio 2.2 and later API)
566  * \param image Structure containing details of inline image
567  */
568  GLS_EXPORT Image(const glsInlineImage &image);
569 
570  /** Create an Image from a buffer of ZLIB compressed data
571  * Image data must already be a power of two!
572  * \param width Width of the image
573  * \param height Height of the image
574  * \param components The size in bytes of each pixel
575  * \param format The OpenGL texture format to use
576  * \param comprLen The size of the compressed image
577  * \param data The buffer of compressed data
578  * \param crcVal The CRC value used to find duplicate textures
579  * \param lineLength The number of bytes in each inline image line
580  */
581  GLS_EXPORT Image(int width,int height,int components,int format,unsigned int comprLen,const unsigned char* const data[], unsigned long crcVal = 0,int lineLength=320);
582 
583  /** Create an Image from a buffer of uncompressed data
584  * Image data must already be a power of two!
585  * \param width Width of the image
586  * \param height Height of the image
587  * \param components The size in bytes of each pixel
588  * \param format The OpenGL texture format to use
589  * \param pix The buffer of uncompressed data
590  * \param crcVal The CRC value used to find duplicate textures
591  * \param lineLength The number of bytes in each inline image line
592  */
593  GLS_EXPORT Image(int width,int height,int components,int format,const unsigned char* const pix[], unsigned long crcVal = 0,int lineLength=320);
594 
595  /** Create an Image from another Image instance.
596  */
597  GLS_EXPORT Image(const Image& source);
598 
599  /** Constructor for an Image object. */
600  GLS_EXPORT Image(void);
601 
602  GLS_EXPORT void DoTexSubImage();
603 
604  /** Unload RGB and mipmap data to save memory */
605  GLS_EXPORT void UnloadRgbData();
606 
607 protected:
608  /** Destroy an Image object. Frees the memory for the RGB buffer. */
609  virtual GLS_EXPORT ~Image(void);
610 
611 public:
612 
613  /** Will delete the Image object if nobody is using it */
614  virtual GLS_EXPORT void DeleteUsage();
615 
616  /** Lets this object know that another something is using this image */
617  virtual GLS_EXPORT void AddUsage();
618 
619  /** \return the number of simultanious users of this image */
620  virtual GLS_EXPORT int NumUsers();
621 
622  /** Overloaded operator for copying Image objects. */
623  virtual GLS_EXPORT void operator = (Image &im);
624 
625  /** Scale the texture to the nearest power of two */
626  virtual GLS_EXPORT void ScaleTexture(void);
627 
628  /** \return the CRC value for this image. 0 indicates an invalid value or that duplicate checking is disabled for the Image.
629  */
630  virtual GLS_EXPORT unsigned long CrcValue();
631 
632  /** Equality operator. Determines if two images are equal
633  * \param im Image to test
634  * \return boolean result
635  */
636  virtual GLS_EXPORT int operator == (Image &im);
637 
638  /** Determines if an image #1 is the same a notional image based on the following criteria
639  * \param image Image #1
640  * \param width Width of notional image
641  * \param height Height of notional image
642  * \param components Number of components in the notional image
643  * \param crcVal 32 bit CRC of notional image
644  * \param glTexCompress GlTextureCompresssion setting for the image
645  * \return True if they are equal
646  */
647  static GLS_EXPORT bool IsDuplicate(Image *image, int width,int height,int components,unsigned long crcVal, bool glTexCompress);
648 
649  /** Tries to find an image that is the same as this image
650  * \return A pointer to the first image in the image instance list that matches or NULL if not found
651  */
652  virtual GLS_EXPORT Image *FindDuplicate();
653 
654  /** Tries to find an image that is the same as a notional image based on the following criteria
655  * \param width Width of notional image
656  * \param height Height of notional image
657  * \param components Number of components in the notional image
658  * \param crcVal 32 bit CRC of notional image
659  * \param glTexCompress GlTextureCompresssion setting for the image
660  * \return A pointer to the first image in the image instance list that matches or NULL if not found
661  */
662  static GLS_EXPORT Image* FindDuplicate(int width,int height,int components,unsigned long crcVal, bool glTexCompress);
663 
664  /** Enable or disable mipmaps for this image
665  * \param set Whether or not mipmaps are enabled
666  */
667  virtual GLS_EXPORT void MipMap(bool set);
668 
669  /** \return Whether or not mipmaps are enabled */
670  virtual GLS_EXPORT bool MipMap();
671 
672  /** Enable or disable Open GL texture compression for this image
673  * \param set Whether or not Open GL texture compression is enabled
674  */
675  virtual GLS_EXPORT void GlTextureCompression(bool set);
676 
677  /** \return Whether or not Open GL texture compression is enabled */
678  virtual GLS_EXPORT bool GlTextureCompression();
679 
680  /** Scales the image up to the nearest power of two in each dimension */
681  virtual GLS_EXPORT void ScaleToPowerOfTwo(void);
682 
683  /** Replaces the current image with the specified.
684  * The new image must be the same _texWidth, _texHeight, and _pixelSize
685  * as the current image */
686  virtual GLS_EXPORT void ReplaceImage(unsigned char *imageData);
687 
688  /** Replaces the current image with the specified.
689  * \param imageData buffer containing the new image data
690  * \param width new Width of the image. Must be a power of two (2^n).
691  * \param height new Height of the image. Must be a power of two (2^n).
692  * \param pixelSize The size in bytes of each pixel
693  * \returns true if successful, false if the operation is unsupported or there was an error.
694  */
695  virtual GLS_EXPORT bool ReplaceImage(unsigned char *imageData, int width, int height, int pixelSize);
696 
697  /** Allocates the local rgb buffer from the glsInlineImage for this image.
698  * \returns true if successful, false on failure (e.g. this image doesn't have a glsInlineImage associated with it.)
699  */
700  virtual GLS_EXPORT bool LoadInlineImageData();
701 
702  /** Frees the local rgb buffer if this image has a glsInlineImage associated with it.
703  * \returns true if the buffer was freed, false if this Image does not have a glsInlineImage associated with it.
704  */
705  virtual GLS_EXPORT bool FreeInlineImageData();
706 
707  /** Removes the association between this Image and it's glsInlineImage data.
708  * This will use more memory, but makes the Image independent of the static data.
709  * This method will attempt to load the image before disconnecting from the glsInlineImage,
710  * if the load fails, ImageData() will return NULL afterwards.
711  * After calling this method, the Image will no longer have a glsInlineImage associated with it.
712  *
713  * \param loadImage If true, the _rgbBuf will be loaded from the inline image data before disconnecting
714  * If false, the _rgbBuf will be allocated but not initialized.
715  * \returns true if the Image had a glsInlineImage associated with it
716  * \return false if the Image did not have a glsInlineImage associated with it.
717  */
718  virtual GLS_EXPORT bool DisconnectInlineImage(bool loadImage = true);
719 
720  /** If AllowImageSharing is true, this Image may be deleted by the texture palette if a duplicate texture is found.
721  * This also implies that the Image will not change and may be used in place of another texture
722  * with the same CrcValue(). If false this causes Image::CrcValue() to always return 0,
723  * disabling duplicate checking. (default: true)
724  * Note: If this Image is already in a TexturePalette, you should call TexturePalette::DisableImageSharing()
725  * instead of this method.
726  */
727  virtual GLS_EXPORT void AllowImageSharing(bool value);
728  virtual GLS_EXPORT bool AllowImageSharing();
729 
730  /** Determine if non-power of two (NPOT) textures are allowed for this image.
731  * \return true if non-power of two (NPOT) textures are allowed.
732  */
733  GLS_EXPORT bool IsAllowNPOTTextures( void ) const;
734 }; // class Image
735 
736 } // namespace disti
737 
738 #endif
Definition: image.h:76
unsigned long _image_data_size
Definition: image.h:86
virtual bool MipMap()
glsImageCodec
Definition: image.h:59
const int IMG_NON_TRANSPARENT
Definition: image.h:56
A class to handle file paths.
virtual void ScaleTexture(void)
Definition: image.h:160
unsigned long _crc
Definition: image.h:84
static bool _globalMipMapEnabled
Definition: image.h:213
virtual int operator==(Image &im)
virtual void BindTexture(void)
Definition: IFontImage.h:54
virtual void DeallocateTextureBinding(void)
bool _glTextureCompression
Definition: image.h:194
unsigned int _textureHandle
Definition: image.h:164
virtual int Height(void)
bool operator==(const FileID &rhs) const
glsImageCodec _codec
Definition: image.h:83
void ConvertLuminanceAlphaToAlpha()
off_t _fileSize
Definition: image.h:349
time_t _modTime
Definition: image.h:350
void UnloadRgbData()
const int MAX_MIP_MAP_IMAGES
Definition: image.h:57
virtual float TextureCoordX(void)
bool _gl_texture_compression
Definition: image.h:85
unsigned int _components
Definition: image.h:81
Definition: image.h:69
unsigned char * _mmBuf[MAX_MIP_MAP_IMAGES]
Definition: image.h:181
virtual unsigned long CrcValue()
virtual void SetError(char *errorMessage)
virtual void SetPixel(int x, int y, float color[])
bool _mipMap
Definition: image.h:180
virtual ~Image(void)
virtual char * GetError(void)
virtual unsigned char * GetRasterPosition(int x, int y)
virtual unsigned int TextureHandle() const
Definition: image.h:502
virtual Image * FindDuplicate()
Definition: image.h:271
virtual void ReplaceImage(unsigned char *imageData)
void SetNPOTState(ImageNPOTMode state)
int _height
Definition: image.h:170
virtual void ScaleToPowerOfTwo(void)
virtual bool TextureHandleValid()
virtual float TextureCoordY(void)
A file for all GL Studio files to include.
Image * _img
Definition: image.h:402
virtual bool DisconnectInlineImage(bool loadImage=true)
void DisassociateFileWithImage(const FileID &fileID)
unsigned int _pixel_format
Definition: image.h:82
int _pixelSize
Definition: image.h:166
bool GetReleaseImageDataDefault(void)
virtual int Size(void)
virtual int PixelSize(void)
int _errorFound
Definition: image.h:167
Definition: image.h:62
Definition: image.h:68
FilePathClass _path
Definition: image.h:347
virtual void Draw(void)
bool _allowImageSharing
Definition: image.h:198
Definition: image.h:287
_BufferType * _alpha_buffer
Definition: image.h:90
virtual void Initialize(int width, int height, int pixelSize)
_BufferType * _rgb_buffer
Definition: image.h:89
float _texCoord[2]
Definition: image.h:174
virtual bool AllocatePixelMemory(bool zeroOut=true)
The List_c class. Generic linked list.
unsigned long _crcValue
Definition: image.h:183
virtual Image * Clone()
static List_c & InstanceList()
virtual void CalculateMipMapImages(bool reCalc=false)
virtual bool GlTextureCompression()
Definition: gls_mutex.h:53
int _texWidth
Definition: image.h:171
Generally useful defines, macros, enumerations and function prototypes.
const FilePathClass & GetPath(void) const
Definition: image.h:61
static disti::Mutex & InstanceListMutex()
virtual bool LoadInlineImageData()
bool IsAllowNPOTTextures(void) const
unsigned int _height
Definition: image.h:80
Definition: image.h:63
virtual void SetTexWidthHeight(void)
Definition: file_path_class.h:60
FileIDArray _associatedFiles
Definition: image.h:403
unsigned int _line_length
Definition: image.h:88
FileID & operator=(const FileID &rhs)
unsigned long _alpha_data_size
Definition: image.h:87
virtual int ErrorFound(void)
glsInlineImage * _staticInlineImage
Definition: image.h:196
virtual void operator=(Image &im)
FileIDArray & GetAssociatedFiles(void)
int _width
Definition: image.h:169
int _pixelFormat
Definition: image.h:165
const int IMG_TRANSPARENT
Definition: image.h:55
void SetReleaseImageDataDefault(bool enable)
int _texHeight
Definition: image.h:172
char * _errorMessage
Definition: image.h:168
virtual char * PixelFormatString(void)
LoadOptions _loadOptions
Definition: image.h:348
bool _replaceImageFlag
Definition: image.h:187
bool _releaseImageData
Definition: image.h:189
int _numUsers
Definition: image.h:185
bool FileIsAssociatedWithImage(const FileID &fileID, FileID &foundFileID)
ImageNPOTMode
Definition: image.h:66
virtual int Width(void)
static bool IsDuplicate(Image *image, int width, int height, int components, unsigned long crcVal, bool glTexCompress)
virtual bool FreeInlineImageData()
InstanceListEntry(Image *img, const FilePathClass &imgPath=FilePathClass(), const LoadOptions &imgLoadOptions=LoadOptions())
virtual bool AllocateMipMapMemory(bool zeroOut=true)
DynamicArray< FileID, false > FileIDArray
Definition: image.h:354
bool IsReleaseImageDataEnabled()
virtual unsigned char * ImageData(void)
Definition: image.h:70
unsigned char * _rgbBuf
Definition: image.h:178
static FilePathClass GetAbsoluteFilePath(const FilePathClass &imgPath)
virtual void DeleteUsage()
virtual void SetAsCurrentTexture(void)
virtual void AllocateTextureBinding(void)
void AssociateFileWithImage(const FileID &fileID)
virtual int TextureHeight(void)
ImageNPOTMode DoesSystemSupportNPOT()
void AddSelfToInstanceList()
virtual int NumUsers()
virtual void AddUsage()
bool operator==(const LoadOptions &rhs) const
Definition: image.h:280
void SetReleaseImageDataEnabled(bool enable)
Definition: bmpimage.h:46
bool _allowNPOT
Definition: image.h:200
Definition: list.h:135
static bool _glTextureCompressionSupported
Definition: image.h:191
static void EmptyInstanceList()
virtual int TextureWidth(void)
unsigned int _width
Definition: image.h:79
virtual int PixelFormat(void)
IFontImage.
const LoadOptions & GetLoadOptions(void) const