00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef DICOOPXT_H
00031 #define DICOOPXT_H
00032
00033 #include "dcmtk/config/osconfig.h"
00034
00035 #include "dcmtk/dcmimage/dicoopx.h"
00036 #include "dcmtk/dcmimage/dicopx.h"
00037 #include "dcmtk/dcmimgle/dipxrept.h"
00038
00039 #include "dcmtk/ofstd/ofbmanip.h"
00040
00041
00042
00043
00044
00045
00048 template<class T1, class T2>
00049 class DiColorOutputPixelTemplate
00050 : public DiColorOutputPixel,
00051 public DiPixelRepresentationTemplate<T2>
00052 {
00053
00054 public:
00055
00067 DiColorOutputPixelTemplate(void *buffer,
00068 const DiColorPixel *pixel,
00069 const unsigned long count,
00070 const unsigned long frame,
00071 const int bits1,
00072 const int bits2,
00073 const int planar,
00074 const int inverse)
00075 : DiColorOutputPixel(pixel, count, frame),
00076 Data(NULL),
00077 DeleteData(buffer == NULL),
00078 isPlanar(planar)
00079 {
00080 if ((pixel != NULL) && (Count > 0) && (FrameSize >= Count))
00081 {
00082 Data = OFstatic_cast(T2 *, buffer);
00083 convert(OFstatic_cast(const T1 **, OFconst_cast(void *, pixel->getData())), frame * FrameSize, bits1, bits2, planar, inverse);
00084 }
00085 }
00086
00096 DiColorOutputPixelTemplate(void *buffer,
00097 const DiPixel *pixel,
00098 const unsigned long count,
00099 const unsigned long frame,
00100 const unsigned long ,
00101 const int planar)
00102 : DiColorOutputPixel(pixel, count, frame),
00103 Data(NULL),
00104 DeleteData(buffer == NULL),
00105 isPlanar(planar)
00106 {
00107 if ((pixel != NULL) && (Count > 0) && (FrameSize >= Count))
00108 Data = OFstatic_cast(T2 *, buffer);
00109 }
00110
00113 virtual ~DiColorOutputPixelTemplate()
00114 {
00115 if (DeleteData)
00116 delete[] Data;
00117 }
00118
00123 inline EP_Representation getRepresentation() const
00124 {
00125 return DiPixelRepresentationTemplate<T2>::getRepresentation();
00126 }
00127
00132 inline size_t getItemSize() const
00133 {
00134 return sizeof(T2) * 3;
00135 }
00136
00141 inline const void *getData() const
00142 {
00143 return OFstatic_cast(const void *, Data);
00144 }
00145
00150 virtual void *getDataPtr()
00151 {
00152 return OFstatic_cast(void *, Data);
00153 }
00154
00161 inline const void *getPlane(const int plane) const
00162 {
00163 void *result = NULL;
00164 if (Data != NULL)
00165 {
00166 if (plane <= 0)
00167 result = OFstatic_cast(void *, Data);
00168 else
00169 {
00170 if (isPlanar)
00171 result = OFstatic_cast(void *, Data + ((plane == 1) ? 1 : 2) * FrameSize);
00172 else
00173 result = OFstatic_cast(void *, Data + ((plane == 1) ? 1 : 2));
00174 }
00175 }
00176 return result;
00177 }
00178
00185 int writePPM(STD_NAMESPACE ostream& stream) const
00186 {
00187 if (Data != NULL)
00188 {
00189 register T2 *p = Data;
00190 register unsigned long i;
00191 register int j;
00192 for (i = FrameSize; i != 0; --i)
00193 for (j = 3; j != 0; --j)
00194 stream << OFstatic_cast(unsigned long, *(p++)) << " ";
00195 return 1;
00196 }
00197 return 0;
00198 }
00199
00206 int writePPM(FILE *stream) const
00207 {
00208 if (Data != NULL)
00209 {
00210 register T2 *p = Data;
00211 register unsigned long i;
00212 register int j;
00213 for (i = FrameSize; i != 0; --i)
00214 for (j = 3; j != 0; --j)
00215 fprintf(stream, "%lu ", OFstatic_cast(unsigned long, *(p++)));
00216 return 1;
00217 }
00218 return 0;
00219 }
00220
00221
00222 protected:
00223
00225 T2 *Data;
00226
00227
00228 private:
00229
00239 void convert(const T1 *pixel[3],
00240 const unsigned long start,
00241 const int bits1,
00242 const int bits2,
00243 const int planar,
00244 const int inverse)
00245 {
00246 if ((pixel[0] != NULL) && (pixel[1] != NULL) && (pixel[2] != NULL))
00247 {
00248 if (Data == NULL)
00249 Data = new T2[FrameSize * 3];
00250 if (Data != NULL)
00251 {
00252 DCMIMAGE_DEBUG("converting color pixel data to output format");
00253 register T2 *q = Data;
00254 register unsigned long i;
00255 const T2 max2 = OFstatic_cast(T2, DicomImageClass::maxval(bits2));
00256 if (planar)
00257 {
00258 register const T1 *p;
00259 if (bits1 == bits2)
00260 {
00261 for (int j = 0; j < 3; ++j)
00262 {
00263 p = pixel[j] + start;
00264
00265 if (inverse)
00266 {
00267 for (i = Count; i != 0; --i)
00268 *(q++) = max2 - OFstatic_cast(T2, *(p++));
00269 } else {
00270 for (i = Count; i != 0; --i)
00271 *(q++) = OFstatic_cast(T2, *(p++));
00272 }
00273 if (Count < FrameSize)
00274 {
00275 OFBitmanipTemplate<T2>::zeroMem(q, FrameSize - Count);
00276 q += (FrameSize - Count);
00277 }
00278 }
00279 }
00280 else if (bits1 < bits2)
00281 {
00282 const double gradient1 = OFstatic_cast(double, DicomImageClass::maxval(bits2)) /
00283 OFstatic_cast(double, DicomImageClass::maxval(bits1));
00284 const T2 gradient2 = OFstatic_cast(T2, gradient1);
00285 for (int j = 0; j < 3; ++j)
00286 {
00287 p = pixel[j] + start;
00288 if (gradient1 == OFstatic_cast(double, gradient2))
00289 {
00290
00291 if (inverse)
00292 {
00293 for (i = Count; i != 0; --i)
00294 *(q++) = max2 - OFstatic_cast(T2, *(p++)) * gradient2;
00295 } else {
00296 for (i = Count; i != 0; --i)
00297 *(q++) = OFstatic_cast(T2, *(p++)) * gradient2;
00298 }
00299 } else {
00300
00301 if (inverse)
00302 {
00303 for (i = Count; i != 0; --i)
00304 *(q++) = max2 - OFstatic_cast(T2, OFstatic_cast(double, *(p++)) * gradient1);
00305 } else {
00306 for (i = Count; i != 0; --i)
00307 *(q++) = OFstatic_cast(T2, OFstatic_cast(double, *(p++)) * gradient1);
00308 }
00309 }
00310 if (Count < FrameSize)
00311 {
00312 OFBitmanipTemplate<T2>::zeroMem(q, FrameSize - Count);
00313 q += (FrameSize - Count);
00314 }
00315 }
00316 }
00317 else
00318 {
00319 const int shift = bits1 - bits2;
00320 for (int j = 0; j < 3; ++j)
00321 {
00322 p = pixel[j] + start;
00323
00324 if (inverse)
00325 {
00326 for (i = Count; i != 0; --i)
00327 *(q++) = max2 - OFstatic_cast(T2, *(p++) >> shift);
00328 } else {
00329 for (i = Count; i != 0; --i)
00330 *(q++) = OFstatic_cast(T2, *(p++) >> shift);
00331 }
00332 if (Count < FrameSize)
00333 {
00334 OFBitmanipTemplate<T2>::zeroMem(q, FrameSize - Count);
00335 q += (FrameSize - Count);
00336 }
00337 }
00338 }
00339 }
00340 else
00341 {
00342 register int j;
00343 if (bits1 == bits2)
00344 {
00345
00346 if (inverse)
00347 {
00348 for (i = start; i < start + Count; ++i)
00349 for (j = 0; j < 3; ++j)
00350 *(q++) = max2 - OFstatic_cast(T2, pixel[j][i]);
00351 } else {
00352 for (i = start; i < start + Count; ++i)
00353 for (j = 0; j < 3; ++j)
00354 *(q++) = OFstatic_cast(T2, pixel[j][i]);
00355 }
00356 }
00357 else if (bits1 < bits2)
00358 {
00359 const double gradient1 = OFstatic_cast(double, DicomImageClass::maxval(bits2)) /
00360 OFstatic_cast(double, DicomImageClass::maxval(bits1));
00361 const T2 gradient2 = OFstatic_cast(T2, gradient1);
00362 if (gradient1 == OFstatic_cast(double, gradient2))
00363 {
00364
00365 if (inverse)
00366 {
00367 for (i = start; i < start + Count; ++i)
00368 for (j = 0; j < 3; ++j)
00369 *(q++) = max2 - OFstatic_cast(T2, pixel[j][i]) * gradient2;
00370 } else {
00371 for (i = start; i < start + Count; ++i)
00372 for (j = 0; j < 3; ++j)
00373 *(q++) = OFstatic_cast(T2, pixel[j][i]) * gradient2;
00374 }
00375 } else {
00376
00377 if (inverse)
00378 {
00379 for (i = start; i < start + Count; ++i)
00380 for (j = 0; j < 3; ++j)
00381 *(q++) = max2 - OFstatic_cast(T2, OFstatic_cast(double, pixel[j][i]) * gradient1);
00382 } else {
00383 for (i = start; i < start + Count; ++i)
00384 for (j = 0; j < 3; ++j)
00385 *(q++) = OFstatic_cast(T2, OFstatic_cast(double, pixel[j][i]) * gradient1);
00386 }
00387 }
00388 }
00389 else
00390 {
00391 const int shift = bits1 - bits2;
00392
00393 if (inverse)
00394 {
00395 for (i = start; i < start + Count; ++i)
00396 for (j = 0; j < 3; ++j)
00397 *(q++) = max2 - OFstatic_cast(T2, pixel[j][i] >> shift);
00398 } else {
00399 for (i = start; i < start + Count; ++i)
00400 for (j = 0; j < 3; ++j)
00401 *(q++) = OFstatic_cast(T2, pixel[j][i] >> shift);
00402 }
00403 }
00404 if (Count < FrameSize)
00405 OFBitmanipTemplate<T2>::zeroMem(q, 3 * (FrameSize - Count));
00406 }
00407 }
00408 } else
00409 Data = NULL;
00410 }
00411
00413 int DeleteData;
00415 int isPlanar;
00416
00417
00418
00419 DiColorOutputPixelTemplate(const DiColorOutputPixelTemplate<T1,T2> &);
00420 DiColorOutputPixelTemplate<T1,T2> &operator=(const DiColorOutputPixelTemplate<T1,T2> &);
00421 };
00422
00423
00424 #endif
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522