rle_encoder.tpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2008 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
00024 */
00030 #include <cassert>
00031 
00032 /*----------------------------------------------------------------------------*/
00041 template<typename OutputBuffer >
00042 template<typename Iterator>
00043 void claw::rle_encoder<OutputBuffer>::encode( Iterator first, Iterator last,
00044                                               output_buffer_type& output ) const
00045 {
00046   const unsigned int max_encodable = output.max_encodable();
00047   const unsigned int min_interesting = output.min_interesting();
00048   raw_buffer_type raw_buffer;
00049 
00050   assert( max_encodable > 0 );
00051 
00052   while (first != last)
00053     {
00054       unsigned int count = 1;
00055       pattern_type pattern = *first;
00056       Iterator saved_it = first;
00057       ++first;
00058       bool ok = true;
00059 
00060       // try to find enough similar data
00061       while ( ok && (first != last) && (count < max_encodable) )
00062         if (*first == pattern)
00063           {
00064             ++count;
00065             ++first;
00066           }
00067         else
00068           ok = false;
00069 
00070       // if we have enough data
00071       if ( count >= min_interesting )
00072         {
00073           if ( !raw_buffer.empty() )
00074             {
00075               output.raw( raw_buffer.begin(), raw_buffer.end() );
00076               raw_buffer.clear();
00077             }
00078 
00079           output.encode( count, pattern );
00080         }
00081       else
00082         raw_buffer.insert( raw_buffer.end(), saved_it, first );
00083     }
00084 
00085   
00086   if ( !raw_buffer.empty() )
00087     output.raw( raw_buffer.begin(), raw_buffer.end() );
00088 } // rle_encoder::encode()

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1