| 1 | /* |
| 2 | Copyright (C) 1999-2006 Id Software, Inc. and contributors. |
| 3 | For a list of contributors, see the accompanying CONTRIBUTORS file. |
| 4 | |
| 5 | This file is part of NetRadiant. |
| 6 | |
| 7 | NetRadiant is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | NetRadiant is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with NetRadiant; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #include "plugin.h" |
| 23 | #include "debugging/debugging.h" |
| 24 | #include "ifilesystem.h" |
| 25 | #include "iimage.h" |
| 26 | |
| 27 | #include "imagelib.h" |
| 28 | |
| 29 | // ====== WEBP loader functionality ====== |
| 30 | |
| 31 | #include "webp/decode.h" |
| 32 | |
| 33 | Image* LoadWEBPBuff( unsigned char* buffer, size_t buffer_length ){ |
| 34 | int image_width; |
| 35 | int image_height; |
| 36 | |
| 37 | if ( !WebPGetInfo( (byte *) buffer, buffer_length, &image_width, &image_height) ){ |
| 38 | globalErrorStream() << "libwebp error: WebPGetInfo: can't get image info\n"; |
| 39 | return 0; |
| 40 | } |
| 41 | |
| 42 | // allocate the pixel buffer |
| 43 | RGBAImage* image = new RGBAImage( image_width, image_height ); |
| 44 | int out_stride = image_width *sizeof(RGBAPixel); |
| 45 | int out_size = image_height * out_stride; |
| 46 | |
| 47 | if ( !WebPDecodeRGBAInto( (byte *) buffer, buffer_length, image->getRGBAPixels(), out_size, out_stride ) ) |
| 48 | { |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | return image; |
| 53 | } |
| 54 | |
| 55 | Image* LoadWEBP( ArchiveFile& file ){ |
| 56 | ScopedArchiveBuffer buffer( file ); |
| 57 | return LoadWEBPBuff( buffer.buffer, buffer.length ); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | #include "modulesystem/singletonmodule.h" |
| 62 | |
| 63 | |
| 64 | class ImageDependencies : public GlobalFileSystemModuleRef |
| 65 | { |
| 66 | }; |
| 67 | |
| 68 | class ImageWEBPAPI |
| 69 | { |
| 70 | _QERPlugImageTable m_imagewebp; |
| 71 | public: |
| 72 | typedef _QERPlugImageTable Type; |
| 73 | STRING_CONSTANT( Name, "webp" ); |
| 74 | |
| 75 | ImageWEBPAPI(){ |
| 76 | m_imagewebp.loadImage = LoadWEBP; |
| 77 | } |
| 78 | _QERPlugImageTable* getTable(){ |
| 79 | return &m_imagewebp; |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | typedef SingletonModule<ImageWEBPAPI, ImageDependencies> ImageWEBPModule; |
| 84 | |
| 85 | ImageWEBPModule g_ImageWEBPModule; |
| 86 | |
| 87 | |
| 88 | extern "C" void RADIANT_DLLEXPORT Radiant_RegisterModules( ModuleServer& server ){ |
| 89 | initialiseModule( server ); |
| 90 | |
| 91 | g_ImageWEBPModule.selfRegister(); |
| 92 | } |