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 DICOPXT_H
00035 #define DICOPXT_H
00036
00037 #include "dcmtk/config/osconfig.h"
00038 #include "dcmtk/dcmdata/dctypes.h"
00039 #include "dcmtk/ofstd/ofbmanip.h"
00040
00041 #include "dcmtk/dcmimage/dicopx.h"
00042 #include "dcmtk/dcmimgle/dipxrept.h"
00043
00044
00045
00046
00047
00048 inline Uint8 removeSign(const Uint8 value, const Uint8)
00049 {
00050 return value;
00051 }
00052
00053
00054 inline Uint16 removeSign(const Uint16 value, const Uint16)
00055 {
00056 return value;
00057 }
00058
00059
00060 inline Uint32 removeSign(const Uint32 value, const Uint32)
00061 {
00062 return value;
00063 }
00064
00065
00066 inline Uint8 removeSign(const Sint8 value, const Sint8 offset)
00067 {
00068 return OFstatic_cast(Uint8, OFstatic_cast(Sint16, value) + OFstatic_cast(Sint16, offset) + 1);
00069 }
00070
00071
00072 inline Uint16 removeSign(const Sint16 value, const Sint16 offset)
00073 {
00074 return OFstatic_cast(Uint16, OFstatic_cast(Sint32, value) + OFstatic_cast(Sint32, offset) + 1);
00075 }
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096 inline Uint32 removeSign(const Sint32 value, const Uint32 mask)
00097 {
00098 return OFstatic_cast(Uint32, value) ^ mask;
00099 }
00100
00101
00102
00103
00104
00105
00108 template<class T>
00109 class DiColorPixelTemplate
00110 : public DiColorPixel,
00111 public DiPixelRepresentationTemplate<T>
00112 {
00113
00114 public:
00115
00124 DiColorPixelTemplate(const DiDocument *docu,
00125 const DiInputPixel *pixel,
00126 const Uint16 samples,
00127 EI_Status &status,
00128 const Uint16 sample_rate = 0)
00129 : DiColorPixel(docu, pixel, samples, status, sample_rate)
00130 {
00131 Data[0] = NULL;
00132 Data[1] = NULL;
00133 Data[2] = NULL;
00134 }
00135
00138 virtual ~DiColorPixelTemplate()
00139 {
00140 delete[] Data[0];
00141 delete[] Data[1];
00142 delete[] Data[2];
00143 }
00144
00149 inline EP_Representation getRepresentation() const
00150 {
00151 return DiPixelRepresentationTemplate<T>::getRepresentation();
00152 }
00153
00159 inline const void *getData() const
00160 {
00161 return OFstatic_cast(const void *, Data);
00162 }
00163
00169 inline void *getDataPtr()
00170 {
00171 return OFstatic_cast(void *, Data);
00172 }
00173
00179 inline void *getDataArrayPtr()
00180 {
00181 return OFstatic_cast(void *, Data);
00182 }
00183
00193 OFBool getPixelData(void *data,
00194 const size_t count) const
00195 {
00196 OFBool result = OFFalse;
00197
00198 if ((data != NULL) && (count >= Count * 3) &&
00199 (Data[0] != NULL) && (Data[1] != NULL) && (Data[2] != NULL))
00200 {
00201
00202 OFBitmanipTemplate<T>::copyMem(Data[0], OFstatic_cast(T *, data), Count);
00203 OFBitmanipTemplate<T>::copyMem(Data[1], OFstatic_cast(T *, data) + Count, Count);
00204 OFBitmanipTemplate<T>::copyMem(Data[2], OFstatic_cast(T *, data) + 2 * Count, Count);
00205 result = OFTrue;
00206 }
00207 return result;
00208 }
00209
00225 unsigned long createDIB(void *&data,
00226 const unsigned long size,
00227 const Uint16 width,
00228 const Uint16 height,
00229 const unsigned long frame,
00230 const int fromBits,
00231 const int toBits,
00232 const int mode,
00233 const int upsideDown,
00234 const int padding) const
00235 {
00236 unsigned long bytes = 0;
00237 if ((Data[0] != NULL) && (Data[1] != NULL) && (Data[2] != NULL) && (toBits <= 8))
00238 {
00239 const unsigned long count = OFstatic_cast(unsigned long, width) * OFstatic_cast(unsigned long, height);
00240 const unsigned long start = count * frame + ((upsideDown) ?
00241 OFstatic_cast(unsigned long, height - 1) * OFstatic_cast(unsigned long, width) : 0);
00242 const signed long nextRow = (upsideDown) ? -2 * OFstatic_cast(signed long, width) : 0;
00243 register const T *r = Data[0] + start;
00244 register const T *g = Data[1] + start;
00245 register const T *b = Data[2] + start;
00246 register Uint16 x;
00247 register Uint16 y;
00248 if (mode == 24)
00249 {
00250 const unsigned long wid3 = OFstatic_cast(unsigned long, width) * 3;
00251
00252 const int gap = (padding) ? OFstatic_cast(int, (4 - wid3 & 0x3) & 0x3) : 0;
00253 unsigned long fsize = (wid3 + gap) * OFstatic_cast(unsigned long, height);
00254 if ((data == NULL) || (size >= fsize))
00255 {
00256 if (data == NULL)
00257 data = new Uint8[fsize];
00258 if (data != NULL)
00259 {
00260 register Uint8 *q = OFstatic_cast(Uint8 *, data);
00261 if (fromBits == toBits)
00262 {
00263
00264 for (y = height; y != 0; y--)
00265 {
00266 for (x = width; x != 0; x--)
00267 {
00268
00269 *(q++) = OFstatic_cast(Uint8, *(b++));
00270 *(q++) = OFstatic_cast(Uint8, *(g++));
00271 *(q++) = OFstatic_cast(Uint8, *(r++));
00272 }
00273 r += nextRow; g += nextRow; b += nextRow;
00274 q += gap;
00275 }
00276 }
00277 else if (fromBits < toBits)
00278 {
00279
00280 const double gradient1 = OFstatic_cast(double, DicomImageClass::maxval(toBits)) /
00281 OFstatic_cast(double, DicomImageClass::maxval(fromBits));
00282 const Uint8 gradient2 = OFstatic_cast(Uint8, gradient1);
00283 if (gradient1 == OFstatic_cast(double, gradient2))
00284 {
00285 for (y = height; y != 0; y--)
00286 {
00287 for (x = width; x != 0; x--)
00288 {
00289
00290 *(q++) = OFstatic_cast(Uint8, *(b++) * gradient2);
00291 *(q++) = OFstatic_cast(Uint8, *(g++) * gradient2);
00292 *(q++) = OFstatic_cast(Uint8, *(r++) * gradient2);
00293 }
00294 r += nextRow; g += nextRow; b += nextRow;
00295 q += gap;
00296 }
00297 } else {
00298 for (y = height; y != 0; y--)
00299 {
00300 for (x = width; x != 0; x--)
00301 {
00302
00303 *(q++) = OFstatic_cast(Uint8, OFstatic_cast(double, *(b++)) * gradient1);
00304 *(q++) = OFstatic_cast(Uint8, OFstatic_cast(double, *(g++)) * gradient1);
00305 *(q++) = OFstatic_cast(Uint8, OFstatic_cast(double, *(r++)) * gradient1);
00306 }
00307 r += nextRow; g += nextRow; b += nextRow;
00308 q += gap;
00309 }
00310 }
00311 }
00312 else
00313 {
00314
00315 const int shift = fromBits - toBits;
00316 for (y = height; y != 0; y--)
00317 {
00318 for (x = width; x != 0; x--)
00319 {
00320
00321 *(q++) = OFstatic_cast(Uint8, *(b++) >> shift);
00322 *(q++) = OFstatic_cast(Uint8, *(g++) >> shift);
00323 *(q++) = OFstatic_cast(Uint8, *(r++) >> shift);
00324 }
00325 r += nextRow; g += nextRow; b += nextRow;
00326 q += gap;
00327 }
00328 }
00329 bytes = fsize;
00330 }
00331 }
00332 }
00333 else if (mode == 32)
00334 {
00335 const unsigned long fsize = count * 4;
00336 if ((data == NULL) || (size >= fsize))
00337 {
00338 if (data == NULL)
00339 data = new Uint32[count];
00340 if (data != NULL)
00341 {
00342 register Uint32 *q = OFstatic_cast(Uint32 *, data);
00343 if (fromBits == toBits)
00344 {
00345
00346 for (y = height; y != 0; y--)
00347 {
00348 for (x = width; x != 0; x--)
00349 {
00350
00351 *(q++) = (OFstatic_cast(Uint32, *(b++)) << 24) |
00352 (OFstatic_cast(Uint32, *(g++)) << 16) |
00353 (OFstatic_cast(Uint32, *(r++)) << 8);
00354 }
00355 r += nextRow; g += nextRow; b += nextRow;
00356 }
00357 }
00358 else if (fromBits < toBits)
00359 {
00360
00361 const double gradient1 = OFstatic_cast(double, DicomImageClass::maxval(toBits)) /
00362 OFstatic_cast(double, DicomImageClass::maxval(fromBits));
00363 const Uint32 gradient2 = OFstatic_cast(Uint32, gradient1);
00364 if (gradient1 == OFstatic_cast(double, gradient2))
00365 {
00366 for (y = height; y != 0; y--)
00367 {
00368 for (x = width; x != 0; x--)
00369 {
00370
00371 *(q++) = (OFstatic_cast(Uint32, *(b++) * gradient2) << 24) |
00372 (OFstatic_cast(Uint32, *(g++) * gradient2) << 16) |
00373 (OFstatic_cast(Uint32, *(r++) * gradient2) << 8);
00374 }
00375 r += nextRow; g += nextRow; b += nextRow;
00376 }
00377 } else {
00378 for (y = height; y != 0; y--)
00379 {
00380 for (x = width; x != 0; x--)
00381 {
00382
00383 *(q++) = (OFstatic_cast(Uint32, OFstatic_cast(double, *(b++)) * gradient1) << 24) |
00384 (OFstatic_cast(Uint32, OFstatic_cast(double, *(g++)) * gradient1) << 16) |
00385 (OFstatic_cast(Uint32, OFstatic_cast(double, *(r++)) * gradient1) << 8);
00386 }
00387 r += nextRow; g += nextRow; b += nextRow;
00388 }
00389 }
00390 }
00391 else
00392 {
00393
00394 const int shift = fromBits - toBits;
00395 for (y = height; y != 0; y--)
00396 {
00397 for (x = width; x != 0; x--)
00398 {
00399
00400 *(q++) = (OFstatic_cast(Uint32, *(b++) >> shift) << 24) |
00401 (OFstatic_cast(Uint32, *(g++) >> shift) << 16) |
00402 (OFstatic_cast(Uint32, *(r++) >> shift) << 8);
00403 }
00404 r += nextRow; g += nextRow; b += nextRow;
00405 }
00406 }
00407 bytes = fsize;
00408 }
00409 }
00410 }
00411 }
00412 return bytes;
00413 }
00414
00426 unsigned long createAWTBitmap(void *&data,
00427 const Uint16 width,
00428 const Uint16 height,
00429 const unsigned long frame,
00430 const int fromBits,
00431 const int toBits) const
00432 {
00433 data = NULL;
00434 unsigned long bytes = 0;
00435 if ((Data[0] != NULL) && (Data[1] != NULL) && (Data[2] != NULL) && (toBits <= 8))
00436 {
00437 const unsigned long count = OFstatic_cast(unsigned long, width) * OFstatic_cast(unsigned long, height);
00438 data = new Uint32[count];
00439 if (data != NULL)
00440 {
00441 const unsigned long start = count * frame;
00442 register const T *r = Data[0] + start;
00443 register const T *g = Data[1] + start;
00444 register const T *b = Data[2] + start;
00445 register Uint32 *q = OFstatic_cast(Uint32 *, data);
00446 register unsigned long i;
00447 if (fromBits == toBits)
00448 {
00449
00450 for (i = count; i != 0; --i)
00451 {
00452
00453 *(q++) = (OFstatic_cast(Uint32, *(r++)) << 24) |
00454 (OFstatic_cast(Uint32, *(g++)) << 16) |
00455 (OFstatic_cast(Uint32, *(b++)) << 8);
00456 }
00457 }
00458 else if (fromBits < toBits)
00459 {
00460
00461 const double gradient1 = OFstatic_cast(double, DicomImageClass::maxval(toBits)) /
00462 OFstatic_cast(double, DicomImageClass::maxval(fromBits));
00463 const Uint32 gradient2 = OFstatic_cast(Uint32, gradient1);
00464 if (gradient1 == OFstatic_cast(double, gradient2))
00465 {
00466 for (i = count; i != 0; --i)
00467 {
00468
00469 *(q++) = (OFstatic_cast(Uint32, *(r++) * gradient2) << 24) |
00470 (OFstatic_cast(Uint32, *(g++) * gradient2) << 16) |
00471 (OFstatic_cast(Uint32, *(b++) * gradient2) << 8);
00472 }
00473 } else {
00474 for (i = count; i != 0; --i)
00475 {
00476
00477 *(q++) = (OFstatic_cast(Uint32, OFstatic_cast(double, *(r++)) * gradient1) << 24) |
00478 (OFstatic_cast(Uint32, OFstatic_cast(double, *(g++)) * gradient1) << 16) |
00479 (OFstatic_cast(Uint32, OFstatic_cast(double, *(b++)) * gradient1) << 8);
00480 }
00481 }
00482 }
00483 else
00484 {
00485
00486 const int shift = fromBits - toBits;
00487 for (i = count; i != 0; --i)
00488 {
00489
00490 *(q++) = (OFstatic_cast(Uint32, *(r++) >> shift) << 24) |
00491 (OFstatic_cast(Uint32, *(g++) >> shift) << 16) |
00492 (OFstatic_cast(Uint32, *(b++) >> shift) << 8);
00493 }
00494 }
00495 bytes = count * 4;
00496 }
00497 }
00498 return bytes;
00499 }
00500
00501
00502 protected:
00503
00509 DiColorPixelTemplate(const DiColorPixel *pixel,
00510 const unsigned long count)
00511 : DiColorPixel(pixel, count)
00512 {
00513 Data[0] = NULL;
00514 Data[1] = NULL;
00515 Data[2] = NULL;
00516 }
00517
00524 inline int Init(const void *pixel)
00525 {
00526 int result = 0;
00527 if (pixel != NULL)
00528 {
00529 result = 1;
00530
00531 for (int j = 0; j < 3; j++)
00532 {
00533 Data[j] = new T[Count];
00534 if (Data[j] != NULL)
00535 {
00536
00537 if (InputCount < Count)
00538 OFBitmanipTemplate<T>::zeroMem(Data[j] + InputCount, Count - InputCount);
00539 } else
00540 result = 0;
00541 }
00542 }
00543 return result;
00544 }
00545
00546
00548 T *Data[3];
00549
00550
00551 private:
00552
00553
00554
00555 DiColorPixelTemplate(const DiColorPixelTemplate<T> &);
00556 DiColorPixelTemplate<T> &operator=(const DiColorPixelTemplate<T> &);
00557 };
00558
00559
00560 #endif
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640