1 | /* |
---|
2 | ----------------------------------------------------------------------------- |
---|
3 | This source file is part of OGRE |
---|
4 | (Object-oriented Graphics Rendering Engine) |
---|
5 | For the latest info, see http://www.ogre3d.org/ |
---|
6 | |
---|
7 | Copyright (c) 2000-2006 Torus Knot Software Ltd |
---|
8 | Also see acknowledgements in Readme.html |
---|
9 | |
---|
10 | This program is free software; you can redistribute it and/or modify it under |
---|
11 | the terms of the GNU Lesser General Public License as published by the Free Software |
---|
12 | Foundation; either version 2 of the License, or (at your option) any later |
---|
13 | version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, but WITHOUT |
---|
16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
---|
17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU Lesser General Public License along with |
---|
20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
---|
21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
---|
22 | http://www.gnu.org/copyleft/lesser.txt. |
---|
23 | |
---|
24 | You may alternatively use this source under the terms of a specific version of |
---|
25 | the OGRE Unrestricted License provided you have obtained such a license from |
---|
26 | Torus Knot Software Ltd. |
---|
27 | ----------------------------------------------------------------------------- |
---|
28 | */ |
---|
29 | |
---|
30 | #include "OgreStableHeaders.h" |
---|
31 | |
---|
32 | #include "OgreRoot.h" |
---|
33 | #include "OgreRenderSystem.h" |
---|
34 | #include "OgreFreeImageCodec.h" |
---|
35 | #include "OgreImage.h" |
---|
36 | #include "OgreException.h" |
---|
37 | |
---|
38 | #include "OgreLogManager.h" |
---|
39 | #include "OgreStringConverter.h" |
---|
40 | |
---|
41 | #include <FreeImage.h> |
---|
42 | |
---|
43 | namespace Ogre { |
---|
44 | |
---|
45 | FreeImageCodec::RegisteredCodecList FreeImageCodec::msCodecList; |
---|
46 | //--------------------------------------------------------------------- |
---|
47 | void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) |
---|
48 | { |
---|
49 | // Callback method as required by FreeImage to report problems |
---|
50 | StringUtil::StrStreamType str; |
---|
51 | str << "FreeImage error: '" << message << "'"; |
---|
52 | |
---|
53 | const char* typeName = FreeImage_GetFormatFromFIF(fif); |
---|
54 | if (typeName) |
---|
55 | { |
---|
56 | str << " when loading format " << typeName; |
---|
57 | } |
---|
58 | |
---|
59 | LogManager::getSingleton().logMessage(str.str()); |
---|
60 | |
---|
61 | } |
---|
62 | //--------------------------------------------------------------------- |
---|
63 | void FreeImageCodec::startup(void) |
---|
64 | { |
---|
65 | FreeImage_Initialise(false); |
---|
66 | |
---|
67 | LogManager::getSingleton().logMessage( |
---|
68 | LML_NORMAL, |
---|
69 | "FreeImage version: " + String(FreeImage_GetVersion())); |
---|
70 | LogManager::getSingleton().logMessage( |
---|
71 | LML_NORMAL, |
---|
72 | FreeImage_GetCopyrightMessage()); |
---|
73 | |
---|
74 | // Register codecs |
---|
75 | StringUtil::StrStreamType strExt; |
---|
76 | strExt << "Supported formats: "; |
---|
77 | bool first = true; |
---|
78 | for (int i = 0; i < FreeImage_GetFIFCount(); ++i) |
---|
79 | { |
---|
80 | |
---|
81 | // Skip DDS codec since FreeImage does not have the option |
---|
82 | // to keep DXT data compressed, we'll use our own codec |
---|
83 | if ((FREE_IMAGE_FORMAT)i == FIF_DDS) |
---|
84 | continue; |
---|
85 | |
---|
86 | String exts(FreeImage_GetFIFExtensionList((FREE_IMAGE_FORMAT)i)); |
---|
87 | if (!first) |
---|
88 | { |
---|
89 | strExt << ","; |
---|
90 | } |
---|
91 | first = false; |
---|
92 | strExt << exts; |
---|
93 | |
---|
94 | // Pull off individual formats (separated by comma by FI) |
---|
95 | StringVector extsVector = StringUtil::split(exts, ","); |
---|
96 | for (StringVector::iterator v = extsVector.begin(); v != extsVector.end(); ++v) |
---|
97 | { |
---|
98 | ImageCodec* codec = new FreeImageCodec(*v, i); |
---|
99 | msCodecList.push_back(codec); |
---|
100 | Codec::registerCodec(codec); |
---|
101 | } |
---|
102 | } |
---|
103 | LogManager::getSingleton().logMessage( |
---|
104 | LML_NORMAL, |
---|
105 | strExt.str()); |
---|
106 | |
---|
107 | // Set error handler |
---|
108 | FreeImage_SetOutputMessage(FreeImageErrorHandler); |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | } |
---|
114 | //--------------------------------------------------------------------- |
---|
115 | void FreeImageCodec::shutdown(void) |
---|
116 | { |
---|
117 | FreeImage_DeInitialise(); |
---|
118 | |
---|
119 | for (RegisteredCodecList::iterator i = msCodecList.begin(); |
---|
120 | i != msCodecList.end(); ++i) |
---|
121 | { |
---|
122 | Codec::unRegisterCodec(*i); |
---|
123 | delete *i; |
---|
124 | } |
---|
125 | msCodecList.clear(); |
---|
126 | |
---|
127 | } |
---|
128 | //--------------------------------------------------------------------- |
---|
129 | FreeImageCodec::FreeImageCodec(const String &type, unsigned int fiType): |
---|
130 | mType(type), |
---|
131 | mFreeImageType(fiType) |
---|
132 | { |
---|
133 | } |
---|
134 | //--------------------------------------------------------------------- |
---|
135 | FIBITMAP* FreeImageCodec::encode(MemoryDataStreamPtr& input, CodecDataPtr& pData) const |
---|
136 | { |
---|
137 | FIBITMAP* ret = 0; |
---|
138 | |
---|
139 | ImageData* pImgData = static_cast< ImageData * >( pData.getPointer() ); |
---|
140 | PixelBox src(pImgData->width, pImgData->height, pImgData->depth, pImgData->format, input->getPtr()); |
---|
141 | |
---|
142 | // The required format, which will adjust to the format |
---|
143 | // actually supported by FreeImage. |
---|
144 | PixelFormat requiredFormat = pImgData->format; |
---|
145 | |
---|
146 | // determine the settings |
---|
147 | FREE_IMAGE_TYPE imageType; |
---|
148 | switch(pImgData->format) |
---|
149 | { |
---|
150 | case PF_R5G6B5: |
---|
151 | case PF_B5G6R5: |
---|
152 | case PF_R8G8B8: |
---|
153 | case PF_B8G8R8: |
---|
154 | case PF_A8R8G8B8: |
---|
155 | case PF_X8R8G8B8: |
---|
156 | case PF_A8B8G8R8: |
---|
157 | case PF_X8B8G8R8: |
---|
158 | case PF_B8G8R8A8: |
---|
159 | case PF_R8G8B8A8: |
---|
160 | case PF_A4L4: |
---|
161 | case PF_BYTE_LA: |
---|
162 | case PF_R3G3B2: |
---|
163 | case PF_A4R4G4B4: |
---|
164 | case PF_A1R5G5B5: |
---|
165 | case PF_A2R10G10B10: |
---|
166 | case PF_A2B10G10R10: |
---|
167 | // I'd like to be able to use r/g/b masks to get FreeImage to load the data |
---|
168 | // in it's existing format, but that doesn't work, FreeImage needs to have |
---|
169 | // data in RGB[A] (big endian) and BGR[A] (little endian), always. |
---|
170 | if (PixelUtil::hasAlpha(pImgData->format)) |
---|
171 | { |
---|
172 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG |
---|
173 | requiredFormat = PF_BYTE_RGBA; |
---|
174 | #else |
---|
175 | requiredFormat = PF_BYTE_BGRA; |
---|
176 | #endif |
---|
177 | } |
---|
178 | else |
---|
179 | { |
---|
180 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG |
---|
181 | requiredFormat = PF_BYTE_RGB; |
---|
182 | #else |
---|
183 | requiredFormat = PF_BYTE_BGR; |
---|
184 | #endif |
---|
185 | } |
---|
186 | // fall through |
---|
187 | case PF_L8: |
---|
188 | case PF_A8: |
---|
189 | imageType = FIT_BITMAP; |
---|
190 | break; |
---|
191 | |
---|
192 | case PF_L16: |
---|
193 | imageType = FIT_UINT16; |
---|
194 | break; |
---|
195 | |
---|
196 | case PF_SHORT_GR: |
---|
197 | requiredFormat = PF_SHORT_RGB; |
---|
198 | // fall through |
---|
199 | case PF_SHORT_RGB: |
---|
200 | imageType = FIT_RGB16; |
---|
201 | break; |
---|
202 | |
---|
203 | case PF_SHORT_RGBA: |
---|
204 | imageType = FIT_RGBA16; |
---|
205 | break; |
---|
206 | |
---|
207 | case PF_FLOAT16_R: |
---|
208 | requiredFormat = PF_FLOAT32_R; |
---|
209 | // fall through |
---|
210 | case PF_FLOAT32_R: |
---|
211 | imageType = FIT_FLOAT; |
---|
212 | break; |
---|
213 | |
---|
214 | case PF_FLOAT16_GR: |
---|
215 | case PF_FLOAT16_RGB: |
---|
216 | case PF_FLOAT32_GR: |
---|
217 | requiredFormat = PF_FLOAT32_RGB; |
---|
218 | // fall through |
---|
219 | case PF_FLOAT32_RGB: |
---|
220 | imageType = FIT_RGBF; |
---|
221 | break; |
---|
222 | |
---|
223 | case PF_FLOAT16_RGBA: |
---|
224 | requiredFormat = PF_FLOAT32_RGBA; |
---|
225 | // fall through |
---|
226 | case PF_FLOAT32_RGBA: |
---|
227 | imageType = FIT_RGBAF; |
---|
228 | break; |
---|
229 | |
---|
230 | default: |
---|
231 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, "Invalid image format", "FreeImageCodec::encode"); |
---|
232 | }; |
---|
233 | |
---|
234 | bool conversionRequired = false; |
---|
235 | |
---|
236 | unsigned char* srcData = input->getPtr(); |
---|
237 | PixelBox convBox(pImgData->width, pImgData->height, 1, requiredFormat); |
---|
238 | if (requiredFormat != pImgData->format) |
---|
239 | { |
---|
240 | conversionRequired = true; |
---|
241 | // Allocate memory |
---|
242 | convBox.data = new uchar[convBox.getConsecutiveSize()]; |
---|
243 | // perform conversion and reassign source |
---|
244 | PixelBox src(pImgData->width, pImgData->height, 1, pImgData->format, input->getPtr()); |
---|
245 | PixelUtil::bulkPixelConversion(src, convBox); |
---|
246 | srcData = static_cast<unsigned char*>(convBox.data); |
---|
247 | |
---|
248 | } |
---|
249 | |
---|
250 | unsigned bpp = static_cast<unsigned>(PixelUtil::getNumElemBits(requiredFormat)); |
---|
251 | |
---|
252 | ret = FreeImage_AllocateT( |
---|
253 | imageType, |
---|
254 | static_cast<int>(pImgData->width), |
---|
255 | static_cast<int>(pImgData->height), |
---|
256 | bpp); |
---|
257 | |
---|
258 | if (requiredFormat == PF_L8 || requiredFormat == PF_A8) |
---|
259 | { |
---|
260 | // Must explicitly tell FreeImage that this is greyscale by setting |
---|
261 | // a "grey" palette (otherwise it will save as a normal RGB |
---|
262 | // palettized image). |
---|
263 | FIBITMAP *tmp = FreeImage_ConvertToGreyscale(ret); |
---|
264 | FreeImage_Unload(ret); |
---|
265 | ret = tmp; |
---|
266 | } |
---|
267 | |
---|
268 | size_t dstPitch = FreeImage_GetPitch(ret); |
---|
269 | size_t srcPitch = pImgData->width * PixelUtil::getNumElemBytes(requiredFormat); |
---|
270 | |
---|
271 | |
---|
272 | // Copy data, invert scanlines and respect FreeImage pitch |
---|
273 | uchar* pSrc; |
---|
274 | uchar* pDst = FreeImage_GetBits(ret); |
---|
275 | for (size_t y = 0; y < pImgData->height; ++y) |
---|
276 | { |
---|
277 | pSrc = srcData + (pImgData->height - y - 1) * srcPitch; |
---|
278 | memcpy(pDst, pSrc, srcPitch); |
---|
279 | pDst += dstPitch; |
---|
280 | } |
---|
281 | |
---|
282 | if (conversionRequired) |
---|
283 | { |
---|
284 | // delete temporary conversion area |
---|
285 | delete [] static_cast<uchar*>(convBox.data); |
---|
286 | } |
---|
287 | |
---|
288 | return ret; |
---|
289 | } |
---|
290 | //--------------------------------------------------------------------- |
---|
291 | DataStreamPtr FreeImageCodec::code(MemoryDataStreamPtr& input, Codec::CodecDataPtr& pData) const |
---|
292 | { |
---|
293 | FIBITMAP* fiBitmap = encode(input, pData); |
---|
294 | |
---|
295 | // open memory chunk allocated by FreeImage |
---|
296 | FIMEMORY* mem = FreeImage_OpenMemory(); |
---|
297 | // write data into memory |
---|
298 | FreeImage_SaveToMemory((FREE_IMAGE_FORMAT)mFreeImageType, fiBitmap, mem); |
---|
299 | // Grab data information |
---|
300 | BYTE* data; |
---|
301 | DWORD size; |
---|
302 | FreeImage_AcquireMemory(mem, &data, &size); |
---|
303 | // Copy data into our own buffer |
---|
304 | BYTE* ourData = new BYTE[size]; |
---|
305 | memcpy(ourData, data, size); |
---|
306 | // Wrap data in stream, tell it to free on close |
---|
307 | DataStreamPtr outstream(new MemoryDataStream(ourData, size, true)); |
---|
308 | // Now free FreeImage memory buffers |
---|
309 | FreeImage_CloseMemory(mem); |
---|
310 | // Unload bitmap |
---|
311 | FreeImage_Unload(fiBitmap); |
---|
312 | |
---|
313 | return outstream; |
---|
314 | |
---|
315 | |
---|
316 | } |
---|
317 | //--------------------------------------------------------------------- |
---|
318 | void FreeImageCodec::codeToFile(MemoryDataStreamPtr& input, |
---|
319 | const String& outFileName, Codec::CodecDataPtr& pData) const |
---|
320 | { |
---|
321 | FIBITMAP* fiBitmap = encode(input, pData); |
---|
322 | |
---|
323 | FreeImage_Save((FREE_IMAGE_FORMAT)mFreeImageType, fiBitmap, outFileName.c_str()); |
---|
324 | FreeImage_Unload(fiBitmap); |
---|
325 | |
---|
326 | |
---|
327 | } |
---|
328 | //--------------------------------------------------------------------- |
---|
329 | Codec::DecodeResult FreeImageCodec::decode(DataStreamPtr& input) const |
---|
330 | { |
---|
331 | // Buffer stream into memory (TODO: override IO functions instead?) |
---|
332 | MemoryDataStream memStream(input, true); |
---|
333 | |
---|
334 | FIMEMORY* fiMem = |
---|
335 | FreeImage_OpenMemory(memStream.getPtr(), static_cast<DWORD>(memStream.size())); |
---|
336 | |
---|
337 | FIBITMAP* fiBitmap = FreeImage_LoadFromMemory( |
---|
338 | (FREE_IMAGE_FORMAT)mFreeImageType, fiMem); |
---|
339 | if (!fiBitmap) |
---|
340 | { |
---|
341 | OGRE_EXCEPT(Exception::ERR_INTERNAL_ERROR, |
---|
342 | "Error decoding image", |
---|
343 | "FreeImageCodec::decode"); |
---|
344 | } |
---|
345 | |
---|
346 | |
---|
347 | ImageData* imgData = new ImageData(); |
---|
348 | MemoryDataStreamPtr output; |
---|
349 | |
---|
350 | imgData->depth = 1; // only 2D formats handled by this codec |
---|
351 | imgData->width = FreeImage_GetWidth(fiBitmap); |
---|
352 | imgData->height = FreeImage_GetHeight(fiBitmap); |
---|
353 | imgData->num_mipmaps = 0; // no mipmaps in non-DDS |
---|
354 | imgData->flags = 0; |
---|
355 | |
---|
356 | // Must derive format first, this may perform conversions |
---|
357 | |
---|
358 | FREE_IMAGE_TYPE imageType = FreeImage_GetImageType(fiBitmap); |
---|
359 | FREE_IMAGE_COLOR_TYPE colourType = FreeImage_GetColorType(fiBitmap); |
---|
360 | unsigned bpp = FreeImage_GetBPP(fiBitmap); |
---|
361 | |
---|
362 | switch(imageType) |
---|
363 | { |
---|
364 | case FIT_UNKNOWN: |
---|
365 | case FIT_COMPLEX: |
---|
366 | case FIT_UINT32: |
---|
367 | case FIT_INT32: |
---|
368 | case FIT_DOUBLE: |
---|
369 | default: |
---|
370 | OGRE_EXCEPT(Exception::ERR_ITEM_NOT_FOUND, |
---|
371 | "Unknown or unsupported image format", |
---|
372 | "FreeImageCodec::decode"); |
---|
373 | |
---|
374 | break; |
---|
375 | case FIT_BITMAP: |
---|
376 | // Standard image type |
---|
377 | // Perform any colour conversions for greyscale |
---|
378 | if (colourType == FIC_MINISWHITE || colourType == FIC_MINISBLACK) |
---|
379 | { |
---|
380 | FIBITMAP* newBitmap = FreeImage_ConvertToGreyscale(fiBitmap); |
---|
381 | // free old bitmap and replace |
---|
382 | FreeImage_Unload(fiBitmap); |
---|
383 | fiBitmap = newBitmap; |
---|
384 | // get new formats |
---|
385 | bpp = FreeImage_GetBPP(fiBitmap); |
---|
386 | colourType = FreeImage_GetColorType(fiBitmap); |
---|
387 | } |
---|
388 | // Perform any colour conversions for RGB |
---|
389 | else if (bpp < 8 || colourType == FIC_PALETTE || colourType == FIC_CMYK) |
---|
390 | { |
---|
391 | FIBITMAP* newBitmap = FreeImage_ConvertTo24Bits(fiBitmap); |
---|
392 | // free old bitmap and replace |
---|
393 | FreeImage_Unload(fiBitmap); |
---|
394 | fiBitmap = newBitmap; |
---|
395 | // get new formats |
---|
396 | bpp = FreeImage_GetBPP(fiBitmap); |
---|
397 | colourType = FreeImage_GetColorType(fiBitmap); |
---|
398 | } |
---|
399 | |
---|
400 | // by this stage, 8-bit is greyscale, 16/24/32 bit are RGB[A] |
---|
401 | switch(bpp) |
---|
402 | { |
---|
403 | case 8: |
---|
404 | imgData->format = PF_L8; |
---|
405 | break; |
---|
406 | case 16: |
---|
407 | // Determine 555 or 565 from green mask |
---|
408 | // cannot be 16-bit greyscale since that's FIT_UINT16 |
---|
409 | if(FreeImage_GetGreenMask(fiBitmap) == FI16_565_GREEN_MASK) |
---|
410 | { |
---|
411 | imgData->format = PF_R5G6B5; |
---|
412 | } |
---|
413 | else |
---|
414 | { |
---|
415 | // FreeImage doesn't support 4444 format so must be 1555 |
---|
416 | imgData->format = PF_A1R5G5B5; |
---|
417 | } |
---|
418 | break; |
---|
419 | case 24: |
---|
420 | // FreeImage differs per platform |
---|
421 | // PF_BYTE_BGR[A] for little endian (== PF_ARGB native) |
---|
422 | // PF_BYTE_RGB[A] for big endian (== PF_RGBA native) |
---|
423 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG |
---|
424 | imgData->format = PF_BYTE_RGB; |
---|
425 | #else |
---|
426 | imgData->format = PF_BYTE_BGR; |
---|
427 | #endif |
---|
428 | break; |
---|
429 | case 32: |
---|
430 | #if OGRE_ENDIAN == OGRE_ENDIAN_BIG |
---|
431 | imgData->format = PF_BYTE_RGBA; |
---|
432 | #else |
---|
433 | imgData->format = PF_BYTE_BGRA; |
---|
434 | #endif |
---|
435 | break; |
---|
436 | |
---|
437 | |
---|
438 | }; |
---|
439 | break; |
---|
440 | case FIT_UINT16: |
---|
441 | case FIT_INT16: |
---|
442 | // 16-bit greyscale |
---|
443 | imgData->format = PF_L16; |
---|
444 | break; |
---|
445 | case FIT_FLOAT: |
---|
446 | // Single-component floating point data |
---|
447 | imgData->format = PF_FLOAT32_R; |
---|
448 | break; |
---|
449 | case FIT_RGB16: |
---|
450 | imgData->format = PF_SHORT_RGB; |
---|
451 | break; |
---|
452 | case FIT_RGBA16: |
---|
453 | imgData->format = PF_SHORT_RGBA; |
---|
454 | break; |
---|
455 | case FIT_RGBF: |
---|
456 | imgData->format = PF_FLOAT32_RGB; |
---|
457 | break; |
---|
458 | case FIT_RGBAF: |
---|
459 | imgData->format = PF_FLOAT32_RGBA; |
---|
460 | break; |
---|
461 | |
---|
462 | |
---|
463 | }; |
---|
464 | |
---|
465 | unsigned char* srcData = FreeImage_GetBits(fiBitmap); |
---|
466 | unsigned srcPitch = FreeImage_GetPitch(fiBitmap); |
---|
467 | |
---|
468 | // Final data - invert image and trim pitch at the same time |
---|
469 | size_t dstPitch = imgData->width * PixelUtil::getNumElemBytes(imgData->format); |
---|
470 | imgData->size = dstPitch * imgData->height; |
---|
471 | // Bind output buffer |
---|
472 | output.bind(new MemoryDataStream(imgData->size)); |
---|
473 | |
---|
474 | uchar* pSrc; |
---|
475 | uchar* pDst = output->getPtr(); |
---|
476 | for (size_t y = 0; y < imgData->height; ++y) |
---|
477 | { |
---|
478 | pSrc = srcData + (imgData->height - y - 1) * srcPitch; |
---|
479 | memcpy(pDst, pSrc, dstPitch); |
---|
480 | pDst += dstPitch; |
---|
481 | } |
---|
482 | |
---|
483 | |
---|
484 | FreeImage_Unload(fiBitmap); |
---|
485 | FreeImage_CloseMemory(fiMem); |
---|
486 | |
---|
487 | |
---|
488 | DecodeResult ret; |
---|
489 | ret.first = output; |
---|
490 | ret.second = CodecDataPtr(imgData); |
---|
491 | return ret; |
---|
492 | |
---|
493 | } |
---|
494 | //--------------------------------------------------------------------- |
---|
495 | String FreeImageCodec::getType() const |
---|
496 | { |
---|
497 | return mType; |
---|
498 | } |
---|
499 | } |
---|