32 #include "dcmtk/config/osconfig.h" 33 #include "dcmtk/ofstd/oflist.h" 35 #define INCLUDE_CSTRING 36 #include "dcmtk/ofstd/ofstdinc.h" 38 #define DcmRLEEncoder_BLOCKSIZE 16384 52 virtual void write(
const unsigned char *buf,
size_t bufsize) =0;
74 , currentBlock_(new unsigned char[DcmRLEEncoder_BLOCKSIZE])
77 , RLE_buff_(new unsigned char[132])
82 if ((! RLE_buff_)||(! currentBlock_)) fail_ = 1;
83 else RLE_buff_[0] = 0;
89 delete[] currentBlock_;
91 OFListIterator(
unsigned char *) first = blockList_.begin();
92 OFListIterator(
unsigned char *) last = blockList_.end();
96 first = blockList_.erase(first);
104 inline void add(
unsigned char ch)
111 if (OFstatic_cast(
int, ch) == RLE_prev_) RLE_pcount_++;
123 RLE_buff_[RLE_bindex_++] = OFstatic_cast(
unsigned char, RLE_prev_);
127 RLE_buff_[RLE_bindex_++] = OFstatic_cast(
unsigned char, RLE_prev_);
135 RLE_buff_[0] = OFstatic_cast(
unsigned char, RLE_bindex_-2);
139 RLE_buff_[1] = OFstatic_cast(
unsigned char, RLE_prev_);
141 for (; RLE_pcount_>0; RLE_pcount_-=128)
147 if (RLE_pcount_ > 128) RLE_buff_[0] = 0x81;
148 else RLE_buff_[0] = OFstatic_cast(
unsigned char, 257 - RLE_pcount_);
158 if (RLE_bindex_ > 129)
164 RLE_buff_[1] = RLE_buff_[129];
166 RLE_buff_[2] = RLE_buff_[130];
181 inline void add(
const unsigned char *buf,
size_t bufcount)
185 while (bufcount--) add(*buf++);
202 for (; RLE_pcount_>0; --RLE_pcount_) RLE_buff_[RLE_bindex_++] = OFstatic_cast(
unsigned char, RLE_prev_);
206 if (RLE_bindex_ > 129)
212 RLE_buff_[1] = RLE_buff_[129];
214 RLE_buff_[2] = RLE_buff_[130];
220 RLE_buff_[0] = OFstatic_cast(
unsigned char, RLE_bindex_-2);
225 if (RLE_pcount_ >= 2)
227 RLE_buff_[1] = OFstatic_cast(
unsigned char, RLE_prev_);
229 for (; RLE_pcount_>0; RLE_pcount_-=128)
235 if (RLE_pcount_ > 128) RLE_buff_[0] = 0x81;
236 else RLE_buff_[0] = OFstatic_cast(
unsigned char, 257 - RLE_pcount_);
258 size_t result = blockList_.size() * DcmRLEEncoder_BLOCKSIZE + offset_;
259 if (pad_ && (result & 1)) result++;
268 if (fail_)
return OFTrue;
else return OFFalse;
275 inline void write(
void *target)
const 277 if ((!fail_) && target)
279 unsigned char *current = NULL;
280 unsigned char *target8 = OFstatic_cast(
unsigned char *, target);
281 OFListConstIterator(
unsigned char *) first = blockList_.begin();
282 OFListConstIterator(
unsigned char *) last = blockList_.end();
283 while (first != last)
286 memcpy(target8, current, DcmRLEEncoder_BLOCKSIZE);
287 target8 += DcmRLEEncoder_BLOCKSIZE;
292 memcpy(target8, currentBlock_, offset_);
296 if (pad_ && ((blockList_.size() * DcmRLEEncoder_BLOCKSIZE + offset_) & 1))
312 OFListConstIterator(
unsigned char *) first = blockList_.begin();
313 OFListConstIterator(
unsigned char *) last = blockList_.end();
314 while (first != last)
316 os.
write(*first, DcmRLEEncoder_BLOCKSIZE);
321 os.
write(currentBlock_, offset_);
325 if (pad_ && ((blockList_.size() * DcmRLEEncoder_BLOCKSIZE + offset_) & 1))
346 inline void move(
size_t numberOfBytes)
349 while (i < numberOfBytes)
351 if (offset_ == DcmRLEEncoder_BLOCKSIZE)
353 blockList_.push_back(currentBlock_);
354 currentBlock_ =
new unsigned char[DcmRLEEncoder_BLOCKSIZE];
362 currentBlock_[offset_++] = RLE_buff_[i++];
void add(const unsigned char *buf, size_t bufcount)
this method adds a block of bytes to the byte stream to be compressed with the RLE compressor...
unsigned char * currentBlock_
this member points to a block of size DcmRLEEncoder_BLOCKSIZE (unless fail_ is true).
size_t size() const
returns the size of compressed RLE stream in bytes.
DcmRLEEncoder(int doPad)
default constructor
void write(DcmEncoderOutputStream &os) const
copies the compressed RLE byte stream into an output stream
void move(size_t numberOfBytes)
this method moves the given number of bytes from buff_ to currentBlock_ and "flushes" currentBlock_ t...
int RLE_pcount_
repeat counter, for RLE compressor may temporarily become negative, guaranteed to be >= 0 between met...
size_t offset_
contains the number of bytes already written the the memory block pointed to by currentBlock_.
void add(unsigned char ch)
this method adds one byte to the byte stream to be compressed with the RLE compressor.
this class implements an RLE compressor conforming to the DICOM standard.
abstract class that defines an interface through which encoder classes (such as DcmRLEEncoder) may ex...
~DcmRLEEncoder()
destructor
virtual ~DcmEncoderOutputStream()
Virtual Desctructor.
virtual void write(const unsigned char *buf, size_t bufsize)=0
write the given buffer into the output stream
OFBool fail() const
returns true if the RLE compressor has run out of memory.
int RLE_prev_
value of the last byte fed to the RLE compressor.
int fail_
this flag indicates a failure of the RLE codec.
void flush()
this method finalizes the compressed RLE stream, i.e.
OFList< unsigned char * > blockList_
this member contains a list of memory blocks of size DcmRLEEncoder_BLOCKSIZE which already have been ...
unsigned int RLE_bindex_
index of next unused byte in RLE_buff_.
void write(void *target) const
copies the compressed RLE byte stream into a target array of at least size() bytes.
unsigned char * RLE_buff_
this member points to a buffer of 132 bytes that is used by the RLE encoding algorithm.
int pad_
this flag indicates whether the RLE codec must pad encoded data to an even number of bytes (as requir...