- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-IL/src/il_nvidia.cpp

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2009 by Denton Woods
    00005 // Last modified: 01/19/2009
    00006 //
    00007 // Filename: src-IL/src/il_nvidia.cpp
    00008 //
    00009 // Description: Implements access to the nVidia Texture Tools library.
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "il_internal.h"
    00015 #include "il_dds.h"
    00016 #include "il_manip.h"
    00017 #include <limits.h>
    00018
    00019
    00020 #ifdef IL_USE_DXTC_NVIDIA
    00021 #include <nvtt/nvtt.h>
    00022 #include <nvcore/Memory.h>
    00023
    00024 using namespace nvtt;
    00025
    00026 #if defined(_WIN32) && defined(IL_USE_PRAGMA_LIBS)
    00027         #if defined(_MSC_VER) || defined(__BORLANDC__)
    00028                 #ifndef _DEBUG
    00029                         #pragma comment(lib, "nvcore.lib")
    00030                         #pragma comment(lib, "nvtt.lib")
    00031                 #else
    00032                         #pragma comment(lib, "nvcore-d.lib")
    00033                         #pragma comment(lib, "nvtt-d.lib")
    00034                 #endif
    00035         #endif
    00036 #endif
    00037
    00038
    00039 struct ilOutputHandlerMem : public nvtt::OutputHandler
    00040 {
    00041         ilOutputHandlerMem(ILuint Width, ILuint Height, ILenum DxtType)
    00042         {
    00043                 Width = Width + (4 - (Width % 4)) % 4;    // Operates on 4x4 blocks,
    00044                 Height = Height + (4 - (Height % 4)) % 4; //  so gives extra room.
    00045
    00046                 switch (DxtType)
    00047                 {
    00048                         case IL_DXT1:
    00049                         case IL_DXT1A:
    00050                                 Size = Width * Height / 2;
    00051                                 break;
    00052                         case IL_DXT3:
    00053                         case IL_DXT5:
    00054                                 Size = Width * Height;
    00055                                 break;
    00056
    00057                         default:  // NVTT does not accept DXT2 or DXT4.
    00058                                 // Should error somehow...
    00059                                 break;
    00060                 }
    00061                 NewData = (ILubyte*)ialloc(Size);
    00062                 if (NewData == NULL)
    00063                         return;
    00064                 Temp = NewData;
    00065         }
    00066
    00067         virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
    00068         {
    00069                 // ignore.
    00070         }
    00071         virtual bool writeData(const void *data, int size)
    00072         {
    00073                 memcpy(Temp, data, size);
    00074                 Temp += size;
    00075                 return true;
    00076         }
    00077
    00078         ILubyte *NewData, *Temp;
    00079         ILuint  Size;
    00080 };
    00081
    00082
    00084 //  The data must be in unsigned byte RGBA format.  The alpha channel will be ignored if DxtType is IL_DXT1.
    00085 //  DxtSize is used to return the size in bytes of the DXTC data returned.
    00086 ILAPI ILubyte* ILAPIENTRY ilNVidiaCompressDXT(ILubyte *Data, ILuint Width, ILuint Height, ILuint Depth, ILenum DxtFormat, ILuint *DxtSize)
    00087 {
    00088         if (Data == NULL) {  // We cannot operate on a null pointer.
    00089                 ilSetError(IL_INVALID_PARAM);
    00090                 return NULL;
    00091         }
    00092
    00093         // The nVidia Texture Tools library does not support volume textures yet.
    00094         if (Depth != 1) {
    00095                 ilSetError(IL_INVALID_PARAM);
    00096                 return NULL;
    00097         }
    00098
    00099         InputOptions inputOptions;
    00100         inputOptions.setTextureLayout(TextureType_2D, Width, Height);
    00101         inputOptions.setMipmapData(Data, Width, Height);
    00102         inputOptions.setMipmapGeneration(false, -1);  //@TODO: Use this in certain cases.
    00103
    00104         OutputOptions outputOptions;
    00105         ilOutputHandlerMem outputHandler(Width, Height, DxtFormat);
    00106         outputOptions.setOutputHeader(false);
    00107         outputOptions.setOutputHandler(&outputHandler);
    00108
    00109         if (outputHandler.NewData == NULL)
    00110                 return NULL;
    00111
    00112         CompressionOptions compressionOptions;
    00113         switch (DxtFormat)
    00114         {
    00115                 case IL_DXT1:
    00116                         compressionOptions.setFormat(Format_DXT1);
    00117                         break;
    00118                 case IL_DXT1A:
    00119                         compressionOptions.setFormat(Format_DXT1a);
    00120                         break;
    00121                 case IL_DXT3:
    00122                         compressionOptions.setFormat(Format_DXT1);
    00123                         break;
    00124                 case IL_DXT5:
    00125                         compressionOptions.setFormat(Format_DXT5);
    00126                         break;
    00127                 default:  // Does not support DXT2 or DXT4.
    00128                         ilSetError(IL_INVALID_PARAM);
    00129                         break;
    00130         }
    00131
    00132         Compressor compressor;
    00133         compressor.process(inputOptions, compressionOptions, outputOptions);
    00134
    00135         *DxtSize = outputHandler.Size;
    00136         return outputHandler.NewData;
    00137 }
    00138
    00139
    00140
    00141 //
    00142 //
    00143 // The following is just a repeat of above, but it works generically on file streams or lumps.
    00144 //  @TODO: Merge these two together.
    00145 //
    00146 //
    00147
    00148
    00149 struct ilOutputHandlerFile : public nvtt::OutputHandler
    00150 {
    00151         ilOutputHandlerFile(ILuint Width, ILuint Height, ILenum DxtType)
    00152         {
    00153                 return;
    00154         }
    00155
    00156         virtual void beginImage(int size, int width, int height, int depth, int face, int miplevel)
    00157         {
    00158                 // ignore.
    00159         }
    00160         virtual bool writeData(const void *data, int size)
    00161         {
    00162                 if (iwrite(data, 1, size) == size)
    00163                         return true;
    00164                 return false;
    00165         }
    00166
    00167 };
    00168
    00169
    00171 //  This version is supposed to be completely internal to DevIL.
    00172 //  The data must be in unsigned byte RGBA format.  The alpha channel will be ignored if DxtType is IL_DXT1.
    00173 ILuint ilNVidiaCompressDXTFile(ILubyte *Data, ILuint Width, ILuint Height, ILuint Depth, ILenum DxtFormat)
    00174 {
    00175         ILuint FilePos = itellw();
    00176
    00177         // The nVidia Texture Tools library does not support volume textures yet.
    00178         if (Depth != 1) {
    00179                 ilSetError(IL_INVALID_PARAM);
    00180                 return 0;
    00181         }
    00182
    00183         InputOptions inputOptions;
    00184         inputOptions.setTextureLayout(TextureType_2D, Width, Height);
    00185         inputOptions.setMipmapData(Data, Width, Height);
    00186         inputOptions.setMipmapGeneration(false, -1);  //@TODO: Use this in certain cases.
    00187
    00188         OutputOptions outputOptions;
    00189         ilOutputHandlerFile outputHandler(Width, Height, DxtFormat);
    00190         outputOptions.setOutputHeader(false);
    00191         outputOptions.setOutputHandler(&outputHandler);
    00192
    00193         CompressionOptions compressionOptions;
    00194         switch (DxtFormat)
    00195         {
    00196                 case IL_DXT1:
    00197                         compressionOptions.setFormat(Format_DXT1);
    00198                         break;
    00199                 case IL_DXT1A:
    00200                         compressionOptions.setFormat(Format_DXT1a);
    00201                         break;
    00202                 case IL_DXT3:
    00203                         compressionOptions.setFormat(Format_DXT1);
    00204                         break;
    00205                 case IL_DXT5:
    00206                         compressionOptions.setFormat(Format_DXT5);
    00207                         break;
    00208                 default:  // Does not support DXT2 or DXT4.
    00209                         ilSetError(IL_INVALID_PARAM);
    00210                         break;
    00211         }
    00212
    00213         Compressor compressor;
    00214         compressor.process(inputOptions, compressionOptions, outputOptions);
    00215
    00216         return itellw() - FilePos;  // Return the number of characters written.
    00217 }
    00218
    00219
    00220 #endif//IL_NO_DXTC_NVIDIA