corona  1.0.2
corona.h
Go to the documentation of this file.
00001 
00019 #ifndef CORONA_H
00020 #define CORONA_H
00021 
00022 
00023 #ifndef __cplusplus
00024 #error Corona requires C++
00025 #endif
00026 
00027 
00028 #include <stddef.h>
00029 #include <string>
00030 
00031 
00032 // DLLs in Windows should use the standard calling convention
00033 #ifndef COR_CALL
00034 #  if defined(WIN32) || defined(_WIN32)
00035 #    define COR_CALL __stdcall
00036 #  else
00037 #    define COR_CALL
00038 #  endif
00039 #endif
00040 
00041 // Export functions from the DLL
00042 #ifndef COR_DECL
00043 #  if defined(WIN32) || defined(_WIN32)
00044 #    ifdef CORONA_EXPORTS
00045 #      define COR_DECL __declspec(dllexport)
00046 #    else
00047 #      define COR_DECL
00048 #    endif
00049 #  else
00050 #    define COR_DECL
00051 #  endif
00052 #endif
00053 
00054 
00055 // evil "identifier is too long in debug information" warning
00056 #ifdef _MSC_VER
00057 #pragma warning(disable : 4786)
00058 #endif
00059 
00060 
00061 #define COR_FUNCTION(ret) extern "C" COR_DECL ret COR_CALL
00062 
00063 
00064 namespace corona {
00065 
00069   enum FileFormat {
00070     FF_AUTODETECT = 0x0100,
00071     FF_PNG        = 0x0101,
00072     FF_JPEG       = 0x0102,
00073     FF_PCX        = 0x0103,
00074     FF_BMP        = 0x0104,
00075     FF_TGA        = 0x0105,
00076     FF_GIF        = 0x0106,
00077   };
00078 
00083   enum PixelFormat {
00084     PF_DONTCARE = 0x0200,  
00086     PF_R8G8B8A8 = 0x0201,  
00087     PF_R8G8B8   = 0x0202,  
00088     PF_I8       = 0x0203,  
00089     PF_B8G8R8A8 = 0x0204,  
00090     PF_B8G8R8   = 0x0205,  
00091   };
00092 
00097   enum CoordinateAxis {
00098     CA_X     = 0x0001,
00099     CA_Y     = 0x0002,
00100   };
00101 
00109   class DLLInterface {
00110   private:
00115     virtual void COR_CALL destroy() = 0;
00116 
00117   public:
00123     void operator delete(void* p) {
00124       if (p) {
00125         DLLInterface* i = static_cast<DLLInterface*>(p);
00126         i->destroy();
00127       }
00128     }
00129   };
00130 
00131 
00136   template<class Interface>
00137   class DLLImplementation : public Interface {
00138   public:
00143     virtual ~DLLImplementation() { }
00144 
00148     virtual void COR_CALL destroy() {
00149       delete this;
00150     }
00151 
00156     void operator delete(void* p) {
00157       ::operator delete(p);
00158     }
00159   };
00160 
00161   
00167   class Image : public DLLInterface {
00168   public:
00173     virtual int COR_CALL getWidth() = 0;
00174 
00179     virtual int COR_CALL getHeight() = 0;
00180 
00185     virtual PixelFormat COR_CALL getFormat() = 0;
00186 
00193     virtual void* COR_CALL getPixels() = 0;
00194 
00201     virtual void* COR_CALL getPalette() = 0;
00202 
00208     virtual int COR_CALL getPaletteSize() = 0;
00209 
00215     virtual PixelFormat COR_CALL getPaletteFormat() = 0;
00216   };
00217 
00218 
00225   class File : public DLLInterface {
00226   public:
00227 
00231     enum SeekMode {
00232       BEGIN,    
00233       CURRENT,  
00234       END       
00236     };
00237 
00246     virtual int COR_CALL read(void* buffer, int size) = 0;
00247 
00256     virtual int COR_CALL write(const void* buffer, int size) = 0;
00257 
00269     virtual bool COR_CALL seek(int position, SeekMode mode) = 0;
00270 
00276     virtual int COR_CALL tell() = 0;
00277   };
00278 
00279 
00281   class FileFormatDesc {
00282   protected:
00283     ~FileFormatDesc() { }
00284 
00285   public:
00287     virtual FileFormat getFormat() = 0;
00288 
00290     virtual const char* getDescription() = 0;
00291 
00294     virtual size_t getExtensionCount() = 0;
00295     virtual const char* getExtension(size_t i) = 0;
00297   };
00298 
00299 
00301   namespace hidden {
00302 
00303     // these are extern "C" so we don't mangle the names
00304 
00305 
00306     // API information
00307 
00308     COR_FUNCTION(const char*) CorGetVersion();
00309 
00310     COR_FUNCTION(FileFormatDesc**) CorGetSupportedReadFormats();
00311     COR_FUNCTION(FileFormatDesc**) CorGetSupportedWriteFormats();
00312 
00313     // creation
00314 
00315     COR_FUNCTION(Image*) CorCreateImage(
00316       int width,
00317       int height,
00318       PixelFormat format);
00319 
00320     COR_FUNCTION(Image*) CorCreateImageWithPixels(
00321       int width,
00322       int height,
00323       PixelFormat format,
00324       void* pixels);
00325 
00326     COR_FUNCTION(Image*) CorCreatePalettizedImage(
00327       int width,
00328       int height,
00329       PixelFormat format, // must be a palettized format
00330       int palette_size,
00331       PixelFormat palette_format);
00332 
00333     COR_FUNCTION(Image*) CorCloneImage(
00334       Image* source,
00335       PixelFormat format);
00336 
00337     // loading
00338 
00339     COR_FUNCTION(Image*) CorOpenImage(
00340       const char* filename,
00341       FileFormat file_format);
00342 
00343     COR_FUNCTION(Image*) CorOpenImageFromFile(
00344       File* file,
00345       FileFormat file_format);
00346 
00347     // saving
00348 
00349     COR_FUNCTION(bool) CorSaveImage(
00350       const char* filename,
00351       FileFormat file_format,
00352       Image* image);
00353 
00354     COR_FUNCTION(bool) CorSaveImageToFile(
00355       File* file,
00356       FileFormat file_format,
00357       Image* image);
00358 
00359     // conversion
00360 
00361     COR_FUNCTION(Image*) CorConvertImage(
00362       Image* image,
00363       PixelFormat format);
00364 
00365     COR_FUNCTION(Image*) CorConvertPalette(
00366       Image* image,
00367       PixelFormat palette_format);
00368 
00369     COR_FUNCTION(Image*) CorFlipImage(
00370       Image* image,
00371       int coordinate_axis);
00372 
00373     // files
00374 
00375     COR_FUNCTION(File*) CorOpenFile(const char* name, bool writeable);
00376     COR_FUNCTION(File*) CorCreateMemoryFile(const void* buffer, int size);
00377 
00378     // utility
00379 
00380     COR_FUNCTION(int) CorGetPixelSize(PixelFormat format);
00381   }
00382 
00383 
00384   /* PUBLIC API */
00385 
00386 
00392   inline const char* GetVersion() {
00393     return hidden::CorGetVersion();
00394   }
00395 
00396 
00402   inline FileFormatDesc** GetSupportedReadFormats() {
00403     return hidden::CorGetSupportedReadFormats();
00404   }
00405 
00411   inline FileFormatDesc** GetSupportedWriteFormats() {
00412     return hidden::CorGetSupportedWriteFormats();
00413   }
00414 
00415 
00431   inline Image* CreateImage(
00432     int width,
00433     int height,
00434     PixelFormat format,
00435     void* pixels = 0)
00436   {
00437     return hidden::CorCreateImageWithPixels(width, height, format, pixels);
00438   }
00439 
00450   inline Image* CreateImage(
00451     int width,
00452     int height,
00453     PixelFormat format,
00454     int palette_size,
00455     PixelFormat palette_format)
00456   {
00457     return hidden::CorCreatePalettizedImage(
00458       width, height, format,
00459       palette_size, palette_format);
00460   }
00461 
00474   inline Image* CloneImage(
00475     Image* source,
00476     PixelFormat format = PF_DONTCARE)
00477   {
00478     return hidden::CorCloneImage(source, format);
00479   }
00480 
00497   inline Image* OpenImage(
00498     const char* filename,
00499     PixelFormat pixel_format = PF_DONTCARE,
00500     FileFormat file_format = FF_AUTODETECT)
00501   {
00502     return hidden::CorConvertImage(
00503       hidden::CorOpenImage(filename, file_format),
00504       pixel_format);
00505   }
00506 
00508   inline Image* OpenImage(
00509     const std::string& filename,
00510     PixelFormat pixel_format = PF_DONTCARE,
00511     FileFormat file_format = FF_AUTODETECT)
00512   {
00513     return OpenImage(filename.c_str(), pixel_format, file_format);
00514   }
00515 
00536   inline Image* OpenImage(
00537     File* file,
00538     PixelFormat pixel_format = PF_DONTCARE,
00539     FileFormat file_format = FF_AUTODETECT)
00540   {
00541     return hidden::CorConvertImage(
00542       hidden::CorOpenImageFromFile(file, file_format),
00543       pixel_format);
00544   }
00545 
00547   inline Image* OpenImage(
00548     const char* filename,
00549     FileFormat file_format,
00550     PixelFormat pixel_format = PF_DONTCARE)
00551   {
00552     return OpenImage(filename, pixel_format, file_format);
00553   }
00554 
00556   inline Image* OpenImage(
00557     File* file,
00558     FileFormat file_format,
00559     PixelFormat pixel_format = PF_DONTCARE)
00560   {
00561     return OpenImage(file, pixel_format, file_format);
00562   }
00563 
00578   inline bool SaveImage(
00579     const char* filename,
00580     FileFormat file_format,
00581     Image* image)
00582   {
00583     return hidden::CorSaveImage(filename, file_format, image);
00584   }
00585 
00587   inline bool SaveImage(
00588     const std::string& filename,
00589     FileFormat file_format,
00590     Image* image)
00591   {
00592     return SaveImage(filename.c_str(), file_format, image);
00593   }
00594 
00612   inline bool SaveImage(
00613     File* file,
00614     FileFormat file_format,
00615     Image* image)
00616   {
00617     return hidden::CorSaveImageToFile(file, file_format, image);
00618   }
00619 
00634   inline Image* ConvertImage(Image* source, PixelFormat format) {
00635     return hidden::CorConvertImage(source, format);
00636   }
00637 
00652   inline Image* ConvertPalette(Image* source, PixelFormat palette_format) {
00653     return hidden::CorConvertPalette(source, palette_format);
00654   }
00655 
00665   inline Image* FlipImage(Image* source, int coordinate_axis) {
00666     return hidden::CorFlipImage(source, coordinate_axis);
00667   }
00668 
00675   inline File* OpenFile(const char* filename, bool writeable) {
00676     return hidden::CorOpenFile(filename, writeable);
00677   }
00678 
00692   inline File* CreateMemoryFile(const void* buffer, int size) {
00693     return hidden::CorCreateMemoryFile(buffer, size);
00694   }
00695 
00703   inline int GetPixelSize(PixelFormat format) {
00704     return hidden::CorGetPixelSize(format);
00705   }
00706 
00715   inline bool IsDirect(PixelFormat format) {
00716     return (format == PF_R8G8B8A8 || format == PF_R8G8B8 ||
00717             format == PF_B8G8R8A8 || format == PF_B8G8R8);
00718   }
00719 
00728   inline bool IsPalettized(PixelFormat format) {
00729     return format == PF_I8;
00730   }
00731 
00740   inline int GetPaletteSize(PixelFormat format) {
00741     return (format == PF_I8 ? 256 : 0);
00742   }
00743 
00744 }
00745 
00746 
00747 #endif