- Main Page
- Data Structures
- Files

- File List

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

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2001 by Denton Woods
    00005 // Last modified: 05/25/2001 <--Y2K Compliant! =]
    00006 //
    00007 // Filename: src-IL/src/il_raw.c
    00008 //
    00009 // Description: "Raw" file functions
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "il_internal.h"
    00015 #ifndef IL_NO_RAW
    00016
    00017
    00018 ILboolean iLoadRawInternal(void);
    00019 ILboolean iSaveRawInternal(void);
    00020
    00021
    00023 ILboolean ilLoadRaw(ILconst_string FileName)
    00024 {
    00025         ILHANDLE        RawFile;
    00026         ILboolean       bRaw = IL_FALSE;
    00027
    00028         // No need to check for raw
    00029         /*if (!iCheckExtension(FileName, "raw")) {
    00030                 ilSetError(IL_INVALID_EXTENSION);
    00031                 return bRaw;
    00032         }*/
    00033
    00034         RawFile = iopenr(FileName);
    00035         if (RawFile == NULL) {
    00036                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00037                 return bRaw;
    00038         }
    00039
    00040         bRaw = ilLoadRawF(RawFile);
    00041         icloser(RawFile);
    00042
    00043         return bRaw;
    00044 }
    00045
    00046
    00048 ILboolean ilLoadRawF(ILHANDLE File)
    00049 {
    00050         ILuint          FirstPos;
    00051         ILboolean       bRet;
    00052
    00053         iSetInputFile(File);
    00054         FirstPos = itell();
    00055         bRet = iLoadRawInternal();
    00056         iseek(FirstPos, IL_SEEK_SET);
    00057
    00058         return bRet;
    00059 }
    00060
    00061
    00063 ILboolean ilLoadRawL(const void *Lump, ILuint Size)
    00064 {
    00065         iSetInputLump(Lump, Size);
    00066         return iLoadRawInternal();
    00067 }
    00068
    00069
    00070 // Internal function to load a raw image
    00071 ILboolean iLoadRawInternal()
    00072 {
    00073         if (iCurImage == NULL) {
    00074                 ilSetError(IL_ILLEGAL_OPERATION);
    00075                 return IL_FALSE;
    00076         }
    00077
    00078
    00079         iCurImage->Width = GetLittleUInt();
    00080
    00081         iCurImage->Height = GetLittleUInt();
    00082
    00083         iCurImage->Depth = GetLittleUInt();
    00084
    00085         iCurImage->Bpp = (ILubyte)igetc();
    00086
    00087         if (iread(&iCurImage->Bpc, 1, 1) != 1)
    00088                 return IL_FALSE;
    00089
    00090         if (!ilTexImage(iCurImage->Width, iCurImage->Height, iCurImage->Depth, iCurImage->Bpp, 0, ilGetTypeBpc(iCurImage->Bpc), NULL)) {
    00091                 return IL_FALSE;
    00092         }
    00093         iCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
    00094
    00095         // Tries to read the correct amount of data
    00096         if (iread(iCurImage->Data, 1, iCurImage->SizeOfData) < iCurImage->SizeOfData)
    00097                 return IL_FALSE;
    00098
    00099         if (ilIsEnabled(IL_ORIGIN_SET)) {
    00100                 iCurImage->Origin = ilGetInteger(IL_ORIGIN_MODE);
    00101         }
    00102         else {
    00103                 iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00104         }
    00105
    00106         if (iCurImage->Bpp == 1)
    00107                 iCurImage->Format = IL_LUMINANCE;
    00108         else if (iCurImage->Bpp == 3)
    00109                 iCurImage->Format = IL_RGB;
    00110         else  // 4
    00111                 iCurImage->Format = IL_RGBA;
    00112
    00113         ilFixImage();
    00114
    00115         return IL_TRUE;
    00116 }
    00117
    00118
    00120 ILboolean ilSaveRaw(const ILstring FileName)
    00121 {
    00122         ILHANDLE        RawFile;
    00123         ILuint          RawSize;
    00124
    00125         if (ilGetBoolean(IL_FILE_MODE) == IL_FALSE) {
    00126                 if (iFileExists(FileName)) {
    00127                         ilSetError(IL_FILE_ALREADY_EXISTS);
    00128                         return IL_FALSE;
    00129                 }
    00130         }
    00131
    00132         RawFile = iopenw(FileName);
    00133         if (RawFile == NULL) {
    00134                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00135                 return IL_FALSE;
    00136         }
    00137
    00138         RawSize = ilSaveRawF(RawFile);
    00139         iclosew(RawFile);
    00140
    00141         if (RawSize == 0)
    00142                 return IL_FALSE;
    00143         return IL_TRUE;
    00144 }
    00145
    00146
    00148 ILuint ilSaveRawF(ILHANDLE File)
    00149 {
    00150         ILuint Pos;
    00151         iSetOutputFile(File);
    00152         Pos = itellw();
    00153         if (iSaveRawInternal() == IL_FALSE)
    00154                 return 0;  // Error occurred
    00155         return itellw() - Pos;  // Return the number of bytes written.
    00156 }
    00157
    00158
    00160 ILuint ilSaveRawL(void *Lump, ILuint Size)
    00161 {
    00162         ILuint Pos = itellw();
    00163         iSetOutputLump(Lump, Size);
    00164         if (iSaveRawInternal() == IL_FALSE)
    00165                 return 0;  // Error occurred
    00166         return itellw() - Pos;  // Return the number of bytes written.
    00167 }
    00168
    00169
    00170 // Internal function used to load the raw data.
    00171 ILboolean iSaveRawInternal()
    00172 {
    00173         if (iCurImage == NULL) {
    00174                 ilSetError(IL_ILLEGAL_OPERATION);
    00175                 return IL_FALSE;
    00176         }
    00177
    00178         SaveLittleUInt(iCurImage->Width);
    00179         SaveLittleUInt(iCurImage->Height);
    00180         SaveLittleUInt(iCurImage->Depth);
    00181         iputc(iCurImage->Bpp);
    00182         iputc(iCurImage->Bpc);
    00183         iwrite(iCurImage->Data, 1, iCurImage->SizeOfData);
    00184
    00185         return IL_TRUE;
    00186 }
    00187
    00188
    00189 #endif//IL_NO_RAW