PVRTC.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /******************************************************************************/
  2. #ifdef __linux__
  3. #define USE_DLL 1
  4. #else
  5. #define USE_DLL 0
  6. #endif
  7. #if USE_DLL
  8. #include "stdafx.h"
  9. #define private public
  10. #define protected public
  11. #endif
  12. #include "../begin.h"
  13. #include "PVRTex/PVRTexture.h"
  14. #include "PVRTex/PVRTextureUtilities.h"
  15. #include "../end.h"
  16. #if USE_DLL
  17. pvrtexture::PixelType::PixelType(uint64 Type) {PixelTypeID=Type;}
  18. pvrtexture::PixelType::PixelType(uint8 C1Name, uint8 C2Name, uint8 C3Name, uint8 C4Name, uint8 C1Bits, uint8 C2Bits, uint8 C3Bits, uint8 C4Bits)
  19. {
  20. PixelTypeChar[0]=C1Name;
  21. PixelTypeChar[1]=C2Name;
  22. PixelTypeChar[2]=C3Name;
  23. PixelTypeChar[3]=C4Name;
  24. PixelTypeChar[4]=C1Bits;
  25. PixelTypeChar[5]=C2Bits;
  26. PixelTypeChar[6]=C3Bits;
  27. PixelTypeChar[7]=C4Bits;
  28. }
  29. #endif
  30. namespace EE{
  31. /******************************************************************************/
  32. typedef unsigned char Byte;
  33. typedef void * Ptr ;
  34. typedef const void *CPtr ;
  35. typedef bool Bool;
  36. typedef int Int ;
  37. /******************************************************************************/
  38. Bool _CompressPVRTC(Int w, Int h, CPtr data, Int type, Int quality, Ptr &compressed_data, Int &compressed_size)
  39. {
  40. Bool ok=false;
  41. compressed_data=NULL;
  42. compressed_size=0;
  43. using namespace pvrtexture;
  44. #if USE_DLL
  45. Byte _texture[SIZE(CPVRTexture)]; memset(_texture, 0, SIZE(_texture));
  46. CPVRTexture &texture=(CPVRTexture&)_texture;
  47. PVRTextureHeaderV3 &header=texture.m_sHeader;
  48. header=PVRTextureHeaderV3();
  49. header.u32Width=w;
  50. header.u32Height=h;
  51. header.u64PixelFormat=PVRStandard8PixelType.PixelTypeID;
  52. texture.m_stDataSize=w*h*4; // assumes that image is RGBA (size Color == 4)
  53. texture.m_pTextureData=(uint8*)malloc(texture.m_stDataSize); memcpy(texture.m_pTextureData, data, Int(texture.m_stDataSize)); // !! 'texture.m_pTextureData' must have its own unique memory !!
  54. typedef bool (*TranscodeType)(CPVRTexture& sTexture, const PixelType ptFormat, const EPVRTVariableType eChannelType, const EPVRTColourSpace eColourspace, const ECompressorQuality eQuality, const bool bDoDither);
  55. #if DEBUG
  56. TranscodeType temp=Transcode; // make sure that the type is OK
  57. #endif
  58. DLL dll;
  59. #ifdef _WIN32
  60. #if X64
  61. if(dll.createFile("C:/Esenthel/ThirdPartyLibs/PVRTC/PVRTex/Windows_x86_64/Dynamic/PVRTexLib.dll"))
  62. if(TranscodeType Transcode=(TranscodeType)dll.getFunc("?Transcode@pvrtexture@@YA_NAEAVCPVRTexture@1@TPixelType@1@W4EPVRTVariableType@@W4EPVRTColourSpace@@W4ECompressorQuality@1@_N@Z"))
  63. #else
  64. if(dll.createFile("C:/Esenthel/ThirdPartyLibs/PVRTC/PVRTex/Windows_x86_32/Dynamic/PVRTexLib.dll"))
  65. if(TranscodeType Transcode=(TranscodeType)dll.getFunc("?Transcode@pvrtexture@@YA_NAAVCPVRTexture@1@TPixelType@1@W4EPVRTVariableType@@W4EPVRTColourSpace@@W4ECompressorQuality@1@_N@Z"))
  66. #endif
  67. #elif defined __linux__
  68. if(dll.createFile("libPVRTexLib.so"))
  69. if(TranscodeType Transcode=(TranscodeType)dll.getFunc("_ZN10pvrtexture9TranscodeERNS_11CPVRTextureENS_9PixelTypeE17EPVRTVariableType16EPVRTColourSpaceNS_18ECompressorQualityEb"))
  70. #endif
  71. #else
  72. CPVRTextureHeader header(PVRStandard8PixelType.PixelTypeID, h, w);
  73. CPVRTexture texture(header, data);
  74. #endif
  75. if(Transcode(texture, EPVRTPixelFormat(type), ePVRTVarTypeUnsignedByteNorm, ePVRTCSpacelRGB, ECompressorQuality(quality), false)) // dithering makes smooth gradients (like sky) very bad so always disable them
  76. {
  77. #if USE_DLL
  78. Ptr data=texture.m_pTextureData;
  79. Int size=texture.m_stDataSize;
  80. #else
  81. Ptr data=texture.getDataPtr ();
  82. Int size=texture.getDataSize();
  83. #endif
  84. if( data && size>0)
  85. {
  86. memcpy(compressed_data=malloc(size), data, compressed_size=size);
  87. ok=true;
  88. }
  89. }
  90. #if USE_DLL
  91. free(texture.m_pTextureData);
  92. #endif
  93. return ok;
  94. }
  95. /******************************************************************************/
  96. } // namespace
  97. /******************************************************************************/