examples/Multimedia/ICL/ICLCodec/PNGConvert.cpp

00001 // PNGConvert.CPP
00002 //
00003 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
00004 // All rights reserved.
00005 // This component and the accompanying materials are made available
00006 // under the terms of "Eclipse Public License v1.0"
00007 // which accompanies this distribution, and is available
00008 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
00009 //
00010 // Initial Contributors:
00011 // Nokia Corporation - initial contribution.
00012 //
00013 // Contributors:
00014 //
00015 // Description:
00016 //
00017 //
00018 
00019 // General BAFL headers
00020 #include <barsc.h>
00021 #include <barsread.h>
00022 #include <bautils.h>
00023 // ICL headers
00024 #include <imageconversion.h>
00025 
00026 #include "ImageUtils.h"
00027 #include <101F4122_extra.rsg>
00028 #include "uids.h"
00029 #include "PNGCodec.h"
00030 
00031 //
00032 // PNG decoder class
00033 //
00034 
00035 // Simple factory function
00036 CPngDecoder* CPngDecoder::NewL()
00037         {
00038         return new(ELeave) CPngDecoder;
00039         }
00040 
00041 CPngDecoder::CPngDecoder()
00042         {
00043         }
00044 
00045 // Destructor calls base class cleanup
00046 CPngDecoder::~CPngDecoder()
00047         {
00048         Cleanup();
00049         }
00050 
00051 // Gets the image type: always PNG, no sub type
00052 void CPngDecoder::ImageType(TInt aFrameNumber, TUid& aImageType, TUid& aImageSubType) const
00053         {
00054         __ASSERT_ALWAYS(aFrameNumber == 0, Panic(KErrArgument));
00055         aImageType = KImageTypePNGUid;
00056         aImageSubType = KNullUid;
00057         }
00058 
00059 // Scans the image header.
00060 void CPngDecoder::ScanDataL()
00061         {
00062         // Validate that format is correct
00063         ReadFormatL();
00064         ASSERT(ImageReadCodec() == NULL);
00065 
00066         // Create a codec to read the PNG image
00067         CPngReadCodec* imageReadCodec;
00068         imageReadCodec = new(ELeave) CPngReadCodec;
00069         // Let the framework takes ownership of the codec
00070         SetImageReadCodec(imageReadCodec);
00071         imageReadCodec->ConstructL();
00072 
00073         // Fill in image information for all frames
00074         ReadFrameHeadersL();
00075         }
00076 
00077 // Validate that the file is PNG format
00078 void CPngDecoder::ReadFormatL()
00079         {
00080         // Read initial data block
00081         TPtrC8 bufferDes;
00082         ReadDataL(0, bufferDes, KPngFileSignatureLength);
00083 
00084         // Validate the header.
00085         if (bufferDes.Length() < KPngFileSignatureLength)
00086                 User::Leave(KErrUnderflow);
00087 
00088         const TUint8* ptr = bufferDes.Ptr();
00089         if (Mem::Compare(ptr, KPngFileSignatureLength, KPngSignature, KPngFileSignatureLength)!=0)
00090                 User::Leave(KErrCorrupt);
00091 
00092         // Set start position of image data following the header
00093         SetStartPosition(KPngFileSignatureLength);
00094 
00095         // Set maximum data length as don't know exactly
00096         SetDataLength(KMaxTInt);
00097         }
00098 
00099 // Gets text descriptions of image properties
00100 CFrameInfoStrings* CPngDecoder::FrameInfoStringsL(RFs& aFs, TInt aFrameNumber)
00101         {
00102         const TUid KPngCodecDllUid = {KExPNGCodecDllUidValue};
00103 
00104         // Strings are read from 101F4122_extra.rss
00105         RResourceFile resourceFile;
00106         OpenExtraResourceFileLC(aFs,KPngCodecDllUid,resourceFile);
00107         HBufC8* resourceInfo = resourceFile.AllocReadLC(THEDECODERINFO);
00108         TResourceReader resourceReader;
00109         resourceReader.SetBuffer(resourceInfo);
00110 
00111         TBuf<KCodecResourceStringMax> info;
00112         TBuf<KCodecResourceStringMax> templte;
00113 
00114         const TFrameInfo& frameInfo = FrameInfo(aFrameNumber);
00115         CFrameInfoStrings* frameInfoStrings = CFrameInfoStrings::NewLC();
00116 
00117         // Set decoder name
00118         info = resourceReader.ReadTPtrC();
00119         frameInfoStrings->SetDecoderL(info);
00120         // Set image format name
00121         info = resourceReader.ReadTPtrC();
00122         frameInfoStrings->SetFormatL(info);
00123         // Set image dimensions
00124         TInt width = frameInfo.iOverallSizeInPixels.iWidth;
00125         TInt height = frameInfo.iOverallSizeInPixels.iHeight;
00126         TInt depth = frameInfo.iBitsPerPixel;
00127         templte = resourceReader.ReadTPtrC();
00128         info.Format(templte, width, height);
00129         frameInfoStrings->SetDimensionsL(info);
00130         // Set image depth, for colour or b/w as appropriate
00131         CDesCArrayFlat* resourceArray = resourceReader.ReadDesCArrayL();
00132         CleanupStack::PushL(resourceArray);
00133         TUint formatIndex = (frameInfo.iFlags & TFrameInfo::EColor) ? 1 : 0;
00134         templte = (*resourceArray)[formatIndex];
00135         CleanupStack::PopAndDestroy(resourceArray);
00136         info.Format(templte, depth);
00137         frameInfoStrings->SetDepthL(info);
00138         // Set image details strings
00139         info = resourceReader.ReadTPtrC(); // read details, then see if we use it
00140         if (frameInfo.iFlags & TFrameInfo::EAlphaChannel && frameInfo.iFlags & TFrameInfo::EColor)
00141                 {
00142                 frameInfoStrings->SetDetailsL(info);
00143                 }
00144         // Cleanup and return
00145         CleanupStack::Pop(frameInfoStrings); 
00146         CleanupStack::PopAndDestroy(2); // resourceInfo + resourceFile
00147         return frameInfoStrings;
00148         }
00149 
00150 //
00151 // PNG encoder class
00152 //
00153 
00154 // Simple factory function
00155 CPngEncoder* CPngEncoder::NewL()
00156         {
00157         return new(ELeave) CPngEncoder;
00158         }
00159 
00160 CPngEncoder::CPngEncoder()
00161         {
00162         }
00163 
00164 // Destructor calls base class cleanup
00165 CPngEncoder::~CPngEncoder()
00166         {
00167         CImageEncoderPlugin::Cleanup();
00168         }
00169 
00170 // Sets up the codec to encode the frame
00171 void CPngEncoder::PrepareEncoderL(const CFrameImageData* aFrameImageData)
00172         {
00173         // Default encode parameters
00174         TInt bpp = 24;
00175         TBool color = ETrue;
00176         TBool paletted = EFalse;
00177         TInt compressionLevel = TPngEncodeData::EDefaultCompression;
00178 
00179         // Use encode params in aFrameImageData, if present
00180         const TInt count = (aFrameImageData) ? aFrameImageData->FrameDataCount() : 0;
00181         for (TInt i=0; i < count; i++)
00182                 {
00183                 const TFrameDataBlock* encoderData = aFrameImageData->GetFrameData(i);
00184                 if (encoderData->DataType() == KPNGEncodeDataUid)
00185                         {
00186                         const TPngEncodeData* pngEncodeData = static_cast<const TPngEncodeData*>(encoderData);
00187                         bpp = pngEncodeData->iBitsPerPixel;
00188                         color = pngEncodeData->iColor;
00189                         paletted = pngEncodeData->iPaletted;
00190                         compressionLevel = pngEncodeData->iLevel;
00191                         }
00192                 }
00193 
00194         // Create the codec to write a PNG image
00195         CPngWriteCodec* codec = new(ELeave) CPngWriteCodec(bpp, color, paletted, compressionLevel);
00196         // Let the framework takes ownership of the codec
00197         SetImageWriteCodec(codec);              
00198         codec->ConstructL();
00199         }

Generated by  doxygen 1.6.2