Click here to Skip to main content
15,892,199 members
Articles / Desktop Programming / MFC

ImageStone - A Powerful C++ Class Library for Image Manipulation

Rate me:
Please Sign up or sign in to vote.
4.81/5 (250 votes)
6 Dec 2011Zlib3 min read 1.1M   51.5K   405  
An article on a library for image manipulation
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>ImageStone: Rle.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Generated by Doxygen 1.4.1 -->
<div class="qindex"><a class="qindex" href="index.html">Main&nbsp;Page</a> | <a class="qindex" href="hierarchy.html">Class&nbsp;Hierarchy</a> | <a class="qindex" href="annotated.html">Class&nbsp;List</a> | <a class="qindex" href="files.html">File&nbsp;List</a> | <a class="qindex" href="functions.html">Class&nbsp;Members</a></div>
<h1>Rle.h</h1><div class="fragment"><pre class="fragment">00001 <span class="comment">/*</span>
00002 <span class="comment"> *   Copyright (C) =USTC= Fu Li</span>
00003 <span class="comment"> *</span>
00004 <span class="comment"> *   Author   :  Fu Li</span>
00005 <span class="comment"> *   Create   :  2000-9-6</span>
00006 <span class="comment"> *   Home     :  http://www.crazy-bit.com/</span>
00007 <span class="comment"> *   Mail     :  crazybit@263.net</span>
00008 <span class="comment"> *   History  :  </span>
00009 <span class="comment"> */</span>
00010 <span class="preprocessor">#ifndef __PCL_COMPRESS__2000_09_06__H__</span>
00011 <span class="preprocessor"></span><span class="preprocessor">#define __PCL_COMPRESS__2000_09_06__H__</span>
00012 <span class="preprocessor"></span><span class="preprocessor">#include "../FColor.h"</span>
00013 
00014 <span class="comment">//=============================================================================</span>
<a name="l00018"></a><a class="code" href="class_f_c_compress.html">00018</a> <span class="comment"></span><span class="keyword">class </span><a class="code" href="class_f_c_compress.html">FCCompress</a>
00019 {
00020 <span class="keyword">public</span>:
00025     <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="class_f_c_compress.html#e0">RLE_PCX_Encode</a> (<span class="keyword">const</span> BYTE* pInBuffer, <span class="keywordtype">int</span> nInSize, BYTE* pOutBuffer) ;
00030     <span class="keyword">static</span> <span class="keywordtype">int</span> <a class="code" href="class_f_c_compress.html#e1">RLE_PCX_Decode</a> (<span class="keyword">const</span> BYTE* pInBuffer, <span class="keywordtype">int</span> nInSize, BYTE* pOutBuffer) ;
00031 
00038     <span class="keyword">static</span> BYTE* <a class="code" href="class_f_c_compress.html#e2">RLE_TGA_EncodeLine</a> (<span class="keyword">const</span> BYTE* pInBuffer, <span class="keywordtype">int</span> iColorBit, <span class="keywordtype">int</span> iNumPixel, BYTE* pOutBuffer) ;
00045     <span class="keyword">static</span> BYTE* <a class="code" href="class_f_c_compress.html#e3">RLE_TGA_DecodeLine</a> (<span class="keyword">const</span> BYTE* pInBuffer, <span class="keywordtype">int</span> iColorBit, <span class="keywordtype">int</span> iNumPixel, BYTE* pOutBuffer) ;
00046 };
00047 
00048 <span class="comment">//=============================================================================</span>
00049 <span class="comment">// inline Implement</span>
00050 <span class="comment">//=============================================================================</span>
<a name="l00051"></a><a class="code" href="class_f_c_compress.html#e0">00051</a> <span class="keyword">inline</span> <span class="keywordtype">int</span> <a class="code" href="class_f_c_compress.html#e0">FCCompress::RLE_PCX_Encode</a> (<span class="keyword">const</span> BYTE* pInBuffer, <span class="keywordtype">int</span> nInSize, BYTE* pOutBuffer)
00052 {
00053     <span class="keyword">const</span> BYTE   * pOutStart = pOutBuffer ;
00054     <span class="keywordflow">while</span> (nInSize-- &gt; 0)
00055     {
00056         <span class="keyword">const</span> BYTE   byData = *pInBuffer++ ;
00057         BYTE         cCount = 1 ;
00058         <span class="keywordflow">while</span> ( (cCount &lt; 0x3F) &amp;&amp; (nInSize != 0) )
00059             <span class="keywordflow">if</span> (*pInBuffer != byData) <span class="comment">// Stat. the repeat BYTE</span>
00060                 break ;
00061             <span class="keywordflow">else</span>
00062             {
00063                 cCount++ ; pInBuffer++ ; nInSize-- ;
00064             }
00065 
00066         <span class="keywordflow">if</span> (cCount == 1) <span class="comment">// unique</span>
00067         {
00068             <span class="keywordflow">if</span> ( byData &gt;= 0xC0 ) <span class="comment">// Data &gt;= 0xC0</span>
00069             {
00070                 *pOutBuffer++ = 0xC1 ;
00071                 *pOutBuffer++ = byData ;
00072             }
00073             <span class="keywordflow">else</span>
00074                 *pOutBuffer++ = byData ; <span class="comment">// Data &lt; 0xC0, write directly</span>
00075         }
00076         <span class="keywordflow">else</span> <span class="comment">// repeat</span>
00077         {
00078             *pOutBuffer++ = 0xC0 | cCount ;
00079             *pOutBuffer++ = byData ;
00080         }
00081     }
00082     <span class="keywordflow">return</span> (<span class="keywordtype">int</span>)(pOutBuffer - pOutStart) ;
00083 }
00084 <span class="comment">//-----------------------------------------------------------------------------</span>
<a name="l00085"></a><a class="code" href="class_f_c_compress.html#e1">00085</a> <span class="keyword">inline</span> <span class="keywordtype">int</span> <a class="code" href="class_f_c_compress.html#e1">FCCompress::RLE_PCX_Decode</a> (<span class="keyword">const</span> BYTE* pInBuffer, <span class="keywordtype">int</span> nInSize, BYTE* pOutBuffer)
00086 {
00087     <span class="keyword">const</span> BYTE   * pOutStart = pOutBuffer ;
00088     <span class="keywordflow">while</span> (nInSize-- &gt; 0)
00089     {
00090         <span class="keyword">const</span> BYTE   byData = *pInBuffer++ ; <span class="comment">// read byte and move ptr to next</span>
00091         <span class="keywordflow">if</span> ( byData &gt;= 0xC0 )
00092         {
00093             <span class="comment">// error : the inbuffer has been exhausted.</span>
00094             <span class="keywordflow">if</span> (nInSize &lt;= 0)
00095             {
00096                 assert(<span class="keyword">false</span>) ; <span class="keywordflow">goto</span> rleOver;
00097             }
00098 
00099             BYTE     cNum = byData &amp; 0x3F ; <span class="comment">// repeat current byte Num</span>
00100             ::memset (pOutBuffer, *pInBuffer++, cNum) ; <span class="comment">// memset func will check "Num" =? 0</span>
00101             pOutBuffer += cNum ;
00102             nInSize-- ;
00103         }
00104         <span class="keywordflow">else</span>
00105             *pOutBuffer++ = byData ;
00106     }
00107 rleOver:
00108     <span class="keywordflow">return</span> (<span class="keywordtype">int</span>)(pOutBuffer - pOutStart) ;
00109 }
00110 <span class="comment">//-----------------------------------------------------------------------------</span>
<a name="l00111"></a><a class="code" href="class_f_c_compress.html#e2">00111</a> <span class="keyword">inline</span> BYTE* <a class="code" href="class_f_c_compress.html#e2">FCCompress::RLE_TGA_EncodeLine</a> (<span class="keyword">const</span> BYTE* InBuffer, <span class="keywordtype">int</span> iColorBit, <span class="keywordtype">int</span> iNumPixel, BYTE* OutBuffer)
00112 {
00113     iColorBit /= 8 ; <span class="comment">// convert to bytes : 1,2,3,4</span>
00114     <span class="keywordflow">while</span> (iNumPixel &gt; 0)
00115     {
00116         DWORD      Data = 0, Next = 0, Count = 1 ;
00117         <a class="code" href="class_f_c_color.html#e3">FCColor::CopyPixel</a> (&amp;Data, InBuffer, iColorBit) ; <span class="comment">// first pixel</span>
00118         InBuffer += iColorBit ; iNumPixel-- ;
00119         <span class="keywordflow">while</span> ((Count &lt; 0x80) &amp;&amp; (iNumPixel &gt; 0)) <span class="comment">// Stat. the repeat pixel</span>
00120         {
00121             <a class="code" href="class_f_c_color.html#e3">FCColor::CopyPixel</a> (&amp;Next, InBuffer, iColorBit) ; <span class="comment">// next pixel</span>
00122             <span class="keywordflow">if</span> (Next != Data)
00123                 break ;
00124             InBuffer += iColorBit ; iNumPixel-- ; Count++ ;
00125         }
00126 
00127         *OutBuffer++ = 0x80 | (BYTE)--Count ;
00128         <a class="code" href="class_f_c_color.html#e3">FCColor::CopyPixel</a> (OutBuffer, &amp;Data, iColorBit) ;
00129         OutBuffer += iColorBit ;
00130     }
00131     <span class="keywordflow">return</span> OutBuffer ;
00132 }
00133 <span class="comment">//-----------------------------------------------------------------------------</span>
<a name="l00134"></a><a class="code" href="class_f_c_compress.html#e3">00134</a> <span class="keyword">inline</span> BYTE* <a class="code" href="class_f_c_compress.html#e3">FCCompress::RLE_TGA_DecodeLine</a> (<span class="keyword">const</span> BYTE* InBuffer, <span class="keywordtype">int</span> iColorBit, <span class="keywordtype">int</span> iNumPixel, BYTE* OutBuffer)
00135 {
00136     iColorBit /= 8 ; <span class="comment">// convert to bytes : 1,2,3,4</span>
00137     <span class="keywordflow">while</span> (iNumPixel &gt; 0)
00138     {
00139         <span class="keyword">const</span> BYTE    byData = *InBuffer++ ; <span class="comment">// Next Byte</span>
00140         <span class="keywordflow">if</span> (byData &amp; 0x80) <span class="comment">// Data &gt;= 0x80</span>
00141         {
00142             <span class="keyword">const</span> <span class="keywordtype">int</span>    nNum = (byData &amp; 0x7F) + 1 ; <span class="comment">// number of repeat pixel</span>
00143             iNumPixel -= nNum ;
00144             <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i=0 ; i &lt; nNum ; i++, OutBuffer += iColorBit)
00145                 <a class="code" href="class_f_c_color.html#e3">FCColor::CopyPixel</a> (OutBuffer, InBuffer, iColorBit) ;
00146             InBuffer += iColorBit ;
00147         }
00148         <span class="keywordflow">else</span>
00149         {
00150             <span class="comment">// copy directly</span>
00151             <span class="keyword">const</span> <span class="keywordtype">int</span>   n = byData + 1, <span class="comment">// non-repeat pixel</span>
00152                         nByte = n * iColorBit ; <span class="comment">// calculate copy bytes</span>
00153             iNumPixel -= n ;
00154             ::memcpy (OutBuffer, InBuffer, nByte) ;
00155             OutBuffer += nByte ;
00156             InBuffer += nByte ;
00157         }
00158     }
00159     assert (iNumPixel == 0) ;
00160     <span class="keywordflow">return</span> const_cast&lt;BYTE*&gt;(InBuffer) ;
00161 }
00162 
00163 <span class="preprocessor">#endif</span>
</pre></div><hr size="1"><address style="align: right;"><small>Generated on Wed Mar 8 11:12:47 2006 for ImageStone by&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.1 </small></address>
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The zlib/libpng License


Written By
Team Leader PhoXo
China China
graduate from University of Science and Technology of China at 2002.

Now I work at www.phoxo.com.

Comments and Discussions