1 //-----------------------------------------------------------------------------
2 //
3 // ImageLib Sources
4 // Copyright (C) 2000-2009 by Denton Woods
5 // Last modified: 03/07/2009
6 //
7 // Filename: src-IL/src/il_raw.c
8 //
9 // Description: "Raw" file functions
10 //
11 //-----------------------------------------------------------------------------
12
13
14 #include "il_internal.h"
15 #ifndef IL_NO_RAW
16
17
18 ILboolean iLoadRawInternal(void);
19 ILboolean iSaveRawInternal(void);
20
21
22 //! Reads a raw file
ilLoadRaw(ILconst_string FileName)23 ILboolean ilLoadRaw(ILconst_string FileName)
24 {
25 ILHANDLE RawFile;
26 ILboolean bRaw = IL_FALSE;
27
28 // No need to check for raw
29 /*if (!iCheckExtension(FileName, "raw")) {
30 ilSetError(IL_INVALID_EXTENSION);
31 return bRaw;
32 }*/
33
34 RawFile = iopenr(FileName);
35 if (RawFile == NULL) {
36 ilSetError(IL_COULD_NOT_OPEN_FILE);
37 return bRaw;
38 }
39
40 bRaw = ilLoadRawF(RawFile);
41 icloser(RawFile);
42
43 return bRaw;
44 }
45
46
47 //! Reads an already-opened raw file
ilLoadRawF(ILHANDLE File)48 ILboolean ilLoadRawF(ILHANDLE File)
49 {
50 ILuint FirstPos;
51 ILboolean bRet;
52
53 iSetInputFile(File);
54 FirstPos = itell();
55 bRet = iLoadRawInternal();
56 iseek(FirstPos, IL_SEEK_SET);
57
58 return bRet;
59 }
60
61
62 //! Reads from a raw memory "lump"
ilLoadRawL(const void * Lump,ILuint Size)63 ILboolean ilLoadRawL(const void *Lump, ILuint Size)
64 {
65 iSetInputLump(Lump, Size);
66 return iLoadRawInternal();
67 }
68
69
70 // Internal function to load a raw image
iLoadRawInternal()71 ILboolean iLoadRawInternal()
72 {
73 if (iCurImage == NULL) {
74 ilSetError(IL_ILLEGAL_OPERATION);
75 return IL_FALSE;
76 }
77
78
79 iCurImage->Width = GetLittleUInt();
80
81 iCurImage->Height = GetLittleUInt();
82
83 iCurImage->Depth = GetLittleUInt();
84
85 iCurImage->Bpp = (ILubyte)igetc();
86
87 if (iread(&iCurImage->Bpc, 1, 1) != 1)
88 return IL_FALSE;
89
90 if (!ilTexImage(iCurImage->Width, iCurImage->Height, iCurImage->Depth, iCurImage->Bpp, 0, ilGetTypeBpc(iCurImage->Bpc), NULL)) {
91 return IL_FALSE;
92 }
93 iCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
94
95 // Tries to read the correct amount of data
96 if (iread(iCurImage->Data, 1, iCurImage->SizeOfData) < iCurImage->SizeOfData)
97 return IL_FALSE;
98
99 if (ilIsEnabled(IL_ORIGIN_SET)) {
100 iCurImage->Origin = ilGetInteger(IL_ORIGIN_MODE);
101 }
102 else {
103 iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
104 }
105
106 if (iCurImage->Bpp == 1)
107 iCurImage->Format = IL_LUMINANCE;
108 else if (iCurImage->Bpp == 3)
109 iCurImage->Format = IL_RGB;
110 else // 4
111 iCurImage->Format = IL_RGBA;
112
113 return ilFixImage();
114 }
115
116
117 //! Writes a Raw file
ilSaveRaw(const ILstring FileName)118 ILboolean ilSaveRaw(const ILstring FileName)
119 {
120 ILHANDLE RawFile;
121 ILuint RawSize;
122
123 if (ilGetBoolean(IL_FILE_MODE) == IL_FALSE) {
124 if (iFileExists(FileName)) {
125 ilSetError(IL_FILE_ALREADY_EXISTS);
126 return IL_FALSE;
127 }
128 }
129
130 RawFile = iopenw(FileName);
131 if (RawFile == NULL) {
132 ilSetError(IL_COULD_NOT_OPEN_FILE);
133 return IL_FALSE;
134 }
135
136 RawSize = ilSaveRawF(RawFile);
137 iclosew(RawFile);
138
139 if (RawSize == 0)
140 return IL_FALSE;
141 return IL_TRUE;
142 }
143
144
145 //! Writes Raw to an already-opened file
ilSaveRawF(ILHANDLE File)146 ILuint ilSaveRawF(ILHANDLE File)
147 {
148 ILuint Pos;
149 iSetOutputFile(File);
150 Pos = itellw();
151 if (iSaveRawInternal() == IL_FALSE)
152 return 0; // Error occurred
153 return itellw() - Pos; // Return the number of bytes written.
154 }
155
156
157 //! Writes Raw to a memory "lump"
ilSaveRawL(void * Lump,ILuint Size)158 ILuint ilSaveRawL(void *Lump, ILuint Size)
159 {
160 ILuint Pos;
161 iSetOutputLump(Lump, Size);
162 Pos = itellw();
163 if (iSaveRawInternal() == IL_FALSE)
164 return 0; // Error occurred
165 return itellw() - Pos; // Return the number of bytes written.
166 }
167
168
169 // Internal function used to load the raw data.
iSaveRawInternal()170 ILboolean iSaveRawInternal()
171 {
172 if (iCurImage == NULL) {
173 ilSetError(IL_ILLEGAL_OPERATION);
174 return IL_FALSE;
175 }
176
177 SaveLittleUInt(iCurImage->Width);
178 SaveLittleUInt(iCurImage->Height);
179 SaveLittleUInt(iCurImage->Depth);
180 iputc(iCurImage->Bpp);
181 iputc(iCurImage->Bpc);
182 iwrite(iCurImage->Data, 1, iCurImage->SizeOfData);
183
184 return IL_TRUE;
185 }
186
187
188 #endif//IL_NO_RAW
189