llimagepng.cpp

Go to the documentation of this file.
00001 /*
00002  * @file llimagepng.cpp
00003  * @brief LLImageFormatted glue to encode / decode PNG files.
00004  *
00005  * $LicenseInfo:firstyear=2007&license=viewergpl$
00006  * 
00007  * Copyright (c) 2007, Linden Research, Inc.
00008  * 
00009  * Second Life Viewer Source Code
00010  * The source code in this file ("Source Code") is provided by Linden Lab
00011  * to you under the terms of the GNU General Public License, version 2.0
00012  * ("GPL"), unless you have obtained a separate licensing agreement
00013  * ("Other License"), formally executed by you and Linden Lab.  Terms of
00014  * the GPL can be found in doc/GPL-license.txt in this distribution, or
00015  * online at http://secondlife.com/developers/opensource/gplv2
00016  * 
00017  * There are special exceptions to the terms and conditions of the GPL as
00018  * it is applied to this Source Code. View the full text of the exception
00019  * in the file doc/FLOSS-exception.txt in this software distribution, or
00020  * online at http://secondlife.com/developers/opensource/flossexception
00021  * 
00022  * By copying, modifying or distributing this software, you acknowledge
00023  * that you have read and understood your obligations described above,
00024  * and agree to abide by those obligations.
00025  * 
00026  * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
00027  * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
00028  * COMPLETENESS OR PERFORMANCE.
00029  * $/LicenseInfo$
00030  */
00031 
00032 #include "linden_common.h"
00033 #include "stdtypes.h"
00034 #include "llerror.h"
00035 
00036 #include "llimage.h"
00037 #include "llpngwrapper.h"
00038 #include "llimagepng.h"
00039 
00040 // ---------------------------------------------------------------------------
00041 // LLImagePNG
00042 // ---------------------------------------------------------------------------
00043 LLImagePNG::LLImagePNG()
00044     : LLImageFormatted(IMG_CODEC_PNG),
00045           mTmpWriteBuffer(NULL)
00046 {
00047 }
00048 
00049 LLImagePNG::~LLImagePNG()
00050 {
00051         if (mTmpWriteBuffer)
00052         {
00053                 delete[] mTmpWriteBuffer;
00054         }
00055 }
00056 
00057 // Virtual
00058 // Parse PNG image information and set the appropriate
00059 // width, height and component (channel) information.
00060 BOOL LLImagePNG::updateData()
00061 {
00062     resetLastError();
00063 
00064     // Check to make sure that this instance has been initialized with data
00065     if (!getData() || (0 == getDataSize()))
00066     {
00067         setLastError("Uninitialized instance of LLImagePNG");
00068         return FALSE;
00069     }
00070 
00071         // Decode the PNG data and extract sizing information
00072         LLPngWrapper pngWrapper;
00073         LLPngWrapper::ImageInfo infop;
00074         if (! pngWrapper.readPng(getData(), NULL, &infop))
00075         {
00076                 setLastError(pngWrapper.getErrorMessage());
00077                 return FALSE;
00078         }
00079 
00080         setSize(infop.mWidth, infop.mHeight, infop.mComponents);
00081 
00082         return TRUE;
00083 }
00084 
00085 // Virtual
00086 // Decode an in-memory PNG image into the raw RGB or RGBA format
00087 // used within SecondLife.
00088 BOOL LLImagePNG::decode(LLImageRaw* raw_image, F32 decode_time)
00089 {
00090         llassert_always(raw_image);
00091 
00092     resetLastError();
00093 
00094     // Check to make sure that this instance has been initialized with data
00095     if (!getData() || (0 == getDataSize()))
00096     {
00097         setLastError("LLImagePNG trying to decode an image with no data!");
00098         return FALSE;
00099     }
00100 
00101         // Decode the PNG data into the raw image
00102         LLPngWrapper pngWrapper;
00103         if (! pngWrapper.readPng(getData(), raw_image))
00104         {
00105                 setLastError(pngWrapper.getErrorMessage());
00106                 return FALSE;
00107         }
00108 
00109         return TRUE;
00110 }
00111 
00112 // Virtual
00113 // Encode the in memory RGB image into PNG format.
00114 BOOL LLImagePNG::encode(const LLImageRaw* raw_image, F32 encode_time)
00115 {
00116         llassert_always(raw_image);
00117 
00118     resetLastError();
00119 
00120         // Image logical size
00121         setSize(raw_image->getWidth(), raw_image->getHeight(), raw_image->getComponents());
00122 
00123         // Temporary buffer to hold the encoded image. Note: the final image
00124         // size should be much smaller due to compression.
00125         if (mTmpWriteBuffer)
00126         {
00127                 delete[] mTmpWriteBuffer;
00128         }
00129         U32 bufferSize = getWidth() * getHeight() * getComponents() + 1024;
00130     U8* mTmpWriteBuffer = new U8[ bufferSize ];
00131 
00132         // Delegate actual encoding work to wrapper
00133         LLPngWrapper pngWrapper;
00134         if (! pngWrapper.writePng(raw_image, mTmpWriteBuffer))
00135         {
00136                 setLastError(pngWrapper.getErrorMessage());
00137                 return FALSE;
00138         }
00139 
00140         // Resize internal buffer and copy from temp
00141         U32 encodedSize = pngWrapper.getFinalSize();
00142         allocateData(encodedSize);
00143         memcpy(getData(), mTmpWriteBuffer, encodedSize);
00144 
00145         delete[] mTmpWriteBuffer;
00146 
00147         return TRUE;
00148 }
00149 

Generated on Thu Jul 1 06:08:44 2010 for Second Life Viewer by  doxygen 1.4.7