diff options
Diffstat (limited to 'libraries/irrlicht-1.8/source/Irrlicht/CImageLoaderPPM.cpp')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/CImageLoaderPPM.cpp | 554 |
1 files changed, 277 insertions, 277 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/CImageLoaderPPM.cpp b/libraries/irrlicht-1.8/source/Irrlicht/CImageLoaderPPM.cpp index a0776fa..e0ad1c4 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/CImageLoaderPPM.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/CImageLoaderPPM.cpp | |||
@@ -1,277 +1,277 @@ | |||
1 | // Copyright (C) 2007-2012 Christian Stehno | 1 | // Copyright (C) 2007-2012 Christian Stehno |
2 | // This file is part of the "Irrlicht Engine". | 2 | // This file is part of the "Irrlicht Engine". |
3 | // For conditions of distribution and use, see copyright notice in irrlicht.h | 3 | // For conditions of distribution and use, see copyright notice in irrlicht.h |
4 | 4 | ||
5 | #include "CImageLoaderPPM.h" | 5 | #include "CImageLoaderPPM.h" |
6 | 6 | ||
7 | #ifdef _IRR_COMPILE_WITH_PPM_LOADER_ | 7 | #ifdef _IRR_COMPILE_WITH_PPM_LOADER_ |
8 | 8 | ||
9 | #include "IReadFile.h" | 9 | #include "IReadFile.h" |
10 | #include "CColorConverter.h" | 10 | #include "CColorConverter.h" |
11 | #include "CImage.h" | 11 | #include "CImage.h" |
12 | #include "os.h" | 12 | #include "os.h" |
13 | #include "fast_atof.h" | 13 | #include "fast_atof.h" |
14 | #include "coreutil.h" | 14 | #include "coreutil.h" |
15 | 15 | ||
16 | namespace irr | 16 | namespace irr |
17 | { | 17 | { |
18 | namespace video | 18 | namespace video |
19 | { | 19 | { |
20 | 20 | ||
21 | 21 | ||
22 | //! constructor | 22 | //! constructor |
23 | CImageLoaderPPM::CImageLoaderPPM() | 23 | CImageLoaderPPM::CImageLoaderPPM() |
24 | { | 24 | { |
25 | #ifdef _DEBUG | 25 | #ifdef _DEBUG |
26 | setDebugName("CImageLoaderPPM"); | 26 | setDebugName("CImageLoaderPPM"); |
27 | #endif | 27 | #endif |
28 | } | 28 | } |
29 | 29 | ||
30 | 30 | ||
31 | //! returns true if the file maybe is able to be loaded by this class | 31 | //! returns true if the file maybe is able to be loaded by this class |
32 | //! based on the file extension (e.g. ".tga") | 32 | //! based on the file extension (e.g. ".tga") |
33 | bool CImageLoaderPPM::isALoadableFileExtension(const io::path& filename) const | 33 | bool CImageLoaderPPM::isALoadableFileExtension(const io::path& filename) const |
34 | { | 34 | { |
35 | return core::hasFileExtension ( filename, "ppm", "pgm", "pbm" ); | 35 | return core::hasFileExtension ( filename, "ppm", "pgm", "pbm" ); |
36 | } | 36 | } |
37 | 37 | ||
38 | 38 | ||
39 | //! returns true if the file maybe is able to be loaded by this class | 39 | //! returns true if the file maybe is able to be loaded by this class |
40 | bool CImageLoaderPPM::isALoadableFileFormat(io::IReadFile* file) const | 40 | bool CImageLoaderPPM::isALoadableFileFormat(io::IReadFile* file) const |
41 | { | 41 | { |
42 | c8 id[2]={0}; | 42 | c8 id[2]={0}; |
43 | file->read(&id, 2); | 43 | file->read(&id, 2); |
44 | return (id[0]=='P' && id[1]>'0' && id[1]<'7'); | 44 | return (id[0]=='P' && id[1]>'0' && id[1]<'7'); |
45 | } | 45 | } |
46 | 46 | ||
47 | 47 | ||
48 | //! creates a surface from the file | 48 | //! creates a surface from the file |
49 | IImage* CImageLoaderPPM::loadImage(io::IReadFile* file) const | 49 | IImage* CImageLoaderPPM::loadImage(io::IReadFile* file) const |
50 | { | 50 | { |
51 | IImage* image; | 51 | IImage* image; |
52 | 52 | ||
53 | if (file->getSize() < 12) | 53 | if (file->getSize() < 12) |
54 | return 0; | 54 | return 0; |
55 | 55 | ||
56 | c8 id[2]; | 56 | c8 id[2]; |
57 | file->read(&id, 2); | 57 | file->read(&id, 2); |
58 | 58 | ||
59 | if (id[0]!='P' || id[1]<'1' || id[1]>'6') | 59 | if (id[0]!='P' || id[1]<'1' || id[1]>'6') |
60 | return 0; | 60 | return 0; |
61 | 61 | ||
62 | const u8 format = id[1] - '0'; | 62 | const u8 format = id[1] - '0'; |
63 | const bool binary = format>3; | 63 | const bool binary = format>3; |
64 | 64 | ||
65 | core::stringc token; | 65 | core::stringc token; |
66 | getNextToken(file, token); | 66 | getNextToken(file, token); |
67 | const u32 width = core::strtoul10(token.c_str()); | 67 | const u32 width = core::strtoul10(token.c_str()); |
68 | 68 | ||
69 | getNextToken(file, token); | 69 | getNextToken(file, token); |
70 | const u32 height = core::strtoul10(token.c_str()); | 70 | const u32 height = core::strtoul10(token.c_str()); |
71 | 71 | ||
72 | u8* data = 0; | 72 | u8* data = 0; |
73 | const u32 size = width*height; | 73 | const u32 size = width*height; |
74 | if (format==1 || format==4) | 74 | if (format==1 || format==4) |
75 | { | 75 | { |
76 | skipToNextToken(file); // go to start of data | 76 | skipToNextToken(file); // go to start of data |
77 | 77 | ||
78 | const u32 bytesize = size/8+(size & 3)?1:0; | 78 | const u32 bytesize = size/8+(size & 3)?1:0; |
79 | if (binary) | 79 | if (binary) |
80 | { | 80 | { |
81 | if (file->getSize()-file->getPos() < (long)bytesize) | 81 | if (file->getSize()-file->getPos() < (long)bytesize) |
82 | return 0; | 82 | return 0; |
83 | data = new u8[bytesize]; | 83 | data = new u8[bytesize]; |
84 | file->read(data, bytesize); | 84 | file->read(data, bytesize); |
85 | } | 85 | } |
86 | else | 86 | else |
87 | { | 87 | { |
88 | if (file->getSize()-file->getPos() < (long)(2*size)) // optimistic test | 88 | if (file->getSize()-file->getPos() < (long)(2*size)) // optimistic test |
89 | return 0; | 89 | return 0; |
90 | data = new u8[bytesize]; | 90 | data = new u8[bytesize]; |
91 | memset(data, 0, bytesize); | 91 | memset(data, 0, bytesize); |
92 | u32 shift=0; | 92 | u32 shift=0; |
93 | for (u32 i=0; i<size; ++i) | 93 | for (u32 i=0; i<size; ++i) |
94 | { | 94 | { |
95 | getNextToken(file, token); | 95 | getNextToken(file, token); |
96 | if (token == "1") | 96 | if (token == "1") |
97 | data[i/8] |= (0x01 << shift); | 97 | data[i/8] |= (0x01 << shift); |
98 | if (++shift == 8) | 98 | if (++shift == 8) |
99 | shift=0; | 99 | shift=0; |
100 | } | 100 | } |
101 | } | 101 | } |
102 | image = new CImage(ECF_A1R5G5B5, core::dimension2d<u32>(width, height)); | 102 | image = new CImage(ECF_A1R5G5B5, core::dimension2d<u32>(width, height)); |
103 | if (image) | 103 | if (image) |
104 | CColorConverter::convert1BitTo16Bit(data, (s16*)image->lock(), width, height); | 104 | CColorConverter::convert1BitTo16Bit(data, (s16*)image->lock(), width, height); |
105 | } | 105 | } |
106 | else | 106 | else |
107 | { | 107 | { |
108 | getNextToken(file, token); | 108 | getNextToken(file, token); |
109 | const u32 maxDepth = core::strtoul10(token.c_str()); | 109 | const u32 maxDepth = core::strtoul10(token.c_str()); |
110 | if (maxDepth > 255) // no double bytes yet | 110 | if (maxDepth > 255) // no double bytes yet |
111 | return 0; | 111 | return 0; |
112 | 112 | ||
113 | skipToNextToken(file); // go to start of data | 113 | skipToNextToken(file); // go to start of data |
114 | 114 | ||
115 | if (format==2 || format==5) | 115 | if (format==2 || format==5) |
116 | { | 116 | { |
117 | if (binary) | 117 | if (binary) |
118 | { | 118 | { |
119 | if (file->getSize()-file->getPos() < (long)size) | 119 | if (file->getSize()-file->getPos() < (long)size) |
120 | return 0; | 120 | return 0; |
121 | data = new u8[size]; | 121 | data = new u8[size]; |
122 | file->read(data, size); | 122 | file->read(data, size); |
123 | image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height)); | 123 | image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height)); |
124 | if (image) | 124 | if (image) |
125 | { | 125 | { |
126 | u8* ptr = (u8*)image->lock(); | 126 | u8* ptr = (u8*)image->lock(); |
127 | for (u32 i=0; i<size; ++i) | 127 | for (u32 i=0; i<size; ++i) |
128 | { | 128 | { |
129 | *ptr++ = data[i]; | 129 | *ptr++ = data[i]; |
130 | *ptr++ = data[i]; | 130 | *ptr++ = data[i]; |
131 | *ptr++ = data[i]; | 131 | *ptr++ = data[i]; |
132 | *ptr++ = 255; | 132 | *ptr++ = 255; |
133 | } | 133 | } |
134 | } | 134 | } |
135 | } | 135 | } |
136 | else | 136 | else |
137 | { | 137 | { |
138 | if (file->getSize()-file->getPos() < (long)(2*size)) // optimistic test | 138 | if (file->getSize()-file->getPos() < (long)(2*size)) // optimistic test |
139 | return 0; | 139 | return 0; |
140 | image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height)); | 140 | image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height)); |
141 | if (image) | 141 | if (image) |
142 | { | 142 | { |
143 | u8* ptr = (u8*)image->lock(); | 143 | u8* ptr = (u8*)image->lock(); |
144 | for (u32 i=0; i<size; ++i) | 144 | for (u32 i=0; i<size; ++i) |
145 | { | 145 | { |
146 | getNextToken(file, token); | 146 | getNextToken(file, token); |
147 | const u8 num = (u8)core::strtoul10(token.c_str()); | 147 | const u8 num = (u8)core::strtoul10(token.c_str()); |
148 | *ptr++ = num; | 148 | *ptr++ = num; |
149 | *ptr++ = num; | 149 | *ptr++ = num; |
150 | *ptr++ = num; | 150 | *ptr++ = num; |
151 | *ptr++ = 255; | 151 | *ptr++ = 255; |
152 | } | 152 | } |
153 | } | 153 | } |
154 | } | 154 | } |
155 | } | 155 | } |
156 | else | 156 | else |
157 | { | 157 | { |
158 | const u32 bytesize = 3*size; | 158 | const u32 bytesize = 3*size; |
159 | if (binary) | 159 | if (binary) |
160 | { | 160 | { |
161 | if (file->getSize()-file->getPos() < (long)bytesize) | 161 | if (file->getSize()-file->getPos() < (long)bytesize) |
162 | return 0; | 162 | return 0; |
163 | data = new u8[bytesize]; | 163 | data = new u8[bytesize]; |
164 | file->read(data, bytesize); | 164 | file->read(data, bytesize); |
165 | image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height)); | 165 | image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height)); |
166 | if (image) | 166 | if (image) |
167 | { | 167 | { |
168 | u8* ptr = (u8*)image->lock(); | 168 | u8* ptr = (u8*)image->lock(); |
169 | for (u32 i=0; i<size; ++i) | 169 | for (u32 i=0; i<size; ++i) |
170 | { | 170 | { |
171 | *ptr++ = data[3*i]; | 171 | *ptr++ = data[3*i]; |
172 | *ptr++ = data[3*i+1]; | 172 | *ptr++ = data[3*i+1]; |
173 | *ptr++ = data[3*i+2]; | 173 | *ptr++ = data[3*i+2]; |
174 | *ptr++ = 255; | 174 | *ptr++ = 255; |
175 | } | 175 | } |
176 | } | 176 | } |
177 | } | 177 | } |
178 | else | 178 | else |
179 | { | 179 | { |
180 | if (file->getSize()-file->getPos() < (long)(2*bytesize)) // optimistic test | 180 | if (file->getSize()-file->getPos() < (long)(2*bytesize)) // optimistic test |
181 | return 0; | 181 | return 0; |
182 | image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height)); | 182 | image = new CImage(ECF_A8R8G8B8, core::dimension2d<u32>(width, height)); |
183 | if (image) | 183 | if (image) |
184 | { | 184 | { |
185 | u8* ptr = (u8*)image->lock(); | 185 | u8* ptr = (u8*)image->lock(); |
186 | for (u32 i=0; i<size; ++i) | 186 | for (u32 i=0; i<size; ++i) |
187 | { | 187 | { |
188 | getNextToken(file, token); | 188 | getNextToken(file, token); |
189 | *ptr++ = (u8)core::strtoul10(token.c_str()); | 189 | *ptr++ = (u8)core::strtoul10(token.c_str()); |
190 | getNextToken(file, token); | 190 | getNextToken(file, token); |
191 | *ptr++ = (u8)core::strtoul10(token.c_str()); | 191 | *ptr++ = (u8)core::strtoul10(token.c_str()); |
192 | getNextToken(file, token); | 192 | getNextToken(file, token); |
193 | *ptr++ = (u8)core::strtoul10(token.c_str()); | 193 | *ptr++ = (u8)core::strtoul10(token.c_str()); |
194 | *ptr++ = 255; | 194 | *ptr++ = 255; |
195 | } | 195 | } |
196 | } | 196 | } |
197 | } | 197 | } |
198 | } | 198 | } |
199 | } | 199 | } |
200 | 200 | ||
201 | if (image) | 201 | if (image) |
202 | image->unlock(); | 202 | image->unlock(); |
203 | 203 | ||
204 | delete [] data; | 204 | delete [] data; |
205 | 205 | ||
206 | return image; | 206 | return image; |
207 | } | 207 | } |
208 | 208 | ||
209 | 209 | ||
210 | //! read the next token from file | 210 | //! read the next token from file |
211 | void CImageLoaderPPM::getNextToken(io::IReadFile* file, core::stringc& token) const | 211 | void CImageLoaderPPM::getNextToken(io::IReadFile* file, core::stringc& token) const |
212 | { | 212 | { |
213 | token = ""; | 213 | token = ""; |
214 | c8 c; | 214 | c8 c; |
215 | while(file->getPos()<file->getSize()) | 215 | while(file->getPos()<file->getSize()) |
216 | { | 216 | { |
217 | file->read(&c, 1); | 217 | file->read(&c, 1); |
218 | if (c=='#') | 218 | if (c=='#') |
219 | { | 219 | { |
220 | while (c!='\n' && c!='\r' && (file->getPos()<file->getSize())) | 220 | while (c!='\n' && c!='\r' && (file->getPos()<file->getSize())) |
221 | file->read(&c, 1); | 221 | file->read(&c, 1); |
222 | } | 222 | } |
223 | else if (!core::isspace(c)) | 223 | else if (!core::isspace(c)) |
224 | { | 224 | { |
225 | token.append(c); | 225 | token.append(c); |
226 | break; | 226 | break; |
227 | } | 227 | } |
228 | } | 228 | } |
229 | while(file->getPos()<file->getSize()) | 229 | while(file->getPos()<file->getSize()) |
230 | { | 230 | { |
231 | file->read(&c, 1); | 231 | file->read(&c, 1); |
232 | if (c=='#') | 232 | if (c=='#') |
233 | { | 233 | { |
234 | while (c!='\n' && c!='\r' && (file->getPos()<file->getSize())) | 234 | while (c!='\n' && c!='\r' && (file->getPos()<file->getSize())) |
235 | file->read(&c, 1); | 235 | file->read(&c, 1); |
236 | } | 236 | } |
237 | else if (!core::isspace(c)) | 237 | else if (!core::isspace(c)) |
238 | token.append(c); | 238 | token.append(c); |
239 | else | 239 | else |
240 | break; | 240 | break; |
241 | } | 241 | } |
242 | } | 242 | } |
243 | 243 | ||
244 | 244 | ||
245 | //! skip to next token (skip whitespace) | 245 | //! skip to next token (skip whitespace) |
246 | void CImageLoaderPPM::skipToNextToken(io::IReadFile* file) const | 246 | void CImageLoaderPPM::skipToNextToken(io::IReadFile* file) const |
247 | { | 247 | { |
248 | c8 c; | 248 | c8 c; |
249 | while(file->getPos()<file->getSize()) | 249 | while(file->getPos()<file->getSize()) |
250 | { | 250 | { |
251 | file->read(&c, 1); | 251 | file->read(&c, 1); |
252 | if (c=='#') | 252 | if (c=='#') |
253 | { | 253 | { |
254 | while (c!='\n' && c!='\r' && (file->getPos()<file->getSize())) | 254 | while (c!='\n' && c!='\r' && (file->getPos()<file->getSize())) |
255 | file->read(&c, 1); | 255 | file->read(&c, 1); |
256 | } | 256 | } |
257 | else if (!core::isspace(c)) | 257 | else if (!core::isspace(c)) |
258 | { | 258 | { |
259 | file->seek(-1, true); // put back | 259 | file->seek(-1, true); // put back |
260 | break; | 260 | break; |
261 | } | 261 | } |
262 | } | 262 | } |
263 | } | 263 | } |
264 | 264 | ||
265 | 265 | ||
266 | //! creates a loader which is able to load windows bitmaps | 266 | //! creates a loader which is able to load windows bitmaps |
267 | IImageLoader* createImageLoaderPPM() | 267 | IImageLoader* createImageLoaderPPM() |
268 | { | 268 | { |
269 | return new CImageLoaderPPM; | 269 | return new CImageLoaderPPM; |
270 | } | 270 | } |
271 | 271 | ||
272 | 272 | ||
273 | } // end namespace video | 273 | } // end namespace video |
274 | } // end namespace irr | 274 | } // end namespace irr |
275 | 275 | ||
276 | #endif | 276 | #endif |
277 | 277 | ||