Main Page | Modules | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | Related Pages | Examples

lz77comp.h

00001 //  Copyright (c) 1996-2002 by Autodesk, Inc.
00002 //
00003 //  By using this code, you are agreeing to the terms and conditions of
00004 //  the License Agreement included in the documentation for this code.
00005 //
00006 //  AUTODESK MAKES NO WARRANTIES, EXPRESS OR IMPLIED, AS TO THE CORRECTNESS
00007 //  OF THIS CODE OR ANY DERIVATIVE WORKS WHICH INCORPORATE IT. AUTODESK
00008 //  PROVIDES THE CODE ON AN "AS-IS" BASIS AND EXPLICITLY DISCLAIMS ANY
00009 //  LIABILITY, INCLUDING CONSEQUENTIAL AND INCIDENTAL DAMAGES FOR ERRORS,
00010 //  OMISSIONS, AND OTHER PROBLEMS IN THE CODE.
00011 //
00012 //  Use, duplication, or disclosure by the U.S. Government is subject to
00013 //  restrictions set forth in FAR 52.227-19 (Commercial Computer Software
00014 //  Restricted Rights) and DFAR 252.227-7013(c)(1)(ii) (Rights in Technical
00015 //  Data and Computer Software), as applicable.
00016 //
00017 
00018 #if !defined LZ77COMP_HEADER
00019 #define LZ77COMP_HEADER
00020 
00021 #include "whiptk/whipcore.h"
00022 #include "whiptk/fifo.h"
00023 #include "whiptk/file.h"
00024 #include "whiptk/lzdefs.h"
00025 
00026 #ifdef  DWFCORE_BUILD_ZLIB
00027 #include "dwfcore/zlib/zlib.h"
00028 #else
00029 #include <zlib.h>
00030 #endif
00031 
00032 #define WD_LZ_BYTES_USED_FOR_HASH                4
00033 #define WD_MAX_HASH_ENTRIES_TO_SEARCH            64
00034 #define WD_LZ_HASH_TABLE_SIZE                    65536
00035 #define WD_MAXIMUM_MATCH_LENGTH                  (255 + 15 + WD_LZ_COMPRESSION_AND_OFFSET_CODE_BYTES)
00036 #define WD_MAX_LITERAL_DATA_STREAM               (255 + 15)
00037 
00039 
00040 class WT_History_Item
00041 {
00042 private:
00043     WT_Byte                 m_value;
00044     WT_History_Item *       m_next;
00045     WT_History_Item **      m_backlink;
00046 
00047 /* FIXME:  The copy constructor and assignment operators should not be public, but private!
00048    Unfortunately, when private, we generate the following error:
00049 
00050     ../../inc\whiptk\fifo.h(155) : error C2248: 'WT_History_Item::operator`='' : cannot access private member declared in class 'WT_History_Item'
00051             ../../inc\whiptk\lz77comp.h(56) : see declaration of 'WT_History_Item::operator`=''
00052             ../../inc\whiptk\lz77comp.h(36) : see declaration of 'WT_History_Item'
00053             ../../inc\whiptk\fifo.h(54) : while compiling class-template member function 'WT_Result WT_FIFO<_ItemType>::add(int,const _ItemType *)'
00054             with
00055             [
00056                 _ItemType=WT_History_Item
00057             ]
00058             ../../inc\whiptk\lz77comp.h(103) : see reference to class template instantiation 'WT_FIFO<_ItemType>' being compiled
00059             with
00060             [
00061                 _ItemType=WT_History_Item
00062             ]
00063     Alternatively, a proper definition of these members could be supplied, although it's difficult to ascertain what that should be.
00064 */
00065 public:
00066     WT_History_Item (WT_History_Item const &)
00067         : m_value(0)
00068         , m_next(WD_Null)
00069         , m_backlink(WD_Null)
00070     {
00071         WD_Complain ("cannot copy WT_History_Item");
00072     } // prohibited
00073 
00074 
00075     WT_History_Item & operator= (WT_History_Item const &)
00076     {
00077         WD_Complain ("cannot assign WT_History_Item");
00078         return *this;
00079     } // prohibited
00080 
00081 public:
00082     // Constructors, Destructors
00083 
00084     WT_History_Item()
00085         : m_value(0)
00086         , m_next(WD_Null)
00087         , m_backlink(WD_Null)
00088     { }
00089 
00090     ~WT_History_Item()
00091     { }
00092 
00093     void unlink()
00094     {
00095         if ((*m_backlink = m_next) != WD_Null)
00096             m_next->set_backlink(m_backlink);
00097     }
00098 
00099     void set_value(WT_Byte data)
00100     {    m_value = data;    }
00101     WT_Byte    value() const
00102     {    return m_value;    }
00103 
00104     void set_next(WT_History_Item * next)
00105     {    m_next = next;    }
00106     WT_History_Item * & next()
00107     {    return m_next;    }
00108 
00109     void set_backlink(WT_History_Item ** backlink)
00110     {    m_backlink = backlink;    }
00111     WT_History_Item ** & backlink()
00112     {    return m_backlink;    }
00113 };
00114 
00115 
00117 
00118 class WT_LZ_Compressor : public WT_Compressor
00119 {
00120 private:
00121     WT_FIFO<WT_Byte>            m_candidate;
00122     WT_FIFO<WT_History_Item>    m_history;
00123     WT_History_Item *           m_hash_table[WD_LZ_HASH_TABLE_SIZE];
00124     WT_Boolean                  m_at_least_one_extendible;
00125     WT_FIFO<WT_Byte>            m_literal_output_buf;
00126     WT_File &                   m_file;
00127     WT_Boolean                  m_compression_started;
00128     int                         m_best_match_length;
00129     WT_History_Item *           m_best_match;
00130     WT_Boolean                  m_best_match_extendible;
00131     int                         m_hash_entries_searched;
00132 
00133     void    preload_history_buffer();
00134 
00135 #ifdef DEBUG_ASSERTIONS
00136     int                            m_total_literal_bytes;
00137     int                            m_total_literal_strings;
00138     int                            m_num_literal_string_overflows;
00139     int                            m_total_compressed_bytes;
00140     int                            m_total_compression_codes;
00141     int                            m_biggest_compressed_string;
00142     int                            m_hash_misses;
00143     int                            m_hash_hits;
00144 #endif
00145 
00146     WT_LZ_Compressor(WT_LZ_Compressor const &)
00147         : WT_Compressor()
00148         , m_candidate()
00149         , m_history()
00150         , m_at_least_one_extendible()
00151         , m_literal_output_buf()
00152         , m_file(_WT_File_g_none)
00153         , m_compression_started()
00154         , m_best_match_length()
00155         , m_best_match()
00156         , m_best_match_extendible()
00157         , m_hash_entries_searched()
00158 #ifdef DEBUG_ASSERTIONS
00159         , m_total_literal_bytes()
00160         , m_total_literal_strings()
00161         , m_num_literal_string_overflows()
00162         , m_total_compressed_bytes()
00163         , m_total_compression_codes()
00164         , m_biggest_compressed_string()
00165         , m_hash_misses()
00166         , m_hash_hits()
00167 #endif
00168     {
00169         WD_Complain ("cannot copy WT_LZ_Compressor");
00170     } // prohibited
00171 
00172     WT_LZ_Compressor operator= (WT_LZ_Compressor const &)
00173     {
00174         WD_Complain ("cannot assign WT_LZ_Compressor");
00175         return *this;
00176     } // prohibited
00177 
00178 public:
00179 
00180     // Constructors, Destructors
00181 
00182     WT_LZ_Compressor(WT_File & file)
00183         : m_history(WD_LZ_HISTORY_BUFFER_SIZE)
00184         , m_at_least_one_extendible(WD_False)
00185         , m_literal_output_buf()
00186         , m_file(file)
00187         , m_compression_started(WD_False)
00188         , m_best_match_length(WD_LZ_COMPRESSION_AND_OFFSET_CODE_BYTES)
00189         , m_best_match(WD_Null)
00190         , m_best_match_extendible(WD_False)
00191         , m_hash_entries_searched(0)
00192 
00193 #ifdef DEBUG_ASSERTIONS
00194         , m_total_literal_bytes(0)
00195         , m_total_literal_strings(0)
00196         , m_num_literal_string_overflows(0)
00197         , m_total_compressed_bytes(0)
00198         , m_total_compression_codes(0)
00199         , m_biggest_compressed_string(0)
00200         , m_hash_misses(0)
00201         , m_hash_hits(0)
00202 #endif
00203     { }
00204 
00205     ~WT_LZ_Compressor()
00206     { }
00207 
00208     WT_Result    compress(int in_size, void const * in_buf);
00209              WT_Result    output_match();
00210              void        extend_best_match_length();
00211              void        find_better_match(WT_History_Item * history_item);
00212     WT_Result    start();
00213     WT_Boolean   is_compression_started() { return m_compression_started; }
00214     WT_Result    stop();
00215     WT_Result    add_to_history_buffer(WT_Byte a_byte, WT_Boolean output);
00216 };
00217 
00218 
00220 
00221 class WT_ZLib_Compressor : public WT_Compressor
00222 {
00223 private:
00224     WT_File &                   m_file;
00225     WT_Boolean                  m_compression_started;
00226     z_stream                    m_zlib_stream; /* compression stream */
00227     WT_Byte                     m_zlib_output_buffer[WD_ZLIB_COMPRESSION_OUTPUT_BUFFER_SIZE];
00228 
00229     WT_Result   preload_history_buffer();
00230 
00231     WT_ZLib_Compressor(WT_ZLib_Compressor const &)
00232         : WT_Compressor()
00233         , m_file(_WT_File_g_none)
00234         , m_compression_started(WD_False)
00235         , m_zlib_stream()
00236     {
00237         WD_Complain ("cannot copy WT_ZLib_Compressor");
00238     } // prohibited
00239 
00240     WT_ZLib_Compressor & operator= (WT_ZLib_Compressor const &)
00241     {
00242         WD_Complain ("cannot assign WT_ZLib_Compressor");
00243         return *this;
00244     } // prohibited
00245 
00246 
00247 public:
00248 
00249     // Constructors, Destructors
00250 
00251     WT_ZLib_Compressor(WT_File & file)
00252         : m_file(file)
00253         , m_compression_started(WD_False)
00254     { }
00255 
00256     ~WT_ZLib_Compressor()
00257     { }
00258 
00259     WT_Result    start();
00260     WT_Boolean   is_compression_started() { return m_compression_started; }
00261     WT_Result    stop();
00262     WT_Result    compress(int in_size, void const * in_buf);
00263 };
00264 
00265 #endif // LZ77COMP_HEADER

Generated on Tue May 17 12:07:44 2005 for Autodesk DWF Whip 2D Toolkit by  doxygen 1.4.1