1 //-----------------------------------------------------------------------------
2 //
3 // ImageLib Sources
4 // Copyright (C) 2000-2009 by Denton Woods
5 // Last modified: 01/30/2009
6 //
7 // Filename: src-IL/src/il_size.c
8 //
9 // Description: Determines the size of output files for lump writing.
10 //
11 //-----------------------------------------------------------------------------
12 
13 #include "il_internal.h"
14 
15 
16 ILuint iTargaSize(void);
17 
18 
19 ILuint CurPos;  // Fake "file" pointer.
20 ILuint MaxPos;
21 
22 
23 //! Fake seek function
iSizeSeek(ILint Offset,ILuint Mode)24 ILint ILAPIENTRY iSizeSeek(ILint Offset, ILuint Mode)
25 {
26 	switch (Mode)
27 	{
28 		case IL_SEEK_SET:
29 			CurPos = Offset;
30 			if (CurPos > MaxPos)
31 				MaxPos = CurPos;
32 			break;
33 
34 		case IL_SEEK_CUR:
35 			CurPos = CurPos + Offset;
36 			break;
37 
38 		case IL_SEEK_END:
39 			CurPos = MaxPos + Offset;  // Offset should be negative in this case.
40 			break;
41 
42 		default:
43 			ilSetError(IL_INTERNAL_ERROR);  // Should never happen!
44 			return -1;  // Error code
45 	}
46 
47 	if (CurPos > MaxPos)
48 		MaxPos = CurPos;
49 
50 	return 0;  // Code for success
51 }
52 
iSizeTell(void)53 ILuint ILAPIENTRY iSizeTell(void)
54 {
55 	return CurPos;
56 }
57 
iSizePutc(ILubyte Char)58 ILint ILAPIENTRY iSizePutc(ILubyte Char)
59 {
60 	CurPos++;
61 	if (CurPos > MaxPos)
62 		MaxPos = CurPos;
63 	return Char;
64 }
65 
iSizeWrite(const void * Buffer,ILuint Size,ILuint Number)66 ILint ILAPIENTRY iSizeWrite(const void *Buffer, ILuint Size, ILuint Number)
67 {
68 	CurPos += Size * Number;
69 	if (CurPos > MaxPos)
70 		MaxPos = CurPos;
71 	return Number;
72 }
73 
74 
75 //@TODO: Do computations for uncompressed formats without going through the
76 //       whole writing process.
77 
78 //! Returns the size of the memory buffer needed to save the current image into this Type.
79 //  A return value of 0 is an error.
ilDetermineSize(ILenum Type)80 ILuint ilDetermineSize(ILenum Type)
81 {
82 	MaxPos = CurPos = 0;
83 	iSetOutputFake();  // Sets iputc, iwrite, etc. to functions above.
84 
85 	switch (Type)
86 	{
87 		#ifndef IL_NO_BMP
88 		case IL_BMP:
89 			ilSaveBmpL(NULL, 0);
90 			break;
91 		#endif//IL_NO_BMP
92 
93 		#ifndef IL_NO_DDS
94 		case IL_DDS:
95 			ilSaveDdsL(NULL, 0);
96 			break;
97 		#endif//IL_NO_DDS
98 
99 		#ifndef IL_NO_EXR
100 		case IL_EXR:
101 			ilSaveExrL(NULL, 0);
102 			break;
103 		#endif//IL_NO_EXR
104 
105 		#ifndef IL_NO_HDR
106 		case IL_HDR:
107 			ilSaveHdrL(NULL, 0);
108 			break;
109 		#endif//IL_NO_HDR
110 
111 		#ifndef IL_NO_JP2
112 		case IL_JP2:
113 			ilSaveJp2L(NULL, 0);
114 			break;
115 		#endif//IL_NO_JP2
116 
117 		#ifndef IL_NO_JPG
118 		case IL_JPG:
119 			ilSaveJpegL(NULL, 0);
120 			break;
121 		#endif//IL_NO_JPG
122 
123 		#ifndef IL_NO_PCX
124 		case IL_PCX:
125 			ilSavePcxL(NULL, 0);
126 			break;
127 		#endif//IL_NO_PCX
128 
129 		#ifndef IL_NO_PNG
130 		case IL_PNG:
131 			ilSavePngL(NULL, 0);
132 			break;
133 		#endif//IL_NO_PNG
134 
135 		#ifndef IL_NO_PNM
136 		case IL_PNM:
137 			ilSavePnmL(NULL, 0);
138 			break;
139 		#endif//IL_NO_PNM
140 
141 		#ifndef IL_NO_PSD
142 		case IL_PSD:
143 			ilSavePsdL(NULL, 0);
144 			break;
145 		#endif//IL_NO_PSD
146 
147 		#ifndef IL_NO_RAW
148 		case IL_RAW:
149 			ilSaveRawL(NULL, 0);
150 			break;
151 		#endif//IL_NO_RAW
152 
153 		#ifndef IL_NO_SGI
154 		case IL_SGI:
155 			ilSaveSgiL(NULL, 0);
156 			break;
157 		#endif//IL_NO_SGI
158 
159 		#ifndef IL_NO_TGA
160 		case IL_TGA:
161 			//ilSaveTargaL(NULL, 0);
162 			return iTargaSize();
163 			break;
164 		#endif//IL_NO_TGA
165 
166 		#ifndef IL_NO_TIF
167 		case IL_TIF:
168 			ilSaveTiffL(NULL, 0);
169 			break;
170 		#endif//IL_NO_TIF
171 
172 		#ifndef IL_NO_WBMP
173 		case IL_WBMP:
174 			ilSaveWbmpL(NULL, 0);
175 			break;
176 		#endif//IL_NO_WBMP
177 
178 		default:
179 			// 0 is an error for this.
180 			ilSetError(IL_INVALID_ENUM);
181 			return 0;
182 	}
183 
184 	return MaxPos;
185 }
186