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
00031
00032
00033
00034 #ifndef DICOOPXT_H
00035 #define DICOOPXT_H
00036
00037 #include "dcmtk/config/osconfig.h"
00038 #include "dcmtk/dcmdata/dctypes.h"
00039
00040 #include "dcmtk/dcmimage/dicoopx.h"
00041 #include "dcmtk/dcmimage/dicopx.h"
00042 #include "dcmtk/dcmimgle/dipxrept.h"
00043 #include "dcmtk/dcmimgle/diutils.h"
00044
00045
00046
00047
00048
00049
00052 template<class T1, class T2>
00053 class DiColorOutputPixelTemplate
00054 : public DiColorOutputPixel,
00055 public DiPixelRepresentationTemplate<T2>
00056 {
00057
00058 public:
00059
00071 DiColorOutputPixelTemplate(void *buffer,
00072 const DiColorPixel *pixel,
00073 const unsigned long count,
00074 const unsigned long frame,
00075 const int bits1,
00076 const int bits2,
00077 const int planar,
00078 const int inverse)
00079 : DiColorOutputPixel(pixel, count, frame),
00080 Data(NULL),
00081 DeleteData(buffer == NULL),
00082 isPlanar(planar)
00083 {
00084 if ((pixel != NULL) && (Count > 0) && (FrameSize >= Count))
00085 {
00086 Data = OFstatic_cast(T2 *, buffer);
00087 convert(OFstatic_cast(const T1 **, OFconst_cast(void *, pixel->getData())), frame * FrameSize, bits1, bits2, planar, inverse);
00088 }
00089 }
00090
00100 DiColorOutputPixelTemplate(void *buffer,
00101 const DiPixel *pixel,
00102 const unsigned long count,
00103 const unsigned long frame,
00104 const unsigned long ,
00105 const int planar)
00106 : DiColorOutputPixel(pixel, count, frame),
00107 Data(NULL),
00108 DeleteData(buffer == NULL),
00109 isPlanar(planar)
00110 {
00111 if ((pixel != NULL) && (Count > 0) && (FrameSize >= Count))
00112 Data = OFstatic_cast(T2 *, buffer);
00113 }
00114
00117 virtual ~DiColorOutputPixelTemplate()
00118 {
00119 if (DeleteData)
00120 delete[] Data;
00121 }
00122
00127 inline EP_Representation getRepresentation() const
00128 {
00129 return DiPixelRepresentationTemplate<T2>::getRepresentation();
00130 }
00131
00136 inline size_t getItemSize() const
00137 {
00138 return sizeof(T2) * 3;
00139 }
00140
00145 inline const void *getData() const
00146 {
00147 return OFstatic_cast(const void *, Data);
00148 }
00149
00154 virtual void *getDataPtr()
00155 {
00156 return OFstatic_cast(void *, Data);
00157 }
00158
00165 inline const void *getPlane(const int plane) const
00166 {
00167 void *result = NULL;
00168 if (Data != NULL)
00169 {
00170 if (plane <= 0)
00171 result = OFstatic_cast(void *, Data);
00172 else
00173 {
00174 if (isPlanar)
00175 result = OFstatic_cast(void *, Data + ((plane == 1) ? 1 : 2) * FrameSize);
00176 else
00177 result = OFstatic_cast(void *, Data + ((plane == 1) ? 1 : 2));
00178 }
00179 }
00180 return result;
00181 }
00182
00189 int writePPM(ostream &stream) const
00190 {
00191 if (Data != NULL)
00192 {
00193 register T2 *p = Data;
00194 register unsigned long i;
00195 register int j;
00196 for (i = FrameSize; i != 0; --i)
00197 for (j = 3; j != 0; --j)
00198 stream << OFstatic_cast(unsigned long, *(p++)) << " ";
00199 return 1;
00200 }
00201 return 0;
00202 }
00203
00210 int writePPM(FILE *stream) const
00211 {
00212 if (Data != NULL)
00213 {
00214 register T2 *p = Data;
00215 register unsigned long i;
00216 register int j;
00217 for (i = FrameSize; i != 0; --i)
00218 for (j = 3; j != 0; --j)
00219 fprintf(stream, "%lu ", OFstatic_cast(unsigned long, *(p++)));
00220 return 1;
00221 }
00222 return 0;
00223 }
00224
00225
00226 protected:
00227
00229 T2 *Data;
00230
00231
00232 private:
00233
00243 void convert(const T1 *pixel[3],
00244 const unsigned long start,
00245 const int bits1,
00246 const int bits2,
00247 const int planar,
00248 const int inverse)
00249 {
00250 if ((pixel[0] != NULL) && (pixel[1] != NULL) && (pixel[2] != NULL))
00251 {
00252 if (Data == NULL)
00253 Data = new T2[FrameSize * 3];
00254 if (Data != NULL)
00255 {
00256 register T2 *q = Data;
00257 register unsigned long i;
00258 const T2 max2 = OFstatic_cast(T2, DicomImageClass::maxval(bits2));
00259 if (planar)
00260 {
00261 register const T1 *p;
00262 if (bits1 == bits2)
00263 {
00264 for (int j = 0; j < 3; ++j)
00265 {
00266 p = pixel[j] + start;
00267
00268 if (inverse)
00269 {
00270 for (i = Count; i != 0; --i)
00271 *(q++) = max2 - OFstatic_cast(T2, *(p++));
00272 } else {
00273 for (i = Count; i != 0; --i)
00274 *(q++) = OFstatic_cast(T2, *(p++));
00275 }
00276 if (Count < FrameSize)
00277 {
00278 OFBitmanipTemplate<T2>::zeroMem(q, FrameSize - Count);
00279 q += (FrameSize - Count);
00280 }
00281 }
00282 }
00283 else if (bits1 < bits2)
00284 {
00285 const double gradient1 = OFstatic_cast(double, DicomImageClass::maxval(bits2)) /
00286 OFstatic_cast(double, DicomImageClass::maxval(bits1));
00287 const T2 gradient2 = OFstatic_cast(T2, gradient1);
00288 for (int j = 0; j < 3; ++j)
00289 {
00290 p = pixel[j] + start;
00291 if (gradient1 == OFstatic_cast(double, gradient2))
00292 {
00293
00294 if (inverse)
00295 {
00296 for (i = Count; i != 0; --i)
00297 *(q++) = max2 - OFstatic_cast(T2, *(p++)) * gradient2;
00298 } else {
00299 for (i = Count; i != 0; --i)
00300 *(q++) = OFstatic_cast(T2, *(p++)) * gradient2;
00301 }
00302 } else {
00303
00304 if (inverse)
00305 {
00306 for (i = Count; i != 0; --i)
00307 *(q++) = max2 - OFstatic_cast(T2, OFstatic_cast(double, *(p++)) * gradient1);
00308 } else {
00309 for (i = Count; i != 0; --i)
00310 *(q++) = OFstatic_cast(T2, OFstatic_cast(double, *(p++)) * gradient1);
00311 }
00312 }
00313 if (Count < FrameSize)
00314 {
00315 OFBitmanipTemplate<T2>::zeroMem(q, FrameSize - Count);
00316 q += (FrameSize - Count);
00317 }
00318 }
00319 }
00320 else
00321 {
00322 const int shift = bits1 - bits2;
00323 for (int j = 0; j < 3; ++j)
00324 {
00325 p = pixel[j] + start;
00326
00327 if (inverse)
00328 {
00329 for (i = Count; i != 0; --i)
00330 *(q++) = max2 - OFstatic_cast(T2, *(p++) >> shift);
00331 } else {
00332 for (i = Count; i != 0; --i)
00333 *(q++) = OFstatic_cast(T2, *(p++) >> shift);
00334 }
00335 if (Count < FrameSize)
00336 {
00337 OFBitmanipTemplate<T2>::zeroMem(q, FrameSize - Count);
00338 q += (FrameSize - Count);
00339 }
00340 }
00341 }
00342 }
00343 else
00344 {
00345 register int j;
00346 if (bits1 == bits2)
00347 {
00348
00349 if (inverse)
00350 {
00351 for (i = start; i < start + Count; ++i)
00352 for (j = 0; j < 3; ++j)
00353 *(q++) = max2 - OFstatic_cast(T2, pixel[j][i]);
00354 } else {
00355 for (i = start; i < start + Count; ++i)
00356 for (j = 0; j < 3; ++j)
00357 *(q++) = OFstatic_cast(T2, pixel[j][i]);
00358 }
00359 }
00360 else if (bits1 < bits2)
00361 {
00362 const double gradient1 = OFstatic_cast(double, DicomImageClass::maxval(bits2)) /
00363 OFstatic_cast(double, DicomImageClass::maxval(bits1));
00364 const T2 gradient2 = OFstatic_cast(T2, gradient1);
00365 if (gradient1 == OFstatic_cast(double, gradient2))
00366 {
00367
00368 if (inverse)
00369 {
00370 for (i = start; i < start + Count; ++i)
00371 for (j = 0; j < 3; ++j)
00372 *(q++) = max2 - OFstatic_cast(T2, pixel[j][i]) * gradient2;
00373 } else {
00374 for (i = start; i < start + Count; ++i)
00375 for (j = 0; j < 3; ++j)
00376 *(q++) = OFstatic_cast(T2, pixel[j][i]) * gradient2;
00377 }
00378 } else {
00379
00380 if (inverse)
00381 {
00382 for (i = start; i < start + Count; ++i)
00383 for (j = 0; j < 3; ++j)
00384 *(q++) = max2 - OFstatic_cast(T2, OFstatic_cast(double, pixel[j][i]) * gradient1);
00385 } else {
00386 for (i = start; i < start + Count; ++i)
00387 for (j = 0; j < 3; ++j)
00388 *(q++) = OFstatic_cast(T2, OFstatic_cast(double, pixel[j][i]) * gradient1);
00389 }
00390 }
00391 }
00392 else
00393 {
00394 const int shift = bits1 - bits2;
00395
00396 if (inverse)
00397 {
00398 for (i = start; i < start + Count; ++i)
00399 for (j = 0; j < 3; ++j)
00400 *(q++) = max2 - OFstatic_cast(T2, pixel[j][i] >> shift);
00401 } else {
00402 for (i = start; i < start + Count; ++i)
00403 for (j = 0; j < 3; ++j)
00404 *(q++) = OFstatic_cast(T2, pixel[j][i] >> shift);
00405 }
00406 }
00407 if (Count < FrameSize)
00408 OFBitmanipTemplate<T2>::zeroMem(q, 3 * (FrameSize - Count));
00409 }
00410 }
00411 } else
00412 Data = NULL;
00413 }
00414
00416 int DeleteData;
00418 int isPlanar;
00419
00420
00421
00422 DiColorOutputPixelTemplate(const DiColorOutputPixelTemplate<T1,T2> &);
00423 DiColorOutputPixelTemplate<T1,T2> &operator=(const DiColorOutputPixelTemplate<T1,T2> &);
00424 };
00425
00426
00427 #endif
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