File: dcostrmz.cc

package info (click to toggle)
dcmtk 3.6.0-12
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,976 kB
  • sloc: cpp: 193,852; ansic: 46,292; sh: 4,020; makefile: 3,969; perl: 3,278; lex: 94
file content (395 lines) | stat: -rw-r--r-- 11,892 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*
 *
 *  Copyright (C) 2002-2010, OFFIS e.V.
 *  All rights reserved.  See COPYRIGHT file for details.
 *
 *  This software and supporting documentation were developed by
 *
 *    OFFIS e.V.
 *    R&D Division Health
 *    Escherweg 2
 *    D-26121 Oldenburg, Germany
 *
 *
 *  Module:  dcmdata
 *
 *  Author:  Marco Eichelberg
 *
 *  Purpose: zlib compression filter for output streams
 *
 *  Last Update:      $Author: joergr $
 *  Update Date:      $Date: 2010-10-14 13:14:08 $
 *  CVS/RCS Revision: $Revision: 1.8 $
 *  Status:           $State: Exp $
 *
 *  CVS/RCS Log at end of file
 *
 */

#include "dcmtk/config/osconfig.h"

#ifdef WITH_ZLIB

#include "dcmtk/dcmdata/dcostrmz.h"
#include "dcmtk/dcmdata/dcerror.h"

#define DCMZLIBOUTPUTFILTER_BUFSIZE 4096

/* taken from zutil.h */
#if MAX_MEM_LEVEL >= 8
#define DEF_MEM_LEVEL 8
#else
#define DEF_MEM_LEVEL  MAX_MEM_LEVEL
#endif

OFGlobal<int> dcmZlibCompressionLevel(Z_DEFAULT_COMPRESSION);


DcmZLibOutputFilter::DcmZLibOutputFilter()
: DcmOutputFilter()
, current_(NULL)
, zstream_(new z_stream)
, status_(EC_MemoryExhausted)
, flushed_(OFFalse)
, inputBuf_(new unsigned char[DCMZLIBOUTPUTFILTER_BUFSIZE])
, inputBufStart_(0)
, inputBufCount_(0)
, outputBuf_(new unsigned char[DCMZLIBOUTPUTFILTER_BUFSIZE])
, outputBufStart_(0)
, outputBufCount_(0)
{
  if (zstream_ && inputBuf_ && outputBuf_)
  {
    zstream_->zalloc = Z_NULL;
    zstream_->zfree = Z_NULL;
    zstream_->opaque = Z_NULL;
#ifdef ZLIB_ENCODE_RFC1950_HEADER
    /* create deflated ZLIB format instead of deflated bitstream format
     * (i.e. RFC 1950 instead of RFC 1951).
     * THE RESULTING BITSTREAM IS NOT DICOM COMPLIANT!
     * Use only for testing, and use with care.
     */
    if (Z_OK == deflateInit(zstream_, dcmZlibCompressionLevel.get()))
#else
    /* windowBits is passed < 0 to suppress zlib header */
    if (Z_OK == deflateInit2(zstream_, dcmZlibCompressionLevel.get(),
        Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY))
#endif
    {
        status_ = EC_Normal;
    }
    else
    {
      OFString etext = "ZLib Error: ";
      if (zstream_->msg) etext += zstream_->msg;
      status_ = makeOFCondition(OFM_dcmdata, 16, OF_error, etext.c_str());
    }
  }
}

DcmZLibOutputFilter::~DcmZLibOutputFilter()
{
  if (zstream_)
  {
    deflateEnd(zstream_); // discards any unprocessed input and does not flush any pending output
    delete zstream_;
  }
  delete[] inputBuf_;
  delete[] outputBuf_;
}


OFBool DcmZLibOutputFilter::good() const
{
  return status_.good();
}

OFCondition DcmZLibOutputFilter::status() const
{
  return status_;
}

OFBool DcmZLibOutputFilter::isFlushed() const
{
  if (status_.bad() || (current_ == NULL)) return OFTrue;
  return (inputBufCount_ == 0) && (outputBufCount_ == 0) && flushed_ && current_->isFlushed();
}


offile_off_t DcmZLibOutputFilter::avail() const
{
  // compute number of bytes available in input buffer
  if (status_.good() ) return DCMZLIBOUTPUTFILTER_BUFSIZE - inputBufCount_;
    else return 0;
}

void DcmZLibOutputFilter::flushOutputBuffer()
{
  if (outputBufCount_)
  {
    // flush from outputBufStart_ to end of data or end of buffer, whatever comes first
    offile_off_t numBytes = (outputBufStart_ + outputBufCount_ > DCMZLIBOUTPUTFILTER_BUFSIZE) ?
      (DCMZLIBOUTPUTFILTER_BUFSIZE - outputBufStart_) : outputBufCount_ ;

    offile_off_t written = current_->write(outputBuf_ + outputBufStart_, numBytes);

    // adjust counters
    outputBufCount_ -= written;
    outputBufStart_ += written;

    if (outputBufStart_ == DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      // wrapped around
      outputBufStart_ = 0;

      // now flush to end of data
      if (outputBufCount_ && written)
      {
        written = current_->write(outputBuf_, outputBufCount_);

        // adjust counters
        outputBufCount_ -= written;
        outputBufStart_ += written;
      }
    }

    // reset buffer start to make things faster
    if (outputBufCount_ == 0) outputBufStart_ = 0;
  }
}

offile_off_t DcmZLibOutputFilter::fillInputBuffer(const void *buf, offile_off_t buflen)
{
  offile_off_t result = 0;
  if (buf && buflen && inputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE)
  {

    const unsigned char *data = OFstatic_cast(const unsigned char *, buf);

    // use first part of input buffer
    if (inputBufStart_ + inputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      result = DCMZLIBOUTPUTFILTER_BUFSIZE - (inputBufStart_ + inputBufCount_);
      if (result > buflen) result = buflen;

      memcpy(inputBuf_ + inputBufStart_ + inputBufCount_, data, OFstatic_cast(size_t, result));
      inputBufCount_ += result;
      data += result;
      buflen -= result;
    }

    // use second part of input buffer
    if (buflen && (inputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE) &&
        inputBufStart_ + inputBufCount_ >= DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      offile_off_t len = DCMZLIBOUTPUTFILTER_BUFSIZE - inputBufCount_;
      if (len > buflen) len = buflen;

      memcpy(inputBuf_ + (inputBufStart_ + inputBufCount_ - DCMZLIBOUTPUTFILTER_BUFSIZE), data, OFstatic_cast(size_t, len));

      inputBufCount_ += len;
      result += len;
    }
  }
  return result;
}

void DcmZLibOutputFilter::compressInputBuffer(OFBool finalize)
{
  if (inputBufCount_ || finalize)
  {
    // flush from inputBufStart_ to end of data or end of buffer, whatever comes first
    offile_off_t numBytes = (inputBufStart_ + inputBufCount_ > DCMZLIBOUTPUTFILTER_BUFSIZE) ?
      (DCMZLIBOUTPUTFILTER_BUFSIZE - inputBufStart_) : inputBufCount_ ;

    offile_off_t written = compress(inputBuf_ + inputBufStart_, numBytes, finalize);

    // adjust counters
    inputBufCount_ -= written;
    inputBufStart_ += written;

    if (inputBufStart_ == DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      // wrapped around
      inputBufStart_ = 0;

      // now flush to end of data
      if (inputBufCount_ && written)
      {
        written = compress(inputBuf_, inputBufCount_, finalize);

        // adjust counters
        inputBufCount_ -= written;
        inputBufStart_ += written;
      }
    }

    // reset buffer start to make things faster
    if (inputBufCount_ == 0) inputBufStart_ = 0;
  }
}

offile_off_t DcmZLibOutputFilter::compress(const void *buf, offile_off_t buflen, OFBool finalize)
{
  offile_off_t result = 0;
  if (outputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE)
  {
    zstream_->next_in = OFstatic_cast(Bytef *, OFconst_cast(void *, buf));
    zstream_->avail_in = OFstatic_cast(uInt, buflen);
    int zstatus;

    // use first part of output buffer
    if (outputBufStart_ + outputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      zstream_->next_out = OFstatic_cast(Bytef *, outputBuf_ + outputBufStart_ + outputBufCount_);
      zstream_->avail_out = OFstatic_cast(uInt, DCMZLIBOUTPUTFILTER_BUFSIZE - (outputBufStart_ + outputBufCount_));
      zstatus = deflate(zstream_, (finalize ? Z_FINISH : 0));

      if (zstatus == Z_OK || zstatus == Z_BUF_ERROR) { /* everything OK */ }
      else if (zstatus == Z_STREAM_END) flushed_ = OFTrue;
      else
      {
        OFString etext = "ZLib Error: ";
        if (zstream_->msg) etext += zstream_->msg;
        status_ = makeOFCondition(OFM_dcmdata, 16, OF_error, etext.c_str());
      }

      outputBufCount_ = DCMZLIBOUTPUTFILTER_BUFSIZE - outputBufStart_ - OFstatic_cast(offile_off_t, zstream_->avail_out);
    }

    // use second part of output buffer
    if ((outputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE) &&
        outputBufStart_ + outputBufCount_ >= DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      zstream_->next_out = OFstatic_cast(Bytef *, outputBuf_ + (outputBufStart_ + outputBufCount_ - DCMZLIBOUTPUTFILTER_BUFSIZE));
      zstream_->avail_out = OFstatic_cast(uInt, DCMZLIBOUTPUTFILTER_BUFSIZE - outputBufCount_);
      zstatus = deflate(zstream_, (finalize ? Z_FINISH : 0));

      if (zstatus == Z_OK || zstatus == Z_BUF_ERROR) { /* everything OK */ }
      else if (zstatus == Z_STREAM_END) flushed_ = OFTrue;
      else
      {
        OFString etext = "ZLib Error: ";
        if (zstream_->msg) etext += zstream_->msg;
        status_ = makeOFCondition(OFM_dcmdata, 16, OF_error, etext.c_str());
      }

      outputBufCount_ =  DCMZLIBOUTPUTFILTER_BUFSIZE - OFstatic_cast(offile_off_t, zstream_->avail_out);
    }

    result = (buflen - OFstatic_cast(offile_off_t, zstream_->avail_in));
  }
  return result;
}

offile_off_t DcmZLibOutputFilter::write(const void *buf, offile_off_t buflen)
{
  if (status_.bad() || (current_ == NULL)) return 0;

  // flush output buffer if necessary
  if (outputBufCount_ == DCMZLIBOUTPUTFILTER_BUFSIZE) flushOutputBuffer();

  // compress pending input from input buffer
  while (status_.good() && inputBufCount_ > 0 && outputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE)
  {
    compressInputBuffer(OFFalse);
    if (outputBufCount_ == DCMZLIBOUTPUTFILTER_BUFSIZE) flushOutputBuffer();
  }

  const unsigned char *data = OFstatic_cast(const unsigned char *, buf);
  offile_off_t result = 0;

  // compress user data only if input buffer is empty
  if (inputBufCount_ == 0)
  {
    while (status_.good() && (buflen > result) && outputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      result += compress(data+result, buflen-result, OFFalse);
      if (outputBufCount_ == DCMZLIBOUTPUTFILTER_BUFSIZE) flushOutputBuffer();
    }
  }

  // finally stuff as much into the input buffer as possible
  result += fillInputBuffer(data+result, buflen-result);

  // total number of bytes consumed from input
  return result;
}


void DcmZLibOutputFilter::flush()
{
  if (status_.good() && current_)
  {
    // flush output buffer first
    if (outputBufCount_ == DCMZLIBOUTPUTFILTER_BUFSIZE) flushOutputBuffer();

    // compress pending input from input buffer
    while (status_.good() && inputBufCount_ > 0 && outputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      compressInputBuffer(OFTrue);
      if (outputBufCount_ == DCMZLIBOUTPUTFILTER_BUFSIZE) flushOutputBuffer();
    }

    while (status_.good() && (! flushed_) && outputBufCount_ < DCMZLIBOUTPUTFILTER_BUFSIZE)
    {
      // create output from compression engine until end of compressed stream
      compress(NULL, 0, OFTrue);
      if (outputBufCount_ == DCMZLIBOUTPUTFILTER_BUFSIZE) flushOutputBuffer();
    }

    // final attempt to flush output buffer
    if (outputBufCount_ > 0) flushOutputBuffer();
  }
}


void DcmZLibOutputFilter::append(DcmConsumer& consumer)
{
  current_ = &consumer;
}

#else  /* WITH_ZLIB */

/* make sure that the object file is not completely empty if compiled
 * without zlib because some linkers might fail otherwise.
 */
void dcostrmz_dummy_function()
{
  return;
}

#endif /* WITH_ZLIB */

/*
 * CVS/RCS Log:
 * $Log: dcostrmz.cc,v $
 * Revision 1.8  2010-10-14 13:14:08  joergr
 * Updated copyright header. Added reference to COPYRIGHT file.
 *
 * Revision 1.7  2009-11-04 09:58:10  uli
 * Switched to logging mechanism provided by the "new" oflog module
 *
 * Revision 1.6  2007-02-19 16:06:10  meichel
 * Class DcmOutputStream and related classes are now safe for use with
 *   large files (2 GBytes or more) if supported by compiler and operating system.
 *
 * Revision 1.5  2005/12/08 15:41:23  meichel
 * Changed include path schema for all DCMTK header files
 *
 * Revision 1.4  2004/04/07 12:19:14  joergr
 * Adapted type casts to new-style typecast operators defined in ofcast.h.
 *
 * Revision 1.3  2002/12/20 14:55:34  wilkens
 * Inserted three casts in order to get rid of compiler warning on Solaris 2.5.1
 * using compiler SC 2.0.1.
 *
 * Revision 1.2  2002/08/29 15:57:49  meichel
 * Updated zlib-related classes to correctly compile when WITH_ZLIB is undefined
 *
 * Revision 1.1  2002/08/27 16:55:54  meichel
 * Initial release of new DICOM I/O stream classes that add support for stream
 *   compression (deflated little endian explicit VR transfer syntax)
 *
 *
 */