annotate OrthancFramework/Sources/Images/ImageProcessing.cpp @ 5141:023569e7155b

moved DicomModification thread safety into ResourceModificationJob (trying to avoid mutex in OrthancFramework as much as possible)
author Alain Mazy <am@osimis.io>
date Fri, 20 Jan 2023 18:10:42 +0100
parents fcdf399f9fc0
children 0ea402b4d901
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
1900
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1612
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
1288
6e7e5ed91c2d upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 993
diff changeset
4 * Department, University Hospital of Liege, Belgium
4870
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium
43e613a7756b upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4831
diff changeset
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 *
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * This program is free software: you can redistribute it and/or
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4083
diff changeset
9 * modify it under the terms of the GNU Lesser General Public License
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4083
diff changeset
10 * as published by the Free Software Foundation, either version 3 of
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4083
diff changeset
11 * the License, or (at your option) any later version.
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
12 *
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * This program is distributed in the hope that it will be useful, but
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4083
diff changeset
16 * Lesser General Public License for more details.
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 *
4119
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4083
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4083
diff changeset
19 * License along with this program. If not, see
bf7b9edf6b81 re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4083
diff changeset
20 * <http://www.gnu.org/licenses/>.
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "../PrecompiledHeaders.h"
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 #include "ImageProcessing.h"
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
27 #include "Image.h"
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
28 #include "ImageTraits.h"
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
29 #include "PixelTraits.h"
2895
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2840
diff changeset
30 #include "../OrthancException.h"
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
32 #ifdef __EMSCRIPTEN__
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
33 /*
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
34 Avoid this error:
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
35 -----------------
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
36 .../boost/math/special_functions/round.hpp:118:12: warning: implicit conversion from 'std::__2::numeric_limits<long long>::type' (aka 'long long') to 'float' changes value from 9223372036854775807 to 9223372036854775808 [-Wimplicit-int-float-conversion]
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
37 .../mnt/c/osi/dev/orthanc/Core/Images/ImageProcessing.cpp:333:28: note: in instantiation of function template specialization 'boost::math::llround<float>' requested here
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
38 .../mnt/c/osi/dev/orthanc/Core/Images/ImageProcessing.cpp:1006:9: note: in instantiation of function template specialization 'Orthanc::MultiplyConstantInternal<unsigned char, true>' requested here
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
39 */
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
40 #pragma GCC diagnostic ignored "-Wimplicit-int-float-conversion"
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
41 #endif
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
42
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
43 #include <boost/math/special_functions/round.hpp>
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
44
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
45 #include <algorithm>
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 #include <cassert>
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
47 #include <limits>
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
48 #include <list>
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
49 #include <map>
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
50 #include <stdint.h>
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
51 #include <string.h>
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
52
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 namespace Orthanc
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 {
4300
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
55 ImageProcessing::ImagePoint::ImagePoint(int32_t x,
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
56 int32_t y) :
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
57 x_(x),
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
58 y_(y)
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
59 {
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
60 }
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
61
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
62 int32_t ImageProcessing::ImagePoint::GetX() const
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
63 {
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
64 return x_;
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
65 }
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
66
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
67 int32_t ImageProcessing::ImagePoint::GetY() const
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
68 {
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
69 return y_;
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
70 }
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
71
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
72 void ImageProcessing::ImagePoint::Set(int32_t x, int32_t y)
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
73 {
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
74 x_ = x;
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
75 y_ = y;
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
76 }
b30a8de92ad9 abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4297
diff changeset
77
4056
55727d85f419 fix for msvc2008
s.jodogne@gmail.com
parents: 4044
diff changeset
78 void ImageProcessing::ImagePoint::ClipTo(int32_t minX, int32_t maxX, int32_t minY, int32_t maxY)
55727d85f419 fix for msvc2008
s.jodogne@gmail.com
parents: 4044
diff changeset
79 {
55727d85f419 fix for msvc2008
s.jodogne@gmail.com
parents: 4044
diff changeset
80 x_ = std::max(minX, std::min(maxX, x_));
55727d85f419 fix for msvc2008
s.jodogne@gmail.com
parents: 4044
diff changeset
81 y_ = std::max(minY, std::min(maxY, y_));
55727d85f419 fix for msvc2008
s.jodogne@gmail.com
parents: 4044
diff changeset
82 }
55727d85f419 fix for msvc2008
s.jodogne@gmail.com
parents: 4044
diff changeset
83
3265
59a184cbb596 ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents: 3259
diff changeset
84 double ImageProcessing::ImagePoint::GetDistanceTo(const ImagePoint& other) const
59a184cbb596 ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents: 3259
diff changeset
85 {
59a184cbb596 ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents: 3259
diff changeset
86 double dx = (double)(other.GetX() - GetX());
59a184cbb596 ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents: 3259
diff changeset
87 double dy = (double)(other.GetY() - GetY());
59a184cbb596 ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents: 3259
diff changeset
88 return sqrt(dx * dx + dy * dy);
59a184cbb596 ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents: 3259
diff changeset
89 }
59a184cbb596 ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents: 3259
diff changeset
90
3565
2999a6e9456b ImageProcessing::ImagePoint::GetDistanceToLine
Alain Mazy <alain@mazy.be>
parents: 3550
diff changeset
91 double ImageProcessing::ImagePoint::GetDistanceToLine(double a, double b, double c) const // where ax + by + c = 0 is the equation of the line
2999a6e9456b ImageProcessing::ImagePoint::GetDistanceToLine
Alain Mazy <alain@mazy.be>
parents: 3550
diff changeset
92 {
2999a6e9456b ImageProcessing::ImagePoint::GetDistanceToLine
Alain Mazy <alain@mazy.be>
parents: 3550
diff changeset
93 return std::abs(a * static_cast<double>(GetX()) + b * static_cast<double>(GetY()) + c) / pow(a * a + b * b, 0.5);
2999a6e9456b ImageProcessing::ImagePoint::GetDistanceToLine
Alain Mazy <alain@mazy.be>
parents: 3550
diff changeset
94 }
2999a6e9456b ImageProcessing::ImagePoint::GetDistanceToLine
Alain Mazy <alain@mazy.be>
parents: 3550
diff changeset
95
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
96 template <typename TargetType, typename SourceType>
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
97 static void ConvertInternal(ImageAccessor& target,
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
98 const ImageAccessor& source)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
99 {
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
100 // WARNING - "::min()" should be replaced by "::lowest()" if
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
101 // dealing with float or double (which is not the case so far)
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
102 assert(sizeof(TargetType) <= 2); // Safeguard to remember about "float/double"
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
103 const TargetType minValue = std::numeric_limits<TargetType>::min();
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
104 const TargetType maxValue = std::numeric_limits<TargetType>::max();
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
105
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
106 const unsigned int width = source.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
107 const unsigned int height = source.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
108
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
109 for (unsigned int y = 0; y < height; y++)
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
110 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
111 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y));
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
112 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y));
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
113
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
114 for (unsigned int x = 0; x < width; x++, t++, s++)
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
115 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
116 if (static_cast<int32_t>(*s) < static_cast<int32_t>(minValue))
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
117 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
118 *t = minValue;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
119 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
120 else if (static_cast<int32_t>(*s) > static_cast<int32_t>(maxValue))
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
121 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
122 *t = maxValue;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
123 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
124 else
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
125 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
126 *t = static_cast<TargetType>(*s);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
127 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
128 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
129 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
130 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
131
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
132
1993
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
133 template <typename SourceType>
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
134 static void ConvertGrayscaleToFloat(ImageAccessor& target,
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
135 const ImageAccessor& source)
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
136 {
1994
4d099fee5eca ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1993
diff changeset
137 assert(sizeof(float) == 4);
4d099fee5eca ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1993
diff changeset
138
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
139 const unsigned int width = source.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
140 const unsigned int height = source.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
141
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
142 for (unsigned int y = 0; y < height; y++)
1993
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
143 {
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
144 float* t = reinterpret_cast<float*>(target.GetRow(y));
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
145 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y));
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
146
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
147 for (unsigned int x = 0; x < width; x++, t++, s++)
1993
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
148 {
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
149 *t = static_cast<float>(*s);
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
150 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
151 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
152 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
153
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
154
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
155 template <PixelFormat TargetFormat>
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
156 static void ConvertFloatToGrayscale(ImageAccessor& target,
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
157 const ImageAccessor& source)
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
158 {
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
159 typedef typename PixelTraits<TargetFormat>::PixelType TargetType;
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
160
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
161 assert(sizeof(float) == 4);
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
162
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
163 const unsigned int width = source.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
164 const unsigned int height = source.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
165
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
166 for (unsigned int y = 0; y < height; y++)
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
167 {
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
168 TargetType* q = reinterpret_cast<TargetType*>(target.GetRow(y));
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
169 const float* p = reinterpret_cast<const float*>(source.GetConstRow(y));
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
170
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
171 for (unsigned int x = 0; x < width; x++, p++, q++)
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
172 {
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
173 PixelTraits<TargetFormat>::FloatToPixel(*q, *p);
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
174 }
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
175 }
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
176 }
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
177
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
178
993
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
179 template <typename TargetType>
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
180 static void ConvertColorToGrayscale(ImageAccessor& target,
1993
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
181 const ImageAccessor& source)
993
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
182 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
183 assert(source.GetFormat() == PixelFormat_RGB24);
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
184
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
185 // WARNING - "::min()" should be replaced by "::lowest()" if
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
186 // dealing with float or double (which is not the case so far)
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
187 assert(sizeof(TargetType) <= 2); // Safeguard to remember about "float/double"
993
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
188 const TargetType minValue = std::numeric_limits<TargetType>::min();
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
189 const TargetType maxValue = std::numeric_limits<TargetType>::max();
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
190
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
191 const unsigned int width = source.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
192 const unsigned int height = source.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
193
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
194 for (unsigned int y = 0; y < height; y++)
993
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
195 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
196 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y));
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
197 const uint8_t* s = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
198
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
199 for (unsigned int x = 0; x < width; x++, t++, s += 3)
993
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
200 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
201 // Y = 0.2126 R + 0.7152 G + 0.0722 B
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
202 int32_t v = (2126 * static_cast<int32_t>(s[0]) +
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
203 7152 * static_cast<int32_t>(s[1]) +
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
204 0722 * static_cast<int32_t>(s[2])) / 10000;
993
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
205
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
206 if (static_cast<int32_t>(v) < static_cast<int32_t>(minValue))
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
207 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
208 *t = minValue;
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
209 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
210 else if (static_cast<int32_t>(v) > static_cast<int32_t>(maxValue))
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
211 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
212 *t = maxValue;
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
213 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
214 else
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
215 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
216 *t = static_cast<TargetType>(v);
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
217 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
218 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
219 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
220 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
221
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
222
2902
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
223 static void MemsetZeroInternal(ImageAccessor& image)
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
224 {
2902
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
225 const unsigned int height = image.GetHeight();
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
226 const size_t lineSize = image.GetBytesPerPixel() * image.GetWidth();
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
227 const size_t pitch = image.GetPitch();
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
228
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
229 uint8_t *p = reinterpret_cast<uint8_t*>(image.GetBuffer());
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
230
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
231 for (unsigned int y = 0; y < height; y++)
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
232 {
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
233 memset(p, 0, lineSize);
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
234 p += pitch;
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
235 }
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
236 }
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
237
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
238
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
239 template <typename PixelType>
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
240 static void SetInternal(ImageAccessor& image,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
241 int64_t constant)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
242 {
2902
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
243 if (constant == 0 &&
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
244 (image.GetFormat() == PixelFormat_Grayscale8 ||
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
245 image.GetFormat() == PixelFormat_Grayscale16 ||
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
246 image.GetFormat() == PixelFormat_Grayscale32 ||
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
247 image.GetFormat() == PixelFormat_Grayscale64 ||
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
248 image.GetFormat() == PixelFormat_SignedGrayscale16))
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
249 {
2902
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
250 MemsetZeroInternal(image);
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
251 }
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
252 else
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
253 {
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
254 const unsigned int width = image.GetWidth();
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
255 const unsigned int height = image.GetHeight();
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
256
2902
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
257 for (unsigned int y = 0; y < height; y++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
258 {
2902
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
259 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
260
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
261 for (unsigned int x = 0; x < width; x++, p++)
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
262 {
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
263 *p = static_cast<PixelType>(constant);
e80b38fb22c6 fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2895
diff changeset
264 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
265 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
266 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
267 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
268
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
269
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
270 template <typename PixelType>
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
271 static void GetMinMaxValueInternal(PixelType& minValue,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
272 PixelType& maxValue,
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
273 const ImageAccessor& source,
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
274 const PixelType LowestValue = std::numeric_limits<PixelType>::min())
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
275 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
276 // Deal with the special case of empty image
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
277 if (source.GetWidth() == 0 ||
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
278 source.GetHeight() == 0)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
279 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
280 minValue = 0;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
281 maxValue = 0;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
282 return;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
283 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
284
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
285 minValue = std::numeric_limits<PixelType>::max();
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
286 maxValue = LowestValue;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
287
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
288 const unsigned int height = source.GetHeight();
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
289 const unsigned int width = source.GetWidth();
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
290
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
291 for (unsigned int y = 0; y < height; y++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
292 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
293 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y));
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
294
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
295 for (unsigned int x = 0; x < width; x++, p++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
296 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
297 if (*p < minValue)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
298 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
299 minValue = *p;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
300 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
301
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
302 if (*p > maxValue)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
303 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
304 maxValue = *p;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
305 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
306 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
307 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
308 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
309
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
310
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
311
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
312 template <typename PixelType>
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
313 static void AddConstantInternal(ImageAccessor& image,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
314 int64_t constant)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
315 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
316 if (constant == 0)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
317 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
318 return;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
319 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
320
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
321 // WARNING - "::min()" should be replaced by "::lowest()" if
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
322 // dealing with float or double (which is not the case so far)
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
323 assert(sizeof(PixelType) <= 2); // Safeguard to remember about "float/double"
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
324 const int64_t minValue = std::numeric_limits<PixelType>::min();
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
325 const int64_t maxValue = std::numeric_limits<PixelType>::max();
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
326
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
327 const unsigned int width = image.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
328 const unsigned int height = image.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
329
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
330 for (unsigned int y = 0; y < height; y++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
331 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
332 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
333
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
334 for (unsigned int x = 0; x < width; x++, p++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
335 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
336 int64_t v = static_cast<int64_t>(*p) + constant;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
337
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
338 if (v > maxValue)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
339 {
876
e21d1a5f5934 fix msvc warnings
jodogne
parents: 863
diff changeset
340 *p = std::numeric_limits<PixelType>::max();
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
341 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
342 else if (v < minValue)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
343 {
876
e21d1a5f5934 fix msvc warnings
jodogne
parents: 863
diff changeset
344 *p = std::numeric_limits<PixelType>::min();
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
345 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
346 else
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
347 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
348 *p = static_cast<PixelType>(v);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
349 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
350 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
351 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
352 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
353
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
354
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
355
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
356 template <typename PixelType,
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
357 bool UseRound>
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
358 static void MultiplyConstantInternal(ImageAccessor& image,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
359 float factor)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
360 {
1334
7f0aa3c0f659 code improvements
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1288
diff changeset
361 if (std::abs(factor - 1.0f) <= std::numeric_limits<float>::epsilon())
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
362 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
363 return;
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
364 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
365
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
366 // WARNING - "::min()" should be replaced by "::lowest()" if
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
367 // dealing with float or double (which is not the case so far)
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
368 assert(sizeof(PixelType) <= 2); // Safeguard to remember about "float/double"
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
369 const int64_t minValue = std::numeric_limits<PixelType>::min();
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
370 const int64_t maxValue = std::numeric_limits<PixelType>::max();
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
371
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
372 const unsigned int width = image.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
373 const unsigned int height = image.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
374
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
375 for (unsigned int y = 0; y < height; y++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
376 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
377 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
378
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
379 for (unsigned int x = 0; x < width; x++, p++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
380 {
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
381 int64_t v;
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
382 if (UseRound)
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
383 {
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
384 assert(sizeof(long long) == sizeof(int64_t));
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
385 // The "round" operation is very costly
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
386 v = boost::math::llround(static_cast<float>(*p) * factor);
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
387 }
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
388 else
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
389 {
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
390 v = static_cast<int64_t>(static_cast<float>(*p) * factor);
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
391 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
392
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
393 if (v > maxValue)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
394 {
876
e21d1a5f5934 fix msvc warnings
jodogne
parents: 863
diff changeset
395 *p = std::numeric_limits<PixelType>::max();
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
396 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
397 else if (v < minValue)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
398 {
876
e21d1a5f5934 fix msvc warnings
jodogne
parents: 863
diff changeset
399 *p = std::numeric_limits<PixelType>::min();
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
400 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
401 else
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
402 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
403 *p = static_cast<PixelType>(v);
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
404 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
405 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
406 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
407 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
408
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
409
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
410 // Computes "a * x + b" at each pixel => Note that this is not the
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
411 // same convention as in "ShiftScale()", but it is the convention of
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
412 // "ShiftScale2()"
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
413 template <typename TargetType,
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
414 typename SourceType,
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
415 bool UseRound,
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
416 bool Invert>
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
417 static void ShiftScaleIntegerInternal(ImageAccessor& target,
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
418 const ImageAccessor& source,
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
419 float a,
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
420 float b)
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
421 // This function can be applied inplace (source == target)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
422 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
423 assert(target.GetFormat() != PixelFormat_Float32);
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
424
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
425 if (source.GetWidth() != target.GetWidth() ||
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
426 source.GetHeight() != target.GetHeight())
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
427 {
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
428 throw OrthancException(ErrorCode_IncompatibleImageSize);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
429 }
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
430
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
431 if (&source == &target &&
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
432 source.GetFormat() != target.GetFormat())
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
433 {
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
434 throw OrthancException(ErrorCode_IncompatibleImageFormat);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
435 }
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
436
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
437 const TargetType minPixelValue = std::numeric_limits<TargetType>::min();
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
438 const TargetType maxPixelValue = std::numeric_limits<TargetType>::max();
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
439 const float minFloatValue = static_cast<float>(minPixelValue);
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
440 const float maxFloatValue = static_cast<float>(maxPixelValue);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
441
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
442 const unsigned int height = target.GetHeight();
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
443 const unsigned int width = target.GetWidth();
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
444
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
445 for (unsigned int y = 0; y < height; y++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
446 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
447 TargetType* p = reinterpret_cast<TargetType*>(target.GetRow(y));
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
448 const SourceType* q = reinterpret_cast<const SourceType*>(source.GetConstRow(y));
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
449
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
450 for (unsigned int x = 0; x < width; x++, p++, q++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
451 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
452 float v = a * static_cast<float>(*q) + b;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
453
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
454 if (v >= maxFloatValue)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
455 {
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
456 *p = maxPixelValue;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
457 }
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
458 else if (v <= minFloatValue)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
459 {
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
460 *p = minPixelValue;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
461 }
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
462 else if (UseRound)
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
463 {
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
464 // The "round" operation is very costly
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
465 assert(sizeof(TargetType) < sizeof(int));
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
466 *p = static_cast<TargetType>(boost::math::iround(v));
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
467 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
468 else
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
469 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
470 *p = static_cast<TargetType>(std::floor(v));
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
471 }
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
472
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
473 if (Invert)
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
474 {
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
475 *p = maxPixelValue - *p;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
476 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
477 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
478 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
479 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
480
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
481
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
482 template <typename SourceType>
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
483 static void ShiftScaleFloatInternal(ImageAccessor& target,
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
484 const ImageAccessor& source,
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
485 float a,
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
486 float b)
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
487 // This function can be applied inplace (source == target)
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
488 {
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
489 assert(target.GetFormat() == PixelFormat_Float32);
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
490
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
491 if (source.GetWidth() != target.GetWidth() ||
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
492 source.GetHeight() != target.GetHeight())
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
493 {
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
494 throw OrthancException(ErrorCode_IncompatibleImageSize);
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
495 }
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
496
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
497 if (&source == &target &&
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
498 source.GetFormat() != target.GetFormat())
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
499 {
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
500 throw OrthancException(ErrorCode_IncompatibleImageFormat);
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
501 }
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
502
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
503 const unsigned int height = target.GetHeight();
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
504 const unsigned int width = target.GetWidth();
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
505
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
506 for (unsigned int y = 0; y < height; y++)
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
507 {
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
508 float* p = reinterpret_cast<float*>(target.GetRow(y));
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
509 const SourceType* q = reinterpret_cast<const SourceType*>(source.GetConstRow(y));
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
510
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
511 for (unsigned int x = 0; x < width; x++, p++, q++)
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
512 {
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
513 *p = a * static_cast<float>(*q) + b;
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
514 }
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
515 }
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
516 }
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
517
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
518
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
519 template <typename PixelType>
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
520 static void ShiftRightInternal(ImageAccessor& image,
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
521 unsigned int shift)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
522 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
523 const unsigned int height = image.GetHeight();
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
524 const unsigned int width = image.GetWidth();
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
525
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
526 for (unsigned int y = 0; y < height; y++)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
527 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
528 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
529
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
530 for (unsigned int x = 0; x < width; x++, p++)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
531 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
532 *p = *p >> shift;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
533 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
534 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
535 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
536
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
537 template <typename PixelType>
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
538 static void ShiftLeftInternal(ImageAccessor& image,
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
539 unsigned int shift)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
540 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
541 const unsigned int height = image.GetHeight();
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
542 const unsigned int width = image.GetWidth();
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
543
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
544 for (unsigned int y = 0; y < height; y++)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
545 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
546 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
547
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
548 for (unsigned int x = 0; x < width; x++, p++)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
549 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
550 *p = *p << shift;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
551 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
552 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
553 }
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
554
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555 void ImageProcessing::Copy(ImageAccessor& target,
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556 const ImageAccessor& source)
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 if (target.GetWidth() != source.GetWidth() ||
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 target.GetHeight() != source.GetHeight())
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 throw OrthancException(ErrorCode_IncompatibleImageSize);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
562 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564 if (target.GetFormat() != source.GetFormat())
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
566 throw OrthancException(ErrorCode_IncompatibleImageFormat);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
567 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
568
4278
9279de56a405 avoid multiple calls to GetWidth() and GetHeight() on pixel loops
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
569 const unsigned int lineSize = source.GetBytesPerPixel() * source.GetWidth();
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
570 assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
571
4278
9279de56a405 avoid multiple calls to GetWidth() and GetHeight() on pixel loops
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
572 const unsigned int height = source.GetHeight();
9279de56a405 avoid multiple calls to GetWidth() and GetHeight() on pixel loops
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
573 for (unsigned int y = 0; y < height; y++)
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
574 {
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
575 memcpy(target.GetRow(y), source.GetConstRow(y), lineSize);
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
576 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
577 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
578
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
579 template <typename TargetType, typename SourceType>
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
580 static void ApplyWindowingInternal(ImageAccessor& target,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
581 const ImageAccessor& source,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
582 float windowCenter,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
583 float windowWidth,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
584 float rescaleSlope,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
585 float rescaleIntercept,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
586 bool invert)
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
587 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
588 assert(sizeof(SourceType) == source.GetBytesPerPixel() &&
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
589 sizeof(TargetType) == target.GetBytesPerPixel());
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
590
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
591 const TargetType maxTargetValue = std::numeric_limits<TargetType>::max();
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
592 const float maxFloatValue = static_cast<float>(maxTargetValue);
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
593
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
594 const float windowIntercept = windowCenter - windowWidth / 2.0f;
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
595 const float windowSlope = (maxFloatValue + 1.0f) / windowWidth;
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
596
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
597 const float a = rescaleSlope * windowSlope;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
598 const float b = (rescaleIntercept - windowIntercept) * windowSlope;
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
599
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
600 if (invert)
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
601 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
602 ShiftScaleIntegerInternal<TargetType, SourceType, false, true>(target, source, a, b);
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
603 }
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
604 else
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
605 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
606 ShiftScaleIntegerInternal<TargetType, SourceType, false, false>(target, source, a, b);
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
607 }
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
608 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
609
3683
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
610 void ImageProcessing::ApplyWindowing_Deprecated(ImageAccessor& target,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
611 const ImageAccessor& source,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
612 float windowCenter,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
613 float windowWidth,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
614 float rescaleSlope,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
615 float rescaleIntercept,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
616 bool invert)
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
617 {
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
618 if (target.GetWidth() != source.GetWidth() ||
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
619 target.GetHeight() != source.GetHeight())
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
620 {
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
621 throw OrthancException(ErrorCode_IncompatibleImageSize);
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
622 }
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
623
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
624 switch (source.GetFormat())
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
625 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
626 case PixelFormat_Float32:
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
627 {
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
628 switch (target.GetFormat())
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
629 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
630 case PixelFormat_Grayscale8:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
631 ApplyWindowingInternal<uint8_t, float>(target, source, windowCenter, windowWidth, rescaleSlope, rescaleIntercept, invert);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
632 break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
633 case PixelFormat_Grayscale16:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
634 ApplyWindowingInternal<uint16_t, float>(target, source, windowCenter, windowWidth, rescaleSlope, rescaleIntercept, invert);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
635 break;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
636 default:
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
637 throw OrthancException(ErrorCode_NotImplemented);
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
638 }
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
639 };break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
640 case PixelFormat_Grayscale8:
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
641 {
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
642 switch (target.GetFormat())
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
643 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
644 case PixelFormat_Grayscale8:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
645 ApplyWindowingInternal<uint8_t, uint8_t>(target, source, windowCenter, windowWidth, rescaleSlope, rescaleIntercept, invert);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
646 break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
647 case PixelFormat_Grayscale16:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
648 ApplyWindowingInternal<uint16_t, uint8_t>(target, source, windowCenter, windowWidth, rescaleSlope, rescaleIntercept, invert);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
649 break;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
650 default:
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
651 throw OrthancException(ErrorCode_NotImplemented);
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
652 }
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
653 };break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
654 case PixelFormat_Grayscale16:
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
655 {
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
656 switch (target.GetFormat())
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
657 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
658 case PixelFormat_Grayscale8:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
659 ApplyWindowingInternal<uint8_t, uint16_t>(target, source, windowCenter, windowWidth, rescaleSlope, rescaleIntercept, invert);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
660 break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
661 case PixelFormat_Grayscale16:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
662 ApplyWindowingInternal<uint16_t, uint16_t>(target, source, windowCenter, windowWidth, rescaleSlope, rescaleIntercept, invert);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
663 break;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
664 default:
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
665 throw OrthancException(ErrorCode_NotImplemented);
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
666 }
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
667 };break;
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
668 default:
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
669 throw OrthancException(ErrorCode_NotImplemented);
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
670 }
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
671 }
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
672
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
673
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
674 void ImageProcessing::Convert(ImageAccessor& target,
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
675 const ImageAccessor& source)
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
676 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
677 if (target.GetWidth() != source.GetWidth() ||
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
678 target.GetHeight() != source.GetHeight())
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
679 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
680 throw OrthancException(ErrorCode_IncompatibleImageSize);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
681 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
682
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
683 const unsigned int width = source.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
684 const unsigned int height = source.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
685
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
686 if (source.GetFormat() == target.GetFormat())
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
687 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
688 Copy(target, source);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
689 return;
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
690 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
691
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
692 if (target.GetFormat() == PixelFormat_Grayscale16 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
693 source.GetFormat() == PixelFormat_Grayscale8)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
694 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
695 ConvertInternal<uint16_t, uint8_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
696 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
697 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
698
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
699 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
700 source.GetFormat() == PixelFormat_Grayscale8)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
701 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
702 ConvertInternal<int16_t, uint8_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
703 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
704 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
705
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
706 if (target.GetFormat() == PixelFormat_Grayscale8 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
707 source.GetFormat() == PixelFormat_Grayscale16)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
708 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
709 ConvertInternal<uint8_t, uint16_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
710 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
711 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
712
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
713 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
714 source.GetFormat() == PixelFormat_Grayscale16)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
715 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
716 ConvertInternal<int16_t, uint16_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
717 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
718 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
719
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
720 if (target.GetFormat() == PixelFormat_Grayscale8 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
721 source.GetFormat() == PixelFormat_SignedGrayscale16)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
722 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
723 ConvertInternal<uint8_t, int16_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
724 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
725 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
726
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
727 if (target.GetFormat() == PixelFormat_Grayscale16 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
728 source.GetFormat() == PixelFormat_SignedGrayscale16)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
729 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
730 ConvertInternal<uint16_t, int16_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
731 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
732 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
733
993
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
734 if (target.GetFormat() == PixelFormat_Grayscale8 &&
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
735 source.GetFormat() == PixelFormat_RGB24)
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
736 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
737 ConvertColorToGrayscale<uint8_t>(target, source);
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
738 return;
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
739 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
740
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
741 if (target.GetFormat() == PixelFormat_Grayscale16 &&
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
742 source.GetFormat() == PixelFormat_RGB24)
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
743 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
744 ConvertColorToGrayscale<uint16_t>(target, source);
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
745 return;
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
746 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
747
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
748 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
749 source.GetFormat() == PixelFormat_RGB24)
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
750 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
751 ConvertColorToGrayscale<int16_t>(target, source);
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
752 return;
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
753 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
754
1993
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
755 if (target.GetFormat() == PixelFormat_Float32 &&
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
756 source.GetFormat() == PixelFormat_Grayscale8)
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
757 {
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
758 ConvertGrayscaleToFloat<uint8_t>(target, source);
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
759 return;
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
760 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
761
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
762 if (target.GetFormat() == PixelFormat_Float32 &&
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
763 source.GetFormat() == PixelFormat_Grayscale16)
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
764 {
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
765 ConvertGrayscaleToFloat<uint16_t>(target, source);
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
766 return;
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
767 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
768
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
769 if (target.GetFormat() == PixelFormat_Float32 &&
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
770 source.GetFormat() == PixelFormat_Grayscale32)
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
771 {
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
772 ConvertGrayscaleToFloat<uint32_t>(target, source);
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
773 return;
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
774 }
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
775
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
776 if (target.GetFormat() == PixelFormat_Float32 &&
1993
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
777 source.GetFormat() == PixelFormat_SignedGrayscale16)
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
778 {
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
779 ConvertGrayscaleToFloat<int16_t>(target, source);
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
780 return;
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
781 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
782
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
783
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
784 if (target.GetFormat() == PixelFormat_Grayscale8 &&
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
785 source.GetFormat() == PixelFormat_RGBA32)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
786 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
787 for (unsigned int y = 0; y < height; y++)
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
788 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
789 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
790 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
791 for (unsigned int x = 0; x < width; x++, q++)
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
792 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
793 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[0]) +
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
794 7152 * static_cast<uint32_t>(p[1]) +
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
795 0722 * static_cast<uint32_t>(p[2])) / 10000);
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
796 p += 4;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
797 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
798 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
799
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
800 return;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
801 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
802
2840
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
803 if (target.GetFormat() == PixelFormat_Grayscale8 &&
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
804 source.GetFormat() == PixelFormat_BGRA32)
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
805 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
806 for (unsigned int y = 0; y < height; y++)
2840
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
807 {
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
808 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
809 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
810 for (unsigned int x = 0; x < width; x++, q++)
2840
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
811 {
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
812 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[2]) +
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
813 7152 * static_cast<uint32_t>(p[1]) +
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
814 0722 * static_cast<uint32_t>(p[0])) / 10000);
2840
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
815 p += 4;
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
816 }
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
817 }
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
818
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
819 return;
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
820 }
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
821
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
822 if (target.GetFormat() == PixelFormat_RGB24 &&
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
823 source.GetFormat() == PixelFormat_RGBA32)
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
824 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
825 for (unsigned int y = 0; y < height; y++)
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
826 {
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
827 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
828 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
829 for (unsigned int x = 0; x < width; x++)
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
830 {
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
831 q[0] = p[0];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
832 q[1] = p[1];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
833 q[2] = p[2];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
834 p += 4;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
835 q += 3;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
836 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
837 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
838
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
839 return;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
840 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
841
2252
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
842 if (target.GetFormat() == PixelFormat_RGB24 &&
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
843 source.GetFormat() == PixelFormat_BGRA32)
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
844 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
845 for (unsigned int y = 0; y < height; y++)
2252
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
846 {
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
847 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
848 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
849 for (unsigned int x = 0; x < width; x++)
2252
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
850 {
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
851 q[0] = p[2];
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
852 q[1] = p[1];
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
853 q[2] = p[0];
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
854 p += 4;
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
855 q += 3;
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
856 }
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
857 }
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
858
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
859 return;
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
860 }
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
861
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
862 if (target.GetFormat() == PixelFormat_RGBA32 &&
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
863 source.GetFormat() == PixelFormat_RGB24)
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
864 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
865 for (unsigned int y = 0; y < height; y++)
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
866 {
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
867 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
868 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
869 for (unsigned int x = 0; x < width; x++)
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
870 {
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
871 q[0] = p[0];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
872 q[1] = p[1];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
873 q[2] = p[2];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
874 q[3] = 255; // Set the alpha channel to full opacity
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
875 p += 3;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
876 q += 4;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
877 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
878 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
879
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
880 return;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
881 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
882
1992
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
883 if (target.GetFormat() == PixelFormat_RGB24 &&
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
884 source.GetFormat() == PixelFormat_Grayscale8)
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
885 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
886 for (unsigned int y = 0; y < height; y++)
1992
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
887 {
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
888 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
889 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
890 for (unsigned int x = 0; x < width; x++)
1992
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
891 {
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
892 q[0] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
893 q[1] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
894 q[2] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
895 p += 1;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
896 q += 3;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
897 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
898 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
899
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
900 return;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
901 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
902
2650
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2645
diff changeset
903 if ((target.GetFormat() == PixelFormat_RGBA32 ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2645
diff changeset
904 target.GetFormat() == PixelFormat_BGRA32) &&
1992
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
905 source.GetFormat() == PixelFormat_Grayscale8)
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
906 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
907 for (unsigned int y = 0; y < height; y++)
1992
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
908 {
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
909 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
910 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
911 for (unsigned int x = 0; x < width; x++)
1992
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
912 {
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
913 q[0] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
914 q[1] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
915 q[2] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
916 q[3] = 255;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
917 p += 1;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
918 q += 4;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
919 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
920 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
921
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
922 return;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
923 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
924
2100
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
925 if (target.GetFormat() == PixelFormat_BGRA32 &&
2495
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
926 source.GetFormat() == PixelFormat_Grayscale16)
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
927 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
928 for (unsigned int y = 0; y < height; y++)
2495
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
929 {
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
930 const uint16_t* p = reinterpret_cast<const uint16_t*>(source.GetConstRow(y));
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
931 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
932 for (unsigned int x = 0; x < width; x++)
2495
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
933 {
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
934 uint8_t value = (*p < 256 ? *p : 255);
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
935 q[0] = value;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
936 q[1] = value;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
937 q[2] = value;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
938 q[3] = 255;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
939 p += 1;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
940 q += 4;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
941 }
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
942 }
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
943
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
944 return;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
945 }
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
946
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
947 if (target.GetFormat() == PixelFormat_BGRA32 &&
2496
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
948 source.GetFormat() == PixelFormat_SignedGrayscale16)
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
949 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
950 for (unsigned int y = 0; y < height; y++)
2496
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
951 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
952 const int16_t* p = reinterpret_cast<const int16_t*>(source.GetConstRow(y));
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
953 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
954 for (unsigned int x = 0; x < width; x++)
2496
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
955 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
956 uint8_t value;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
957 if (*p < 0)
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
958 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
959 value = 0;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
960 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
961 else if (*p > 255)
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
962 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
963 value = 255;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
964 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
965 else
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
966 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
967 value = static_cast<uint8_t>(*p);
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
968 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
969
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
970 q[0] = value;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
971 q[1] = value;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
972 q[2] = value;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
973 q[3] = 255;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
974 p += 1;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
975 q += 4;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
976 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
977 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
978
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
979 return;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
980 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
981
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
982 if (target.GetFormat() == PixelFormat_BGRA32 &&
2100
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
983 source.GetFormat() == PixelFormat_RGB24)
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
984 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
985 for (unsigned int y = 0; y < height; y++)
2100
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
986 {
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
987 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
988 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
989 for (unsigned int x = 0; x < width; x++)
2100
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
990 {
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
991 q[0] = p[2];
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
992 q[1] = p[1];
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
993 q[2] = p[0];
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
994 q[3] = 255;
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
995 p += 3;
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
996 q += 4;
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
997 }
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
998 }
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
999
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
1000 return;
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
1001 }
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
1002
3547
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1003 if ((target.GetFormat() == PixelFormat_BGRA32 &&
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1004 source.GetFormat() == PixelFormat_RGBA32)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1005 || (target.GetFormat() == PixelFormat_RGBA32 &&
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1006 source.GetFormat() == PixelFormat_BGRA32))
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1007 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1008 for (unsigned int y = 0; y < height; y++)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1009 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1010 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1011 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1012 for (unsigned int x = 0; x < width; x++)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1013 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1014 q[0] = p[2];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1015 q[1] = p[1];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1016 q[2] = p[0];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1017 q[3] = p[3];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1018 p += 4;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1019 q += 4;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1020 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1021 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1022
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1023 return;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1024 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1025
2423
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1026 if (target.GetFormat() == PixelFormat_RGB24 &&
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1027 source.GetFormat() == PixelFormat_RGB48)
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1028 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1029 for (unsigned int y = 0; y < height; y++)
2423
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1030 {
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1031 const uint16_t* p = reinterpret_cast<const uint16_t*>(source.GetConstRow(y));
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1032 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1033 for (unsigned int x = 0; x < width; x++)
2423
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1034 {
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1035 q[0] = p[0] >> 8;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1036 q[1] = p[1] >> 8;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1037 q[2] = p[2] >> 8;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1038 p += 3;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1039 q += 3;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1040 }
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1041 }
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1042
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1043 return;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1044 }
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1045
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1046 if (target.GetFormat() == PixelFormat_Grayscale16 &&
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1047 source.GetFormat() == PixelFormat_Float32)
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1048 {
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1049 ConvertFloatToGrayscale<PixelFormat_Grayscale16>(target, source);
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1050 return;
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1051 }
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1052
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1053 if (target.GetFormat() == PixelFormat_Grayscale8 &&
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1054 source.GetFormat() == PixelFormat_Float32)
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1055 {
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1056 ConvertFloatToGrayscale<PixelFormat_Grayscale8>(target, source);
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1057 return;
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1058 }
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1059
4664
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1060 if (target.GetFormat() == PixelFormat_RGB24 &&
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1061 source.GetFormat() == PixelFormat_Float32)
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1062 {
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1063 ConvertFloatToGrayscale<PixelFormat_RGB24>(target, source);
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1064 return;
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1065 }
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1066
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1067 throw OrthancException(ErrorCode_NotImplemented);
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1068 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1069
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1070
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1071
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1072 void ImageProcessing::Set(ImageAccessor& image,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1073 int64_t value)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1074 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1075 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1076 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1077 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1078 SetInternal<uint8_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1079 return;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1080
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1081 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1082 SetInternal<uint16_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1083 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1084
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1085 case PixelFormat_Grayscale32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1086 SetInternal<uint32_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1087 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1088
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1089 case PixelFormat_Grayscale64:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1090 SetInternal<uint64_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1091 return;
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1092
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1093 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1094 SetInternal<int16_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1095 return;
2645
89b789366596 Grayscale64 pixel format
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2496
diff changeset
1096
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1097 case PixelFormat_Float32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1098 assert(sizeof(float) == 4);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1099 SetInternal<float>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1100 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1101
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1102 case PixelFormat_RGBA32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1103 case PixelFormat_BGRA32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1104 case PixelFormat_RGB24:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1105 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1106 uint8_t v = static_cast<uint8_t>(value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1107 Set(image, v, v, v, v); // Use the color version
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1108 return;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1109 }
1994
4d099fee5eca ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1993
diff changeset
1110
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1111 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1112 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1113 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1114 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1115
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1116
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1117 void ImageProcessing::Set(ImageAccessor& image,
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1118 uint8_t red,
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1119 uint8_t green,
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1120 uint8_t blue,
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1121 uint8_t alpha)
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1122 {
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1123 uint8_t p[4];
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1124 unsigned int size;
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1125
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1126 switch (image.GetFormat())
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1127 {
4446
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1128 case PixelFormat_Grayscale8:
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1129 {
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1130 // New in Orthanc 1.9.0
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1131 uint8_t grayscale = (2126 * static_cast<uint16_t>(red) +
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1132 7152 * static_cast<uint16_t>(green) +
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1133 0722 * static_cast<uint16_t>(blue)) / 10000;
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1134 Orthanc::ImageProcessing::Set(image, grayscale);
4447
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4446
diff changeset
1135 return;
4446
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1136 }
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1137
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1138 case PixelFormat_RGBA32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1139 p[0] = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1140 p[1] = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1141 p[2] = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1142 p[3] = alpha;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1143 size = 4;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1144 break;
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1145
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1146 case PixelFormat_BGRA32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1147 p[0] = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1148 p[1] = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1149 p[2] = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1150 p[3] = alpha;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1151 size = 4;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1152 break;
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1153
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1154 case PixelFormat_RGB24:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1155 p[0] = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1156 p[1] = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1157 p[2] = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1158 size = 3;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1159 break;
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1160
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1161 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1162 throw OrthancException(ErrorCode_NotImplemented);
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1163 }
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1164
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1165 const unsigned int width = image.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1166 const unsigned int height = image.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1167
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1168 for (unsigned int y = 0; y < height; y++)
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1169 {
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1170 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y));
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1171
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1172 for (unsigned int x = 0; x < width; x++)
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1173 {
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1174 for (unsigned int i = 0; i < size; i++)
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1175 {
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1176 q[i] = p[i];
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1177 }
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1178
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1179 q += size;
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1180 }
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1181 }
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1182 }
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1183
3547
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1184 void ImageProcessing::Set(ImageAccessor& image,
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
1185 uint8_t red,
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
1186 uint8_t green,
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
1187 uint8_t blue,
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
1188 ImageAccessor& alpha)
3547
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1189 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1190 uint8_t p[4];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1191
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1192 if (alpha.GetWidth() != image.GetWidth() || alpha.GetHeight() != image.GetHeight())
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1193 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1194 throw OrthancException(ErrorCode_IncompatibleImageSize);
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1195 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1196
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1197 if (alpha.GetFormat() != PixelFormat_Grayscale8)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1198 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1199 throw OrthancException(ErrorCode_NotImplemented);
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1200 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1201
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1202 switch (image.GetFormat())
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1203 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1204 case PixelFormat_RGBA32:
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1205 p[0] = red;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1206 p[1] = green;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1207 p[2] = blue;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1208 break;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1209
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1210 case PixelFormat_BGRA32:
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1211 p[0] = blue;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1212 p[1] = green;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1213 p[2] = red;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1214 break;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1215
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1216 default:
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1217 throw OrthancException(ErrorCode_NotImplemented);
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1218 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1219
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1220 const unsigned int width = image.GetWidth();
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1221 const unsigned int height = image.GetHeight();
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1222
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1223 for (unsigned int y = 0; y < height; y++)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1224 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1225 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y));
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1226 uint8_t* a = reinterpret_cast<uint8_t*>(alpha.GetRow(y));
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1227
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1228 for (unsigned int x = 0; x < width; x++)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1229 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1230 for (unsigned int i = 0; i < 3; i++)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1231 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1232 q[i] = p[i];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1233 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1234 q[3] = *a;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1235 q += 4;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1236 ++a;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1237 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1238 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1239 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1240
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1241
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1242 void ImageProcessing::ShiftRight(ImageAccessor& image,
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1243 unsigned int shift)
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1244 {
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1245 if (image.GetWidth() == 0 ||
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1246 image.GetHeight() == 0 ||
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1247 shift == 0)
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1248 {
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1249 // Nothing to do
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1250 return;
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1251 }
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1252
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1253 switch (image.GetFormat())
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1254 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1255 case PixelFormat_Grayscale8:
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1256 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1257 ShiftRightInternal<uint8_t>(image, shift);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1258 break;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1259 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1260
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1261 case PixelFormat_Grayscale16:
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1262 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1263 ShiftRightInternal<uint16_t>(image, shift);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1264 break;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1265 }
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1266 default:
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1267 throw OrthancException(ErrorCode_NotImplemented);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1268 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1269 }
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1270
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1271 void ImageProcessing::ShiftLeft(ImageAccessor& image,
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1272 unsigned int shift)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1273 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1274 if (image.GetWidth() == 0 ||
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1275 image.GetHeight() == 0 ||
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1276 shift == 0)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1277 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1278 // Nothing to do
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1279 return;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1280 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1281
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1282 switch (image.GetFormat())
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1283 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1284 case PixelFormat_Grayscale8:
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1285 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1286 ShiftLeftInternal<uint8_t>(image, shift);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1287 break;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1288 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1289
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1290 case PixelFormat_Grayscale16:
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1291 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1292 ShiftLeftInternal<uint16_t>(image, shift);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1293 break;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1294 }
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1295 default:
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1296 throw OrthancException(ErrorCode_NotImplemented);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1297 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1298 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1299
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1300 void ImageProcessing::GetMinMaxIntegerValue(int64_t& minValue,
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1301 int64_t& maxValue,
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1302 const ImageAccessor& image)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1303 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1304 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1305 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1306 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1307 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1308 uint8_t a, b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1309 GetMinMaxValueInternal<uint8_t>(a, b, image);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1310 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1311 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1312 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1313 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1314
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1315 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1316 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1317 uint16_t a, b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1318 GetMinMaxValueInternal<uint16_t>(a, b, image);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1319 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1320 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1321 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1322 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1323
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1324 case PixelFormat_Grayscale32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1325 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1326 uint32_t a, b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1327 GetMinMaxValueInternal<uint32_t>(a, b, image);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1328 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1329 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1330 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1331 }
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1332
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1333 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1334 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1335 int16_t a, b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1336 GetMinMaxValueInternal<int16_t>(a, b, image);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1337 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1338 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1339 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1340 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1341
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1342 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1343 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1344 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1345 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1346
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1347
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1348 void ImageProcessing::GetMinMaxFloatValue(float& minValue,
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1349 float& maxValue,
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1350 const ImageAccessor& image)
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1351 {
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1352 switch (image.GetFormat())
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1353 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1354 case PixelFormat_Float32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1355 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1356 assert(sizeof(float) == 4);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1357 float a, b;
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1358
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1359 /**
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1360 * WARNING - On floating-point types, the minimal value is
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1361 * "-FLT_MAX" (as implemented by "::lowest()"), not "FLT_MIN"
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1362 * (as implemented by "::min()")
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1363 * https://en.cppreference.com/w/cpp/types/numeric_limits
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1364 **/
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1365 GetMinMaxValueInternal<float>(a, b, image, -std::numeric_limits<float>::max());
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1366 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1367 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1368 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1369 }
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1370
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1371 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1372 throw OrthancException(ErrorCode_NotImplemented);
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1373 }
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1374 }
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1375
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1376
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1377
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1378 void ImageProcessing::AddConstant(ImageAccessor& image,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1379 int64_t value)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1380 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1381 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1382 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1383 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1384 AddConstantInternal<uint8_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1385 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1386
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1387 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1388 AddConstantInternal<uint16_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1389 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1390
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1391 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1392 AddConstantInternal<int16_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1393 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1394
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1395 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1396 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1397 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1398 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1399
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1400
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1401 void ImageProcessing::MultiplyConstant(ImageAccessor& image,
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
1402 float factor,
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
1403 bool useRound)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1404 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1405 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1406 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1407 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1408 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1409 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1410 MultiplyConstantInternal<uint8_t, true>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1411 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1412 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1413 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1414 MultiplyConstantInternal<uint8_t, false>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1415 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1416 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1417
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1418 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1419 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1420 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1421 MultiplyConstantInternal<uint16_t, true>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1422 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1423 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1424 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1425 MultiplyConstantInternal<uint16_t, false>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1426 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1427 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1428
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1429 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1430 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1431 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1432 MultiplyConstantInternal<int16_t, true>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1433 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1434 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1435 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1436 MultiplyConstantInternal<int16_t, false>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1437 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1438 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1439
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1440 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1441 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1442 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1443 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1444
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1445
4780
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1446 static bool IsIdentityRescaling(float offset,
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1447 float scaling)
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1448 {
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1449 return (std::abs(offset) <= 10.0f * std::numeric_limits<float>::epsilon() &&
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1450 std::abs(scaling - 1.0f) <= 10.0f * std::numeric_limits<float>::epsilon());
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1451 }
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1452
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1453
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1454 void ImageProcessing::ShiftScale2(ImageAccessor& image,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1455 float offset,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1456 float scaling,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1457 bool useRound)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1458 {
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1459 // We compute "a * x + b"
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1460 const float a = scaling;
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1461 const float b = offset;
4780
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1462
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1463 if (IsIdentityRescaling(offset, scaling))
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1464 {
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1465 return;
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1466 }
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1467
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1468 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1469 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1470 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1471 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1472 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1473 ShiftScaleIntegerInternal<uint8_t, uint8_t, true, false>(image, image, a, b);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1474 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1475 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1476 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1477 ShiftScaleIntegerInternal<uint8_t, uint8_t, false, false>(image, image, a, b);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1478 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1479 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1480
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1481 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1482 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1483 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1484 ShiftScaleIntegerInternal<uint16_t, uint16_t, true, false>(image, image, a, b);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1485 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1486 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1487 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1488 ShiftScaleIntegerInternal<uint16_t, uint16_t, false, false>(image, image, a, b);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1489 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1490 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1491
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1492 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1493 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1494 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1495 ShiftScaleIntegerInternal<int16_t, int16_t, true, false>(image, image, a, b);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1496 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1497 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1498 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1499 ShiftScaleIntegerInternal<int16_t, int16_t, false, false>(image, image, a, b);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1500 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1501 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1502
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1503 case PixelFormat_Float32:
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1504 if (useRound)
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1505 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1506 ShiftScaleFloatInternal<float>(image, image, a, b);
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1507 }
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1508 else
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1509 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1510 ShiftScaleFloatInternal<float>(image, image, a, b);
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1511 }
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1512 return;
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1513
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1514 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1515 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1516 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1517 }
2281
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1518
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1519
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1520 void ImageProcessing::ShiftScale2(ImageAccessor& target,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1521 const ImageAccessor& source,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1522 float offset,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1523 float scaling,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1524 bool useRound)
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1525 {
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1526 // We compute "a * x + b"
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1527 const float a = scaling;
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1528 const float b = offset;
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1529
4780
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1530 if (target.GetFormat() == source.GetFormat() &&
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1531 IsIdentityRescaling(offset, scaling))
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1532 {
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1533 Copy(target, source);
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1534 return;
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1535 }
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1536
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1537 switch (target.GetFormat())
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1538 {
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1539 case PixelFormat_Grayscale8:
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1540
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1541 switch (source.GetFormat())
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1542 {
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1543 case PixelFormat_Float32:
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1544 if (useRound)
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1545 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1546 ShiftScaleIntegerInternal<uint8_t, float, true, false>(target, source, a, b);
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1547 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1548 else
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1549 {
4974
fcdf399f9fc0 fix ImageProcessing::ShiftScale2() on floating-point images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4948
diff changeset
1550 ShiftScaleIntegerInternal<uint8_t, float, false, false>(target, source, a, b);
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1551 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1552 return;
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1553
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1554 default:
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1555 throw OrthancException(ErrorCode_NotImplemented);
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1556 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1557
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1558 default:
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1559 throw OrthancException(ErrorCode_NotImplemented);
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1560 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1561 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1562
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1563
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1564 void ImageProcessing::ShiftScale(ImageAccessor& image,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1565 float offset,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1566 float scaling,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1567 bool useRound)
4079
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1568 {
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1569 // Rewrite "(x + offset) * scaling" as "a * x + b"
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1570
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1571 const float a = scaling;
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1572 const float b = offset * scaling;
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1573 ShiftScale2(image, b, a, useRound);
4079
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1574 }
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1575
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1576
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1577 void ImageProcessing::ShiftScale(ImageAccessor& target,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1578 const ImageAccessor& source,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1579 float offset,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1580 float scaling,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1581 bool useRound)
4079
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1582 {
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1583 // Rewrite "(x + offset) * scaling" as "a * x + b"
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1584
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1585 const float a = scaling;
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1586 const float b = offset * scaling;
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1587 ShiftScale2(target, source, b, a, useRound);
4079
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1588 }
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1589
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1590
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1591
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1592 void ImageProcessing::Invert(ImageAccessor& image, int64_t maxValue)
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1593 {
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1594 const unsigned int width = image.GetWidth();
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1595 const unsigned int height = image.GetHeight();
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1596
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1597 switch (image.GetFormat())
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1598 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1599 case PixelFormat_Grayscale16:
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1600 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1601 uint16_t maxValueUint16 = (uint16_t)(std::min(maxValue, static_cast<int64_t>(std::numeric_limits<uint16_t>::max())));
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1602
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1603 for (unsigned int y = 0; y < height; y++)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1604 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1605 uint16_t* p = reinterpret_cast<uint16_t*>(image.GetRow(y));
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1606
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1607 for (unsigned int x = 0; x < width; x++, p++)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1608 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1609 *p = maxValueUint16 - (*p);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1610 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1611 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1612
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1613 return;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1614 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1615 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1616 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1617 uint8_t maxValueUint8 = (uint8_t)(std::min(maxValue, static_cast<int64_t>(std::numeric_limits<uint8_t>::max())));
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1618
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1619 for (unsigned int y = 0; y < height; y++)
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1620 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1621 uint8_t* p = reinterpret_cast<uint8_t*>(image.GetRow(y));
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1622
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1623 for (unsigned int x = 0; x < width; x++, p++)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1624 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1625 *p = maxValueUint8 - (*p);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1626 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1627 }
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1628
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1629 return;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1630 }
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1631
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1632 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1633 throw OrthancException(ErrorCode_NotImplemented);
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1634 }
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1635
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1636 }
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1637
2281
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1638 void ImageProcessing::Invert(ImageAccessor& image)
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1639 {
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1640 switch (image.GetFormat())
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1641 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1642 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1643 return Invert(image, 255);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1644 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1645 throw OrthancException(ErrorCode_NotImplemented); // you should use the Invert(image, maxValue) overload
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1646 }
2281
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1647 }
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1648
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1649
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1650
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1651 namespace
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1652 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1653 template <PixelFormat Format>
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1654 class BresenhamPixelWriter
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1655 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1656 private:
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1657 typedef typename PixelTraits<Format>::PixelType PixelType;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1658
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1659 ImageAccessor& image_;
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1660 PixelType value_;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1661
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1662 void PlotLineLow(int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1663 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1664 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1665 int y1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1666 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1667 int dx = x1 - x0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1668 int dy = y1 - y0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1669 int yi = 1;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1670
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1671 if (dy < 0)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1672 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1673 yi = -1;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1674 dy = -dy;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1675 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1676
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1677 int d = 2 * dy - dx;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1678 int y = y0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1679
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1680 for (int x = x0; x <= x1; x++)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1681 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1682 Write(x, y);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1683
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1684 if (d > 0)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1685 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1686 y = y + yi;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1687 d = d - 2 * dx;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1688 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1689
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1690 d = d + 2*dy;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1691 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1692 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1693
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1694 void PlotLineHigh(int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1695 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1696 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1697 int y1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1698 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1699 int dx = x1 - x0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1700 int dy = y1 - y0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1701 int xi = 1;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1702
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1703 if (dx < 0)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1704 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1705 xi = -1;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1706 dx = -dx;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1707 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1708
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1709 int d = 2 * dx - dy;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1710 int x = x0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1711
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1712 for (int y = y0; y <= y1; y++)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1713 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1714 Write(x, y);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1715
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1716 if (d > 0)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1717 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1718 x = x + xi;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1719 d = d - 2 * dy;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1720 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1721
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1722 d = d + 2 * dx;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1723 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1724 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1725
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1726 public:
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1727 BresenhamPixelWriter(ImageAccessor& image,
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1728 int64_t value) :
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1729 image_(image),
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1730 value_(PixelTraits<Format>::IntegerToPixel(value))
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1731 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1732 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1733
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1734 BresenhamPixelWriter(ImageAccessor& image,
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1735 const PixelType& value) :
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1736 image_(image),
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1737 value_(value)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1738 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1739 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1740
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1741 void Write(int x,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1742 int y)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1743 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1744 if (x >= 0 &&
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1745 y >= 0 &&
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1746 static_cast<unsigned int>(x) < image_.GetWidth() &&
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1747 static_cast<unsigned int>(y) < image_.GetHeight())
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1748 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1749 PixelType* p = reinterpret_cast<PixelType*>(image_.GetRow(y));
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1750 p[x] = value_;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1751 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1752 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1753
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1754 void DrawSegment(int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1755 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1756 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1757 int y1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1758 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1759 // This is an implementation of Bresenham's line algorithm
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1760 // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1761
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1762 if (abs(y1 - y0) < abs(x1 - x0))
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1763 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1764 if (x0 > x1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1765 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1766 PlotLineLow(x1, y1, x0, y0);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1767 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1768 else
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1769 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1770 PlotLineLow(x0, y0, x1, y1);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1771 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1772 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1773 else
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1774 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1775 if (y0 > y1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1776 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1777 PlotLineHigh(x1, y1, x0, y0);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1778 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1779 else
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1780 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1781 PlotLineHigh(x0, y0, x1, y1);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1782 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1783 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1784 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1785 };
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1786 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1787
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1788
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1789 void ImageProcessing::DrawLineSegment(ImageAccessor& image,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1790 int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1791 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1792 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1793 int y1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1794 int64_t value)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1795 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1796 switch (image.GetFormat())
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1797 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1798 case PixelFormat_Grayscale8:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1799 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1800 BresenhamPixelWriter<PixelFormat_Grayscale8> writer(image, value);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1801 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1802 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1803 }
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1804
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1805 case PixelFormat_Grayscale16:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1806 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1807 BresenhamPixelWriter<PixelFormat_Grayscale16> writer(image, value);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1808 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1809 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1810 }
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1811
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1812 case PixelFormat_SignedGrayscale16:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1813 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1814 BresenhamPixelWriter<PixelFormat_SignedGrayscale16> writer(image, value);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1815 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1816 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1817 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1818
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1819 default:
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1820 throw OrthancException(ErrorCode_NotImplemented);
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1821 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1822 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1823
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1824
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1825 void ImageProcessing::DrawLineSegment(ImageAccessor& image,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1826 int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1827 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1828 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1829 int y1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1830 uint8_t red,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1831 uint8_t green,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1832 uint8_t blue,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1833 uint8_t alpha)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1834 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1835 switch (image.GetFormat())
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1836 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1837 case PixelFormat_BGRA32:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1838 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1839 PixelTraits<PixelFormat_BGRA32>::PixelType pixel;
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1840 pixel.red_ = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1841 pixel.green_ = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1842 pixel.blue_ = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1843 pixel.alpha_ = alpha;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1844
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1845 BresenhamPixelWriter<PixelFormat_BGRA32> writer(image, pixel);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1846 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1847 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1848 }
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1849
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1850 case PixelFormat_RGBA32:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1851 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1852 PixelTraits<PixelFormat_RGBA32>::PixelType pixel;
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1853 pixel.red_ = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1854 pixel.green_ = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1855 pixel.blue_ = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1856 pixel.alpha_ = alpha;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1857
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1858 BresenhamPixelWriter<PixelFormat_RGBA32> writer(image, pixel);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1859 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1860 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1861 }
2783
65699fcb4e99 PixelTraits<PixelFormat_RGBA32>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2650
diff changeset
1862
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1863 case PixelFormat_RGB24:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1864 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1865 PixelTraits<PixelFormat_RGB24>::PixelType pixel;
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1866 pixel.red_ = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1867 pixel.green_ = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1868 pixel.blue_ = blue;
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1869
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1870 BresenhamPixelWriter<PixelFormat_RGB24> writer(image, pixel);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1871 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1872 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1873 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1874
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1875 default:
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1876 throw OrthancException(ErrorCode_NotImplemented);
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1877 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1878 }
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1879
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1880 void ComputePolygonExtent(int32_t& left, int32_t& right, int32_t& top, int32_t& bottom, const std::vector<ImageProcessing::ImagePoint>& points)
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1881 {
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1882 left = std::numeric_limits<int32_t>::max();
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1883 right = std::numeric_limits<int32_t>::min();
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1884 top = std::numeric_limits<int32_t>::max();
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1885 bottom = std::numeric_limits<int32_t>::min();
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1886
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1887 for (size_t i = 0; i < points.size(); i++)
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1888 {
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1889 const ImageProcessing::ImagePoint& p = points[i];
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1890 left = std::min(p.GetX(), left);
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1891 right = std::max(p.GetX(), right);
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1892 bottom = std::max(p.GetY(), bottom);
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1893 top = std::min(p.GetY(), top);
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1894 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1895 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1896
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1897
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1898 namespace
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1899 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1900 #define USE_POLYGON_FRACTIONS 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1901
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1902 class PolygonEdge
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1903 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1904 private:
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1905 int yUpper;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1906
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1907 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1908 int x;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1909 int xOffset;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1910 int dxPerScanNumerator;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1911 int dxPerScanDenominator;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1912 #else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1913 float xIntersect;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1914 float dxPerScan;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1915 #endif
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1916
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1917 public:
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1918 PolygonEdge(const ImageProcessing::ImagePoint& lower,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1919 const ImageProcessing::ImagePoint& upper,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1920 int yComp)
3365
9345710bbf12 check limits in fillpolygon
Alain Mazy <alain@mazy.be>
parents: 3323
diff changeset
1921 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1922 // cf. "makeEdgeRec()" in textbook
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1923
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1924 assert(upper.GetY() != lower.GetY());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1925
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1926 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1927 x = lower.GetX();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1928 xOffset = 0;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1929 dxPerScanNumerator = upper.GetX() - lower.GetX();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1930 dxPerScanDenominator = upper.GetY() - lower.GetY();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1931 #else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1932 dxPerScan = (static_cast<float>(upper.GetX() - lower.GetX()) /
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1933 static_cast<float>(upper.GetY() - lower.GetY()));
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1934 xIntersect = lower.GetX();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1935 #endif
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1936
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1937 if (upper.GetY() < yComp)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1938 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1939 yUpper = upper.GetY() - 1;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1940 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1941 else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1942 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1943 yUpper = upper.GetY();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1944 }
3365
9345710bbf12 check limits in fillpolygon
Alain Mazy <alain@mazy.be>
parents: 3323
diff changeset
1945 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1946
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1947 void NextScanLine()
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1948 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1949 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1950 xOffset += dxPerScanNumerator;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1951
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1952 while (xOffset >= dxPerScanDenominator)
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1953 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1954 x++;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1955 xOffset -= dxPerScanDenominator;
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1956 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1957
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1958 while (xOffset < 0)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1959 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1960 x--;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1961 xOffset += dxPerScanDenominator;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1962 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1963
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1964 #else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1965 xIntersect += dxPerScan;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1966 #endif
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1967 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1968
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1969
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1970 int GetEnterX() const
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1971 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1972 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1973 assert(xOffset >= 0 && xOffset < dxPerScanDenominator);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1974 if (xOffset == 0)
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1975 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1976 return x;
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1977 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1978 else
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1979 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1980 return x + 1;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1981 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1982 #else
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1983 return static_cast<int>(std::ceil(xIntersect));
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1984 #endif
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1985 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1986
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1987 int GetExitX() const
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1988 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1989 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1990 assert(xOffset >= 0 && xOffset < dxPerScanDenominator);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1991 return x;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1992 #else
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1993 return static_cast<int>(std::floor(xIntersect));
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1994 #endif
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1995 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1996
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1997 int GetUpperY() const
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1998 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1999 return yUpper;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2000 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2001
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2002 bool operator< (const PolygonEdge& other) const
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2003 {
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2004 #if USE_POLYGON_FRACTIONS == 1
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2005 assert(xOffset >= 0 && xOffset < dxPerScanDenominator);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2006 assert(other.xOffset >= 0 && other.xOffset < other.dxPerScanDenominator);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2007 return x < other.x;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2008 #else
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2009 // cf. "insertEdge()" in textbook
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2010 return (xIntersect < other.xIntersect);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2011 #endif
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2012 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2013 };
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2014 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2015
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2016
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2017 // For an index, return y-coordinate of next nonhorizontal line
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2018 static int GetPolygonNextY(const std::vector<ImageProcessing::ImagePoint>& points,
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2019 size_t k)
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2020 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2021 // cf. "yNext()" in textbook
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2022 size_t j = k;
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2023
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2024 for (;;)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2025 {
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2026 j++;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2027 if (j == points.size())
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2028 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2029 j = 0;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2030 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2031
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2032 if (points[k].GetY() != points[j].GetY())
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2033 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2034 return points[j].GetY();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2035 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2036 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2037 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2038
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2039
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2040 static int GetPolygonPreviousY(const std::vector<ImageProcessing::ImagePoint>& points,
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2041 size_t k)
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2042 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2043 size_t j = k;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2044
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2045 for (;;)
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2046 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2047 if (j > 0)
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2048 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2049 j --;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2050 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2051 else
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2052 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2053 j = points.size() - 1;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2054 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2055
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2056 if (points[k].GetY() != points[j].GetY())
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2057 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2058 return points[j].GetY();
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2059 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2060 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2061 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2062
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2063
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2064
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2065 void ImageProcessing::FillPolygon(IPolygonFiller& filler,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2066 const std::vector<ImagePoint>& points)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2067 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2068 /**
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2069 * This implementation is a C++ adaption of Section 3.11 (pages
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2070 * 117-124) of textbook "Computer Graphics - C Version (2nd
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2071 * Edition)" by Hearn and Baker, 1997.
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2072 **/
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2073
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2074 typedef std::map<int, std::list<PolygonEdge> > EdgeTable;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2075
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2076 if (points.size() < 2)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2077 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2078 return;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2079 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2080
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2081 bool onlyHorizontalSegments = true;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2082 for (size_t i = 1; i < points.size(); i++)
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2083 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2084 if (points[0].GetY() != points[i].GetY())
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2085 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2086 onlyHorizontalSegments = false;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2087 break;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2088 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2089 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2090
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2091 if (onlyHorizontalSegments)
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2092 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2093 // Degenerate case: There are only horizontal lines. If this is
4889
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2094 // the case, "GetPolygonPreviousY()" would be an infinite loop
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2095 int x1 = points[0].GetX();
4889
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2096 int x2 = x1;
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2097 for (size_t i = 1; i < points.size(); i++)
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2098 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2099 assert(points[i].GetY() == points[0].GetY());
4889
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2100
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2101 const int x = points[i].GetX();
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2102 x1 = std::min(x1, x);
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2103 x2 = std::max(x2, x);
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2104 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2105 filler.Fill(points[0].GetY(), x1, x2);
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2106 return;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2107 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2108
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2109 EdgeTable globalEdgeTable;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2110
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2111 // cf. "buildEdgeList()" in textbook
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2112
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2113 // Error in the textbook: we use "GetPolygonPreviousY()" instead of "points.size() - 2"
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2114 int yPrev = GetPolygonPreviousY(points, points.size() - 1);
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2115 ImagePoint v1(points[points.size() - 1]);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2116
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2117 for (size_t i = 0; i < points.size(); i++)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2118 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2119 ImagePoint v2(points[i]);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2120
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2121 if (v1.GetY() != v2.GetY())
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2122 {
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2123 // Non-horizontal line
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2124 if (v1.GetY() < v2.GetY())
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2125 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2126 // Up-going edge
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2127 PolygonEdge edge(v1, v2, GetPolygonNextY(points, i));
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2128 globalEdgeTable[v1.GetY()].push_back(edge);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2129 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2130 else if (v1.GetY() > v2.GetY())
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2131 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2132 // Down-going edge
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2133 PolygonEdge edge(v2, v1, yPrev);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2134 globalEdgeTable[v2.GetY()].push_back(edge);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2135 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2136
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2137 // Error in the textbook: "yPrev" must NOT be updated on horizontal lines
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2138 yPrev = v1.GetY();
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2139 }
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2140
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2141 v1 = v2;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2142 }
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2143
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2144 assert(!globalEdgeTable.empty());
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2145
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2146 std::vector<PolygonEdge> activeEdges;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2147
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2148 for (EdgeTable::const_iterator it = globalEdgeTable.begin(); it != globalEdgeTable.end(); ++it)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2149 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2150 // cf. "buildActiveList()" in textbook
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2151 activeEdges.reserve(activeEdges.size() + it->second.size());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2152 for (std::list<PolygonEdge>::const_iterator it2 = it->second.begin(); it2 != it->second.end(); ++it2)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2153 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2154 activeEdges.push_back(*it2);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2155 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2156
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2157 assert(!activeEdges.empty());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2158
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2159 EdgeTable::const_iterator next = it;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2160 ++next;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2161
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2162 int rampEnd;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2163 if (next == globalEdgeTable.end())
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2164 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2165 rampEnd = activeEdges[0].GetUpperY() + 1;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2166
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2167 for (size_t i = 1; i < activeEdges.size(); i++)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2168 {
4873
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4872
diff changeset
2169 rampEnd = std::max(rampEnd, activeEdges[i].GetUpperY() + 1);
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2170 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2171 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2172 else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2173 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2174 rampEnd = next->first;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2175 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2176
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2177 for (int y = it->first; y < rampEnd; y++)
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2178 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2179 // cf. "updateActiveList()" in textbook
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2180 std::vector<PolygonEdge> stillActive;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2181 stillActive.reserve(activeEdges.size());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2182
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2183 for (size_t i = 0; i < activeEdges.size(); i++)
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2184 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2185 if (y <= activeEdges[i].GetUpperY())
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2186 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2187 stillActive.push_back(activeEdges[i]);
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2188 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2189 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2190
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2191 activeEdges.swap(stillActive);
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2192
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2193 assert(activeEdges.size() % 2 == 0);
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2194 std::sort(activeEdges.begin(), activeEdges.end());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2195
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2196 // cf. "fillScan()" in textbook
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2197 for (size_t k = 0; k + 1 < activeEdges.size(); )
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2198 {
4873
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4872
diff changeset
2199 int a = activeEdges[k].GetExitX();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4872
diff changeset
2200 int b = activeEdges[k + 1].GetEnterX();
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2201
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2202 // Fix wrt. the textbook: merge overlapping segments
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2203 k += 2;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2204 while (k + 1 < activeEdges.size() &&
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2205 activeEdges[k].GetExitX() == b)
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2206 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2207 assert(a <= b);
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2208 b = activeEdges[k + 1].GetEnterX();
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2209 k += 2;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2210 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2211
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2212 assert(a <= b);
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2213 filler.Fill(y, a, b);
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2214 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2215
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2216 // cf. "updateActiveList()" in textbook
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2217 for (size_t k = 0; k < activeEdges.size(); k++)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2218 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2219 activeEdges[k].NextScanLine();
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2220 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2221 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2222 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2223 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2224
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2225
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2226 void ImageProcessing::FillPolygon(ImageAccessor& image,
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2227 const std::vector<ImagePoint>& points,
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2228 int64_t value)
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2229 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2230 class Filler : public IPolygonFiller
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2231 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2232 private:
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2233 ImageAccessor& image_;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2234 int64_t value_;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2235
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2236 public:
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2237 Filler(ImageAccessor& image,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2238 int64_t value) :
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2239 image_(image),
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2240 value_(value)
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
2241 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
2242 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2243
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2244 virtual void Fill(int y,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2245 int x1,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2246 int x2) ORTHANC_OVERRIDE
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
2247 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2248 assert(x1 <= x2);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2249
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2250 if (x1 < static_cast<int>(image_.GetWidth()) &&
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2251 x2 >= 0 &&
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2252 y >= 0 &&
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2253 y < static_cast<int>(image_.GetHeight()))
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2254 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2255 unsigned int yy = static_cast<unsigned int>(y);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2256 unsigned int a = static_cast<unsigned int>(std::max(0, x1));
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2257 unsigned int b = static_cast<unsigned int>(std::min(x2, static_cast<int>(image_.GetWidth()) - 1));
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2258
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2259 assert(a <= b);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2260
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2261 ImageAccessor region;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2262 image_.GetRegion(region, a, yy, b - a + 1, 1);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2263 Set(region, value_);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2264 }
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
2265 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2266 };
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2267
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2268
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2269 if (image.GetFormat() == PixelFormat_Grayscale8 ||
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2270 image.GetFormat() == PixelFormat_Grayscale16 ||
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2271 image.GetFormat() == PixelFormat_SignedGrayscale16)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2272 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2273 Filler filler(image, value);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2274 FillPolygon(filler, points);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2275 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2276 else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2277 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2278 throw OrthancException(ErrorCode_NotImplemented);
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2279 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2280 }
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2281
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2282
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2283 template <PixelFormat Format>
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2284 static void ResizeInternal(ImageAccessor& target,
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2285 const ImageAccessor& source)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2286 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2287 assert(target.GetFormat() == source.GetFormat() &&
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2288 target.GetFormat() == Format);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2289
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2290 const unsigned int sourceWidth = source.GetWidth();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2291 const unsigned int sourceHeight = source.GetHeight();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2292 const unsigned int targetWidth = target.GetWidth();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2293 const unsigned int targetHeight = target.GetHeight();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2294
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2295 if (targetWidth == 0 || targetHeight == 0)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2296 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2297 return;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2298 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2299
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2300 if (sourceWidth == 0 || sourceHeight == 0)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2301 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2302 // Avoids division by zero below
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2303 ImageProcessing::Set(target, 0);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2304 return;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2305 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2306
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2307 const float scaleX = static_cast<float>(sourceWidth) / static_cast<float>(targetWidth);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2308 const float scaleY = static_cast<float>(sourceHeight) / static_cast<float>(targetHeight);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2309
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2310
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2311 /**
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2312 * Create two lookup tables to quickly know the (x,y) position
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2313 * in the source image, given the (x,y) position in the target
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2314 * image.
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2315 **/
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2316
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2317 std::vector<unsigned int> lookupX(targetWidth);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2318
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2319 for (unsigned int x = 0; x < targetWidth; x++)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2320 {
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
2321 int sourceX = static_cast<int>(std::floor((static_cast<float>(x) + 0.5f) * scaleX));
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2322 if (sourceX < 0)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2323 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2324 sourceX = 0; // Should never happen
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2325 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2326 else if (sourceX >= static_cast<int>(sourceWidth))
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2327 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2328 sourceX = sourceWidth - 1;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2329 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2330
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2331 lookupX[x] = static_cast<unsigned int>(sourceX);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2332 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2333
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2334 std::vector<unsigned int> lookupY(targetHeight);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2335
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2336 for (unsigned int y = 0; y < targetHeight; y++)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2337 {
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
2338 int sourceY = static_cast<int>(std::floor((static_cast<float>(y) + 0.5f) * scaleY));
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2339 if (sourceY < 0)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2340 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2341 sourceY = 0; // Should never happen
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2342 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2343 else if (sourceY >= static_cast<int>(sourceHeight))
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2344 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2345 sourceY = sourceHeight - 1;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2346 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2347
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2348 lookupY[y] = static_cast<unsigned int>(sourceY);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2349 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2350
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2351
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2352 /**
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2353 * Actual resizing
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2354 **/
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2355
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2356 for (unsigned int targetY = 0; targetY < targetHeight; targetY++)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2357 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2358 unsigned int sourceY = lookupY[targetY];
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2359
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2360 for (unsigned int targetX = 0; targetX < targetWidth; targetX++)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2361 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2362 unsigned int sourceX = lookupX[targetX];
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2363
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2364 typename ImageTraits<Format>::PixelType pixel;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2365 ImageTraits<Format>::GetPixel(pixel, source, sourceX, sourceY);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2366 ImageTraits<Format>::SetPixel(target, pixel, targetX, targetY);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2367 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2368 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2369 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2370
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2371
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2372
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2373 void ImageProcessing::Resize(ImageAccessor& target,
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2374 const ImageAccessor& source)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2375 {
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
2376 if (source.GetFormat() != target.GetFormat())
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2377 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2378 throw OrthancException(ErrorCode_IncompatibleImageFormat);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2379 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2380
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2381 if (source.GetWidth() == target.GetWidth() &&
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2382 source.GetHeight() == target.GetHeight())
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2383 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2384 Copy(target, source);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2385 return;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2386 }
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2387
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2388 switch (source.GetFormat())
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2389 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2390 case PixelFormat_Grayscale8:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2391 ResizeInternal<PixelFormat_Grayscale8>(target, source);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2392 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2393
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2394 case PixelFormat_Float32:
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2395 ResizeInternal<PixelFormat_Float32>(target, source);
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2396 break;
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2397
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2398 case PixelFormat_RGB24:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2399 ResizeInternal<PixelFormat_RGB24>(target, source);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2400 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2401
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2402 default:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2403 throw OrthancException(ErrorCode_NotImplemented);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2404 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2405 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2406
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2407
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2408 ImageAccessor* ImageProcessing::Halve(const ImageAccessor& source,
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2409 bool forceMinimalPitch)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2410 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3690
diff changeset
2411 std::unique_ptr<Image> target(new Image(source.GetFormat(), source.GetWidth() / 2,
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3690
diff changeset
2412 source.GetHeight() / 2, forceMinimalPitch));
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2413 Resize(*target, source);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2414 return target.release();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2415 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2416
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2417
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2418 template <PixelFormat Format>
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2419 static void FlipXInternal(ImageAccessor& image)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2420 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2421 const unsigned int height = image.GetHeight();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2422 const unsigned int width = image.GetWidth();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2423
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2424 for (unsigned int y = 0; y < height; y++)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2425 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2426 for (unsigned int x1 = 0; x1 < width / 2; x1++)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2427 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2428 unsigned int x2 = width - 1 - x1;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2429
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2430 typename ImageTraits<Format>::PixelType a, b;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2431 ImageTraits<Format>::GetPixel(a, image, x1, y);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2432 ImageTraits<Format>::GetPixel(b, image, x2, y);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2433 ImageTraits<Format>::SetPixel(image, a, x2, y);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2434 ImageTraits<Format>::SetPixel(image, b, x1, y);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2435 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2436 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2437 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2438
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2439
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2440 void ImageProcessing::FlipX(ImageAccessor& image)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2441 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2442 switch (image.GetFormat())
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2443 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2444 case PixelFormat_Grayscale8:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2445 FlipXInternal<PixelFormat_Grayscale8>(image);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2446 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2447
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2448 case PixelFormat_RGB24:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2449 FlipXInternal<PixelFormat_RGB24>(image);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2450 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2451
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2452 default:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2453 throw OrthancException(ErrorCode_NotImplemented);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2454 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2455 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2456
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2457
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2458 template <PixelFormat Format>
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2459 static void FlipYInternal(ImageAccessor& image)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2460 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2461 const unsigned int height = image.GetHeight();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2462 const unsigned int width = image.GetWidth();
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2463
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2464 for (unsigned int y1 = 0; y1 < height / 2; y1++)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2465 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2466 unsigned int y2 = height - 1 - y1;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2467
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2468 for (unsigned int x = 0; x < width; x++)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2469 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2470 typename ImageTraits<Format>::PixelType a, b;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2471 ImageTraits<Format>::GetPixel(a, image, x, y1);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2472 ImageTraits<Format>::GetPixel(b, image, x, y2);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2473 ImageTraits<Format>::SetPixel(image, a, x, y2);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2474 ImageTraits<Format>::SetPixel(image, b, x, y1);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2475 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2476 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2477 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2478
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2479
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2480 void ImageProcessing::FlipY(ImageAccessor& image)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2481 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2482 switch (image.GetFormat())
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2483 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2484 case PixelFormat_Grayscale8:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2485 FlipYInternal<PixelFormat_Grayscale8>(image);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2486 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2487
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2488 case PixelFormat_RGB24:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2489 FlipYInternal<PixelFormat_RGB24>(image);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2490 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2491
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2492 default:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2493 throw OrthancException(ErrorCode_NotImplemented);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2494 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2495 }
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2496
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2497
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2498 // This is a slow implementation of horizontal convolution on one
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2499 // individual channel, that checks for out-of-image values
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2500 template <typename RawPixel, unsigned int ChannelsCount>
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
2501 static float GetHorizontalConvolutionFloatSecure(const ImageAccessor& source,
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2502 const std::vector<float>& horizontal,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2503 size_t horizontalAnchor,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2504 unsigned int x,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2505 unsigned int y,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2506 float leftBorder,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2507 float rightBorder,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2508 unsigned int channel)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2509 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2510 const RawPixel* row = reinterpret_cast<const RawPixel*>(source.GetConstRow(y)) + channel;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2511
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2512 float p = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2513
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2514 for (unsigned int k = 0; k < horizontal.size(); k++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2515 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2516 float value;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2517
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2518 if (x + k < horizontalAnchor) // Negation of "x - horizontalAnchor + k >= 0"
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2519 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2520 value = leftBorder;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2521 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2522 else if (x + k >= source.GetWidth() + horizontalAnchor) // Negation of "x - horizontalAnchor + k < width"
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2523 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2524 value = rightBorder;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2525 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2526 else
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2527 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2528 // The value lies within the image
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2529 value = row[(x - horizontalAnchor + k) * ChannelsCount];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2530 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2531
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2532 p += value * horizontal[k];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2533 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2534
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2535 return p;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2536 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2537
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2538
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2539
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2540 // This is an implementation of separable convolution that uses
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2541 // floating-point arithmetics, and an intermediate Float32
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2542 // image. The out-of-image values are taken as the border
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2543 // value. Further optimization is possible.
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2544 template <typename RawPixel, unsigned int ChannelsCount, bool UseRound>
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2545 static void SeparableConvolutionFloat(ImageAccessor& image /* inplace */,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2546 const std::vector<float>& horizontal,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2547 size_t horizontalAnchor,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2548 const std::vector<float>& vertical,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2549 size_t verticalAnchor,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2550 float normalization)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2551 {
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
2552 // WARNING - "::min()" should be replaced by "::lowest()" if
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
2553 // dealing with float or double (which is not the case so far)
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
2554 assert(sizeof(RawPixel) <= 2); // Safeguard to remember about "float/double"
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
2555
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2556 const unsigned int width = image.GetWidth();
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2557 const unsigned int height = image.GetHeight();
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2558
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2559
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2560 /**
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2561 * Horizontal convolution
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2562 **/
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2563
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2564 Image tmp(PixelFormat_Float32, ChannelsCount * width, height, false);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2565
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2566 for (unsigned int y = 0; y < height; y++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2567 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2568 const RawPixel* row = reinterpret_cast<const RawPixel*>(image.GetConstRow(y));
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2569
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2570 float leftBorder[ChannelsCount], rightBorder[ChannelsCount];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2571
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2572 for (unsigned int c = 0; c < ChannelsCount; c++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2573 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2574 leftBorder[c] = row[c];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2575 rightBorder[c] = row[ChannelsCount * (width - 1) + c];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2576 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2577
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2578 float* p = static_cast<float*>(tmp.GetRow(y));
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2579
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2580 if (width < horizontal.size())
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2581 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2582 // It is not possible to have the full kernel within the image, use the direct implementation
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2583 for (unsigned int x = 0; x < width; x++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2584 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2585 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2586 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2587 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount>
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2588 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2589 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2590 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2591 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2592 else
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2593 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2594 // Deal with the left border
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2595 for (unsigned int x = 0; x < horizontalAnchor; x++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2596 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2597 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2598 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2599 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount>
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2600 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2601 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2602 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2603
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2604 // Deal with the central portion of the image (all pixel values
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2605 // scanned by the kernel lie inside the image)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2606
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2607 for (unsigned int x = 0; x < width - horizontal.size() + 1; x++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2608 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2609 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2610 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2611 *p = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2612 for (unsigned int k = 0; k < horizontal.size(); k++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2613 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2614 *p += static_cast<float>(row[(x + k) * ChannelsCount + c]) * horizontal[k];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2615 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2616 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2617 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2618
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2619 // Deal with the right border
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
2620 for (unsigned int x = static_cast<unsigned int>(
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
2621 horizontalAnchor + width - horizontal.size() + 1); x < width; x++)
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2622 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2623 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2624 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2625 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount>
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2626 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2627 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2628 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2629 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2630 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2631
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2632
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2633 /**
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2634 * Vertical convolution
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2635 **/
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2636
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2637 std::vector<const float*> rows(vertical.size());
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2638
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2639 for (unsigned int y = 0; y < height; y++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2640 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2641 for (unsigned int k = 0; k < vertical.size(); k++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2642 {
3504
18566f9e1831 unit testing new functions in ImageProcessing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3503
diff changeset
2643 if (y + k < verticalAnchor)
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2644 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2645 rows[k] = reinterpret_cast<const float*>(tmp.GetConstRow(0)); // Use top border
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2646 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2647 else if (y + k >= height + verticalAnchor)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2648 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2649 rows[k] = reinterpret_cast<const float*>(tmp.GetConstRow(height - 1)); // Use bottom border
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2650 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2651 else
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2652 {
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
2653 rows[k] = reinterpret_cast<const float*>(tmp.GetConstRow(static_cast<unsigned int>(y + k - verticalAnchor)));
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2654 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2655 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2656
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2657 RawPixel* p = reinterpret_cast<RawPixel*>(image.GetRow(y));
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2658
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2659 for (unsigned int x = 0; x < width; x++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2660 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2661 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2662 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2663 float accumulator = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2664
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2665 for (unsigned int k = 0; k < vertical.size(); k++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2666 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2667 accumulator += rows[k][ChannelsCount * x + c] * vertical[k];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2668 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2669
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2670 accumulator *= normalization;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2671
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2672 if (accumulator <= static_cast<float>(std::numeric_limits<RawPixel>::min()))
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2673 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2674 *p = std::numeric_limits<RawPixel>::min();
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2675 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2676 else if (accumulator >= static_cast<float>(std::numeric_limits<RawPixel>::max()))
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2677 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2678 *p = std::numeric_limits<RawPixel>::max();
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2679 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2680 else
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2681 {
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2682 if (UseRound)
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2683 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2684 assert(sizeof(RawPixel) < sizeof(int));
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2685 *p = static_cast<RawPixel>(boost::math::iround(accumulator));
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2686 }
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2687 else
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2688 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2689 *p = static_cast<RawPixel>(accumulator);
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2690 }
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2691 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2692 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2693 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2694 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2695 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2696
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2697
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2698 void ImageProcessing::SeparableConvolution(ImageAccessor& image /* inplace */,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2699 const std::vector<float>& horizontal,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2700 size_t horizontalAnchor,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2701 const std::vector<float>& vertical,
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2702 size_t verticalAnchor,
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2703 bool useRound)
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2704 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2705 if (horizontal.size() == 0 ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2706 vertical.size() == 0 ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2707 horizontalAnchor >= horizontal.size() ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2708 verticalAnchor >= vertical.size())
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2709 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2710 throw OrthancException(ErrorCode_ParameterOutOfRange);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2711 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2712
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2713 if (image.GetWidth() == 0 ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2714 image.GetHeight() == 0)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2715 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2716 return;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2717 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2718
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2719 /**
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2720 * Compute normalization
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2721 **/
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2722
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2723 float sumHorizontal = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2724 for (size_t i = 0; i < horizontal.size(); i++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2725 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2726 sumHorizontal += horizontal[i];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2727 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2728
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2729 float sumVertical = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2730 for (size_t i = 0; i < vertical.size(); i++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2731 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2732 sumVertical += vertical[i];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2733 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2734
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2735 if (fabsf(sumHorizontal) <= std::numeric_limits<float>::epsilon() ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2736 fabsf(sumVertical) <= std::numeric_limits<float>::epsilon())
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2737 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2738 throw OrthancException(ErrorCode_ParameterOutOfRange, "Singular convolution kernel");
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2739 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2740
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2741 const float normalization = 1.0f / (sumHorizontal * sumVertical);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2742
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2743 switch (image.GetFormat())
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2744 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2745 case PixelFormat_Grayscale8:
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2746 if (useRound)
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2747 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2748 SeparableConvolutionFloat<uint8_t, 1u, true>
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2749 (image, horizontal, horizontalAnchor, vertical, verticalAnchor, normalization);
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2750 }
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2751 else
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2752 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2753 SeparableConvolutionFloat<uint8_t, 1u, false>
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2754 (image, horizontal, horizontalAnchor, vertical, verticalAnchor, normalization);
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2755 }
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2756 break;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2757
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2758 case PixelFormat_RGB24:
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2759 if (useRound)
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2760 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2761 SeparableConvolutionFloat<uint8_t, 3u, true>
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2762 (image, horizontal, horizontalAnchor, vertical, verticalAnchor, normalization);
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2763 }
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2764 else
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2765 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2766 SeparableConvolutionFloat<uint8_t, 3u, false>
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2767 (image, horizontal, horizontalAnchor, vertical, verticalAnchor, normalization);
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2768 }
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2769 break;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2770
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2771 default:
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2772 throw OrthancException(ErrorCode_NotImplemented);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2773 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2774 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2775
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2776
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2777 void ImageProcessing::SmoothGaussian5x5(ImageAccessor& image,
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2778 bool useRound)
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2779 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2780 std::vector<float> kernel(5);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2781 kernel[0] = 1;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2782 kernel[1] = 4;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2783 kernel[2] = 6;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2784 kernel[3] = 4;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2785 kernel[4] = 1;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2786
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2787 SeparableConvolution(image, kernel, 2, kernel, 2, useRound);
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2788 }
3549
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2789
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2790
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2791 void ImageProcessing::FitSize(ImageAccessor& target,
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2792 const ImageAccessor& source)
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2793 {
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2794 if (target.GetWidth() == 0 ||
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2795 target.GetHeight() == 0)
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2796 {
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2797 return;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2798 }
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2799
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2800 if (source.GetWidth() == target.GetWidth() &&
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2801 source.GetHeight() == target.GetHeight())
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2802 {
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2803 Copy(target, source);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2804 return;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2805 }
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2806
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2807 Set(target, 0);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2808
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2809 // Preserve the aspect ratio
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2810 float cw = static_cast<float>(source.GetWidth());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2811 float ch = static_cast<float>(source.GetHeight());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2812 float r = std::min(
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2813 static_cast<float>(target.GetWidth()) / cw,
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2814 static_cast<float>(target.GetHeight()) / ch);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2815
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2816 unsigned int sw = std::min(static_cast<unsigned int>(boost::math::iround(cw * r)), target.GetWidth());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2817 unsigned int sh = std::min(static_cast<unsigned int>(boost::math::iround(ch * r)), target.GetHeight());
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2818
3549
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2819 Image resized(target.GetFormat(), sw, sh, false);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2820
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2821 //ImageProcessing::SmoothGaussian5x5(source);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2822 ImageProcessing::Resize(resized, source);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2823
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2824 assert(target.GetWidth() >= resized.GetWidth() &&
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2825 target.GetHeight() >= resized.GetHeight());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2826 unsigned int offsetX = (target.GetWidth() - resized.GetWidth()) / 2;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2827 unsigned int offsetY = (target.GetHeight() - resized.GetHeight()) / 2;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2828
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2829 ImageAccessor region;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2830 target.GetRegion(region, offsetX, offsetY, resized.GetWidth(), resized.GetHeight());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2831 ImageProcessing::Copy(region, resized);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2832 }
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2833
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2834
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2835 ImageAccessor* ImageProcessing::FitSize(const ImageAccessor& source,
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2836 unsigned int width,
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2837 unsigned int height)
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2838 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3690
diff changeset
2839 std::unique_ptr<ImageAccessor> target(new Image(source.GetFormat(), width, height, false));
3549
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2840 FitSize(*target, source);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2841 return target.release();
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2842 }
4082
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2843
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2844
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2845 ImageAccessor* ImageProcessing::FitSizeKeepAspectRatio(const ImageAccessor& source,
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2846 unsigned int width,
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2847 unsigned int height)
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2848 {
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2849 std::unique_ptr<ImageAccessor> target(new Image(source.GetFormat(), width, height, false));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2850 Set(*target, 0);
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2851
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2852 if (width != 0 &&
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2853 height != 0 &&
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2854 source.GetWidth() != 0 &&
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2855 source.GetHeight() != 0)
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2856 {
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2857 float ratio = std::min(static_cast<float>(width) / static_cast<float>(source.GetWidth()),
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2858 static_cast<float>(height) / static_cast<float>(source.GetHeight()));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2859
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2860 unsigned int resizedWidth = static_cast<unsigned int>(
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2861 boost::math::iround(ratio * static_cast<float>(source.GetWidth())));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2862
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2863 unsigned int resizedHeight = static_cast<unsigned int>(
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2864 boost::math::iround(ratio * static_cast<float>(source.GetHeight())));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2865
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2866 std::unique_ptr<ImageAccessor> resized(FitSize(source, resizedWidth, resizedHeight));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2867
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2868 ImageAccessor region;
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2869 target->GetRegion(region, (width - resizedWidth) / 2,
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2870 (height - resizedHeight) / 2, resizedWidth, resizedHeight);
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2871 Copy(region, *resized);
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2872 }
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2873
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2874 return target.release();
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2875 }
4443
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2876
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2877
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2878 void ImageProcessing::ConvertJpegYCbCrToRgb(ImageAccessor& image)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2879 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2880 // http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.3.html#sect_C.7.6.3.1.2
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2881 // https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2882
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2883 // TODO - Check out the outcome of Mathieu's discussion about
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2884 // truncation of YCbCr-to-RGB conversion:
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2885 // https://groups.google.com/forum/#!msg/comp.protocols.dicom/JHuGeyWbTz8/ARoTWrJzAQAJ
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2886
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2887 const unsigned int width = image.GetWidth();
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2888 const unsigned int height = image.GetHeight();
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2889 const unsigned int pitch = image.GetPitch();
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2890 uint8_t* buffer = reinterpret_cast<uint8_t*>(image.GetBuffer());
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2891
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2892 if (image.GetFormat() != PixelFormat_RGB24 ||
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2893 pitch < 3 * width)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2894 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2895 throw OrthancException(ErrorCode_IncompatibleImageFormat);
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2896 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2897
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2898 for (unsigned int y = 0; y < height; y++)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2899 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2900 uint8_t* p = buffer + y * pitch;
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2901
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2902 for (unsigned int x = 0; x < width; x++, p += 3)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2903 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2904 const float Y = p[0];
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2905 const float Cb = p[1];
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2906 const float Cr = p[2];
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2907
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2908 const float result[3] = {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2909 Y + 1.402f * (Cr - 128.0f),
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2910 Y - 0.344136f * (Cb - 128.0f) - 0.714136f * (Cr - 128.0f),
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2911 Y + 1.772f * (Cb - 128.0f)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2912 };
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2913
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2914 for (uint8_t i = 0; i < 3 ; i++)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2915 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2916 if (result[i] < 0)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2917 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2918 p[i] = 0;
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2919 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2920 else if (result[i] > 255)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2921 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2922 p[i] = 255;
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2923 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2924 else
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2925 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2926 p[i] = static_cast<uint8_t>(result[i]);
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2927 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2928 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2929 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2930 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2931 }
4529
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2932
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2933
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2934 void ImageProcessing::SwapEndianness(ImageAccessor& image /* inplace */)
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2935 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2936 const unsigned int width = image.GetWidth();
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2937 const unsigned int height = image.GetHeight();
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2938
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2939 switch (image.GetFormat())
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2940 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2941 case PixelFormat_Grayscale8:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2942 case PixelFormat_RGB24:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2943 case PixelFormat_RGBA32:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2944 case PixelFormat_BGRA32:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2945 // No swapping required
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2946 break;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2947
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2948 case PixelFormat_Grayscale16:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2949 case PixelFormat_SignedGrayscale16:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2950 for (unsigned int y = 0; y < height; y++)
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2951 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2952 uint8_t* t = reinterpret_cast<uint8_t*>(image.GetRow(y));
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2953 for (unsigned int x = 0; x < width; x++)
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2954 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2955 uint8_t a = t[0];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2956 t[0] = t[1];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2957 t[1] = a;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2958 t += 2;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2959 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2960 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2961 break;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2962
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2963 case PixelFormat_Grayscale32:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2964 case PixelFormat_Float32:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2965 for (unsigned int y = 0; y < height; y++)
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2966 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2967 uint8_t* t = reinterpret_cast<uint8_t*>(image.GetRow(y));
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2968 for (unsigned int x = 0; x < width; x++)
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2969 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2970 uint8_t a = t[0];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2971 uint8_t b = t[1];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2972 t[0] = t[3];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2973 t[1] = t[2];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2974 t[2] = b;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2975 t[3] = a;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2976 t += 4;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2977 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2978 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2979 break;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2980
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2981 case PixelFormat_RGB48: // uint16_t per channel
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2982 for (unsigned int y = 0; y < height; y++)
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2983 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2984 uint8_t* t = reinterpret_cast<uint8_t*>(image.GetRow(y));
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2985 for (unsigned int x = 0; x < 3 * width; x++)
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2986 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2987 uint8_t a = t[0];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2988 t[0] = t[1];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2989 t[1] = a;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2990 t += 2;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2991 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2992 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2993 break;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2994
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2995 default:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2996 throw OrthancException(ErrorCode_NotImplemented);
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2997 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2998 }
4947
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
2999
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3000
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3001 template <typename PixelType,
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3002 typename Functor>
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3003 static void ApplyImageOntoImage(Functor f,
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3004 ImageAccessor& image /* inout */,
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3005 const ImageAccessor& other)
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3006 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3007 const unsigned int width = image.GetWidth();
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3008 const unsigned int height = image.GetHeight();
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3009
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3010 if (width != other.GetWidth() ||
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3011 height != other.GetHeight())
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3012 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3013 throw OrthancException(ErrorCode_IncompatibleImageSize);
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3014 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3015 else if (image.GetFormat() != other.GetFormat() ||
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3016 GetBytesPerPixel(image.GetFormat()) != sizeof(PixelType))
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3017 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3018 throw OrthancException(ErrorCode_IncompatibleImageFormat);
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3019 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3020 else
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3021 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3022 for (unsigned int y = 0; y < height; y++)
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3023 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3024 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3025 const PixelType* q = reinterpret_cast<const PixelType*>(other.GetConstRow(y));
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3026
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3027 for (unsigned int x = 0; x < width; x++, p++, q++)
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3028 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3029 f(*p, *q);
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3030 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3031 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3032 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3033 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3034
4948
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3035
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3036 namespace
4947
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3037 {
4948
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3038 // For older version of gcc, templated functors cannot be defined
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3039 // as types internal to functions, hence the anonymous namespace
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3040
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3041 struct MaximumFunctor
4947
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3042 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3043 void operator() (uint8_t& a, const uint8_t& b)
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3044 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3045 a = std::max(a, b);
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3046 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3047
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3048 void operator() (uint16_t& a, const uint16_t& b)
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3049 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3050 a = std::max(a, b);
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3051 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3052 };
4948
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3053 }
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3054
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3055
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3056 void ImageProcessing::Maximum(ImageAccessor& image,
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3057 const ImageAccessor& other)
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3058 {
4947
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3059 switch (image.GetFormat())
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3060 {
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3061 case PixelFormat_Grayscale8:
4948
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3062 ApplyImageOntoImage<uint8_t, MaximumFunctor>(MaximumFunctor(), image, other);
4947
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3063 return;
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3064
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3065 case PixelFormat_Grayscale16:
4948
5f6b13202e85 fix for older gcc versions
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4947
diff changeset
3066 ApplyImageOntoImage<uint16_t, MaximumFunctor>(MaximumFunctor(), image, other);
4947
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3067 return;
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3068
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3069 default:
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3070 throw OrthancException(ErrorCode_NotImplemented);
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3071 }
dfbe764995cf added ParsedDicomFile::DecodeAllOverlays()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4889
diff changeset
3072 }
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3073 }