- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-IL/src/il_register.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2008 by Denton Woods
    00005 // Last modified: 11/07/2008
    00006 //
    00007 // Filename: src-IL/src/il_register.c
    00008 //
    00009 // Description: Allows the caller to specify user-defined callback functions
    00010 //                               to open files DevIL does not support, to parse files
    00011 //                               differently, or anything else a person can think up.
    00012 //
    00013 //-----------------------------------------------------------------------------
    00014
    00015
    00016 #include "il_internal.h"
    00017 #include "il_register.h"
    00018 #include <string.h>
    00019
    00020
    00021 // Linked lists of registered formats
    00022 iFormatL *LoadProcs = NULL;
    00023 iFormatS *SaveProcs = NULL;
    00024
    00025
    00026 ILboolean ILAPIENTRY ilRegisterLoad(ILconst_string Ext, IL_LOADPROC Load) {
    00027         iFormatL *TempNode, *NewNode;
    00028
    00029         TempNode = LoadProcs;
    00030         if (TempNode != NULL) {
    00031                 while (TempNode->Next != NULL) {
    00032                         TempNode = TempNode->Next;
    00033                         if (!iStrCmp(TempNode->Ext, Ext)) {  // already registered
    00034                                 return IL_TRUE;
    00035                         }
    00036                 }
    00037         }
    00038
    00039         NewNode = (iFormatL*)ialloc(sizeof(iFormatL));
    00040         if (NewNode == NULL) {
    00041                 return IL_FALSE;
    00042         }
    00043
    00044         if (LoadProcs == NULL) {
    00045                 LoadProcs = NewNode;
    00046         }
    00047         else {
    00048                 TempNode->Next = NewNode;
    00049         }
    00050
    00051         NewNode->Ext = ilStrDup(Ext);
    00052         NewNode->Load = Load;
    00053         NewNode->Next = NULL;
    00054
    00055         return IL_TRUE;
    00056 }
    00057
    00058
    00059 ILboolean ILAPIENTRY ilRegisterSave(ILconst_string Ext, IL_SAVEPROC Save)
    00060 {
    00061         iFormatS *TempNode, *NewNode;
    00062
    00063         TempNode = SaveProcs;
    00064         if (TempNode != NULL) {
    00065                 while (TempNode->Next != NULL) {
    00066                         TempNode = TempNode->Next;
    00067                         if (!iStrCmp(TempNode->Ext, Ext)) {  // already registered
    00068                                 return IL_TRUE;
    00069                         }
    00070                 }
    00071         }
    00072
    00073         NewNode = (iFormatS*)ialloc(sizeof(iFormatL));
    00074         if (NewNode == NULL) {
    00075                 return IL_FALSE;
    00076         }
    00077
    00078         if (SaveProcs == NULL) {
    00079                 SaveProcs = NewNode;
    00080         }
    00081         else {
    00082                 TempNode->Next = NewNode;
    00083         }
    00084
    00085         NewNode->Ext = ilStrDup(Ext);
    00086         NewNode->Save = Save;
    00087         NewNode->Next = NULL;
    00088
    00089         return IL_TRUE;
    00090 }
    00091
    00092
    00094 ILboolean ILAPIENTRY ilRemoveLoad(ILconst_string Ext)
    00095 {
    00096         iFormatL *TempNode = LoadProcs, *PrevNode = NULL;
    00097
    00098         while (TempNode != NULL) {
    00099                 if (!iStrCmp(Ext, TempNode->Ext)) {
    00100                         if (PrevNode == NULL) {  // first node in the list
    00101                                 LoadProcs = TempNode->Next;
    00102                                 ifree((void*)TempNode->Ext);
    00103                                 ifree(TempNode);
    00104                         }
    00105                         else {
    00106                                 PrevNode->Next = TempNode->Next;
    00107                                 ifree((void*)TempNode->Ext);
    00108                                 ifree(TempNode);
    00109                         }
    00110
    00111                         return IL_TRUE;
    00112                 }
    00113
    00114                 PrevNode = TempNode;
    00115                 TempNode = TempNode->Next;
    00116         }
    00117
    00118         return IL_FALSE;
    00119 }
    00120
    00121
    00123 ILboolean ILAPIENTRY ilRemoveSave(ILconst_string Ext)
    00124 {
    00125         iFormatS *TempNode = SaveProcs, *PrevNode = NULL;
    00126
    00127         while (TempNode != NULL) {
    00128                 if (!iStrCmp(Ext, TempNode->Ext)) {
    00129                         if (PrevNode == NULL) {  // first node in the list
    00130                                 SaveProcs = TempNode->Next;
    00131                                 ifree((void*)TempNode->Ext);
    00132                                 ifree(TempNode);
    00133                         }
    00134                         else {
    00135                                 PrevNode->Next = TempNode->Next;
    00136                                 ifree((void*)TempNode->Ext);
    00137                                 ifree(TempNode);
    00138                         }
    00139
    00140                         return IL_TRUE;
    00141                 }
    00142
    00143                 PrevNode = TempNode;
    00144                 TempNode = TempNode->Next;
    00145         }
    00146
    00147         return IL_FALSE;
    00148 }
    00149
    00150
    00151 // Automatically removes all registered formats.
    00152 void ilRemoveRegistered()
    00153 {
    00154         iFormatL *TempNodeL = LoadProcs;
    00155         iFormatS *TempNodeS = SaveProcs;
    00156
    00157         while (LoadProcs != NULL) {
    00158                 TempNodeL = LoadProcs->Next;
    00159                 ifree((void*)LoadProcs->Ext);
    00160                 ifree(LoadProcs);
    00161                 LoadProcs = TempNodeL;
    00162         }
    00163
    00164         while (SaveProcs != NULL) {
    00165                 TempNodeS = SaveProcs->Next;
    00166                 ifree((void*)SaveProcs->Ext);
    00167                 ifree(SaveProcs);
    00168                 SaveProcs = TempNodeS;
    00169         }
    00170
    00171         return;
    00172 }
    00173
    00174
    00175 ILboolean iRegisterLoad(ILconst_string FileName)
    00176 {
    00177         iFormatL        *TempNode = LoadProcs;
    00178         ILstring        Ext = iGetExtension(FileName);
    00179         ILenum          Error;
    00180
    00181         if (!Ext)
    00182                 return IL_FALSE;
    00183
    00184         while (TempNode != NULL) {
    00185                 if (!iStrCmp(Ext, TempNode->Ext)) {
    00186                         Error = TempNode->Load(FileName);
    00187                         if (Error == IL_NO_ERROR || Error == 0) {  // 0 and IL_NO_ERROR are both valid.
    00188                                 return IL_TRUE;
    00189                         }
    00190                         else {
    00191                                 ilSetError(Error);
    00192                                 return IL_FALSE;
    00193                         }
    00194                 }
    00195                 TempNode = TempNode->Next;
    00196         }
    00197
    00198         return IL_FALSE;
    00199 }
    00200
    00201
    00202 ILboolean iRegisterSave(ILconst_string FileName)
    00203 {
    00204         iFormatS        *TempNode = SaveProcs;
    00205         ILstring        Ext = iGetExtension(FileName);
    00206         ILenum          Error;
    00207
    00208         if (!Ext)
    00209                 return IL_FALSE;
    00210
    00211         while (TempNode != NULL) {
    00212                 if (!iStrCmp(Ext, TempNode->Ext)) {
    00213                         Error = TempNode->Save(FileName);
    00214                         if (Error == IL_NO_ERROR || Error == 0) {  // 0 and IL_NO_ERROR are both valid.
    00215                                 return IL_TRUE;
    00216                         }
    00217                         else {
    00218                                 ilSetError(Error);
    00219                                 return IL_FALSE;
    00220                         }
    00221                 }
    00222                 TempNode = TempNode->Next;
    00223         }
    00224
    00225         return IL_FALSE;
    00226 }
    00227
    00228
    00229 //
    00230 // "Reporting" functions
    00231 //
    00232
    00233 void ILAPIENTRY ilRegisterOrigin(ILenum Origin)
    00234 {
    00235         switch (Origin)
    00236         {
    00237                 case IL_ORIGIN_LOWER_LEFT:
    00238                 case IL_ORIGIN_UPPER_LEFT:
    00239                         iCurImage->Origin = Origin;
    00240                         break;
    00241                 default:
    00242                         ilSetError(IL_INVALID_ENUM);
    00243         }
    00244         return;
    00245 }
    00246
    00247
    00248 void ILAPIENTRY ilRegisterFormat(ILenum Format)
    00249 {
    00250         switch (Format)
    00251         {
    00252                 case IL_COLOUR_INDEX:
    00253                 case IL_RGB:
    00254                 case IL_RGBA:
    00255                 case IL_BGR:
    00256                 case IL_BGRA:
    00257                 case IL_LUMINANCE:
    00258                 case IL_LUMINANCE_ALPHA:
    00259                         iCurImage->Format = Format;
    00260                         break;
    00261                 default:
    00262                         ilSetError(IL_INVALID_ENUM);
    00263         }
    00264         return;
    00265 }
    00266
    00267
    00268 ILboolean ILAPIENTRY ilRegisterNumFaces(ILuint Num)
    00269 {
    00270         ILimage *Next, *Prev;
    00271
    00272         ilBindImage(ilGetCurName());  // Make sure the current image is actually bound.
    00273         ilCloseImage(iCurImage->Faces);  // Close any current mipmaps.
    00274
    00275         iCurImage->Faces = NULL;
    00276         if (Num == 0)  // Just gets rid of all the mipmaps.
    00277                 return IL_TRUE;
    00278
    00279         iCurImage->Faces = ilNewImage(1, 1, 1, 1, 1);
    00280         if (iCurImage->Faces == NULL)
    00281                 return IL_FALSE;
    00282         Next = iCurImage->Faces;
    00283         Num--;
    00284
    00285         while (Num) {
    00286                 Next->Faces = ilNewImage(1, 1, 1, 1, 1);
    00287                 if (Next->Faces == NULL) {
    00288                         // Clean up before we error out.
    00289                         Prev = iCurImage->Faces;
    00290                         while (Prev) {
    00291                                 Next = Prev->Faces;
    00292                                 ilCloseImage(Prev);
    00293                                 Prev = Next;
    00294                         }
    00295                         return IL_FALSE;
    00296                 }
    00297                 Next = Next->Faces;
    00298                 Num--;
    00299         }
    00300
    00301         return IL_TRUE;
    00302 }
    00303
    00304
    00305 ILboolean ILAPIENTRY ilRegisterMipNum(ILuint Num)
    00306 {
    00307         ILimage *Next, *Prev;
    00308
    00309         ilBindImage(ilGetCurName());  // Make sure the current image is actually bound.
    00310         ilCloseImage(iCurImage->Mipmaps);  // Close any current mipmaps.
    00311
    00312         iCurImage->Mipmaps = NULL;
    00313         if (Num == 0)  // Just gets rid of all the mipmaps.
    00314                 return IL_TRUE;
    00315
    00316         iCurImage->Mipmaps = ilNewImage(1, 1, 1, 1, 1);
    00317         if (iCurImage->Mipmaps == NULL)
    00318                 return IL_FALSE;
    00319         Next = iCurImage->Mipmaps;
    00320         Num--;
    00321
    00322         while (Num) {
    00323                 Next->Next = ilNewImage(1, 1, 1, 1, 1);
    00324                 if (Next->Next == NULL) {
    00325                         // Clean up before we error out.
    00326                         Prev = iCurImage->Mipmaps;
    00327                         while (Prev) {
    00328                                 Next = Prev->Next;
    00329                                 ilCloseImage(Prev);
    00330                                 Prev = Next;
    00331                         }
    00332                         return IL_FALSE;
    00333                 }
    00334                 Next = Next->Next;
    00335                 Num--;
    00336         }
    00337
    00338         return IL_TRUE;
    00339 }
    00340
    00341
    00342 ILboolean ILAPIENTRY ilRegisterNumImages(ILuint Num)
    00343 {
    00344         ILimage *Next, *Prev;
    00345
    00346         ilBindImage(ilGetCurName());  // Make sure the current image is actually bound.
    00347         ilCloseImage(iCurImage->Next);  // Close any current "next" images.
    00348
    00349         iCurImage->Next = NULL;
    00350         if (Num == 0)  // Just gets rid of all the "next" images.
    00351                 return IL_TRUE;
    00352
    00353         iCurImage->Next = ilNewImage(1, 1, 1, 1, 1);
    00354         if (iCurImage->Next == NULL)
    00355                 return IL_FALSE;
    00356         Next = iCurImage->Next;
    00357         Num--;
    00358
    00359         while (Num) {
    00360                 Next->Next = ilNewImage(1, 1, 1, 1, 1);
    00361                 if (Next->Next == NULL) {
    00362                         // Clean up before we error out.
    00363                         Prev = iCurImage->Next;
    00364                         while (Prev) {
    00365                                 Next = Prev->Next;
    00366                                 ilCloseImage(Prev);
    00367                                 Prev = Next;
    00368                         }
    00369                         return IL_FALSE;
    00370                 }
    00371                 Next = Next->Next;
    00372                 Num--;
    00373         }
    00374
    00375         return IL_TRUE;
    00376 }
    00377
    00378
    00379 void ILAPIENTRY ilRegisterType(ILenum Type)
    00380 {
    00381         switch (Type)
    00382         {
    00383                 case IL_BYTE:
    00384                 case IL_UNSIGNED_BYTE:
    00385                 case IL_SHORT:
    00386                 case IL_UNSIGNED_SHORT:
    00387                 case IL_INT:
    00388                 case IL_UNSIGNED_INT:
    00389                 case IL_FLOAT:
    00390                 case IL_DOUBLE:
    00391                         iCurImage->Type = Type;
    00392                         break;
    00393                 default:
    00394                         ilSetError(IL_INVALID_ENUM);
    00395         }
    00396
    00397         return;
    00398 }
    00399
    00400
    00401 void ILAPIENTRY ilRegisterPal(void *Pal, ILuint Size, ILenum Type)
    00402 {
    00403         if (!iCurImage->Pal.Palette || !iCurImage->Pal.PalSize || iCurImage->Pal.PalType != IL_PAL_NONE) {
    00404                 ifree(iCurImage->Pal.Palette);
    00405         }
    00406
    00407         iCurImage->Pal.PalSize = Size;
    00408         iCurImage->Pal.PalType = Type;
    00409         iCurImage->Pal.Palette = (ILubyte*)ialloc(Size);
    00410         if (iCurImage->Pal.Palette == NULL)
    00411                 return;
    00412
    00413         if (Pal != NULL) {
    00414                 memcpy(iCurImage->Pal.Palette, Pal, Size);
    00415         }
    00416         else {
    00417                 ilSetError(IL_INVALID_PARAM);
    00418         }
    00419
    00420         return;
    00421 }
    00422
    00423
    00424 ILboolean ILAPIENTRY ilSetDuration(ILuint Duration)
    00425 {
    00426         if (iCurImage == NULL) {
    00427                 ilSetError(IL_ILLEGAL_OPERATION);
    00428                 return IL_FALSE;
    00429         }
    00430
    00431         iCurImage->Duration = Duration;
    00432
    00433         return IL_TRUE;
    00434 }