smsprotocols/smsstack/ems/src/EMSPictureIE.cpp
changeset 0 3553901f7fa8
child 24 6638e7f4bd8f
equal deleted inserted replaced
-1:000000000000 0:3553901f7fa8
       
     1 /*
       
     2 * Copyright (c) 2009 Sony Ericsson Mobile Communications AB
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Sony Ericsson Mobile Communications AB - initial contribution.
       
    11 * Nokia Corporation - additional changes.
       
    12 * 
       
    13 * Contributors:
       
    14 * 
       
    15 * Description:
       
    16 * Implements the Enhanced SMS Picture (Small/Large/Variable) Information Element.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 /**
       
    22  *  @file
       
    23  *  
       
    24  *  Implements  CEMSPictureIE class
       
    25  */
       
    26 
       
    27 
       
    28 #include <emspictureie.h>
       
    29 
       
    30 #include <fbs.h>
       
    31 
       
    32 CEmsPictureIE* CEmsPictureIE::NewL()
       
    33 	{
       
    34 	CEmsPictureIE* self = new (ELeave) CEmsPictureIE();
       
    35 	return self;
       
    36 	}
       
    37 
       
    38 EXPORT_C CEmsPictureIE* CEmsPictureIE::NewL(const CFbsBitmap& aBitmap)
       
    39 /**
       
    40  *  @capability None
       
    41  */
       
    42 	{
       
    43 	CEmsPictureIE* self = new (ELeave) CEmsPictureIE();
       
    44 	CleanupStack::PushL(self);
       
    45 	self->ConstructL(aBitmap);
       
    46 	CleanupStack::Pop(self);
       
    47 	return self;
       
    48 	}
       
    49 
       
    50 EXPORT_C CEmsInformationElement* CEmsPictureIE::DuplicateL() const
       
    51 /**
       
    52  *  @capability None
       
    53  */
       
    54 	{
       
    55 	CEmsPictureIE* copy = new (ELeave) CEmsPictureIE();
       
    56 	CleanupStack::PushL(copy);
       
    57 	copy->CopyL(*this);
       
    58 	CleanupStack::Pop(copy);
       
    59 	return copy;
       
    60 	}
       
    61 
       
    62 EXPORT_C TSize CEmsPictureIE::SizeInPixels() const
       
    63 /**
       
    64  *  @capability None
       
    65  */
       
    66 	{
       
    67 	return iSize;
       
    68 	}
       
    69 
       
    70 void CEmsPictureIE::ConstructL(const CFbsBitmap& aBitmap)
       
    71 	{
       
    72 	__ASSERT_ALWAYS(!iPictureBuf, User::Leave(KErrCorrupt));
       
    73 
       
    74 	iSize = aBitmap.SizeInPixels();
       
    75 	if ((iSize == KSmallPicSize) || (iSize == KLargePicSize))
       
    76 		{
       
    77 		iIdentifier  = (iSize == KSmallPicSize) ? CSmsInformationElement::ESmsEnhancedSmallPicture : CSmsInformationElement::ESmsEnhancedLargePicture;
       
    78 		iPictureBuf = CreatePictureBufferL(aBitmap);
       
    79 		iEncodedBodyLength=iSize.iHeight*iSize.iWidth/8;
       
    80 		}
       
    81 	else
       
    82 		{
       
    83 		// Make sure bitmap is a x of 8 bits wide
       
    84 		iIdentifier  = CSmsInformationElement::ESmsEnhancedVariablePicture;
       
    85 
       
    86 		TSize size = aBitmap.SizeInPixels();
       
    87 		iSize.iHeight = size.iHeight;
       
    88 		if (size.iWidth%8 != 0)
       
    89 			User::Leave(KErrCorrupt);
       
    90 		iPictureBuf = CreatePictureBufferL(aBitmap);
       
    91 
       
    92 		// The length is the picture size plus two bytes to specify
       
    93 		// the width and height
       
    94 		iEncodedBodyLength=iSize.iHeight*iSize.iWidth/8+2;
       
    95 		}
       
    96 	__ASSERT_ALWAYS(iPictureBuf->Length() <= EEnhancedMaxSize, User::Leave(KErrTooBig));
       
    97 	}
       
    98 
       
    99 HBufC8* CEmsPictureIE::CreatePictureBufferL(const CFbsBitmap& aBitmap)
       
   100 	{
       
   101 	__ASSERT_ALWAYS(aBitmap.SizeInPixels().iWidth%8==0, User::Leave(KErrCorrupt));
       
   102 
       
   103 	const TSize size = aBitmap.SizeInPixels();
       
   104 	const TInt numLineBytes = size.iWidth/8;
       
   105 	HBufC8* pictureBuf = HBufC8::NewLC(numLineBytes * size.iHeight);
       
   106 
       
   107 	HBufC8* scanLine = HBufC8::NewLC(numLineBytes);
       
   108 	TPtr8 line(scanLine->Des());
       
   109 
       
   110 	HBufC8* working = HBufC8::NewLC(numLineBytes);
       
   111 	TPtr8 convertedScanLine(working->Des());
       
   112 	convertedScanLine.SetLength(numLineBytes);
       
   113 
       
   114 	TUint8 byte;
       
   115 
       
   116 	for (TInt a=0; a<size.iHeight; a++)
       
   117 		{
       
   118 		aBitmap.GetScanLine(line, TPoint(0,a), size.iWidth, EGray2);
       
   119 
       
   120 		for (TInt word=0; word<numLineBytes; word++)
       
   121 			{
       
   122 			// Convert EMS Black=1 to Epoc Black=0
       
   123 			// and Convert little endean to big.
       
   124 			byte = 0;
       
   125 			for (TInt bit=0; bit<8; bit++)
       
   126 				byte |= (line[word] & (1<<bit)) ? 0 : (1<<(7-bit));
       
   127 
       
   128 			convertedScanLine[word] = byte;
       
   129 			}
       
   130 
       
   131 		pictureBuf->Des().Append(convertedScanLine);
       
   132 		}
       
   133 
       
   134 	CleanupStack::PopAndDestroy(2, scanLine);
       
   135 	CleanupStack::Pop(pictureBuf);
       
   136 	return pictureBuf;
       
   137 	}
       
   138 
       
   139 EXPORT_C void CEmsPictureIE::CopyL(const CEmsPictureIE& aSrc)
       
   140 /**
       
   141  *  @capability None
       
   142  */
       
   143 	{
       
   144 	__ASSERT_ALWAYS(aSrc.iPictureBuf, User::Leave(KErrCorrupt));
       
   145 	CEmsInformationElement::CopyL(aSrc);
       
   146 	iSize = aSrc.iSize;
       
   147 	delete iPictureBuf;
       
   148 	iPictureBuf=NULL;
       
   149 	iPictureBuf = aSrc.iPictureBuf->AllocL();
       
   150 	}
       
   151 
       
   152 /**
       
   153  *  Encodes the information element into its raw format. (no IE id)
       
   154  *  
       
   155  *  @param aPtr the buffer to be used which is to contain the data
       
   156  *  @param TBool boolean to indicate if it is for serialisation or encoding
       
   157  */
       
   158 void CEmsPictureIE::EncodeBodyL(TPtr8 aPtr, TBool) const
       
   159 	{
       
   160 	__ASSERT_ALWAYS(iPictureBuf, User::Leave(KErrCorrupt));
       
   161 	__ASSERT_ALWAYS(iSize.iWidth<255 && iSize.iHeight<255, User::Leave(KErrCorrupt));
       
   162 	TBool variablePicture = ((iSize != KSmallPicSize) && (iSize != KLargePicSize));
       
   163 	__ASSERT_ALWAYS((variablePicture && iIdentifier == CSmsInformationElement::ESmsEnhancedVariablePicture) ||
       
   164 		(!variablePicture && iIdentifier != CSmsInformationElement::ESmsEnhancedVariablePicture), User::Leave(KErrCorrupt));
       
   165 
       
   166 	if (variablePicture)
       
   167 		{
       
   168 		aPtr.Append(static_cast<TUint8>(iSize.iWidth/8));
       
   169 		aPtr.Append(static_cast<TUint8>(iSize.iHeight));
       
   170 		}
       
   171 
       
   172 	aPtr.Append(*iPictureBuf);
       
   173 	}
       
   174 
       
   175 
       
   176 /**
       
   177  *  Decodes the raw data out of an information element into this class.
       
   178  *  
       
   179  *  @param aPtr The raw predefined animation data
       
   180  *  @param TBool boolean to indicate if it is from serialisation
       
   181  *  @leave KErrGeneral If the size of the data does not match what is expected.
       
   182  */
       
   183 void CEmsPictureIE::DecodeBodyL(const TPtrC8 aPtr, TBool)
       
   184 	{
       
   185 	switch (iIdentifier)
       
   186 		{
       
   187 		case CSmsInformationElement::ESmsEnhancedSmallPicture:
       
   188 			__ASSERT_ALWAYS(aPtr.Length()==ESmallBufSize, User::Leave(KErrGeneral));
       
   189 			iSize = KSmallPicSize;
       
   190 			break;
       
   191 		case CSmsInformationElement::ESmsEnhancedLargePicture:
       
   192 			__ASSERT_ALWAYS(aPtr.Length()==ELargeBufSize, User::Leave(KErrGeneral));
       
   193 			iSize = KLargePicSize;
       
   194 			break;
       
   195 		case CSmsInformationElement::ESmsEnhancedVariablePicture:
       
   196 			iSize.iWidth = static_cast<TUint8>(aPtr[0]) * 8;
       
   197 			iSize.iHeight = static_cast<TUint8>(aPtr[1]);
       
   198 			__ASSERT_ALWAYS(aPtr.Length()==(iSize.iWidth/8*iSize.iHeight)+2, User::Leave(KErrGeneral));
       
   199 			break;
       
   200 		default:
       
   201 			User::Leave(KErrArgument);
       
   202 		}
       
   203 
       
   204 	delete iPictureBuf;
       
   205 	iPictureBuf = NULL;
       
   206 	if (iIdentifier == CSmsInformationElement::ESmsEnhancedVariablePicture)
       
   207 		iPictureBuf = aPtr.Mid(2).AllocL();
       
   208 	else
       
   209 		iPictureBuf = aPtr.AllocL();
       
   210 	}
       
   211 
       
   212 CEmsPictureIE::CEmsPictureIE() : CEmsInformationElement(CSmsInformationElement::ESmsEnhancedSmallPicture){};
       
   213 
       
   214 EXPORT_C CEmsPictureIE::~CEmsPictureIE()
       
   215 /**
       
   216  *  @capability None
       
   217  */
       
   218 	{
       
   219 	delete iPictureBuf;
       
   220 	}
       
   221 
       
   222 
       
   223 
       
   224 EXPORT_C CFbsBitmap* CEmsPictureIE::GetBitmapL() const
       
   225 /**
       
   226  *  @capability None
       
   227  */
       
   228 	{
       
   229 
       
   230 	CFbsBitmap* bitmap = new (ELeave) CFbsBitmap();
       
   231 	CleanupStack::PushL(bitmap);
       
   232 
       
   233 	TInt numLineBytes = iSize.iWidth/8;
       
   234 	User::LeaveIfError(bitmap->Create(iSize,EGray2));
       
   235 	HBufC8* working = HBufC8::NewLC(numLineBytes);
       
   236 	TPtr8 convertedScanLine(working->Des());
       
   237 	convertedScanLine.SetLength(numLineBytes);
       
   238 
       
   239 	TUint8 byte = 0;
       
   240 	TUint8* adr=NULL;
       
   241 
       
   242 	for (TInt line =0; line < iSize.iHeight; line++)
       
   243 		{
       
   244 		adr = &((iPictureBuf->Des())[(numLineBytes) * line]);
       
   245 		TPtr8  scanLine(adr, numLineBytes, numLineBytes);
       
   246 
       
   247 		for (TInt word=0; word<numLineBytes; word++)
       
   248 			{
       
   249 			// Convert EMS Black=1 to Epoc Black=0
       
   250 			// and Convert little endean to big.
       
   251 			byte=0;
       
   252 			for (TUint bit=0; bit<8; bit++)
       
   253 				byte |= (scanLine[word] & (1<<bit)) ? 0 : (1<<(7-bit));
       
   254 			convertedScanLine[word] = byte;
       
   255 			}
       
   256 
       
   257 		bitmap->SetScanLine(convertedScanLine, line);
       
   258 		}
       
   259 	CleanupStack::PopAndDestroy(working);
       
   260 	CleanupStack::Pop(bitmap);
       
   261 	return bitmap;
       
   262 	}