examples/Multimedia/ICL/ICLCodec/PNGConvert.cpp

00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 
00031 // General BAFL headers
00032 #include <barsc.h>
00033 #include <barsread.h>
00034 #include <bautils.h>
00035 // ICL headers
00036 #include <imageconversion.h>
00037 
00038 #include "ImageUtils.h"
00039 #include <101F4122_extra.rsg>
00040 #include "uids.h"
00041 #include "PNGCodec.h"
00042 
00043 //
00044 // PNG decoder class
00045 //
00046 
00047 // Simple factory function
00048 CPngDecoder* CPngDecoder::NewL()
00049         {
00050         return new(ELeave) CPngDecoder;
00051         }
00052 
00053 CPngDecoder::CPngDecoder()
00054         {
00055         }
00056 
00057 // Destructor calls base class cleanup
00058 CPngDecoder::~CPngDecoder()
00059         {
00060         Cleanup();
00061         }
00062 
00063 // Gets the image type: always PNG, no sub type
00064 void CPngDecoder::ImageType(TInt aFrameNumber, TUid& aImageType, TUid& aImageSubType) const
00065         {
00066         __ASSERT_ALWAYS(aFrameNumber == 0, Panic(KErrArgument));
00067         aImageType = KImageTypePNGUid;
00068         aImageSubType = KNullUid;
00069         }
00070 
00071 // Scans the image header.
00072 void CPngDecoder::ScanDataL()
00073         {
00074         // Validate that format is correct
00075         ReadFormatL();
00076         ASSERT(ImageReadCodec() == NULL);
00077 
00078         // Create a codec to read the PNG image
00079         CPngReadCodec* imageReadCodec;
00080         imageReadCodec = new(ELeave) CPngReadCodec;
00081         // Let the framework takes ownership of the codec
00082         SetImageReadCodec(imageReadCodec);
00083         imageReadCodec->ConstructL();
00084 
00085         // Fill in image information for all frames
00086         ReadFrameHeadersL();
00087         }
00088 
00089 // Validate that the file is PNG format
00090 void CPngDecoder::ReadFormatL()
00091         {
00092         // Read initial data block
00093         TPtrC8 bufferDes;
00094         ReadDataL(0, bufferDes, KPngFileSignatureLength);
00095 
00096         // Validate the header.
00097         if (bufferDes.Length() < KPngFileSignatureLength)
00098                 User::Leave(KErrUnderflow);
00099 
00100         const TUint8* ptr = bufferDes.Ptr();
00101         if (Mem::Compare(ptr, KPngFileSignatureLength, KPngSignature, KPngFileSignatureLength)!=0)
00102                 User::Leave(KErrCorrupt);
00103 
00104         // Set start position of image data following the header
00105         SetStartPosition(KPngFileSignatureLength);
00106 
00107         // Set maximum data length as don't know exactly
00108         SetDataLength(KMaxTInt);
00109         }
00110 
00111 // Gets text descriptions of image properties
00112 CFrameInfoStrings* CPngDecoder::FrameInfoStringsL(RFs& aFs, TInt aFrameNumber)
00113         {
00114         const TUid KPngCodecDllUid = {KExPNGCodecDllUidValue};
00115 
00116         // Strings are read from 101F4122_extra.rss
00117         RResourceFile resourceFile;
00118         OpenExtraResourceFileLC(aFs,KPngCodecDllUid,resourceFile);
00119         HBufC8* resourceInfo = resourceFile.AllocReadLC(THEDECODERINFO);
00120         TResourceReader resourceReader;
00121         resourceReader.SetBuffer(resourceInfo);
00122 
00123         TBuf<KCodecResourceStringMax> info;
00124         TBuf<KCodecResourceStringMax> templte;
00125 
00126         const TFrameInfo& frameInfo = FrameInfo(aFrameNumber);
00127         CFrameInfoStrings* frameInfoStrings = CFrameInfoStrings::NewLC();
00128 
00129         // Set decoder name
00130         info = resourceReader.ReadTPtrC();
00131         frameInfoStrings->SetDecoderL(info);
00132         // Set image format name
00133         info = resourceReader.ReadTPtrC();
00134         frameInfoStrings->SetFormatL(info);
00135         // Set image dimensions
00136         TInt width = frameInfo.iOverallSizeInPixels.iWidth;
00137         TInt height = frameInfo.iOverallSizeInPixels.iHeight;
00138         TInt depth = frameInfo.iBitsPerPixel;
00139         templte = resourceReader.ReadTPtrC();
00140         info.Format(templte, width, height);
00141         frameInfoStrings->SetDimensionsL(info);
00142         // Set image depth, for colour or b/w as appropriate
00143         CDesCArrayFlat* resourceArray = resourceReader.ReadDesCArrayL();
00144         CleanupStack::PushL(resourceArray);
00145         TUint formatIndex = (frameInfo.iFlags & TFrameInfo::EColor) ? 1 : 0;
00146         templte = (*resourceArray)[formatIndex];
00147         CleanupStack::PopAndDestroy(resourceArray);
00148         info.Format(templte, depth);
00149         frameInfoStrings->SetDepthL(info);
00150         // Set image details strings
00151         info = resourceReader.ReadTPtrC(); // read details, then see if we use it
00152         if (frameInfo.iFlags & TFrameInfo::EAlphaChannel && frameInfo.iFlags & TFrameInfo::EColor)
00153                 {
00154                 frameInfoStrings->SetDetailsL(info);
00155                 }
00156         // Cleanup and return
00157         CleanupStack::Pop(frameInfoStrings); 
00158         CleanupStack::PopAndDestroy(2); // resourceInfo + resourceFile
00159         return frameInfoStrings;
00160         }
00161 
00162 //
00163 // PNG encoder class
00164 //
00165 
00166 // Simple factory function
00167 CPngEncoder* CPngEncoder::NewL()
00168         {
00169         return new(ELeave) CPngEncoder;
00170         }
00171 
00172 CPngEncoder::CPngEncoder()
00173         {
00174         }
00175 
00176 // Destructor calls base class cleanup
00177 CPngEncoder::~CPngEncoder()
00178         {
00179         CImageEncoderPlugin::Cleanup();
00180         }
00181 
00182 // Sets up the codec to encode the frame
00183 void CPngEncoder::PrepareEncoderL(const CFrameImageData* aFrameImageData)
00184         {
00185         // Default encode parameters
00186         TInt bpp = 24;
00187         TBool color = ETrue;
00188         TBool paletted = EFalse;
00189         TInt compressionLevel = TPngEncodeData::EDefaultCompression;
00190 
00191         // Use encode params in aFrameImageData, if present
00192         const TInt count = (aFrameImageData) ? aFrameImageData->FrameDataCount() : 0;
00193         for (TInt i=0; i < count; i++)
00194                 {
00195                 const TFrameDataBlock* encoderData = aFrameImageData->GetFrameData(i);
00196                 if (encoderData->DataType() == KPNGEncodeDataUid)
00197                         {
00198                         const TPngEncodeData* pngEncodeData = static_cast<const TPngEncodeData*>(encoderData);
00199                         bpp = pngEncodeData->iBitsPerPixel;
00200                         color = pngEncodeData->iColor;
00201                         paletted = pngEncodeData->iPaletted;
00202                         compressionLevel = pngEncodeData->iLevel;
00203                         }
00204                 }
00205 
00206         // Create the codec to write a PNG image
00207         CPngWriteCodec* codec = new(ELeave) CPngWriteCodec(bpp, color, paletted, compressionLevel);
00208         // Let the framework takes ownership of the codec
00209         SetImageWriteCodec(codec);              
00210         codec->ConstructL();
00211         }

Generated by  doxygen 1.6.2