Ejemplo n.º 1
0
/**
Generate a Huffman code.

This generates a Huffman code for a given set of code frequencies. The output is a table of
code lengths which can be used to build canonincal encoding tables or decoding trees for use
with the TBitInput and TBitOutput classes.

Entries in the table with a frequency of zero will have a zero code length and thus no
associated huffman encoding. If each such symbol should have a maximum length encoding, they
must be given at least a frequency of 1.

For an alphabet of n symbols, this algorithm has a transient memory overhead of 8n, and a
time complexity of O(n*log(n)).

@param "const TUint32 aFrequency[]" The table of code frequencies
@param "TInt aNumCodes" The number of codes in the table
@param "TUint32 aHuffman[]" The table for the output code-length table. This must be the same
size as the frequency table, and can safely be the same table

@leave "KErrNoMemory" If memory used for code generation cannot be allocated
@panic "USER ???" If the number of codes exceeds Huffman::KMaxCodes
*/
void Huffman::HuffmanL(const TUint32 aFrequency[],TInt aNumCodes,TUint32 aHuffman[])
{
	if(TUint(aNumCodes)>TUint(KMaxCodes))
		throw E32ImageCompressionError(E32ImageCompressionError::HUFFMANTOOMANYCODESERROR);

	// Sort the values into decreasing order of frequency
	TNode* nodes = new TNode[aNumCodes];

	TInt lCount=0;

	for (TInt ii=0;ii<aNumCodes;++ii)
	{
		TInt c=aFrequency[ii];
		if (c!=0)
			InsertInOrder(nodes,lCount++,c,ii|KLeaf);
	}

	// default code length is zero
	memset(aHuffman,0,aNumCodes*sizeof(TUint32));

	if (lCount==0)
	{
		// no codes with frequency>0. No code has a length
	}
	else if (lCount==1)
	{
		// special case for a single value (always encode as "0")
		aHuffman[nodes[0].iRight&~KLeaf]=1;
	}
	else
	{
		// Huffman algorithm: pair off least frequent nodes and reorder
		do
		{
			--lCount;
			TUint c=nodes[lCount].iCount + nodes[lCount-1].iCount;
			nodes[lCount].iLeft=nodes[lCount-1].iRight;
			// re-order the leaves now to reflect new combined frequency 'c'
			InsertInOrder(nodes,lCount-1,c,lCount);
		} while (lCount>1);
		// generate code lengths in aHuffman[]
		HuffmanLengthsL(aHuffman,nodes,1,0);
	}

	delete [] nodes;

	if(!IsValid(aHuffman,aNumCodes))
		throw E32ImageCompressionError(E32ImageCompressionError::HUFFMANINVALIDCODINGERROR);
}
Ejemplo n.º 2
0
int RTFcolourtbl::AddColour (RTFcolourdef& cd)
{
	// Only add if not already present.
	int iSize = (x64_int_cast)the_RTFcolourdef.size();
	
	if((iSize == 0) && (!cd.IsAuto()))
	{
		RTFcolourdef cda;
		cda.MakeAuto();
		InsertInOrder(cda);
		return 0;
	}
	
	for(int i = 0; i < iSize; i++)
	{
		if(cd == the_RTFcolourdef[i])
			return i;
	}
	InsertInOrder(cd);
	return iSize;		
}
Ejemplo n.º 3
0
ERTFToken RTFcolourtbl::ReadToken(ERTFToken Token, RTFParser* Parser)
{
	switch(Token)
	{
 	case ';': 
		
		InsertInOrder(cd);		
		cd.MakeAuto();
		
		break;
		
	default:
		return cd.ReadToken(Token, Parser);
	}
	return rtferr_OK;
}
Ejemplo n.º 4
0
void CUrlRichEditCtrl::ParseAndFormatText(BOOL bForceReformat)
{
	KillTimer(TIMER_REPARSE);
	AF_NOREENTRANT // prevent reentrancy
		
	// parse the control content
	CString sText;
	GetWindowText(sText);
	
	// richedit2 uses '\r\n' whereas richedit uses just '\n'
	if (!CWinClasses::IsClass(*this, WC_RICHEDIT))
		sText.Replace(_T("\r\n"), _T("\n"));
	
	// parse the text into an array of URLPOS
	CUrlArray aUrls;
	LPCTSTR szText = sText;
	BOOL bPrevDelim = TRUE;
	int nPos = 0;
	BOOL bBracedFile = FALSE;
	
	while (*szText) 
	{
		// if nChar < 0 then its a multibyte char and can't be part
		// of a url, so we bump the text buffer by 2 but the pos by 1
#ifndef _UNICODE
		TCHAR nChar = *szText;
		
		if (IsDBCSLeadByte(nChar))
		{
			szText++; 
			szText++;
			nPos++;
			continue;
		}
#endif
		// if the previous item was not a delimiter then there's no
		// point checking for a protocol match so we just update the
		// value of bPrevDelim for the current char
		if (!bPrevDelim)
		{
			bPrevDelim = IsDelim(szText);
			szText++;
			nPos++;
			continue;
		}
		// if the current char is a delim then this can't be the start
		// of a url either
		else if (IsDelim(szText))
		{
			bPrevDelim = TRUE;
			szText++;
			nPos++;
			continue;
		}
		
		// now check for a protocol
		int nProt = MatchProtocol(szText);
		
		// if no match then increment pos and go to next char
		if (nProt == -1)
		{
			bPrevDelim = FALSE;
			szText++;
			nPos++;
			continue;
		}
		
		// check for braces (<...>)
		if (nPos > 0)
			bBracedFile = (szText[-1] == '<');
		else
			bBracedFile = FALSE;
		
		// find the end of the url (URLDELIMS)
		int nLen = 0;
		LPCTSTR szStart = szText;
		
		if (bBracedFile)
		{
			while (*szText && *szText != '>')
			{
				szText++;
				nLen++;
			}
		}
		else
		{
			while (!IsDelim(szText))
			{
				szText++;
				nLen++;
			}
		}
		
		bPrevDelim = TRUE;
		
		// save the result
		URLITEM urli;
		urli.cr.cpMin = nPos;
		urli.cr.cpMax = urli.cr.cpMin + nLen;
		nPos += nLen;
		
		// make sure the url does not end in a punctuation mark
		while (ENDPUNCTUATION.Find(szStart[nLen - 1]) != -1)
		{
			nLen--;
			urli.cr.cpMax--;
		}
		
		// Only save if the link is more than just the protocol
		if (nLen > m_aProtocols[nProt].sProtocol.GetLength())
		{
			urli.sUrl = CString(szStart, nLen);
			urli.bWantNotify = m_aProtocols[nProt].bWantNotify;
			
			InsertInOrder(urli, aUrls);
		}
	}
	
	// compare aUrls with m_aUrls to see if anything has changed
	BOOL bReformat = !sText.IsEmpty() && (bForceReformat || !UrlsMatch(aUrls));
	
	// then overwrite (always)	
	m_aUrls.Copy(aUrls);
	
	if (bReformat)
	{
		BOOL bVisible = IsWindowVisible();
		CRePauseUndo rep(*this);
		
		if (bVisible)
			SetRedraw(FALSE);
		
		// save current selection
		CHARRANGE crSel;
		GetSel(crSel);
		
		// and first line
		int nFirstLine = GetFirstVisibleLine();
		
		// save/reset event mask
		DWORD dwEventMask = SetEventMask(0);
		
		// retrieve default character attribs
		CHARFORMAT cf;
		cf.cbSize = sizeof(cf);
		cf.dwMask = CFM_LINK;
		
		// format urls
		int nUrls = aUrls.GetSize();
		CHARRANGE cr = { 0, 0 };
		
		for (int nUrl = 0; nUrl < nUrls; nUrl++)
		{
			// clear formatting from the end of the previous
			// url to the start of this url
			cr.cpMax = aUrls[nUrl].cr.cpMin;
			cf.dwEffects = 0;
			
			SetSel(cr);
			SetSelectionCharFormat(cf);
			
			// update for next url
			cr.cpMin = aUrls[nUrl].cr.cpMax;
			
			// then format url
			cf.dwEffects = CFM_LINK;
			
			SetSel(aUrls[nUrl].cr);
			SetSelectionCharFormat(cf);
		}	
		
		// clear formatting for whatever's left
		cr.cpMax = -1;
		cf.dwEffects = 0;
		
		SetSel(cr);
		SetSelectionCharFormat(cf);
		
		// restore selection
		SetSel(crSel);
		
		// and first line
		SetFirstVisibleLine(nFirstLine);
		
		// restore event mask
		SetEventMask(dwEventMask);

		if (bVisible)
		{
			SetRedraw(TRUE);
			Invalidate(FALSE);
		}
	}
}