csplugincommon/imageloader/commonimagefile.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

csplugincommon/imageloader/commonimagefile.h
Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 2005 by Jorrit Tyberghein
    00003                   2005 by Frank Richter
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public
    00016     License along with this library; if not, write to the Free
    00017     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00018 */
    00019
    00020 #ifndef __CS_CSPLUGINCOMMON_IMAGELOADER_COMMONIMAGEFILE_H__
    00021 #define __CS_CSPLUGINCOMMON_IMAGELOADER_COMMONIMAGEFILE_H__
    00022
    00027 #include "csextern.h"
    00028 #include "csgfx/imagememory.h"
    00029 #include "csutil/ref.h"
    00030 #include "csutil/scf_interface.h"
    00031 #include "csutil/scf_implementation.h"
    00032 #include "csutil/weakref.h"
    00033 #include "iutil/databuff.h"
    00034 #include "iutil/job.h"
    00035
    00036 struct iObjectRegistry;
    00037
    00046 enum csLoaderDataType
    00047 {
    00049   rdtInvalid,
    00051   rdtR8G8B8,
    00053   rdtRGBpixel,
    00055   rdtIndexed
    00056 };
    00057
    00058 class csCommonImageFile;
    00059
    00064 struct iImageFileLoader : public virtual iBase
    00065 {
    00066   SCF_INTERFACE(iImageFileLoader, 2,0,0);
    00068   virtual bool LoadData () = 0;
    00070   virtual csRef<iDataBuffer> GetRawData() = 0;
    00072   virtual csLoaderDataType GetDataType() = 0;
    00074   virtual int GetWidth() = 0;
    00076   virtual int GetHeight() = 0;
    00078   virtual int GetFormat() = 0;
    00080   virtual void ApplyTo (csImageMemory* image) = 0;
    00082   virtual bool HasKeyColor() const = 0;
    00084   virtual void GetKeyColor (int &r, int &g, int &b) const = 0;
    00085 };
    00086
    00090 class CS_CRYSTALSPACE_EXPORT csCommonImageFileLoader :
    00091   public scfImplementation1<csCommonImageFileLoader, iImageFileLoader>
    00092 {
    00093 protected:
    00095   int Format;
    00100   csRef<iDataBuffer> rawData;
    00102   csLoaderDataType dataType;
    00103   /* rgbaData and indexData are sent to the image when it needs to be filled. */
    00105   csRGBpixel* rgbaData;
    00107   uint8* indexData;
    00109   csRGBpixel* palette;
    00111   size_t paletteCount;
    00113   uint8* alpha;
    00115   bool hasKeycolor;
    00117   csRGBcolor keycolor;
    00119   int Width, Height;
    00120 public:
    00121   csCommonImageFileLoader (int format);
    00122   virtual ~csCommonImageFileLoader();
    00123
    00124   virtual csRef<iDataBuffer> GetRawData()
    00125   { return rawData; }
    00126   virtual csLoaderDataType GetDataType()
    00127   { return dataType; }
    00128   virtual int GetWidth() { return Width; }
    00129   virtual int GetHeight() { return Height; }
    00130   virtual int GetFormat() { return Format; }
    00131   virtual void ApplyTo (csImageMemory* image);
    00132   virtual bool HasKeyColor() const { return hasKeycolor; }
    00133   virtual void GetKeyColor (int &r, int &g, int &b) const
    00134   {
    00135     r = keycolor.red; g = keycolor.green; b = keycolor.blue;
    00136   }
    00137 };
    00138
    00139 #define CSCOMMONIMAGEFILE_THREADED_LOADING
    00140
    00144 class CS_CRYSTALSPACE_EXPORT csCommonImageFile :
    00145   public scfImplementationExt0<csCommonImageFile, csImageMemory>
    00146 {
    00147 protected:
    00148   friend class csCommonImageFileLoader;
    00149
    00150   class CS_CRYSTALSPACE_EXPORT LoaderJob :
    00151     public scfImplementation1<LoaderJob, iJob>
    00152   {
    00154     CS::Threading::Mutex fileToLoadLock;
    00155     csCommonImageFile* fileToLoad;
    00156   public:
    00158     bool loadResult;
    00160     LoaderJob (csCommonImageFile* fileToLoad);
    00161     virtual ~LoaderJob();
    00162
    00163     virtual void Run();
    00164
    00165     void ClearFileToLoad ();
    00166   };
    00167
    00168 #ifdef CSCOMMONIMAGEFILE_THREADED_LOADING
    00169
    00170   // This and jobQueue are mutable so MakeImageData() can be called.
    00171   mutable csRef<LoaderJob> loadJob;
    00173   mutable csRef<iJobQueue> jobQueue;
    00174 #endif
    00175   // This is mutable so MakeImageData() can be called.
    00176   mutable csRef<iImageFileLoader> currentLoader;
    00177   iObjectRegistry* object_reg;
    00178
    00179   csCommonImageFile (iObjectRegistry* object_reg, int format);
    00180   virtual ~csCommonImageFile();
    00181
    00183   virtual bool Load (csRef<iDataBuffer> source);
    00190   virtual csRef<iImageFileLoader> InitLoader (csRef<iDataBuffer> source) = 0;
    00191
    00193   void WaitForJob() const;
    00195   // const so it can be called from GetRaw*().
    00196   void MakeImageData() const;
    00197
    00198   virtual const void *GetImageData ();
    00199   virtual const csRGBpixel* GetPalette ();
    00200   virtual const uint8* GetAlpha ();
    00201
    00202   virtual bool HasKeyColor () const
    00203   {
    00204 #ifdef CSCOMMONIMAGEFILE_THREADED_LOADING
    00205     if (currentLoader)
    00206     {
    00207       return currentLoader->HasKeyColor();
    00208     }
    00209 #endif
    00210     return has_keycolour;
    00211   }
    00212
    00213   virtual void GetKeyColor (int &r, int &g, int &b) const
    00214   {
    00215 #ifdef CSCOMMONIMAGEFILE_THREADED_LOADING
    00216     if (currentLoader)
    00217     {
    00218       // Keycolor may only be available after loading...
    00219       WaitForJob();
    00220       currentLoader->GetKeyColor (r, g, b);
    00221       return;
    00222     }
    00223 #endif
    00224     r = keycolour.red; g = keycolour.green; b = keycolour.blue;
    00225   }
    00226
    00231   static const char* DataTypeString (csLoaderDataType dataType);
    00232   virtual const char* GetRawFormat() const;
    00233   virtual csRef<iDataBuffer> GetRawData() const;
    00234 };
    00235
    00238 #endif // __CS_CSPLUGINCOMMON_IMAGELOADER_COMMONIMAGEFILE_H__