File: dcpxitem.cc

package info (click to toggle)
dcmtk 3.6.9-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,648 kB
  • sloc: ansic: 426,874; cpp: 318,177; makefile: 6,401; sh: 4,341; yacc: 1,026; xml: 482; lex: 321; perl: 277
file content (460 lines) | stat: -rw-r--r-- 18,073 bytes parent folder | download | duplicates (2)
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
 *
 *  Copyright (C) 1994-2023, 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:  Gerd Ehlers, Andreas Barth
 *
 *  Purpose: Implementation of class DcmPixelItem
 *
 */


#include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */

#include "dcmtk/ofstd/ofstream.h"
#include "dcmtk/dcmdata/dcpxitem.h"
#include "dcmtk/dcmdata/dcswap.h"
#include "dcmtk/ofstd/ofstring.h"
#include "dcmtk/ofstd/ofstd.h"
#include "dcmtk/dcmdata/dcistrma.h"    /* for class DcmInputStream */
#include "dcmtk/dcmdata/dcostrma.h"    /* for class DcmOutputStream */
#include "dcmtk/dcmdata/dcwcache.h"    /* for class DcmWriteCache */


// ********************************


DcmPixelItem::DcmPixelItem(const DcmTag &tag,
                           const Uint32 len)
  : DcmOtherByteOtherWord(tag, len)
{
    setTagVR(EVR_pixelItem);
}


DcmPixelItem::DcmPixelItem(const DcmPixelItem &old)
  : DcmOtherByteOtherWord(old)
{
}


DcmPixelItem &DcmPixelItem::operator=(const DcmPixelItem &obj)
{
  DcmOtherByteOtherWord::operator=(obj);
  return *this;
}


OFCondition DcmPixelItem::copyFrom(const DcmObject &rhs)
{
  if (this != &rhs)
  {
    if (rhs.ident() != ident()) return EC_IllegalCall;
    *this = OFstatic_cast(const DcmPixelItem &, rhs);
  }
  return EC_Normal;
}


DcmPixelItem::~DcmPixelItem()
{
}


// ********************************


OFBool DcmPixelItem::isNested() const
{
    OFBool nested = OFFalse;
    if (getParent() != NULL)
    {
        // check for surrounding structure of a pixel sequence
        if ((getParent()->ident() == EVR_pixelSQ))
            nested = OFTrue;
    }
    return nested;
}


DcmItem *DcmPixelItem::getParentItem()
{
    DcmItem *parentItem = NULL;
    if (getParent() != NULL)
    {
        // make sure that the direct parent has the correct type
        if (getParent()->ident() == EVR_pixelSQ)
        {
            DcmObject *parent = getParent()->getParent();
            if (parent != NULL)
            {
                // make sure that it is really a class derived from DcmItem
                switch (parent->ident())
                {
                    case EVR_metainfo:
                    case EVR_dataset:
                    case EVR_item:
                    case EVR_dirRecord:
                        parentItem = OFreinterpret_cast(DcmItem *, parent);
                        break;
                    default:
                        DCMDATA_DEBUG("DcmPixelItem::getParentItem() Parent object has wrong class identifier: "
                            << OFstatic_cast(int, parent->ident())
                            << " (" << DcmVR(parent->ident()).getVRName() << ")");
                        break;
                }
            }
        } else {
            DCMDATA_DEBUG("DcmPixelItem::getParentItem() Direct parent object is not a pixel sequence");
        }
    }
    return parentItem;
}


// ********************************


Uint32 DcmPixelItem::calcElementLength(const E_TransferSyntax xfer,
                                       const E_EncodingType enctype)
{
    /* silence unused arguments warnings */
    OFstatic_cast(void, xfer);
    OFstatic_cast(void, enctype);
    /* get length of the pixel data */
    Uint32 valueLength = getLengthField();
    /* make sure the value did not overflow, clamp it otherwise. */
    if (OFStandard::check32BitAddOverflow(valueLength, 8))
      return OFnumeric_limits<Uint32>::max();
    return valueLength + 8;
}


// ********************************


OFCondition DcmPixelItem::writeTagAndLength(DcmOutputStream &outStream,
                                            const E_TransferSyntax oxfer,
                                            Uint32 &writtenBytes) const
{
    OFCondition l_error = outStream.status();
    if (l_error.good())
    {
        /* write tag information */
        l_error = writeTag(outStream, getTag(), oxfer);
        writtenBytes = 4;
        /* prepare to write the value field */
        Uint32 valueLength = getLengthField();
        DcmXfer outXfer(oxfer);
        /* check byte-ordering */
        const E_ByteOrder oByteOrder = outXfer.getByteOrder();
        if (oByteOrder == EBO_unknown)
        {
            return EC_IllegalCall;
        }
        swapIfNecessary(oByteOrder, gLocalByteOrder, &valueLength, 4, 4);
        // availability of four bytes space in output buffer
        // has been checked by caller.
        writtenBytes += OFstatic_cast(Uint32, outStream.write(&valueLength, 4));
    } else
        writtenBytes = 0;
    return l_error;
}


void DcmPixelItem::print(STD_NAMESPACE ostream &out,
                         const size_t flags,
                         const int level,
                         const char *pixelFileName,
                         size_t *pixelCounter)
{
    /* call inherited method */
    printPixel(out, flags, level, pixelFileName, pixelCounter);
}


OFCondition DcmPixelItem::createOffsetTable(const DcmOffsetList &offsetList)
{
    OFCondition result = EC_Normal;

    size_t numEntries = offsetList.size();
    if (numEntries > 0)
    {
        Uint32 current = 0;
        Uint32 *array = new Uint32[numEntries];
        if (array)
        {
            DCMDATA_DEBUG("DcmPixelItem: creating offset table with " << numEntries << " entries");
            OFListConstIterator(Uint32) first = offsetList.begin();
            OFListConstIterator(Uint32) last = offsetList.end();
            unsigned long idx = 0;
            OFBool overflow = OFFalse;
            while ((first != last) && result.good())
            {
                // check for 32-bit unsigned integer overflow (during previous iteration) and report on this
                if (overflow)
                {
                    DCMDATA_WARN("DcmPixelItem: offset value exceeds maximum (32-bit unsigned integer) for frame #"
                        << (idx + 1) << ", cannot create offset table");
                    result = EC_InvalidBasicOffsetTable;
                }
                // check for odd offset values, should never happen at this point (if list was filled by an encoder)
                else if (current & 1)
                {
                    DCMDATA_WARN("DcmPixelItem: odd offset value (" << current << ") for frame #"
                        << (idx + 1) << ", cannot create offset table");
                    result = EC_InvalidBasicOffsetTable;
                } else {
                    // value "current" is proven to be valid
                    array[idx++] = current;
                    // check for 32-bit unsigned integer overflow (but report only during next iteration)
                    overflow = !OFStandard::safeAdd(current, *first, current);
                    ++first;
                }
            }
            if (result.good())
            {
                result = swapIfNecessary(EBO_LittleEndian, gLocalByteOrder, array, OFstatic_cast(Uint32, numEntries * sizeof(Uint32)), sizeof(Uint32));
                if (result.good())
                    result = putUint8Array(OFreinterpret_cast(Uint8 *, array), OFstatic_cast(unsigned long, numEntries * sizeof(Uint32)));
            }
            delete[] array;
        } else
            result = EC_MemoryExhausted;
    }
    return result;
}


OFCondition DcmPixelItem::writeXML(STD_NAMESPACE ostream &out,
                                   const size_t flags)
{
    if (flags & DCMTypes::XF_useNativeModel)
    {
        /* in Native DICOM Model, there is no concept of a "pixel item" */
        return makeOFCondition(OFM_dcmdata, EC_CODE_CannotConvertToXML, OF_error,
            "Cannot convert Pixel Item to Native DICOM Model");
    } else {
        /* XML start tag for "item" */
        out << "<pixel-item";
        /* value length in bytes = 0..max */
        out << " len=\"" << getLengthField() << "\"";
        /* value loaded = no (or absent)*/
        if (!valueLoaded())
            out << " loaded=\"no\"";
        /* pixel item contains binary data */
        if (!(flags & DCMTypes::XF_writeBinaryData))
            out << " binary=\"hidden\"";
        else if (flags & DCMTypes::XF_encodeBase64)
            out << " binary=\"base64\"";
        else
            out << " binary=\"yes\"";
        out << ">";
        /* write element value (if loaded) */
        if (valueLoaded() && (flags & DCMTypes::XF_writeBinaryData))
        {
            /* encode binary data as Base64 */
            if (flags & DCMTypes::XF_encodeBase64)
            {
                /* pixel items always contain 8 bit data, therefore, byte swapping not required */
                OFStandard::encodeBase64(out, OFstatic_cast(Uint8 *, getValue()), OFstatic_cast(size_t, getLengthField()));
            } else {
                /* get and check 8 bit data */
                Uint8 *byteValues = NULL;
                if (getUint8Array(byteValues).good() && (byteValues != NULL))
                {
                    const unsigned long count = getLengthField();
                    out << STD_NAMESPACE hex << STD_NAMESPACE setfill('0');
                    /* print byte values in hex mode */
                    out << STD_NAMESPACE setw(2) << OFstatic_cast(int, *(byteValues++));
                    for (unsigned long i = 1; i < count; i++)
                        out << "\\" << STD_NAMESPACE setw(2) << OFstatic_cast(int, *(byteValues++));
                    /* reset i/o manipulators */
                    out << STD_NAMESPACE dec << STD_NAMESPACE setfill(' ');
                }
            }
        }
        /* XML end tag for "item" */
        out << "</pixel-item>" << OFendl;
        /* always report success */
        return EC_Normal;
    }
}


OFCondition DcmPixelItem::writeSignatureFormat(
    DcmOutputStream &outStream,
    const E_TransferSyntax oxfer,
    const E_EncodingType enctype,
    DcmWriteCache *wcache)
{
  if (dcmEnableOldSignatureFormat.get())
  {
      /* Old signature format as created by DCMTK releases previous to 3.5.4.
       * This is non-conformant because it includes the item length in pixel items.
       */
      return DcmOtherByteOtherWord::writeSignatureFormat(outStream, oxfer, enctype, wcache);
  }
  else
  {
      DcmWriteCache wcache2;

      /* In case the transfer state is not initialized, this is an illegal call */
      if (getTransferState() == ERW_notInitialized)
          errorFlag = EC_IllegalCall;
      else
      {
          /* if this is not an illegal call, we need to do something. First */
          /* of all, check the error state of the stream that was passed */
          /* only do something if the error state of the stream is ok */
          errorFlag = outStream.status();
          if (errorFlag.good())
          {
              /* create an object that represents the transfer syntax */
              DcmXfer outXfer(oxfer);

              /* pointer to element value if value resides in memory or old-style
               * write behaviour is active (i.e. everything loaded into memory upon write
               */
              Uint8 *value = NULL;
              OFBool accessPossible = OFFalse;

              /* check that we actually do have access to the element's value.
               * If the element is unaccessible (which would mean that the value resides
               * in file and access to the file fails), write an empty element with
               * zero length.
               */
              if (getLengthField() > 0)
              {
                if (valueLoaded())
                {
                  /* get this element's value. Mind the byte ordering (little */
                  /* or big endian) of the transfer syntax which shall be used */
                  value = OFstatic_cast(Uint8 *, getValue(outXfer.getByteOrder()));
                  if (value) accessPossible = OFTrue;
                }
                else
                {
                  /* Use local cache object if needed. This may cause those bytes
                   * that are read but not written because the output stream stalls to
                   * be read again, and the file from being re-opened next time.
                   * Therefore, this case should be avoided.
                   */
                  if (!wcache) wcache = &wcache2;

                  /* initialize cache object. This is safe even if the object was already initialized */
                  wcache->init(this, getLengthField(), getTransferredBytes(), outXfer.getByteOrder());

                  /* access first block of element content */
                  errorFlag = wcache->fillBuffer(*this);

                  /* check that everything worked and the buffer is non-empty now */
                  accessPossible = errorFlag.good() && (! wcache->bufferIsEmpty());
                }
              }

              /* if this element's transfer state is ERW_init (i.e. it has not yet been written to */
              /* the stream) and if the outstream provides enough space for tag and length information */
              /* write tag and length information to it, do something */
              if (getTransferState() == ERW_init)
              {
                  // Force a compression filter (if any) to process the input buffer, by calling outStream.write().
                  // This ensures that we cannot get stuck if there are just a few bytes available in the buffer
                  outStream.write(NULL, 0);

                  /* first compare with DCM_TagInfoLength (12). If there is not enough space
                   * in the buffer, check if the buffer is still sufficient for the requirements
                   * of this element, which may need only 8 instead of 12 bytes.
                   */
                  if (outStream.avail() >= 4)
                  {
                      /* if there is no value, Length (member variable) shall be set to 0 */
                      if (! accessPossible) setLengthField(0);

                      /* write tag and length information (and possibly also data type information) to the stream, */
                      /* mind the transfer syntax and remember the amount of bytes that have been written */
                      errorFlag = writeTag(outStream, getTag(), oxfer);

                      /* if the writing was successful, set this element's transfer */
                      /* state to ERW_inWork and the amount of transferred bytes to 0 */
                      if (errorFlag.good())
                      {
                          setTransferState(ERW_inWork);
                          setTransferredBytes(0);
                      }
                  } else
                      errorFlag = EC_StreamNotifyClient;
              }
              /* if there is a value that has to be written to the stream */
              /* and if this element's transfer state is ERW_inWork */
              if (accessPossible && getTransferState() == ERW_inWork)
              {

                  Uint32 len = 0;
                  if (valueLoaded())
                  {
                      /* write as many bytes as possible to the stream starting at value[getTransferredBytes()] */
                      /* (note that the bytes value[0] to value[getTransferredBytes()-1] have already been */
                      /* written to the stream) */
                      len = OFstatic_cast(Uint32, outStream.write(&value[getTransferredBytes()], getLengthField() - getTransferredBytes()));

                      /* increase the amount of bytes which have been transferred correspondingly */
                      incTransferredBytes(len);

                      /* see if there is something fishy with the stream */
                      errorFlag = outStream.status();
                  }
                  else
                  {
                      Uint32 buflen = 0;
                      OFBool done = getTransferredBytes() == getLengthField();
                      while (! done)
                      {
                        // re-fill buffer from file if empty
                        errorFlag = wcache->fillBuffer(*this);
                        buflen = wcache->contentLength();

                        if (errorFlag.good())
                        {
                          // write as many bytes from cache buffer to stream as possible
                          len = wcache->writeBuffer(outStream);

                          /* increase the amount of bytes which have been transferred correspondingly */
                          incTransferredBytes(len);

                          /* see if there is something fishy with the stream */
                          errorFlag = outStream.status();
                        }

                        // stop writing if something went wrong, we were unable to send all of the buffer content
                        // (which indicates that the output stream needs to be flushed, or everything was sent out.
                        done = errorFlag.bad() || (len < buflen) || (getTransferredBytes() == getLengthField());
                      }
                  }

                  /* if the amount of transferred bytes equals the length of the element's value, the */
                  /* entire value has been written to the stream. Thus, this element's transfer state */
                  /* has to be set to ERW_ready. If this is not the case but the error flag still shows */
                  /* an ok value, there was no more space in the stream and a corresponding return value */
                  /* has to be set. (Isn't the "else if" part superfluous?!?) */
                  if (getLengthField() == getTransferredBytes()) setTransferState(ERW_ready);
                  else if (errorFlag.good()) errorFlag = EC_StreamNotifyClient;
              }
          }
      }
  }

  /* return result value */
  return errorFlag;
}