mmplugins/imagingplugins/codecs/WBMPCodec/WBMPConvert.cpp
changeset 0 40261b775718
equal deleted inserted replaced
-1:000000000000 0:40261b775718
       
     1 // Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include <barsc.h>
       
    17 #include <barsread.h>
       
    18 #include <bautils.h>
       
    19 #include <imageconversion.h>
       
    20 #include "ImageClientMain.h"
       
    21 #include <101F45CD_extra.rsg>
       
    22 #include "icl/ICL_UIDS.hrh"
       
    23 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
       
    24 #include <icl/icl_uids_const.hrh>
       
    25 #include <icl/icl_uids_def.hrh>
       
    26 #include <icl/imagecodecdef.h>
       
    27 #endif
       
    28 #include "WBMPCodec.h"
       
    29 
       
    30 // Constants.
       
    31 const TInt KWbmpMaxFileHeaderSize = 12; // Max possible header size for TInt32 width and height values
       
    32 const TInt KWbmpMinFileHeaderSize = 4;	// Min possible header size for TUint8 width and height values
       
    33 
       
    34 _LIT(KWBMPPanicCategory, "WBMPConvertPlugin");
       
    35 
       
    36 // Global panic function
       
    37 GLDEF_C void Panic(TIclPanic aError)
       
    38 	{
       
    39 	User::Panic(KWBMPPanicCategory, aError);
       
    40 	}
       
    41 
       
    42 
       
    43 // Wbmp decoder.
       
    44 CWbmpDecoder* CWbmpDecoder::NewL()
       
    45 	{
       
    46 	return new(ELeave) CWbmpDecoder;
       
    47 	}
       
    48 
       
    49 CWbmpDecoder::CWbmpDecoder()
       
    50 	{}
       
    51 
       
    52 CWbmpDecoder::~CWbmpDecoder()
       
    53 	{
       
    54 	Cleanup();
       
    55 	}
       
    56 
       
    57 void CWbmpDecoder::ImageType(TInt aFrameNumber, TUid& aImageType, TUid& aImageSubType) const
       
    58 	{
       
    59 	__ASSERT_ALWAYS(aFrameNumber == 0, Panic(EFrameNumberOutOfRange));
       
    60 	aImageType = KImageTypeWBMPUid;
       
    61 	aImageSubType = KNullUid;
       
    62 	}
       
    63 
       
    64 // Scan header.
       
    65 // Validate that format is correct.
       
    66 // Create codec.
       
    67 // Fill in image info. (All frames)
       
    68 void CWbmpDecoder::ScanDataL()
       
    69 	{
       
    70 	ReadFormatL();
       
    71 
       
    72 	ASSERT(ImageReadCodec() == NULL);
       
    73 
       
    74 	CWbmpReadCodec* imageReadCodec = CWbmpReadCodec::NewL(iSize);
       
    75 
       
    76 	SetImageReadCodec(imageReadCodec);
       
    77 	
       
    78 	ReadFrameHeadersL();
       
    79 	}
       
    80 
       
    81 void CWbmpDecoder::ReadFormatL()
       
    82 	{
       
    83 	TPtrC8 bufferDes;
       
    84 
       
    85 	ReadDataL(0, bufferDes, KWbmpMaxFileHeaderSize);
       
    86 
       
    87 	// Validate the header.
       
    88 	if (bufferDes.Length() < KWbmpMinFileHeaderSize)
       
    89 		User::Leave(KErrUnderflow);
       
    90 
       
    91 	const TUint8* ptr = &bufferDes[0];
       
    92 	const TUint8* startPtr = ptr;
       
    93 	const TUint8* endPtr = startPtr + bufferDes.Length();
       
    94 
       
    95 	TUint8 typeField = *ptr++;
       
    96 	if (typeField != 0)
       
    97 		User::Leave(KErrNotSupported);
       
    98 
       
    99 	// TUint8 fixHeaderField = *ptr++;
       
   100 	ptr++; // Skip fixHeaderField 
       
   101 
       
   102 	iSize.iWidth = ReadContinuedValueL(ptr, endPtr);
       
   103 	iSize.iHeight = ReadContinuedValueL(ptr, endPtr);
       
   104 
       
   105 	TInt headerSize = ptr - startPtr;
       
   106 	SetStartPosition(headerSize); // KWbmpFileHeaderSize ??
       
   107 
       
   108 	SetDataLength(headerSize + (((iSize.iWidth + 7) / 8) * iSize.iHeight));
       
   109 
       
   110 	TFrameInfo imageInfo;
       
   111 	imageInfo = ImageInfo();
       
   112 	imageInfo.iFrameCoordsInPixels.SetRect(TPoint(0, 0), iSize);
       
   113 	imageInfo.iOverallSizeInPixels = iSize;
       
   114 	imageInfo.iFrameSizeInTwips = TSize(0, 0);
       
   115 	imageInfo.iBitsPerPixel = 1;
       
   116 	imageInfo.iDelay = 0;
       
   117 	imageInfo.iFlags = 0;
       
   118 	imageInfo.iFrameDisplayMode = EGray2;
       
   119 	SetImageInfo(imageInfo);
       
   120 	}
       
   121 
       
   122 CFrameInfoStrings* CWbmpDecoder::FrameInfoStringsL(RFs& aFs, TInt aFrameNumber)
       
   123 	{
       
   124 
       
   125 	const TUid KWbmpCodecDllUid = {KWBMPCodecDllUidValue};
       
   126 
       
   127 	RResourceFile resourceFile;
       
   128 	OpenExtraResourceFileLC(aFs,KWbmpCodecDllUid,resourceFile);
       
   129 
       
   130 	HBufC8* resourceInfo = resourceFile.AllocReadLC(THEDECODERINFO);
       
   131 	TResourceReader resourceReader;
       
   132 	resourceReader.SetBuffer(resourceInfo);
       
   133 
       
   134 	TBuf<KCodecResourceStringMax> info;
       
   135 	TBuf<KCodecResourceStringMax> templte;
       
   136 
       
   137 	const TFrameInfo& frameInfo = FrameInfo(aFrameNumber);
       
   138 	CFrameInfoStrings* frameInfoStrings = CFrameInfoStrings::NewLC();
       
   139 
       
   140 	info = resourceReader.ReadTPtrC();
       
   141 	frameInfoStrings->SetDecoderL(info);
       
   142 
       
   143 	info = resourceReader.ReadTPtrC();
       
   144 	frameInfoStrings->SetFormatL(info);
       
   145 
       
   146 	TInt width = frameInfo.iOverallSizeInPixels.iWidth;
       
   147 	TInt height = frameInfo.iOverallSizeInPixels.iHeight;
       
   148 
       
   149 	templte = resourceReader.ReadTPtrC();
       
   150 	info.Format(templte, width, height);
       
   151 	frameInfoStrings->SetDimensionsL(info);
       
   152 
       
   153 	info = resourceReader.ReadTPtrC(); // note depth is fixed
       
   154 	frameInfoStrings->SetDepthL(info);
       
   155 
       
   156 	// leave details blank
       
   157 
       
   158 	CleanupStack::Pop(frameInfoStrings); 
       
   159 	CleanupStack::PopAndDestroy(2); // resourceInfo + resourceFile
       
   160 	return frameInfoStrings;
       
   161 	}
       
   162 
       
   163 
       
   164 TInt CWbmpDecoder::ReadContinuedValueL(const TUint8*& aPtr, const TUint8* aEndPtr)
       
   165 	{
       
   166 	TUint32 value = 0;
       
   167 	TInt concatenation;
       
   168     TInt bytecount = 0;
       
   169 	do
       
   170 		{
       
   171 		if (aPtr == aEndPtr)
       
   172 			{
       
   173 			User::Leave(KErrUnderflow);
       
   174 			}
       
   175 		
       
   176 		if ( bytecount > sizeof(TUint32) )
       
   177 			{
       
   178 				User::Leave(KErrCorrupt);
       
   179 				break;
       
   180 			}
       
   181 		value <<= 7;
       
   182 		value |= (aPtr[0] & 0x7f);
       
   183 		if ( value > KWBmpMaxImageSize )
       
   184 				{
       
   185 				User::Leave(KErrCorrupt);
       
   186 				break;
       
   187 				}
       
   188 		concatenation = (aPtr[0] & 0x80);
       
   189 		aPtr++;
       
   190 		bytecount++;
       
   191 		}
       
   192 	while (concatenation);
       
   193 	if ( value > KWBmpMaxImageSize )
       
   194 			{
       
   195 			User::Leave(KErrCorrupt);
       
   196 			}
       
   197 	return value;
       
   198 	}
       
   199 
       
   200 
       
   201