Mercurial > hg > orthanc
comparison Core/Images/ImageProcessing.cpp @ 2981:eff50153a7b3 db-changes
integration mainline->db-changes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 06 Dec 2018 15:58:08 +0100 |
parents | 7791eac62572 |
children | 4e43e67f8ecf |
comparison
equal
deleted
inserted
replaced
2896:3fabf9a673f6 | 2981:eff50153a7b3 |
---|---|
51 const ImageAccessor& source) | 51 const ImageAccessor& source) |
52 { | 52 { |
53 const TargetType minValue = std::numeric_limits<TargetType>::min(); | 53 const TargetType minValue = std::numeric_limits<TargetType>::min(); |
54 const TargetType maxValue = std::numeric_limits<TargetType>::max(); | 54 const TargetType maxValue = std::numeric_limits<TargetType>::max(); |
55 | 55 |
56 for (unsigned int y = 0; y < source.GetHeight(); y++) | 56 const unsigned int width = source.GetWidth(); |
57 const unsigned int height = source.GetHeight(); | |
58 | |
59 for (unsigned int y = 0; y < height; y++) | |
57 { | 60 { |
58 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); | 61 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); |
59 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); | 62 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); |
60 | 63 |
61 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s++) | 64 for (unsigned int x = 0; x < width; x++, t++, s++) |
62 { | 65 { |
63 if (static_cast<int32_t>(*s) < static_cast<int32_t>(minValue)) | 66 if (static_cast<int32_t>(*s) < static_cast<int32_t>(minValue)) |
64 { | 67 { |
65 *t = minValue; | 68 *t = minValue; |
66 } | 69 } |
81 static void ConvertGrayscaleToFloat(ImageAccessor& target, | 84 static void ConvertGrayscaleToFloat(ImageAccessor& target, |
82 const ImageAccessor& source) | 85 const ImageAccessor& source) |
83 { | 86 { |
84 assert(sizeof(float) == 4); | 87 assert(sizeof(float) == 4); |
85 | 88 |
86 for (unsigned int y = 0; y < source.GetHeight(); y++) | 89 const unsigned int width = source.GetWidth(); |
90 const unsigned int height = source.GetHeight(); | |
91 | |
92 for (unsigned int y = 0; y < height; y++) | |
87 { | 93 { |
88 float* t = reinterpret_cast<float*>(target.GetRow(y)); | 94 float* t = reinterpret_cast<float*>(target.GetRow(y)); |
89 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); | 95 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); |
90 | 96 |
91 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s++) | 97 for (unsigned int x = 0; x < width; x++, t++, s++) |
92 { | 98 { |
93 *t = static_cast<float>(*s); | 99 *t = static_cast<float>(*s); |
100 } | |
101 } | |
102 } | |
103 | |
104 | |
105 template <PixelFormat TargetFormat> | |
106 static void ConvertFloatToGrayscale(ImageAccessor& target, | |
107 const ImageAccessor& source) | |
108 { | |
109 typedef typename PixelTraits<TargetFormat>::PixelType TargetType; | |
110 | |
111 assert(sizeof(float) == 4); | |
112 | |
113 const unsigned int width = source.GetWidth(); | |
114 const unsigned int height = source.GetHeight(); | |
115 | |
116 for (unsigned int y = 0; y < height; y++) | |
117 { | |
118 TargetType* q = reinterpret_cast<TargetType*>(target.GetRow(y)); | |
119 const float* p = reinterpret_cast<const float*>(source.GetConstRow(y)); | |
120 | |
121 for (unsigned int x = 0; x < width; x++, p++, q++) | |
122 { | |
123 PixelTraits<TargetFormat>::FloatToPixel(*q, *p); | |
94 } | 124 } |
95 } | 125 } |
96 } | 126 } |
97 | 127 |
98 | 128 |
103 assert(source.GetFormat() == PixelFormat_RGB24); | 133 assert(source.GetFormat() == PixelFormat_RGB24); |
104 | 134 |
105 const TargetType minValue = std::numeric_limits<TargetType>::min(); | 135 const TargetType minValue = std::numeric_limits<TargetType>::min(); |
106 const TargetType maxValue = std::numeric_limits<TargetType>::max(); | 136 const TargetType maxValue = std::numeric_limits<TargetType>::max(); |
107 | 137 |
108 for (unsigned int y = 0; y < source.GetHeight(); y++) | 138 const unsigned int width = source.GetWidth(); |
139 const unsigned int height = source.GetHeight(); | |
140 | |
141 for (unsigned int y = 0; y < height; y++) | |
109 { | 142 { |
110 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); | 143 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); |
111 const uint8_t* s = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 144 const uint8_t* s = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
112 | 145 |
113 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s += 3) | 146 for (unsigned int x = 0; x < width; x++, t++, s += 3) |
114 { | 147 { |
115 // Y = 0.2126 R + 0.7152 G + 0.0722 B | 148 // Y = 0.2126 R + 0.7152 G + 0.0722 B |
116 int32_t v = (2126 * static_cast<int32_t>(s[0]) + | 149 int32_t v = (2126 * static_cast<int32_t>(s[0]) + |
117 7152 * static_cast<int32_t>(s[1]) + | 150 7152 * static_cast<int32_t>(s[1]) + |
118 0722 * static_cast<int32_t>(s[2])) / 10000; | 151 0722 * static_cast<int32_t>(s[2])) / 10000; |
132 } | 165 } |
133 } | 166 } |
134 } | 167 } |
135 | 168 |
136 | 169 |
170 static void MemsetZeroInternal(ImageAccessor& image) | |
171 { | |
172 const unsigned int height = image.GetHeight(); | |
173 const size_t lineSize = image.GetBytesPerPixel() * image.GetWidth(); | |
174 const size_t pitch = image.GetPitch(); | |
175 | |
176 uint8_t *p = reinterpret_cast<uint8_t*>(image.GetBuffer()); | |
177 | |
178 for (unsigned int y = 0; y < height; y++) | |
179 { | |
180 memset(p, 0, lineSize); | |
181 p += pitch; | |
182 } | |
183 } | |
184 | |
185 | |
137 template <typename PixelType> | 186 template <typename PixelType> |
138 static void SetInternal(ImageAccessor& image, | 187 static void SetInternal(ImageAccessor& image, |
139 int64_t constant) | 188 int64_t constant) |
140 { | 189 { |
141 for (unsigned int y = 0; y < image.GetHeight(); y++) | 190 if (constant == 0 && |
142 { | 191 (image.GetFormat() == PixelFormat_Grayscale8 || |
143 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | 192 image.GetFormat() == PixelFormat_Grayscale16 || |
144 | 193 image.GetFormat() == PixelFormat_Grayscale32 || |
145 for (unsigned int x = 0; x < image.GetWidth(); x++, p++) | 194 image.GetFormat() == PixelFormat_Grayscale64 || |
146 { | 195 image.GetFormat() == PixelFormat_SignedGrayscale16)) |
147 *p = static_cast<PixelType>(constant); | 196 { |
197 MemsetZeroInternal(image); | |
198 } | |
199 else | |
200 { | |
201 const unsigned int width = image.GetWidth(); | |
202 const unsigned int height = image.GetHeight(); | |
203 | |
204 for (unsigned int y = 0; y < height; y++) | |
205 { | |
206 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
207 | |
208 for (unsigned int x = 0; x < width; x++, p++) | |
209 { | |
210 *p = static_cast<PixelType>(constant); | |
211 } | |
148 } | 212 } |
149 } | 213 } |
150 } | 214 } |
151 | 215 |
152 | 216 |
165 } | 229 } |
166 | 230 |
167 minValue = std::numeric_limits<PixelType>::max(); | 231 minValue = std::numeric_limits<PixelType>::max(); |
168 maxValue = std::numeric_limits<PixelType>::min(); | 232 maxValue = std::numeric_limits<PixelType>::min(); |
169 | 233 |
234 const unsigned int height = source.GetHeight(); | |
170 const unsigned int width = source.GetWidth(); | 235 const unsigned int width = source.GetWidth(); |
171 | 236 |
172 for (unsigned int y = 0; y < source.GetHeight(); y++) | 237 for (unsigned int y = 0; y < height; y++) |
173 { | 238 { |
174 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y)); | 239 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y)); |
175 | 240 |
176 for (unsigned int x = 0; x < width; x++, p++) | 241 for (unsigned int x = 0; x < width; x++, p++) |
177 { | 242 { |
200 } | 265 } |
201 | 266 |
202 const int64_t minValue = std::numeric_limits<PixelType>::min(); | 267 const int64_t minValue = std::numeric_limits<PixelType>::min(); |
203 const int64_t maxValue = std::numeric_limits<PixelType>::max(); | 268 const int64_t maxValue = std::numeric_limits<PixelType>::max(); |
204 | 269 |
205 for (unsigned int y = 0; y < image.GetHeight(); y++) | 270 const unsigned int width = image.GetWidth(); |
271 const unsigned int height = image.GetHeight(); | |
272 | |
273 for (unsigned int y = 0; y < height; y++) | |
206 { | 274 { |
207 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | 275 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); |
208 | 276 |
209 for (unsigned int x = 0; x < image.GetWidth(); x++, p++) | 277 for (unsigned int x = 0; x < width; x++, p++) |
210 { | 278 { |
211 int64_t v = static_cast<int64_t>(*p) + constant; | 279 int64_t v = static_cast<int64_t>(*p) + constant; |
212 | 280 |
213 if (v > maxValue) | 281 if (v > maxValue) |
214 { | 282 { |
238 return; | 306 return; |
239 } | 307 } |
240 | 308 |
241 const int64_t minValue = std::numeric_limits<PixelType>::min(); | 309 const int64_t minValue = std::numeric_limits<PixelType>::min(); |
242 const int64_t maxValue = std::numeric_limits<PixelType>::max(); | 310 const int64_t maxValue = std::numeric_limits<PixelType>::max(); |
311 | |
243 const unsigned int width = image.GetWidth(); | 312 const unsigned int width = image.GetWidth(); |
244 | 313 const unsigned int height = image.GetHeight(); |
245 for (unsigned int y = 0; y < image.GetHeight(); y++) | 314 |
315 for (unsigned int y = 0; y < height; y++) | |
246 { | 316 { |
247 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | 317 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); |
248 | 318 |
249 for (unsigned int x = 0; x < width; x++, p++) | 319 for (unsigned int x = 0; x < width; x++, p++) |
250 { | 320 { |
332 if (target.GetFormat() != source.GetFormat()) | 402 if (target.GetFormat() != source.GetFormat()) |
333 { | 403 { |
334 throw OrthancException(ErrorCode_IncompatibleImageFormat); | 404 throw OrthancException(ErrorCode_IncompatibleImageFormat); |
335 } | 405 } |
336 | 406 |
337 unsigned int lineSize = GetBytesPerPixel(source.GetFormat()) * source.GetWidth(); | 407 unsigned int lineSize = source.GetBytesPerPixel() * source.GetWidth(); |
338 | 408 |
339 assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize); | 409 assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize); |
340 | 410 |
341 for (unsigned int y = 0; y < source.GetHeight(); y++) | 411 for (unsigned int y = 0; y < source.GetHeight(); y++) |
342 { | 412 { |
352 target.GetHeight() != source.GetHeight()) | 422 target.GetHeight() != source.GetHeight()) |
353 { | 423 { |
354 throw OrthancException(ErrorCode_IncompatibleImageSize); | 424 throw OrthancException(ErrorCode_IncompatibleImageSize); |
355 } | 425 } |
356 | 426 |
427 const unsigned int width = source.GetWidth(); | |
428 const unsigned int height = source.GetHeight(); | |
429 | |
357 if (source.GetFormat() == target.GetFormat()) | 430 if (source.GetFormat() == target.GetFormat()) |
358 { | 431 { |
359 Copy(target, source); | 432 Copy(target, source); |
360 return; | 433 return; |
361 } | 434 } |
449 { | 522 { |
450 ConvertGrayscaleToFloat<int16_t>(target, source); | 523 ConvertGrayscaleToFloat<int16_t>(target, source); |
451 return; | 524 return; |
452 } | 525 } |
453 | 526 |
527 | |
454 if (target.GetFormat() == PixelFormat_Grayscale8 && | 528 if (target.GetFormat() == PixelFormat_Grayscale8 && |
455 source.GetFormat() == PixelFormat_RGBA32) | 529 source.GetFormat() == PixelFormat_RGBA32) |
456 { | 530 { |
457 for (unsigned int y = 0; y < source.GetHeight(); y++) | 531 for (unsigned int y = 0; y < height; y++) |
458 { | 532 { |
459 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 533 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
460 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 534 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
461 for (unsigned int x = 0; x < source.GetWidth(); x++, q++) | 535 for (unsigned int x = 0; x < width; x++, q++) |
462 { | 536 { |
463 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[0]) + | 537 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[0]) + |
464 7152 * static_cast<uint32_t>(p[1]) + | 538 7152 * static_cast<uint32_t>(p[1]) + |
465 0722 * static_cast<uint32_t>(p[2])) / 10000); | 539 0722 * static_cast<uint32_t>(p[2])) / 10000); |
466 p += 4; | 540 p += 4; |
471 } | 545 } |
472 | 546 |
473 if (target.GetFormat() == PixelFormat_Grayscale8 && | 547 if (target.GetFormat() == PixelFormat_Grayscale8 && |
474 source.GetFormat() == PixelFormat_BGRA32) | 548 source.GetFormat() == PixelFormat_BGRA32) |
475 { | 549 { |
476 for (unsigned int y = 0; y < source.GetHeight(); y++) | 550 for (unsigned int y = 0; y < height; y++) |
477 { | 551 { |
478 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 552 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
479 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 553 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
480 for (unsigned int x = 0; x < source.GetWidth(); x++, q++) | 554 for (unsigned int x = 0; x < width; x++, q++) |
481 { | 555 { |
482 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[2]) + | 556 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[2]) + |
483 7152 * static_cast<uint32_t>(p[1]) + | 557 7152 * static_cast<uint32_t>(p[1]) + |
484 0722 * static_cast<uint32_t>(p[0])) / 10000); | 558 0722 * static_cast<uint32_t>(p[0])) / 10000); |
485 p += 4; | 559 p += 4; |
490 } | 564 } |
491 | 565 |
492 if (target.GetFormat() == PixelFormat_RGB24 && | 566 if (target.GetFormat() == PixelFormat_RGB24 && |
493 source.GetFormat() == PixelFormat_RGBA32) | 567 source.GetFormat() == PixelFormat_RGBA32) |
494 { | 568 { |
495 for (unsigned int y = 0; y < source.GetHeight(); y++) | 569 for (unsigned int y = 0; y < height; y++) |
496 { | 570 { |
497 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 571 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
498 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 572 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
499 for (unsigned int x = 0; x < source.GetWidth(); x++) | 573 for (unsigned int x = 0; x < width; x++) |
500 { | 574 { |
501 q[0] = p[0]; | 575 q[0] = p[0]; |
502 q[1] = p[1]; | 576 q[1] = p[1]; |
503 q[2] = p[2]; | 577 q[2] = p[2]; |
504 p += 4; | 578 p += 4; |
510 } | 584 } |
511 | 585 |
512 if (target.GetFormat() == PixelFormat_RGB24 && | 586 if (target.GetFormat() == PixelFormat_RGB24 && |
513 source.GetFormat() == PixelFormat_BGRA32) | 587 source.GetFormat() == PixelFormat_BGRA32) |
514 { | 588 { |
515 for (unsigned int y = 0; y < source.GetHeight(); y++) | 589 for (unsigned int y = 0; y < height; y++) |
516 { | 590 { |
517 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 591 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
518 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 592 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
519 for (unsigned int x = 0; x < source.GetWidth(); x++) | 593 for (unsigned int x = 0; x < width; x++) |
520 { | 594 { |
521 q[0] = p[2]; | 595 q[0] = p[2]; |
522 q[1] = p[1]; | 596 q[1] = p[1]; |
523 q[2] = p[0]; | 597 q[2] = p[0]; |
524 p += 4; | 598 p += 4; |
530 } | 604 } |
531 | 605 |
532 if (target.GetFormat() == PixelFormat_RGBA32 && | 606 if (target.GetFormat() == PixelFormat_RGBA32 && |
533 source.GetFormat() == PixelFormat_RGB24) | 607 source.GetFormat() == PixelFormat_RGB24) |
534 { | 608 { |
535 for (unsigned int y = 0; y < source.GetHeight(); y++) | 609 for (unsigned int y = 0; y < height; y++) |
536 { | 610 { |
537 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 611 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
538 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 612 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
539 for (unsigned int x = 0; x < source.GetWidth(); x++) | 613 for (unsigned int x = 0; x < width; x++) |
540 { | 614 { |
541 q[0] = p[0]; | 615 q[0] = p[0]; |
542 q[1] = p[1]; | 616 q[1] = p[1]; |
543 q[2] = p[2]; | 617 q[2] = p[2]; |
544 q[3] = 255; // Set the alpha channel to full opacity | 618 q[3] = 255; // Set the alpha channel to full opacity |
551 } | 625 } |
552 | 626 |
553 if (target.GetFormat() == PixelFormat_RGB24 && | 627 if (target.GetFormat() == PixelFormat_RGB24 && |
554 source.GetFormat() == PixelFormat_Grayscale8) | 628 source.GetFormat() == PixelFormat_Grayscale8) |
555 { | 629 { |
556 for (unsigned int y = 0; y < source.GetHeight(); y++) | 630 for (unsigned int y = 0; y < height; y++) |
557 { | 631 { |
558 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 632 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
559 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 633 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
560 for (unsigned int x = 0; x < source.GetWidth(); x++) | 634 for (unsigned int x = 0; x < width; x++) |
561 { | 635 { |
562 q[0] = *p; | 636 q[0] = *p; |
563 q[1] = *p; | 637 q[1] = *p; |
564 q[2] = *p; | 638 q[2] = *p; |
565 p += 1; | 639 p += 1; |
572 | 646 |
573 if ((target.GetFormat() == PixelFormat_RGBA32 || | 647 if ((target.GetFormat() == PixelFormat_RGBA32 || |
574 target.GetFormat() == PixelFormat_BGRA32) && | 648 target.GetFormat() == PixelFormat_BGRA32) && |
575 source.GetFormat() == PixelFormat_Grayscale8) | 649 source.GetFormat() == PixelFormat_Grayscale8) |
576 { | 650 { |
577 for (unsigned int y = 0; y < source.GetHeight(); y++) | 651 for (unsigned int y = 0; y < height; y++) |
578 { | 652 { |
579 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 653 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
580 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 654 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
581 for (unsigned int x = 0; x < source.GetWidth(); x++) | 655 for (unsigned int x = 0; x < width; x++) |
582 { | 656 { |
583 q[0] = *p; | 657 q[0] = *p; |
584 q[1] = *p; | 658 q[1] = *p; |
585 q[2] = *p; | 659 q[2] = *p; |
586 q[3] = 255; | 660 q[3] = 255; |
593 } | 667 } |
594 | 668 |
595 if (target.GetFormat() == PixelFormat_BGRA32 && | 669 if (target.GetFormat() == PixelFormat_BGRA32 && |
596 source.GetFormat() == PixelFormat_Grayscale16) | 670 source.GetFormat() == PixelFormat_Grayscale16) |
597 { | 671 { |
598 for (unsigned int y = 0; y < source.GetHeight(); y++) | 672 for (unsigned int y = 0; y < height; y++) |
599 { | 673 { |
600 const uint16_t* p = reinterpret_cast<const uint16_t*>(source.GetConstRow(y)); | 674 const uint16_t* p = reinterpret_cast<const uint16_t*>(source.GetConstRow(y)); |
601 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 675 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
602 for (unsigned int x = 0; x < source.GetWidth(); x++) | 676 for (unsigned int x = 0; x < width; x++) |
603 { | 677 { |
604 uint8_t value = (*p < 256 ? *p : 255); | 678 uint8_t value = (*p < 256 ? *p : 255); |
605 q[0] = value; | 679 q[0] = value; |
606 q[1] = value; | 680 q[1] = value; |
607 q[2] = value; | 681 q[2] = value; |
615 } | 689 } |
616 | 690 |
617 if (target.GetFormat() == PixelFormat_BGRA32 && | 691 if (target.GetFormat() == PixelFormat_BGRA32 && |
618 source.GetFormat() == PixelFormat_SignedGrayscale16) | 692 source.GetFormat() == PixelFormat_SignedGrayscale16) |
619 { | 693 { |
620 for (unsigned int y = 0; y < source.GetHeight(); y++) | 694 for (unsigned int y = 0; y < height; y++) |
621 { | 695 { |
622 const int16_t* p = reinterpret_cast<const int16_t*>(source.GetConstRow(y)); | 696 const int16_t* p = reinterpret_cast<const int16_t*>(source.GetConstRow(y)); |
623 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 697 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
624 for (unsigned int x = 0; x < source.GetWidth(); x++) | 698 for (unsigned int x = 0; x < width; x++) |
625 { | 699 { |
626 uint8_t value; | 700 uint8_t value; |
627 if (*p < 0) | 701 if (*p < 0) |
628 { | 702 { |
629 value = 0; | 703 value = 0; |
650 } | 724 } |
651 | 725 |
652 if (target.GetFormat() == PixelFormat_BGRA32 && | 726 if (target.GetFormat() == PixelFormat_BGRA32 && |
653 source.GetFormat() == PixelFormat_RGB24) | 727 source.GetFormat() == PixelFormat_RGB24) |
654 { | 728 { |
655 for (unsigned int y = 0; y < source.GetHeight(); y++) | 729 for (unsigned int y = 0; y < height; y++) |
656 { | 730 { |
657 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); | 731 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
658 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 732 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
659 for (unsigned int x = 0; x < source.GetWidth(); x++) | 733 for (unsigned int x = 0; x < width; x++) |
660 { | 734 { |
661 q[0] = p[2]; | 735 q[0] = p[2]; |
662 q[1] = p[1]; | 736 q[1] = p[1]; |
663 q[2] = p[0]; | 737 q[2] = p[0]; |
664 q[3] = 255; | 738 q[3] = 255; |
671 } | 745 } |
672 | 746 |
673 if (target.GetFormat() == PixelFormat_RGB24 && | 747 if (target.GetFormat() == PixelFormat_RGB24 && |
674 source.GetFormat() == PixelFormat_RGB48) | 748 source.GetFormat() == PixelFormat_RGB48) |
675 { | 749 { |
676 for (unsigned int y = 0; y < source.GetHeight(); y++) | 750 for (unsigned int y = 0; y < height; y++) |
677 { | 751 { |
678 const uint16_t* p = reinterpret_cast<const uint16_t*>(source.GetConstRow(y)); | 752 const uint16_t* p = reinterpret_cast<const uint16_t*>(source.GetConstRow(y)); |
679 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); | 753 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y)); |
680 for (unsigned int x = 0; x < source.GetWidth(); x++) | 754 for (unsigned int x = 0; x < width; x++) |
681 { | 755 { |
682 q[0] = p[0] >> 8; | 756 q[0] = p[0] >> 8; |
683 q[1] = p[1] >> 8; | 757 q[1] = p[1] >> 8; |
684 q[2] = p[2] >> 8; | 758 q[2] = p[2] >> 8; |
685 p += 3; | 759 p += 3; |
688 } | 762 } |
689 | 763 |
690 return; | 764 return; |
691 } | 765 } |
692 | 766 |
767 if (target.GetFormat() == PixelFormat_Grayscale16 && | |
768 source.GetFormat() == PixelFormat_Float32) | |
769 { | |
770 ConvertFloatToGrayscale<PixelFormat_Grayscale16>(target, source); | |
771 return; | |
772 } | |
773 | |
774 if (target.GetFormat() == PixelFormat_Grayscale8 && | |
775 source.GetFormat() == PixelFormat_Float32) | |
776 { | |
777 ConvertFloatToGrayscale<PixelFormat_Grayscale8>(target, source); | |
778 return; | |
779 } | |
780 | |
693 throw OrthancException(ErrorCode_NotImplemented); | 781 throw OrthancException(ErrorCode_NotImplemented); |
694 } | 782 } |
695 | 783 |
696 | 784 |
697 | 785 |
699 int64_t value) | 787 int64_t value) |
700 { | 788 { |
701 switch (image.GetFormat()) | 789 switch (image.GetFormat()) |
702 { | 790 { |
703 case PixelFormat_Grayscale8: | 791 case PixelFormat_Grayscale8: |
704 memset(image.GetBuffer(), static_cast<uint8_t>(value), image.GetPitch() * image.GetHeight()); | 792 SetInternal<uint8_t>(image, value); |
705 return; | 793 return; |
706 | 794 |
707 case PixelFormat_Grayscale16: | 795 case PixelFormat_Grayscale16: |
708 if (value == 0) | 796 SetInternal<uint16_t>(image, value); |
709 { | |
710 memset(image.GetBuffer(), 0, image.GetPitch() * image.GetHeight()); | |
711 } | |
712 else | |
713 { | |
714 SetInternal<uint16_t>(image, value); | |
715 } | |
716 return; | 797 return; |
717 | 798 |
718 case PixelFormat_Grayscale32: | 799 case PixelFormat_Grayscale32: |
719 if (value == 0) | 800 SetInternal<uint32_t>(image, value); |
720 { | |
721 memset(image.GetBuffer(), 0, image.GetPitch() * image.GetHeight()); | |
722 } | |
723 else | |
724 { | |
725 SetInternal<uint32_t>(image, value); | |
726 } | |
727 return; | 801 return; |
728 | 802 |
729 case PixelFormat_Grayscale64: | 803 case PixelFormat_Grayscale64: |
730 if (value == 0) | 804 SetInternal<uint64_t>(image, value); |
731 { | |
732 memset(image.GetBuffer(), 0, image.GetPitch() * image.GetHeight()); | |
733 } | |
734 else | |
735 { | |
736 SetInternal<uint64_t>(image, value); | |
737 } | |
738 return; | 805 return; |
739 | 806 |
740 case PixelFormat_SignedGrayscale16: | 807 case PixelFormat_SignedGrayscale16: |
741 if (value == 0) | 808 SetInternal<int16_t>(image, value); |
742 { | |
743 memset(image.GetBuffer(), 0, image.GetPitch() * image.GetHeight()); | |
744 } | |
745 else | |
746 { | |
747 SetInternal<int16_t>(image, value); | |
748 } | |
749 return; | 809 return; |
750 | 810 |
751 case PixelFormat_Float32: | 811 case PixelFormat_Float32: |
752 assert(sizeof(float) == 4); | 812 assert(sizeof(float) == 4); |
753 SetInternal<float>(image, value); | 813 SetInternal<float>(image, value); |
795 | 855 |
796 default: | 856 default: |
797 throw OrthancException(ErrorCode_NotImplemented); | 857 throw OrthancException(ErrorCode_NotImplemented); |
798 } | 858 } |
799 | 859 |
800 for (unsigned int y = 0; y < image.GetHeight(); y++) | 860 const unsigned int width = image.GetWidth(); |
861 const unsigned int height = image.GetHeight(); | |
862 | |
863 for (unsigned int y = 0; y < height; y++) | |
801 { | 864 { |
802 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y)); | 865 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y)); |
803 | 866 |
804 for (unsigned int x = 0; x < image.GetWidth(); x++) | 867 for (unsigned int x = 0; x < width; x++) |
805 { | 868 { |
806 for (unsigned int i = 0; i < size; i++) | 869 for (unsigned int i = 0; i < size; i++) |
807 { | 870 { |
808 q[i] = p[i]; | 871 q[i] = p[i]; |
809 } | 872 } |
883 { | 946 { |
884 switch (image.GetFormat()) | 947 switch (image.GetFormat()) |
885 { | 948 { |
886 case PixelFormat_Float32: | 949 case PixelFormat_Float32: |
887 { | 950 { |
888 assert(sizeof(float) == 32); | 951 assert(sizeof(float) == 4); |
889 float a, b; | 952 float a, b; |
890 GetMinMaxValueInternal<float>(a, b, image); | 953 GetMinMaxValueInternal<float>(a, b, image); |
891 minValue = a; | 954 minValue = a; |
892 maxValue = b; | 955 maxValue = b; |
893 break; | 956 break; |
1014 } | 1077 } |
1015 | 1078 |
1016 | 1079 |
1017 void ImageProcessing::Invert(ImageAccessor& image) | 1080 void ImageProcessing::Invert(ImageAccessor& image) |
1018 { | 1081 { |
1082 const unsigned int width = image.GetWidth(); | |
1083 const unsigned int height = image.GetHeight(); | |
1084 | |
1019 switch (image.GetFormat()) | 1085 switch (image.GetFormat()) |
1020 { | 1086 { |
1021 case PixelFormat_Grayscale8: | 1087 case PixelFormat_Grayscale8: |
1022 { | 1088 { |
1023 for (unsigned int y = 0; y < image.GetHeight(); y++) | 1089 for (unsigned int y = 0; y < height; y++) |
1024 { | 1090 { |
1025 uint8_t* p = reinterpret_cast<uint8_t*>(image.GetRow(y)); | 1091 uint8_t* p = reinterpret_cast<uint8_t*>(image.GetRow(y)); |
1026 | 1092 |
1027 for (unsigned int x = 0; x < image.GetWidth(); x++, p++) | 1093 for (unsigned int x = 0; x < width; x++, p++) |
1028 { | 1094 { |
1029 *p = 255 - (*p); | 1095 *p = 255 - (*p); |
1030 } | 1096 } |
1031 } | 1097 } |
1032 | 1098 |