Coverage Report

Created: 2026-11-12 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dcmtk/dcmimage/libsrc/dicoimg.cc
Line
Count
Source
1
/*
2
 *
3
 *  Copyright (C) 1996-2024, OFFIS e.V.
4
 *  All rights reserved.  See COPYRIGHT file for details.
5
 *
6
 *  This software and supporting documentation were developed by
7
 *
8
 *    OFFIS e.V.
9
 *    R&D Division Health
10
 *    Escherweg 2
11
 *    D-26121 Oldenburg, Germany
12
 *
13
 *
14
 *  Module:  dcmimage
15
 *
16
 *  Author:  Joerg Riesmeier
17
 *
18
 *  Purpose: DicomColorImage (Source)
19
 *
20
 */
21
22
23
#include "dcmtk/config/osconfig.h"
24
#include "dcmtk/dcmimage/dicoimg.h"
25
26
#include "dcmtk/dcmdata/dctypes.h"
27
#include "dcmtk/dcmdata/dcdeftag.h"
28
#include "dcmtk/dcmdata/dcpixel.h"
29
#include "dcmtk/ofstd/ofstd.h"
30
31
#include "dcmtk/dcmimgle/dimo2img.h"
32
#include "dcmtk/dcmimage/dicopxt.h"
33
#include "dcmtk/dcmimage/dicocpt.h"
34
#include "dcmtk/dcmimage/dicosct.h"
35
#include "dcmtk/dcmimage/dicoflt.h"
36
#include "dcmtk/dcmimage/dicorot.h"
37
#include "dcmtk/dcmimage/dicoopxt.h"
38
#include "dcmtk/dcmimage/dilogger.h"
39
#include "dcmtk/dcmimgle/diutils.h"
40
41
/*----------------*
42
 *  constructors  *
43
 *----------------*/
44
45
DiColorImage::DiColorImage(const DiDocument *docu,
46
                           const EI_Status status,
47
                           const int spp,
48
                           const OFBool rgb)
49
0
  : DiImage(docu, status, spp),
50
0
    RGBColorModel(rgb),
51
0
    InterData(NULL),
52
0
    OutputData(NULL)
53
0
{
54
0
    detachPixelData();
55
0
}
56
57
58
DiColorImage::DiColorImage(const DiColorImage *image,
59
                           const unsigned long fstart,
60
                           const unsigned long fcount)
61
0
  : DiImage(image, fstart, fcount),
62
0
    RGBColorModel(image->RGBColorModel),
63
0
    InterData(NULL),
64
0
    OutputData(NULL)
65
0
{
66
0
    if (image->InterData != NULL)
67
0
    {
68
0
        const unsigned long fsize = OFstatic_cast(unsigned long, Columns) * OFstatic_cast(unsigned long, Rows);
69
0
        switch (image->InterData->getRepresentation())
70
0
        {
71
0
            case EPR_Uint8:
72
0
                InterData = new DiColorCopyTemplate<Uint8>(image->InterData, fstart, fcount, fsize);
73
0
                break;
74
0
            case EPR_Sint8:
75
0
                InterData = new DiColorCopyTemplate<Sint8>(image->InterData, fstart, fcount, fsize);
76
0
                break;
77
0
            case EPR_Uint16:
78
0
                InterData = new DiColorCopyTemplate<Uint16>(image->InterData, fstart, fcount, fsize);
79
0
                break;
80
0
            case EPR_Sint16:
81
0
                InterData = new DiColorCopyTemplate<Sint16>(image->InterData, fstart, fcount, fsize);
82
0
                break;
83
0
            case EPR_Uint32:
84
0
                InterData = new DiColorCopyTemplate<Uint32>(image->InterData, fstart, fcount, fsize);
85
0
                break;
86
0
            case EPR_Sint32:
87
0
                InterData = new DiColorCopyTemplate<Sint32>(image->InterData, fstart, fcount, fsize);
88
0
                break;
89
0
        }
90
0
        checkInterData(0);
91
0
    }
92
0
}
93
94
95
DiColorImage::DiColorImage(const DiColorImage *image,
96
                           const signed long left_pos,
97
                           const signed long top_pos,
98
                           const Uint16 src_cols,
99
                           const Uint16 src_rows,
100
                           const Uint16 dest_cols,
101
                           const Uint16 dest_rows,
102
                           const int interpolate,
103
                           const int aspect)
104
0
  : DiImage(image, dest_cols, dest_rows, aspect),
105
0
    RGBColorModel(image->RGBColorModel),
106
0
    InterData(NULL),
107
0
    OutputData(NULL)
108
0
{
109
0
    if (image->InterData != NULL)
110
0
    {
111
0
        switch (image->InterData->getRepresentation())
112
0
        {
113
0
            case EPR_Uint8:
114
0
                InterData = new DiColorScaleTemplate<Uint8>(image->InterData, image->Columns, image->Rows, left_pos, top_pos,
115
0
                    src_cols, src_rows, dest_cols, dest_rows, NumberOfFrames, image->BitsPerSample, interpolate);
116
0
                break;
117
0
            case EPR_Uint16:
118
0
                InterData = new DiColorScaleTemplate<Uint16>(image->InterData, image->Columns, image->Rows, left_pos, top_pos,
119
0
                    src_cols, src_rows, dest_cols, dest_rows, NumberOfFrames, image->BitsPerSample, interpolate);
120
0
                break;
121
0
            case EPR_Uint32:
122
0
                InterData = new DiColorScaleTemplate<Uint32>(image->InterData, image->Columns, image->Rows, left_pos, top_pos,
123
0
                    src_cols, src_rows, dest_cols, dest_rows, NumberOfFrames, image->BitsPerSample, interpolate);
124
0
                break;
125
0
            default:
126
0
                DCMIMAGE_WARN("invalid value for inter-representation");
127
0
        }
128
0
        checkInterData(0);
129
0
    }
130
0
}
131
132
133
DiColorImage::DiColorImage(const DiColorImage *image,
134
                           const int horz,
135
                           const int vert)
136
0
  : DiImage(image),
137
0
    RGBColorModel(image->RGBColorModel),
138
0
    InterData(NULL),
139
0
    OutputData(NULL)
140
0
{
141
0
    if (image->InterData != NULL)
142
0
    {
143
0
        switch (image->InterData->getRepresentation())
144
0
        {
145
0
            case EPR_Uint8:
146
0
                InterData = new DiColorFlipTemplate<Uint8>(image->InterData, Columns, Rows, NumberOfFrames, horz, vert);
147
0
                break;
148
0
            case EPR_Uint16:
149
0
                InterData = new DiColorFlipTemplate<Uint16>(image->InterData, Columns, Rows, NumberOfFrames, horz, vert);
150
0
                break;
151
0
            case EPR_Uint32:
152
0
                InterData = new DiColorFlipTemplate<Uint32>(image->InterData, Columns, Rows, NumberOfFrames, horz, vert);
153
0
                break;
154
0
            default:
155
0
                DCMIMAGE_WARN("invalid value for inter-representation");
156
0
        }
157
0
        checkInterData(0);
158
0
    }
159
0
}
160
161
162
DiColorImage::DiColorImage(const DiColorImage *image,
163
                           const int degree)
164
0
  : DiImage(image, degree),
165
0
    RGBColorModel(image->RGBColorModel),
166
0
    InterData(NULL),
167
0
    OutputData(NULL)
168
0
{
169
0
    if (image->InterData != NULL)
170
0
    {
171
0
        switch (image->InterData->getRepresentation())
172
0
        {
173
0
            case EPR_Uint8:
174
0
                InterData = new DiColorRotateTemplate<Uint8>(image->InterData, image->Columns, image->Rows, Columns, Rows,
175
0
                    NumberOfFrames, degree);
176
0
                break;
177
0
            case EPR_Uint16:
178
0
                InterData = new DiColorRotateTemplate<Uint16>(image->InterData, image->Columns, image->Rows, Columns, Rows,
179
0
                    NumberOfFrames, degree);
180
0
                break;
181
0
            case EPR_Uint32:
182
0
                InterData = new DiColorRotateTemplate<Uint32>(image->InterData, image->Columns, image->Rows, Columns, Rows,
183
0
                    NumberOfFrames, degree);
184
0
                break;
185
0
            default:
186
0
                DCMIMAGE_WARN("invalid value for inter-representation");
187
0
        }
188
0
        checkInterData(0);
189
0
    }
190
0
}
191
192
193
/*--------------*
194
 *  destructor  *
195
 *--------------*/
196
197
DiColorImage::~DiColorImage()
198
0
{
199
0
    delete InterData;
200
0
    delete OutputData;
201
0
}
202
203
204
/*********************************************************************/
205
206
207
int DiColorImage::checkInterData(const int mode)
208
0
{
209
0
    if (InterData == NULL)
210
0
    {
211
0
        if (ImageStatus == EIS_Normal)
212
0
        {
213
0
            ImageStatus = EIS_MemoryFailure;
214
0
            DCMIMAGE_ERROR("can't allocate memory for inter-representation");
215
0
        } else
216
0
            ImageStatus = EIS_InvalidImage;
217
0
    }
218
0
    else if (InterData->getData() == NULL)
219
0
        ImageStatus = EIS_InvalidImage;
220
0
    else if (mode && (ImageStatus == EIS_Normal) && isOriginal /* do not check derived images */)
221
0
    {
222
0
        const unsigned long count = OFstatic_cast(unsigned long, Columns) * OFstatic_cast(unsigned long, Rows) * NumberOfFrames;
223
0
        if ((InterData->getInputCount() != count) && ((InterData->getInputCount() >> 1) != ((count + 1) >> 1)))
224
0
        {
225
0
            DCMIMAGE_WARN("computed (" << count << ") and stored (" << InterData->getInputCount()
226
0
                << ") " << "pixel count differ");
227
0
        }
228
0
    }
229
0
    return (ImageStatus == EIS_Normal);
230
0
}
231
232
233
void DiColorImage::deleteOutputData()
234
0
{
235
0
    delete OutputData;
236
0
    OutputData = NULL;
237
0
}
238
239
240
unsigned long DiColorImage::getOutputDataSize(const int bits) const
241
0
{
242
0
    unsigned long result = 0;
243
0
    if ((ImageStatus == EIS_Normal) && (bits > 0) && (bits <= MAX_BITS))
244
0
    {
245
0
        int bytesPerPixel = 1;
246
0
        if (bits > 16)
247
0
            bytesPerPixel = 4;
248
0
        else if (bits > 8)
249
0
            bytesPerPixel = 2;
250
        /* compute number of bytes required to store a rendered frame */
251
0
        result = OFstatic_cast(unsigned long, Columns) * OFstatic_cast(unsigned long, Rows) * 3 /*samples*/ * bytesPerPixel;
252
0
    }
253
0
    return result;
254
0
}
255
256
257
const void *DiColorImage::getOutputData(const unsigned long frame,
258
                                        const int bits,
259
                                        const int planar)
260
0
{
261
0
    return getData(NULL, 0, frame, bits, planar);
262
0
}
263
264
265
int DiColorImage::getOutputData(void *buffer,
266
                                const unsigned long size,
267
                                const unsigned long frame,
268
                                const int bits,
269
                                const int planar)
270
0
{
271
0
    return (getData(buffer, size, frame, bits, planar) != NULL);
272
0
}
273
274
275
const void *DiColorImage::getData(void *buffer,
276
                                  const unsigned long size,
277
                                  const unsigned long frame,
278
                                  const int bits,
279
                                  const int planar)
280
0
{
281
0
    if ((InterData != NULL) && (ImageStatus == EIS_Normal) && (frame < NumberOfFrames) && (bits > 0) && (bits <= MAX_BITS))
282
0
    {
283
0
        if ((buffer == NULL) || (size >= getOutputDataSize(bits)))
284
0
        {
285
0
            deleteOutputData();                             // delete old image data
286
0
            const unsigned long count = OFstatic_cast(unsigned long, Columns) * OFstatic_cast(unsigned long, Rows);
287
0
            const int inverse = (Polarity == EPP_Reverse);
288
0
            switch (InterData->getRepresentation())
289
0
            {
290
0
                case EPR_Uint8:
291
0
                    if (bits <= 8)
292
0
                        OutputData = new DiColorOutputPixelTemplate<Uint8, Uint8>(buffer, InterData, count, frame,
293
0
                            getBits(), bits, planar, inverse);
294
0
                    else if (bits <= 16)
295
0
                        OutputData = new DiColorOutputPixelTemplate<Uint8, Uint16>(buffer, InterData, count, frame,
296
0
                            getBits(), bits, planar, inverse);
297
0
                    else
298
0
                        OutputData = new DiColorOutputPixelTemplate<Uint8, Uint32>(buffer, InterData, count, frame,
299
0
                            getBits(), bits, planar, inverse);
300
0
                    break;
301
0
                case EPR_Uint16:
302
0
                    if (bits <= 8)
303
0
                        OutputData = new DiColorOutputPixelTemplate<Uint16, Uint8>(buffer, InterData, count, frame,
304
0
                            getBits(), bits, planar, inverse);
305
0
                    else if (bits <= 16)
306
0
                        OutputData = new DiColorOutputPixelTemplate<Uint16, Uint16>(buffer, InterData, count, frame,
307
0
                            getBits(), bits, planar, inverse);
308
0
                    else
309
0
                        OutputData = new DiColorOutputPixelTemplate<Uint16, Uint32>(buffer, InterData, count, frame,
310
0
                            getBits(), bits, planar, inverse);
311
0
                    break;
312
0
                case EPR_Uint32:
313
0
                    if (bits <= 8)
314
0
                        OutputData = new DiColorOutputPixelTemplate<Uint32, Uint8>(buffer, InterData, count, frame,
315
0
                            getBits(), bits, planar, inverse);
316
0
                    else if (bits <= 16)
317
0
                        OutputData = new DiColorOutputPixelTemplate<Uint32, Uint16>(buffer, InterData, count, frame,
318
0
                            getBits(), bits, planar, inverse);
319
0
                    else
320
0
                        OutputData = new DiColorOutputPixelTemplate<Uint32, Uint32>(buffer, InterData, count, frame,
321
0
                            getBits(), bits, planar, inverse);
322
0
                    break;
323
0
                default:
324
0
                    DCMIMAGE_WARN("invalid value for inter-representation");
325
0
            }
326
0
            if (OutputData == NULL)
327
0
            {
328
0
                ImageStatus = EIS_MemoryFailure;
329
0
                DCMIMAGE_ERROR("can't allocate memory for inter-representation");
330
0
            }
331
0
            else
332
0
                return OutputData->getData();
333
0
        } else {
334
0
            DCMIMAGE_ERROR("given output buffer is too small (only " << size << " bytes)");
335
0
        }
336
0
    }
337
0
    return NULL;
338
0
}
339
340
341
const void *DiColorImage::getOutputPlane(const int plane) const
342
0
{
343
0
    if (OutputData != NULL)
344
0
        return OutputData->getPlane(plane);
345
0
    return NULL;
346
0
}
347
348
349
DiImage *DiColorImage::createImage(const unsigned long fstart,
350
                                   const unsigned long fcount) const
351
0
{
352
0
    DiImage *image = new DiColorImage(this, fstart, fcount);
353
0
    return image;
354
0
}
355
356
357
DiImage *DiColorImage::createScale(const signed long left_pos,
358
                                   const signed long top_pos,
359
                                   const unsigned long src_cols,
360
                                   const unsigned long src_rows,
361
                                   const unsigned long dest_cols,
362
                                   const unsigned long dest_rows,
363
                                   const int interpolate,
364
                                   const int aspect,
365
                                   const Uint16 /*pvalue*/) const
366
0
{
367
0
    DiImage *image = new DiColorImage(this, left_pos, top_pos, OFstatic_cast(Uint16, src_cols), OFstatic_cast(Uint16, src_rows),
368
0
        OFstatic_cast(Uint16, dest_cols), OFstatic_cast(Uint16, dest_rows), interpolate, aspect);
369
0
    return image;
370
0
}
371
372
373
int DiColorImage::flip(const int horz,
374
                       const int vert)
375
0
{
376
0
    switch (InterData->getRepresentation())
377
0
    {
378
0
        case EPR_Uint8:
379
0
            {
380
0
                DiFlipTemplate<Uint8> dummy(InterData, Columns, Rows, NumberOfFrames, horz, vert);
381
0
            }
382
0
            break;
383
0
        case EPR_Uint16:
384
0
            {
385
0
                DiFlipTemplate<Uint16> dummy(InterData, Columns, Rows, NumberOfFrames, horz, vert);
386
0
            }
387
0
            break;
388
0
        case EPR_Uint32:
389
0
            {
390
0
                DiFlipTemplate<Uint32> dummy(InterData, Columns, Rows, NumberOfFrames, horz, vert);
391
0
            }
392
0
            break;
393
0
        default:
394
0
            DCMIMAGE_WARN("invalid value for inter-representation");
395
0
    }
396
0
    return 1;
397
0
}
398
399
400
DiImage *DiColorImage::createFlip(const int horz,
401
                                  const int vert) const
402
0
{
403
0
    DiImage *image = new DiColorImage(this, horz, vert);
404
0
    return image;
405
0
}
406
407
408
int DiColorImage::rotate(const int degree)
409
0
{
410
0
    const Uint16 old_cols = Columns;                // save old values
411
0
    const Uint16 old_rows = Rows;
412
0
    DiImage::rotate(degree);                        // swap width and height if necessary
413
0
    if ((Columns > 1) && (Rows > 1))
414
0
    {
415
0
        switch (InterData->getRepresentation())
416
0
        {
417
0
            case EPR_Uint8:
418
0
                {
419
0
                    DiRotateTemplate<Uint8> dummy(InterData, old_cols, old_rows, Columns, Rows,
420
0
                        NumberOfFrames, degree);
421
0
                }
422
0
                break;
423
0
            case EPR_Uint16:
424
0
                {
425
0
                    DiRotateTemplate<Uint16> dummy(InterData, old_cols, old_rows, Columns, Rows,
426
0
                        NumberOfFrames, degree);
427
0
                }
428
0
                break;
429
0
            case EPR_Uint32:
430
0
                {
431
0
                    DiRotateTemplate<Uint32> dummy(InterData, old_cols, old_rows, Columns, Rows,
432
0
                        NumberOfFrames, degree);
433
0
                }
434
0
                break;
435
0
            default:
436
0
                DCMIMAGE_WARN("invalid value for inter-representation");
437
0
        }
438
0
    }
439
0
    return 1;
440
0
}
441
442
443
DiImage *DiColorImage::createRotate(const int degree) const
444
0
{
445
0
    DiImage *image = new DiColorImage(this, degree);
446
0
    return image;
447
0
}
448
449
450
DiImage *DiColorImage::createMono(const double red,
451
                                  const double green,
452
                                  const double blue) const
453
0
{
454
0
    DiImage *image = NULL;
455
0
    if (RGBColorModel)
456
0
        image = new DiMono2Image(this, red, green, blue);
457
0
    return image;
458
0
}
459
460
461
unsigned long DiColorImage::createDIB(void *&data,
462
                                      const unsigned long size,
463
                                      const unsigned long frame,
464
                                      const int bits,
465
                                      const int upsideDown,
466
                                      const int padding)
467
0
{
468
0
    if (RGBColorModel && (InterData != NULL))
469
0
    {
470
0
        if (size == 0)
471
0
            data = NULL;
472
0
        if ((bits == 24) || (bits == 32))
473
0
            return InterData->createDIB(data, size, Columns, Rows, frame, getBits() /*fromBits*/, 8 /*toBits*/, bits /*mode*/, upsideDown, padding);
474
0
    }
475
0
    return 0;
476
0
}
477
478
479
unsigned long DiColorImage::createAWTBitmap(void *&data,
480
                                            const unsigned long frame,
481
                                            const int bits)
482
0
{
483
0
    if (RGBColorModel && (InterData != NULL) && (bits == 32))
484
0
        return InterData->createAWTBitmap(data, Columns, Rows, frame, getBits() /*fromBits*/, 8 /*toBits*/);
485
0
    return 0;
486
0
}
487
488
489
void DiColorImage::updateImagePixelModuleAttributes(DcmItem &dataset)
490
0
{
491
0
    DiImage::updateImagePixelModuleAttributes(dataset);
492
    /* remove color palette LUTs (if any) */
493
0
    delete dataset.remove(DCM_PaletteColorLookupTableUID);
494
0
    delete dataset.remove(DCM_RedPaletteColorLookupTableDescriptor);
495
0
    delete dataset.remove(DCM_GreenPaletteColorLookupTableDescriptor);
496
0
    delete dataset.remove(DCM_BluePaletteColorLookupTableDescriptor);
497
0
    delete dataset.remove(DCM_RedPaletteColorLookupTableData);
498
0
    delete dataset.remove(DCM_GreenPaletteColorLookupTableData);
499
0
    delete dataset.remove(DCM_BluePaletteColorLookupTableData);
500
0
    delete dataset.remove(DCM_SegmentedRedPaletteColorLookupTableData);
501
0
    delete dataset.remove(DCM_SegmentedGreenPaletteColorLookupTableData);
502
0
    delete dataset.remove(DCM_SegmentedBluePaletteColorLookupTableData);
503
0
}
504
505
506
// --- write current image to DICOM dataset
507
508
int DiColorImage::writeImageToDataset(DcmItem &dataset,
509
                                      const int /*mode*/,
510
                                      const int planar)
511
0
{
512
0
    int result = 0;
513
0
    if ((InterData != NULL) && (InterData->getCount() > 0) && (BitsPerSample > 0))
514
0
    {
515
        /* create new pixel data element */
516
0
        DcmPixelData *pixel = new DcmPixelData(DCM_PixelData);
517
0
        if (pixel != NULL)
518
0
        {
519
0
            OFBool ok = OFFalse;
520
            /* number of samples */
521
0
            const unsigned long count = InterData->getCount() * 3 /*planes*/;
522
            /* number of pixels per frame */
523
0
            const unsigned long fcount = OFstatic_cast(unsigned long, Columns) * OFstatic_cast(unsigned long, Rows);
524
            /* determine planar configuration */
525
0
            const int planarConfig = (planar == 2) ? InterData->getPlanarConfiguration() : ((planar == 0) ? 0 : 1);
526
0
            switch (InterData->getRepresentation())
527
0
            {
528
0
                case EPR_Uint8:
529
0
                case EPR_Sint8:
530
0
                {
531
                    /* write 8 bit pixel data (OB) */
532
0
                    Uint8 *data = NULL;
533
0
                    if (pixel->createUint8Array(count, data).good() &&
534
0
                        InterData->getPixelData(OFstatic_cast(void *, data), OFstatic_cast(size_t, count), fcount, NumberOfFrames, planarConfig))
535
0
                    {
536
0
                        ok = OFTrue;
537
0
                    }
538
0
                    break;
539
0
                }
540
0
                case EPR_Uint16:
541
0
                case EPR_Sint16:
542
0
                {
543
                    /* write 16 bit pixel data (OW) */
544
0
                    Uint16 *data = NULL;
545
0
                    if (pixel->createUint16Array(count, data).good() &&
546
0
                        InterData->getPixelData(OFstatic_cast(void *, data), OFstatic_cast(size_t, count), fcount, NumberOfFrames, planarConfig))
547
0
                    {
548
0
                        ok = OFTrue;
549
0
                    }
550
0
                    break;
551
0
                }
552
0
                case EPR_Uint32:
553
0
                case EPR_Sint32:
554
0
                {
555
                    /* write 32 bit pixel data (OW) */
556
0
                    Uint16 *data = NULL;
557
0
                    if (pixel->createUint16Array(count * 2 /*double-words*/, data).good() &&
558
0
                        InterData->getPixelData(OFstatic_cast(void *, data), OFstatic_cast(size_t, count), fcount, NumberOfFrames, planarConfig))
559
0
                    {
560
0
                        ok = OFTrue;
561
0
                    }
562
0
                    break;
563
0
                }
564
0
            }
565
            /* check whether pixel data has been written and insert element into the dataset */
566
0
            if (ok && dataset.insert(pixel, OFTrue /*replaceOld*/).good())
567
0
            {
568
0
                char numBuf[20];
569
                /* set color model */
570
0
                if (getInternalColorModel() == EPI_YBR_Full)
571
0
                    dataset.putAndInsertString(DCM_PhotometricInterpretation, "YBR_FULL");
572
0
                else
573
0
                    dataset.putAndInsertString(DCM_PhotometricInterpretation, "RGB");
574
                /* set image resolution */
575
0
                dataset.putAndInsertUint16(DCM_Columns, Columns);
576
0
                dataset.putAndInsertUint16(DCM_Rows, Rows);
577
0
#ifdef PRId32
578
0
                OFStandard::snprintf(numBuf, sizeof(numBuf), "%" PRId32, NumberOfFrames);
579
#elif SIZEOF_LONG == 8
580
                OFStandard::snprintf(numBuf, sizeof(numBuf), "%d", NumberOfFrames);
581
#else
582
                OFStandard::snprintf(numBuf, sizeof(numBuf), "%ld", NumberOfFrames);
583
#endif
584
0
                dataset.putAndInsertString(DCM_NumberOfFrames, numBuf);
585
0
                dataset.putAndInsertUint16(DCM_SamplesPerPixel, 3);
586
0
                dataset.putAndInsertUint16(DCM_PlanarConfiguration, OFstatic_cast(const Uint16, planarConfig));
587
                /* set pixel encoding and data */
588
0
                switch (InterData->getRepresentation())
589
0
                {
590
0
                    case EPR_Uint8:
591
0
                        dataset.putAndInsertUint16(DCM_BitsAllocated, 8);
592
0
                        dataset.putAndInsertUint16(DCM_PixelRepresentation, 0);
593
0
                        break;
594
0
                    case EPR_Sint8:
595
0
                        dataset.putAndInsertUint16(DCM_BitsAllocated, 8);
596
0
                        dataset.putAndInsertUint16(DCM_PixelRepresentation, 1);
597
0
                        break;
598
0
                    case EPR_Uint16:
599
0
                        dataset.putAndInsertUint16(DCM_BitsAllocated, 16);
600
0
                        dataset.putAndInsertUint16(DCM_PixelRepresentation, 0);
601
0
                        break;
602
0
                    case EPR_Sint16:
603
0
                        dataset.putAndInsertUint16(DCM_BitsAllocated, 16);
604
0
                        dataset.putAndInsertUint16(DCM_PixelRepresentation, 1);
605
0
                        break;
606
0
                    case EPR_Uint32:
607
0
                        dataset.putAndInsertUint16(DCM_BitsAllocated, 32);
608
0
                        dataset.putAndInsertUint16(DCM_PixelRepresentation, 0);
609
0
                        break;
610
0
                    case EPR_Sint32:
611
0
                        dataset.putAndInsertUint16(DCM_BitsAllocated, 32);
612
0
                        dataset.putAndInsertUint16(DCM_PixelRepresentation, 1);
613
0
                        break;
614
0
                }
615
0
                dataset.putAndInsertUint16(DCM_BitsStored, OFstatic_cast(Uint16, BitsPerSample));
616
0
                dataset.putAndInsertUint16(DCM_HighBit, OFstatic_cast(Uint16, (BitsPerSample - 1)));
617
                /* update other DICOM attributes */
618
0
                updateImagePixelModuleAttributes(dataset);
619
0
                result = 1;
620
0
            } else {
621
                /* pixel data element has not been inserted, thus delete it */
622
0
                delete pixel;
623
0
            }
624
0
        }
625
0
    }
626
0
    return result;
627
0
}
628
629
630
int DiColorImage::writePPM(STD_NAMESPACE ostream& stream,
631
                           const unsigned long frame,
632
                           const int bits)
633
0
{
634
0
    if (RGBColorModel)
635
0
    {
636
0
        getOutputData(frame, bits);
637
0
        if (OutputData != NULL)
638
0
        {
639
0
            stream << "P3" << OFendl;
640
0
            stream << Columns << " " << Rows << OFendl;
641
0
            stream << DicomImageClass::maxval(bits) << OFendl;
642
0
            int ok = OutputData->writePPM(stream);
643
0
            deleteOutputData();
644
0
            return ok;
645
0
        }
646
0
    }
647
0
    return 0;
648
0
}
649
650
651
int DiColorImage::writePPM(FILE *stream,
652
                           const unsigned long frame,
653
                           const int bits)
654
0
{
655
0
    if (RGBColorModel)
656
0
    {
657
0
        if (stream != NULL)
658
0
        {
659
0
            getOutputData(frame, bits);
660
0
            if (OutputData != NULL)
661
0
            {
662
0
                fprintf(stream, "P3\n%u %u\n%lu\n", Columns, Rows, DicomImageClass::maxval(bits));
663
0
                int ok = OutputData->writePPM(stream);
664
0
                deleteOutputData();
665
0
                return ok;
666
0
            }
667
0
        }
668
0
    }
669
0
    return 0;
670
0
}
671
672
673
int DiColorImage::writeRawPPM(FILE *stream,
674
                              const unsigned long frame,
675
                              const int bits)
676
0
{
677
0
    if (RGBColorModel)
678
0
    {
679
0
        if ((stream != NULL) && (bits <= MAX_RAWPPM_BITS))
680
0
        {
681
0
            getOutputData(frame, bits);
682
0
            if ((OutputData != NULL) && (OutputData->getData() != NULL))
683
0
            {
684
0
                fprintf(stream, "P6\n%u %u\n%lu\n", Columns, Rows, DicomImageClass::maxval(bits));
685
0
                const size_t count = OFstatic_cast(size_t, OutputData->getCount());
686
0
                if (fwrite(OutputData->getData(), OutputData->getItemSize(), count, stream) == count)
687
0
                    return 1;
688
0
            }
689
0
        }
690
0
    }
691
0
    return 0;
692
0
}
693
694
695
int DiColorImage::writeBMP(FILE *stream,
696
                           const unsigned long frame,
697
                           const int bits)
698
0
{
699
0
    if (RGBColorModel && ((bits == 0) || (bits == 24) || (bits == 32)))
700
0
        return DiImage::writeBMP(stream, frame, (bits == 0) ? 24 : bits);
701
0
    return 0;
702
0
}