Home | History | Annotate | Download | only in Compress
      1 // DeltaFilter.cpp
      2 
      3 #include "StdAfx.h"
      4 
      5 #include "../../../C/Delta.h"
      6 
      7 #include "../../Common/MyCom.h"
      8 
      9 #include "../ICoder.h"
     10 
     11 #include "../Common/RegisterCodec.h"
     12 
     13 namespace NCompress {
     14 namespace NDelta {
     15 
     16 struct CDelta
     17 {
     18   unsigned _delta;
     19   Byte _state[DELTA_STATE_SIZE];
     20 
     21   CDelta(): _delta(1) {}
     22   void DeltaInit() { Delta_Init(_state); }
     23 };
     24 
     25 
     26 #ifndef EXTRACT_ONLY
     27 
     28 class CEncoder:
     29   public ICompressFilter,
     30   public ICompressSetCoderProperties,
     31   public ICompressWriteCoderProperties,
     32   CDelta,
     33   public CMyUnknownImp
     34 {
     35 public:
     36   MY_UNKNOWN_IMP3(ICompressFilter, ICompressSetCoderProperties, ICompressWriteCoderProperties)
     37   INTERFACE_ICompressFilter(;)
     38   STDMETHOD(SetCoderProperties)(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps);
     39   STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
     40 };
     41 
     42 STDMETHODIMP CEncoder::Init()
     43 {
     44   DeltaInit();
     45   return S_OK;
     46 }
     47 
     48 STDMETHODIMP_(UInt32) CEncoder::Filter(Byte *data, UInt32 size)
     49 {
     50   Delta_Encode(_state, _delta, data, size);
     51   return size;
     52 }
     53 
     54 STDMETHODIMP CEncoder::SetCoderProperties(const PROPID *propIDs, const PROPVARIANT *props, UInt32 numProps)
     55 {
     56   UInt32 delta = _delta;
     57   for (UInt32 i = 0; i < numProps; i++)
     58   {
     59     const PROPVARIANT &prop = props[i];
     60     PROPID propID = propIDs[i];
     61     if (propID >= NCoderPropID::kReduceSize)
     62       continue;
     63     if (prop.vt != VT_UI4)
     64       return E_INVALIDARG;
     65     switch (propID)
     66     {
     67       case NCoderPropID::kDefaultProp:
     68         delta = (UInt32)prop.ulVal;
     69         if (delta < 1 || delta > 256)
     70           return E_INVALIDARG;
     71         break;
     72       case NCoderPropID::kNumThreads: break;
     73       case NCoderPropID::kLevel: break;
     74       default: return E_INVALIDARG;
     75     }
     76   }
     77   _delta = delta;
     78   return S_OK;
     79 }
     80 
     81 STDMETHODIMP CEncoder::WriteCoderProperties(ISequentialOutStream *outStream)
     82 {
     83   Byte prop = (Byte)(_delta - 1);
     84   return outStream->Write(&prop, 1, NULL);
     85 }
     86 
     87 #endif
     88 
     89 
     90 class CDecoder:
     91   public ICompressFilter,
     92   public ICompressSetDecoderProperties2,
     93   CDelta,
     94   public CMyUnknownImp
     95 {
     96 public:
     97   MY_UNKNOWN_IMP2(ICompressFilter, ICompressSetDecoderProperties2)
     98   INTERFACE_ICompressFilter(;)
     99   STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
    100 };
    101 
    102 STDMETHODIMP CDecoder::Init()
    103 {
    104   DeltaInit();
    105   return S_OK;
    106 }
    107 
    108 STDMETHODIMP_(UInt32) CDecoder::Filter(Byte *data, UInt32 size)
    109 {
    110   Delta_Decode(_state, _delta, data, size);
    111   return size;
    112 }
    113 
    114 STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *props, UInt32 size)
    115 {
    116   if (size != 1)
    117     return E_INVALIDARG;
    118   _delta = (unsigned)props[0] + 1;
    119   return S_OK;
    120 }
    121 
    122 
    123 REGISTER_FILTER_E(Delta,
    124     CDecoder(),
    125     CEncoder(),
    126     3, "Delta")
    127 
    128 }}
    129