annotate OrthancFramework/Sources/Images/ImageProcessing.cpp @ 4934:94a7b681b340 more-tags

added configuration for extra main dicom tags + save signature in metadata + show warning if inconsistent main dicom tags
author Alain Mazy <am@osimis.io>
date Thu, 10 Mar 2022 09:03:24 +0100
parents d8c8274d4e41
children dfbe764995cf
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>
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
417 static void ShiftScaleInternal(ImageAccessor& target,
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
418 const ImageAccessor& source,
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
419 float a,
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
420 float b,
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
421 const TargetType LowestValue)
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
422 // This function can be applied inplace (source == target)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
423 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
424 if (source.GetWidth() != target.GetWidth() ||
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
425 source.GetHeight() != target.GetHeight())
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
426 {
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
427 throw OrthancException(ErrorCode_IncompatibleImageSize);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
428 }
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 if (&source == &target &&
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
431 source.GetFormat() != target.GetFormat())
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
432 {
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
433 throw OrthancException(ErrorCode_IncompatibleImageFormat);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
434 }
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 const TargetType minPixelValue = LowestValue;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
437 const TargetType maxPixelValue = std::numeric_limits<TargetType>::max();
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
438 const float minFloatValue = static_cast<float>(LowestValue);
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
439 const float maxFloatValue = static_cast<float>(maxPixelValue);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
440
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
441 const unsigned int height = target.GetHeight();
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
442 const unsigned int width = target.GetWidth();
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
443
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
444 for (unsigned int y = 0; y < height; y++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
445 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
446 TargetType* p = reinterpret_cast<TargetType*>(target.GetRow(y));
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
447 const SourceType* q = reinterpret_cast<const SourceType*>(source.GetConstRow(y));
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
448
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
449 for (unsigned int x = 0; x < width; x++, p++, q++)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
450 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
451 float v = a * static_cast<float>(*q) + b;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
452
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
453 if (v >= maxFloatValue)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
454 {
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
455 *p = maxPixelValue;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
456 }
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
457 else if (v <= minFloatValue)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
458 {
2482
509041cb57db speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2447
diff changeset
459 *p = minPixelValue;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
460 }
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
461 else if (UseRound)
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
462 {
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
463 // 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
464 assert(sizeof(TargetType) < sizeof(int));
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
465 *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
466 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
467 else
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
468 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
469 *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
470 }
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 if (Invert)
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
473 {
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
474 *p = maxPixelValue - *p;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
475 }
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
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
480 template <typename PixelType>
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
481 static void ShiftRightInternal(ImageAccessor& image,
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
482 unsigned int shift)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
483 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
484 const unsigned int height = image.GetHeight();
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
485 const unsigned int width = image.GetWidth();
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
486
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
487 for (unsigned int y = 0; y < height; y++)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
488 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
489 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
490
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
491 for (unsigned int x = 0; x < width; x++, p++)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
492 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
493 *p = *p >> shift;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
494 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
495 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
496 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
497
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
498 template <typename PixelType>
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
499 static void ShiftLeftInternal(ImageAccessor& image,
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
500 unsigned int shift)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
501 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
502 const unsigned int height = image.GetHeight();
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
503 const unsigned int width = image.GetWidth();
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
504
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
505 for (unsigned int y = 0; y < height; y++)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
506 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
507 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
508
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
509 for (unsigned int x = 0; x < width; x++, p++)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
510 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
511 *p = *p << shift;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
512 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
513 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
514 }
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
515
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 void ImageProcessing::Copy(ImageAccessor& target,
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517 const ImageAccessor& source)
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 if (target.GetWidth() != source.GetWidth() ||
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 target.GetHeight() != source.GetHeight())
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522 throw OrthancException(ErrorCode_IncompatibleImageSize);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 if (target.GetFormat() != source.GetFormat())
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 throw OrthancException(ErrorCode_IncompatibleImageFormat);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529
4278
9279de56a405 avoid multiple calls to GetWidth() and GetHeight() on pixel loops
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
530 const unsigned int lineSize = source.GetBytesPerPixel() * source.GetWidth();
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532
4278
9279de56a405 avoid multiple calls to GetWidth() and GetHeight() on pixel loops
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4201
diff changeset
533 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
534 for (unsigned int y = 0; y < height; y++)
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 {
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
536 memcpy(target.GetRow(y), source.GetConstRow(y), lineSize);
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
540 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
541 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
542 const ImageAccessor& source,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
543 float windowCenter,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
544 float windowWidth,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
545 float rescaleSlope,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
546 float rescaleIntercept,
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
547 bool invert)
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
548 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
549 assert(sizeof(SourceType) == source.GetBytesPerPixel() &&
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
550 sizeof(TargetType) == target.GetBytesPerPixel());
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
551
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
552 // WARNING - "::min()" should be replaced by "::lowest()" if
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
553 // dealing with float or double (which is not the case so far)
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
554 assert(sizeof(TargetType) <= 2); // Safeguard to remember about "float/double"
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
555 const TargetType minTargetValue = std::numeric_limits<TargetType>::min();
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
556 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
557 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
558
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
559 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
560 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
561
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
562 const float a = rescaleSlope * windowSlope;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
563 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
564
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
565 if (invert)
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
566 {
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
567 ShiftScaleInternal<TargetType, SourceType, false, true>(target, source, a, b, minTargetValue);
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
568 }
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
569 else
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
570 {
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
571 ShiftScaleInternal<TargetType, SourceType, false, false>(target, source, a, b, minTargetValue);
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
572 }
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
573 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
574
3683
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
575 void ImageProcessing::ApplyWindowing_Deprecated(ImageAccessor& target,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
576 const ImageAccessor& source,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
577 float windowCenter,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
578 float windowWidth,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
579 float rescaleSlope,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
580 float rescaleIntercept,
12253ddefe5a skeleton for new route: /instances/{id}/rendered
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3682
diff changeset
581 bool invert)
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
582 {
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
583 if (target.GetWidth() != source.GetWidth() ||
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
584 target.GetHeight() != source.GetHeight())
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
585 {
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
586 throw OrthancException(ErrorCode_IncompatibleImageSize);
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
587 }
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
588
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
589 switch (source.GetFormat())
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
590 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
591 case PixelFormat_Float32:
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
592 {
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
593 switch (target.GetFormat())
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
594 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
595 case PixelFormat_Grayscale8:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
596 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
597 break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
598 case PixelFormat_Grayscale16:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
599 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
600 break;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
601 default:
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
602 throw OrthancException(ErrorCode_NotImplemented);
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
603 }
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
604 };break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
605 case PixelFormat_Grayscale8:
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
606 {
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
607 switch (target.GetFormat())
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
608 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
609 case PixelFormat_Grayscale8:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
610 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
611 break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
612 case PixelFormat_Grayscale16:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
613 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
614 break;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
615 default:
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
616 throw OrthancException(ErrorCode_NotImplemented);
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
617 }
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
618 };break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
619 case PixelFormat_Grayscale16:
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
620 {
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
621 switch (target.GetFormat())
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
622 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
623 case PixelFormat_Grayscale8:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
624 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
625 break;
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
626 case PixelFormat_Grayscale16:
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
627 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
628 break;
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
629 default:
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
630 throw OrthancException(ErrorCode_NotImplemented);
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
631 }
3600
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
632 };break;
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
633 default:
4066998150ef /instances/{id}/preview route now takes the windowing into account
Alain Mazy <alain@mazy.be>
parents: 3585
diff changeset
634 throw OrthancException(ErrorCode_NotImplemented);
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
635 }
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
636 }
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
637
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
638
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
639 void ImageProcessing::Convert(ImageAccessor& target,
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
640 const ImageAccessor& source)
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
641 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
642 if (target.GetWidth() != source.GetWidth() ||
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
643 target.GetHeight() != source.GetHeight())
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
644 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
645 throw OrthancException(ErrorCode_IncompatibleImageSize);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
646 }
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
647
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
648 const unsigned int width = source.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
649 const unsigned int height = source.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
650
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651 if (source.GetFormat() == target.GetFormat())
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652 {
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
653 Copy(target, source);
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
654 return;
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
655 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
656
859
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
657 if (target.GetFormat() == PixelFormat_Grayscale16 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
658 source.GetFormat() == PixelFormat_Grayscale8)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
659 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
660 ConvertInternal<uint16_t, uint8_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
661 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
662 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
663
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
664 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
665 source.GetFormat() == PixelFormat_Grayscale8)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
666 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
667 ConvertInternal<int16_t, uint8_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
668 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
669 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
670
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
671 if (target.GetFormat() == PixelFormat_Grayscale8 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
672 source.GetFormat() == PixelFormat_Grayscale16)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
673 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
674 ConvertInternal<uint8_t, uint16_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
675 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
676 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
677
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
678 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
679 source.GetFormat() == PixelFormat_Grayscale16)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
680 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
681 ConvertInternal<int16_t, uint16_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
682 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
683 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
684
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
685 if (target.GetFormat() == PixelFormat_Grayscale8 &&
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
686 source.GetFormat() == PixelFormat_SignedGrayscale16)
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
687 {
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
688 ConvertInternal<uint8_t, int16_t>(target, source);
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
689 return;
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
690 }
610a9a1ed855 ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 854
diff changeset
691
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_SignedGrayscale16)
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, int16_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
993
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
699 if (target.GetFormat() == PixelFormat_Grayscale8 &&
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
700 source.GetFormat() == PixelFormat_RGB24)
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
701 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
702 ConvertColorToGrayscale<uint8_t>(target, source);
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
703 return;
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
704 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
705
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
706 if (target.GetFormat() == PixelFormat_Grayscale16 &&
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
707 source.GetFormat() == PixelFormat_RGB24)
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
708 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
709 ConvertColorToGrayscale<uint16_t>(target, source);
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
710 return;
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
711 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
712
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
713 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
714 source.GetFormat() == PixelFormat_RGB24)
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
715 {
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
716 ConvertColorToGrayscale<int16_t>(target, source);
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
717 return;
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
718 }
501880d76474 improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 876
diff changeset
719
1993
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
720 if (target.GetFormat() == PixelFormat_Float32 &&
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
721 source.GetFormat() == PixelFormat_Grayscale8)
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
722 {
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
723 ConvertGrayscaleToFloat<uint8_t>(target, source);
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
724 return;
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
725 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
726
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
727 if (target.GetFormat() == PixelFormat_Float32 &&
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
728 source.GetFormat() == PixelFormat_Grayscale16)
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
729 {
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
730 ConvertGrayscaleToFloat<uint16_t>(target, source);
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
731 return;
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
732 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
733
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
734 if (target.GetFormat() == PixelFormat_Float32 &&
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
735 source.GetFormat() == PixelFormat_Grayscale32)
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
736 {
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
737 ConvertGrayscaleToFloat<uint32_t>(target, source);
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
738 return;
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
739 }
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
740
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
741 if (target.GetFormat() == PixelFormat_Float32 &&
1993
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
742 source.GetFormat() == PixelFormat_SignedGrayscale16)
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
743 {
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
744 ConvertGrayscaleToFloat<int16_t>(target, source);
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
745 return;
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
746 }
e2a3ff770b48 introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1992
diff changeset
747
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
748
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
749 if (target.GetFormat() == PixelFormat_Grayscale8 &&
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
750 source.GetFormat() == PixelFormat_RGBA32)
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
751 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
752 for (unsigned int y = 0; y < height; y++)
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
753 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
754 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
755 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
756 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
757 {
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
758 *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
759 7152 * static_cast<uint32_t>(p[1]) +
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
760 0722 * static_cast<uint32_t>(p[2])) / 10000);
1610
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
761 p += 4;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
762 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
763 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
764
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
765 return;
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
766 }
2dff2bdffdb8 font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1608
diff changeset
767
2840
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
768 if (target.GetFormat() == PixelFormat_Grayscale8 &&
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
769 source.GetFormat() == PixelFormat_BGRA32)
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
770 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
771 for (unsigned int y = 0; y < height; y++)
2840
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
772 {
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
773 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
774 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
775 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
776 {
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
777 *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
778 7152 * static_cast<uint32_t>(p[1]) +
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
779 0722 * static_cast<uint32_t>(p[0])) / 10000);
2840
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
780 p += 4;
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
781 }
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
782 }
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
783
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
784 return;
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
785 }
f4c232bba1eb bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2783
diff changeset
786
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
787 if (target.GetFormat() == PixelFormat_RGB24 &&
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
788 source.GetFormat() == PixelFormat_RGBA32)
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
789 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
790 for (unsigned int y = 0; y < height; y++)
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
791 {
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
792 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
793 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
794 for (unsigned int x = 0; x < width; x++)
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
795 {
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
796 q[0] = p[0];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
797 q[1] = p[1];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
798 q[2] = p[2];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
799 p += 4;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
800 q += 3;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
801 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
802 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
803
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
804 return;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
805 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
806
2252
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
807 if (target.GetFormat() == PixelFormat_RGB24 &&
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
808 source.GetFormat() == PixelFormat_BGRA32)
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
809 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
810 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
811 {
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
812 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
813 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
814 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
815 {
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
816 q[0] = p[2];
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
817 q[1] = p[1];
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
818 q[2] = p[0];
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
819 p += 4;
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
820 q += 3;
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
821 }
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
822 }
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
823
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
824 return;
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
825 }
002b94046c69 colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2244
diff changeset
826
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
827 if (target.GetFormat() == PixelFormat_RGBA32 &&
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
828 source.GetFormat() == PixelFormat_RGB24)
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
829 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
830 for (unsigned int y = 0; y < height; y++)
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
831 {
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
832 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
833 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
834 for (unsigned int x = 0; x < width; x++)
1608
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
835 {
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
836 q[0] = p[0];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
837 q[1] = p[1];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
838 q[2] = p[2];
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
839 q[3] = 255; // Set the alpha channel to full opacity
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
840 p += 3;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
841 q += 4;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
842 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
843 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
844
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
845 return;
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
846 }
adc6a5704cdb OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1334
diff changeset
847
1992
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
848 if (target.GetFormat() == PixelFormat_RGB24 &&
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
849 source.GetFormat() == PixelFormat_Grayscale8)
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
850 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
851 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
852 {
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
853 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
854 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
855 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
856 {
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
857 q[0] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
858 q[1] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
859 q[2] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
860 p += 1;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
861 q += 3;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
862 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
863 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
864
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
865 return;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
866 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
867
2650
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2645
diff changeset
868 if ((target.GetFormat() == PixelFormat_RGBA32 ||
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2645
diff changeset
869 target.GetFormat() == PixelFormat_BGRA32) &&
1992
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
870 source.GetFormat() == PixelFormat_Grayscale8)
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
871 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
872 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
873 {
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
874 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
875 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
876 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
877 {
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
878 q[0] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
879 q[1] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
880 q[2] = *p;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
881 q[3] = 255;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
882 p += 1;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
883 q += 4;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
884 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
885 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
886
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
887 return;
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
888 }
9161e3ef0d17 new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1900
diff changeset
889
2100
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
890 if (target.GetFormat() == PixelFormat_BGRA32 &&
2495
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
891 source.GetFormat() == PixelFormat_Grayscale16)
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
892 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
893 for (unsigned int y = 0; y < height; y++)
2495
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
894 {
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
895 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
896 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
897 for (unsigned int x = 0; x < width; x++)
2495
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
898 {
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
899 uint8_t value = (*p < 256 ? *p : 255);
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
900 q[0] = value;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
901 q[1] = value;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
902 q[2] = value;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
903 q[3] = 255;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
904 p += 1;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
905 q += 4;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
906 }
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
907 }
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
908
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
909 return;
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
910 }
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
911
cd7b854dbc05 convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2491
diff changeset
912 if (target.GetFormat() == PixelFormat_BGRA32 &&
2496
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
913 source.GetFormat() == PixelFormat_SignedGrayscale16)
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
914 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
915 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
916 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
917 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
918 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
919 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
920 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
921 uint8_t value;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
922 if (*p < 0)
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
923 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
924 value = 0;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
925 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
926 else if (*p > 255)
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
927 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
928 value = 255;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
929 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
930 else
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
931 {
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
932 value = static_cast<uint8_t>(*p);
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
933 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
934
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
935 q[0] = value;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
936 q[1] = value;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
937 q[2] = value;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
938 q[3] = 255;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
939 p += 1;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
940 q += 4;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
941 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
942 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
943
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
944 return;
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
945 }
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
946
3d65adee289a int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2495
diff changeset
947 if (target.GetFormat() == PixelFormat_BGRA32 &&
2100
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
948 source.GetFormat() == PixelFormat_RGB24)
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
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++)
2100
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
951 {
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
952 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
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++)
2100
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
955 {
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
956 q[0] = p[2];
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
957 q[1] = p[1];
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
958 q[2] = p[0];
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
959 q[3] = 255;
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
960 p += 3;
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
961 q += 4;
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
962 }
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
963 }
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
964
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
965 return;
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
966 }
1554fc153a93 conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2089
diff changeset
967
3547
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
968 if ((target.GetFormat() == PixelFormat_BGRA32 &&
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
969 source.GetFormat() == PixelFormat_RGBA32)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
970 || (target.GetFormat() == PixelFormat_RGBA32 &&
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
971 source.GetFormat() == PixelFormat_BGRA32))
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
972 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
973 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
974 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
975 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
976 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
977 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
978 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
979 q[0] = p[2];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
980 q[1] = p[1];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
981 q[2] = p[0];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
982 q[3] = p[3];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
983 p += 4;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
984 q += 4;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
985 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
986 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
987
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
988 return;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
989 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
990
2423
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
991 if (target.GetFormat() == PixelFormat_RGB24 &&
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
992 source.GetFormat() == PixelFormat_RGB48)
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
993 {
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
994 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
995 {
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
996 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
997 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
998 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
999 {
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1000 q[0] = p[0] >> 8;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1001 q[1] = p[1] >> 8;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1002 q[2] = p[2] >> 8;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1003 p += 3;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1004 q += 3;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1005 }
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1006 }
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1007
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1008 return;
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1009 }
5a7c5c541a1d Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2415
diff changeset
1010
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1011 if (target.GetFormat() == PixelFormat_Grayscale16 &&
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1012 source.GetFormat() == PixelFormat_Float32)
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1013 {
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1014 ConvertFloatToGrayscale<PixelFormat_Grayscale16>(target, source);
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1015 return;
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1016 }
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1017
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1018 if (target.GetFormat() == PixelFormat_Grayscale8 &&
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1019 source.GetFormat() == PixelFormat_Float32)
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1020 {
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1021 ConvertFloatToGrayscale<PixelFormat_Grayscale8>(target, source);
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1022 return;
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1023 }
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1024
4664
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1025 if (target.GetFormat() == PixelFormat_RGB24 &&
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1026 source.GetFormat() == PixelFormat_Float32)
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1027 {
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1028 ConvertFloatToGrayscale<PixelFormat_RGB24>(target, source);
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1029 return;
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1030 }
f9eda86f9045 ImageProcessing::Convert() from Float32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4529
diff changeset
1031
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1032 throw OrthancException(ErrorCode_NotImplemented);
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1033 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1034
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1035
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1036
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1037 void ImageProcessing::Set(ImageAccessor& image,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1038 int64_t value)
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1039 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1040 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1041 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1042 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1043 SetInternal<uint8_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1044 return;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1045
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1046 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1047 SetInternal<uint16_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1048 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1049
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1050 case PixelFormat_Grayscale32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1051 SetInternal<uint32_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1052 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1053
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1054 case PixelFormat_Grayscale64:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1055 SetInternal<uint64_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1056 return;
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1057
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1058 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1059 SetInternal<int16_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1060 return;
2645
89b789366596 Grayscale64 pixel format
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2496
diff changeset
1061
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1062 case PixelFormat_Float32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1063 assert(sizeof(float) == 4);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1064 SetInternal<float>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1065 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1066
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1067 case PixelFormat_RGBA32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1068 case PixelFormat_BGRA32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1069 case PixelFormat_RGB24:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1070 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1071 uint8_t v = static_cast<uint8_t>(value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1072 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
1073 return;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1074 }
1994
4d099fee5eca ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1993
diff changeset
1075
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1076 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1077 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1078 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1079 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1080
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1081
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1082 void ImageProcessing::Set(ImageAccessor& image,
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1083 uint8_t red,
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1084 uint8_t green,
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1085 uint8_t blue,
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1086 uint8_t alpha)
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1087 {
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1088 uint8_t p[4];
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1089 unsigned int size;
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1090
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1091 switch (image.GetFormat())
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1092 {
4446
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1093 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
1094 {
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1095 // 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
1096 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
1097 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
1098 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
1099 Orthanc::ImageProcessing::Set(image, grayscale);
4447
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4446
diff changeset
1100 return;
4446
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1101 }
19e8540064d8 ImageProcessing::Set(image, r, g, b, a) applicable to grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4443
diff changeset
1102
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1103 case PixelFormat_RGBA32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1104 p[0] = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1105 p[1] = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1106 p[2] = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1107 p[3] = alpha;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1108 size = 4;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1109 break;
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1110
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1111 case PixelFormat_BGRA32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1112 p[0] = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1113 p[1] = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1114 p[2] = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1115 p[3] = alpha;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1116 size = 4;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1117 break;
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1118
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1119 case PixelFormat_RGB24:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1120 p[0] = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1121 p[1] = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1122 p[2] = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1123 size = 3;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1124 break;
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1125
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1126 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1127 throw OrthancException(ErrorCode_NotImplemented);
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1128 }
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1129
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1130 const unsigned int width = image.GetWidth();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1131 const unsigned int height = image.GetHeight();
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1132
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1133 for (unsigned int y = 0; y < height; y++)
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1134 {
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1135 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y));
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1136
2904
0dd54ee073db float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2902
diff changeset
1137 for (unsigned int x = 0; x < width; x++)
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1138 {
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1139 for (unsigned int i = 0; i < size; i++)
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1140 {
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1141 q[i] = p[i];
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1142 }
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1143
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1144 q += size;
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1145 }
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1146 }
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1147 }
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1148
3547
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1149 void ImageProcessing::Set(ImageAccessor& image,
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
1150 uint8_t red,
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
1151 uint8_t green,
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
1152 uint8_t blue,
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
1153 ImageAccessor& alpha)
3547
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1154 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1155 uint8_t p[4];
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1156
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1157 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
1158 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1159 throw OrthancException(ErrorCode_IncompatibleImageSize);
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1160 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1161
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1162 if (alpha.GetFormat() != PixelFormat_Grayscale8)
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1163 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1164 throw OrthancException(ErrorCode_NotImplemented);
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1165 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1166
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1167 switch (image.GetFormat())
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1168 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1169 case PixelFormat_RGBA32:
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1170 p[0] = red;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1171 p[1] = green;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1172 p[2] = blue;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1173 break;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1174
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1175 case PixelFormat_BGRA32:
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1176 p[0] = blue;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1177 p[1] = green;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1178 p[2] = red;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1179 break;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1180
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1181 default:
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1182 throw OrthancException(ErrorCode_NotImplemented);
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1183 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1184
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1185 const unsigned int width = image.GetWidth();
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1186 const unsigned int height = image.GetHeight();
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1187
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1188 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
1189 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1190 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
1191 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
1192
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1193 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
1194 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1195 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
1196 {
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1197 q[i] = p[i];
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 q[3] = *a;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1200 q += 4;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1201 ++a;
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1202 }
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 }
dabe17e23e23 Copy RGBA to BGRA & Set with alpha
Alain Mazy <alain@mazy.be>
parents: 3545
diff changeset
1205
2089
7a969f235adf PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1994
diff changeset
1206
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1207 void ImageProcessing::ShiftRight(ImageAccessor& image,
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1208 unsigned int shift)
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1209 {
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1210 if (image.GetWidth() == 0 ||
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1211 image.GetHeight() == 0 ||
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1212 shift == 0)
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1213 {
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1214 // Nothing to do
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1215 return;
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1216 }
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1217
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1218 switch (image.GetFormat())
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1219 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1220 case PixelFormat_Grayscale8:
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1221 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1222 ShiftRightInternal<uint8_t>(image, shift);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1223 break;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1224 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1225
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1226 case PixelFormat_Grayscale16:
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1227 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1228 ShiftRightInternal<uint16_t>(image, shift);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1229 break;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1230 }
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1231 default:
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1232 throw OrthancException(ErrorCode_NotImplemented);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1233 }
854
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1234 }
ff530685e46a fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 853
diff changeset
1235
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1236 void ImageProcessing::ShiftLeft(ImageAccessor& image,
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1237 unsigned int shift)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1238 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1239 if (image.GetWidth() == 0 ||
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1240 image.GetHeight() == 0 ||
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1241 shift == 0)
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1242 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1243 // Nothing to do
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1244 return;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1245 }
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1246
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1247 switch (image.GetFormat())
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1248 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1249 case PixelFormat_Grayscale8:
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1250 {
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1251 ShiftLeftInternal<uint8_t>(image, shift);
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1252 break;
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1253 }
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_Grayscale16:
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 ShiftLeftInternal<uint16_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 }
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1260 default:
3545
551945086617 ShiftLeft + ShiftRight
Alain Mazy <alain@mazy.be>
parents: 3525
diff changeset
1261 throw OrthancException(ErrorCode_NotImplemented);
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 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1264
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1265 void ImageProcessing::GetMinMaxIntegerValue(int64_t& minValue,
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1266 int64_t& maxValue,
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1267 const ImageAccessor& image)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1268 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1269 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1270 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1271 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1272 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1273 uint8_t a, b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1274 GetMinMaxValueInternal<uint8_t>(a, b, image);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1275 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1276 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1277 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1278 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1279
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1280 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1281 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1282 uint16_t a, b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1283 GetMinMaxValueInternal<uint16_t>(a, b, image);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1284 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1285 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1286 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1287 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1288
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1289 case PixelFormat_Grayscale32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1290 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1291 uint32_t a, b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1292 GetMinMaxValueInternal<uint32_t>(a, b, image);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1293 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1294 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1295 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1296 }
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1297
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1298 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1299 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1300 int16_t a, b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1301 GetMinMaxValueInternal<int16_t>(a, b, image);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1302 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1303 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1304 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1305 }
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1306
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1307 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1308 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1309 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1310 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1311
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1312
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1313 void ImageProcessing::GetMinMaxFloatValue(float& minValue,
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1314 float& maxValue,
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1315 const ImageAccessor& image)
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1316 {
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1317 switch (image.GetFormat())
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1318 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1319 case PixelFormat_Float32:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1320 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1321 assert(sizeof(float) == 4);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1322 float a, b;
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1323
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1324 /**
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1325 * 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
1326 * "-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
1327 * (as implemented by "::min()")
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1328 * 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
1329 **/
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
1330 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
1331 minValue = a;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1332 maxValue = b;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1333 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1334 }
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1335
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1336 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1337 throw OrthancException(ErrorCode_NotImplemented);
2415
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1338 }
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1339 }
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1340
7e217a1cc63f PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2281
diff changeset
1341
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1342
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1343 void ImageProcessing::AddConstant(ImageAccessor& image,
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1344 int64_t value)
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 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1347 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1348 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1349 AddConstantInternal<uint8_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1350 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1351
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1352 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1353 AddConstantInternal<uint16_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1354 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1355
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1356 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1357 AddConstantInternal<int16_t>(image, value);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1358 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1359
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1360 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1361 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1362 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1363 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1364
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1365
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1366 void ImageProcessing::MultiplyConstant(ImageAccessor& image,
2488
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
1367 float factor,
345725b9350c back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2487
diff changeset
1368 bool useRound)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1369 {
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1370 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1371 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1372 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1373 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1374 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1375 MultiplyConstantInternal<uint8_t, true>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1376 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1377 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1378 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1379 MultiplyConstantInternal<uint8_t, false>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1380 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1381 return;
863
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_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1384 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1385 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1386 MultiplyConstantInternal<uint16_t, true>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1387 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1388 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1389 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1390 MultiplyConstantInternal<uint16_t, false>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1391 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1392 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1393
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1394 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1395 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1396 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1397 MultiplyConstantInternal<int16_t, true>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1398 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1399 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1400 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1401 MultiplyConstantInternal<int16_t, false>(image, factor);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1402 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1403 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1404
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1405 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1406 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1407 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1408 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1409
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1410
4780
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1411 static bool IsIdentityRescaling(float offset,
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1412 float scaling)
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1413 {
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1414 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
1415 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
1416 }
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1417
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1418
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1419 void ImageProcessing::ShiftScale2(ImageAccessor& image,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1420 float offset,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1421 float scaling,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1422 bool useRound)
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1423 {
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1424 // We compute "a * x + b"
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1425 const float a = scaling;
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1426 const float b = offset;
4780
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1427
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1428 if (IsIdentityRescaling(offset, scaling))
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1429 {
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1430 return;
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1431 }
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1432
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1433 switch (image.GetFormat())
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1434 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1435 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1436 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1437 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1438 ShiftScaleInternal<uint8_t, uint8_t, true, false>(image, image, a, b, std::numeric_limits<uint8_t>::min());
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1439 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1440 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1441 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1442 ShiftScaleInternal<uint8_t, uint8_t, false, false>(image, image, a, b, std::numeric_limits<uint8_t>::min());
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1443 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1444 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1445
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1446 case PixelFormat_Grayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1447 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1448 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1449 ShiftScaleInternal<uint16_t, uint16_t, true, false>(image, image, a, b, std::numeric_limits<uint16_t>::min());
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1450 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1451 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1452 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1453 ShiftScaleInternal<uint16_t, uint16_t, false, false>(image, image, a, b, std::numeric_limits<uint16_t>::min());
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1454 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1455 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1456
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1457 case PixelFormat_SignedGrayscale16:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1458 if (useRound)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1459 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1460 ShiftScaleInternal<int16_t, int16_t, true, false>(image, image, a, b, std::numeric_limits<int16_t>::min());
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1461 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1462 else
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1463 {
3682
5f64c866108a merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3640
diff changeset
1464 ShiftScaleInternal<int16_t, int16_t, false, false>(image, image, a, b, std::numeric_limits<int16_t>::min());
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1465 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1466 return;
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1467
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1468 case PixelFormat_Float32:
3684
3971ec6b1f72 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3683
diff changeset
1469 // "::min()" must be replaced by "::lowest()" or "-::max()" if dealing with float or double.
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1470 if (useRound)
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1471 {
3684
3971ec6b1f72 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3683
diff changeset
1472 ShiftScaleInternal<float, float, true, false>(image, image, a, b, -std::numeric_limits<float>::max());
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1473 }
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1474 else
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1475 {
3684
3971ec6b1f72 fix for older compilers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3683
diff changeset
1476 ShiftScaleInternal<float, float, false, false>(image, image, a, b, -std::numeric_limits<float>::max());
3505
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1477 }
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1478 return;
b2d4dd16dae8 removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3504
diff changeset
1479
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1480 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1481 throw OrthancException(ErrorCode_NotImplemented);
863
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1482 }
3c0d0836f704 refactoring
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 862
diff changeset
1483 }
2281
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1484
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1485
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1486 void ImageProcessing::ShiftScale2(ImageAccessor& target,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1487 const ImageAccessor& source,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1488 float offset,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1489 float scaling,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1490 bool useRound)
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1491 {
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1492 // We compute "a * x + b"
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1493 const float a = scaling;
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1494 const float b = offset;
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1495
4780
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1496 if (target.GetFormat() == source.GetFormat() &&
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1497 IsIdentityRescaling(offset, scaling))
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1498 {
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1499 Copy(target, source);
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1500 return;
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1501 }
ebb801d44e06 optimization in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4664
diff changeset
1502
3690
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1503 switch (target.GetFormat())
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1504 {
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1505 case PixelFormat_Grayscale8:
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1506
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1507 switch (source.GetFormat())
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1508 {
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1509 case PixelFormat_Float32:
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1510 if (useRound)
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1511 {
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1512 ShiftScaleInternal<uint8_t, float, true, false>(
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1513 target, source, a, b, std::numeric_limits<uint8_t>::min());
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1514 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1515 else
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1516 {
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1517 ShiftScaleInternal<uint8_t, float, false, false>(
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1518 target, source, a, b, std::numeric_limits<uint8_t>::min());
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1519 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1520 return;
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1521
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1522 default:
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1523 throw OrthancException(ErrorCode_NotImplemented);
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1524 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1525
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1526 default:
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1527 throw OrthancException(ErrorCode_NotImplemented);
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1528 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1529 }
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1530
a9ce35d67c3c implementation of "/instances/.../rendered" for grayscale images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3684
diff changeset
1531
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1532 void ImageProcessing::ShiftScale(ImageAccessor& image,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1533 float offset,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1534 float scaling,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1535 bool useRound)
4079
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1536 {
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1537 // Rewrite "(x + offset) * scaling" as "a * x + b"
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1538
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1539 const float a = scaling;
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1540 const float b = offset * scaling;
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1541 ShiftScale2(image, b, a, useRound);
4079
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1542 }
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1543
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1544
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1545 void ImageProcessing::ShiftScale(ImageAccessor& target,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1546 const ImageAccessor& source,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1547 float offset,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1548 float scaling,
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1549 bool useRound)
4079
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1550 {
4080
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1551 // Rewrite "(x + offset) * scaling" as "a * x + b"
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1552
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1553 const float a = scaling;
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1554 const float b = offset * scaling;
f18eaade6153 simplification in ImageProcessing::ShiftScale()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4079
diff changeset
1555 ShiftScale2(target, source, b, a, useRound);
4079
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1556 }
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1557
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1558
73c22208272f ImageProcessing::ShiftScale2()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3712
diff changeset
1559
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1560 void ImageProcessing::Invert(ImageAccessor& image, int64_t maxValue)
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1561 {
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1562 const unsigned int width = image.GetWidth();
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1563 const unsigned int height = image.GetHeight();
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1564
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1565 switch (image.GetFormat())
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1566 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1567 case PixelFormat_Grayscale16:
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1568 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1569 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
1570
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1571 for (unsigned int y = 0; y < height; y++)
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1572 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1573 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
1574
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1575 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
1576 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1577 *p = maxValueUint16 - (*p);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1578 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1579 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1580
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1581 return;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1582 }
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1583 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1584 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1585 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
1586
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1587 for (unsigned int y = 0; y < height; y++)
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1588 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1589 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
1590
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1591 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
1592 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1593 *p = maxValueUint8 - (*p);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1594 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1595 }
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1596
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1597 return;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1598 }
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1599
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1600 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1601 throw OrthancException(ErrorCode_NotImplemented);
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1602 }
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1603
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1604 }
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1605
2281
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1606 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
1607 {
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1608 switch (image.GetFormat())
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1609 {
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1610 case PixelFormat_Grayscale8:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1611 return Invert(image, 255);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1612 default:
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1613 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
1614 }
2281
e002430baa41 Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2252
diff changeset
1615 }
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1616
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1617
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1618
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1619 namespace
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1620 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1621 template <PixelFormat Format>
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1622 class BresenhamPixelWriter
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1623 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1624 private:
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1625 typedef typename PixelTraits<Format>::PixelType PixelType;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1626
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1627 ImageAccessor& image_;
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1628 PixelType value_;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1629
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1630 void PlotLineLow(int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1631 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1632 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1633 int y1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1634 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1635 int dx = x1 - x0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1636 int dy = y1 - y0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1637 int yi = 1;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1638
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1639 if (dy < 0)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1640 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1641 yi = -1;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1642 dy = -dy;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1643 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1644
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1645 int d = 2 * dy - dx;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1646 int y = y0;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1647
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1648 for (int x = x0; x <= x1; x++)
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 Write(x, y);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1651
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1652 if (d > 0)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1653 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1654 y = y + yi;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1655 d = d - 2 * dx;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1656 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1657
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1658 d = d + 2*dy;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1659 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1660 }
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 PlotLineHigh(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 xi = 1;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1670
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1671 if (dx < 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 xi = -1;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1674 dx = -dx;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1675 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1676
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1677 int d = 2 * dx - dy;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1678 int x = x0;
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 y = y0; y <= y1; y++)
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 x = x + xi;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1687 d = d - 2 * dy;
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 * dx;
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 public:
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1695 BresenhamPixelWriter(ImageAccessor& image,
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1696 int64_t value) :
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1697 image_(image),
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1698 value_(PixelTraits<Format>::IntegerToPixel(value))
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1699 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1700 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1701
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1702 BresenhamPixelWriter(ImageAccessor& image,
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1703 const PixelType& value) :
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1704 image_(image),
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1705 value_(value)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1706 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1707 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1708
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1709 void Write(int x,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1710 int y)
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 if (x >= 0 &&
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1713 y >= 0 &&
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1714 static_cast<unsigned int>(x) < image_.GetWidth() &&
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1715 static_cast<unsigned int>(y) < image_.GetHeight())
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1716 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1717 PixelType* p = reinterpret_cast<PixelType*>(image_.GetRow(y));
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1718 p[x] = value_;
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1719 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1720 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1721
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1722 void DrawSegment(int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1723 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1724 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1725 int y1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1726 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1727 // This is an implementation of Bresenham's line algorithm
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1728 // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1729
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1730 if (abs(y1 - y0) < abs(x1 - x0))
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 if (x0 > x1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1733 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1734 PlotLineLow(x1, y1, x0, y0);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1735 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1736 else
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1737 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1738 PlotLineLow(x0, y0, x1, y1);
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 else
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1742 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1743 if (y0 > y1)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1744 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1745 PlotLineHigh(x1, y1, x0, y0);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1746 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1747 else
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 PlotLineHigh(x0, y0, x1, y1);
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1750 }
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 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1755
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1756
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1757 void ImageProcessing::DrawLineSegment(ImageAccessor& image,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1758 int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1759 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1760 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1761 int y1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1762 int64_t value)
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 switch (image.GetFormat())
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1765 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1766 case PixelFormat_Grayscale8:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1767 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1768 BresenhamPixelWriter<PixelFormat_Grayscale8> writer(image, value);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1769 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1770 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1771 }
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1772
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1773 case PixelFormat_Grayscale16:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1774 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1775 BresenhamPixelWriter<PixelFormat_Grayscale16> writer(image, value);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1776 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1777 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1778 }
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1779
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1780 case PixelFormat_SignedGrayscale16:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1781 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1782 BresenhamPixelWriter<PixelFormat_SignedGrayscale16> writer(image, value);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1783 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1784 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1785 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1786
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1787 default:
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1788 throw OrthancException(ErrorCode_NotImplemented);
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1789 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1790 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1791
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1792
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1793 void ImageProcessing::DrawLineSegment(ImageAccessor& image,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1794 int x0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1795 int y0,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1796 int x1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1797 int y1,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1798 uint8_t red,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1799 uint8_t green,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1800 uint8_t blue,
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1801 uint8_t alpha)
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1802 {
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1803 switch (image.GetFormat())
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_BGRA32:
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 PixelTraits<PixelFormat_BGRA32>::PixelType pixel;
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1808 pixel.red_ = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1809 pixel.green_ = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1810 pixel.blue_ = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1811 pixel.alpha_ = alpha;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1812
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1813 BresenhamPixelWriter<PixelFormat_BGRA32> writer(image, pixel);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1814 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1815 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1816 }
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1817
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1818 case PixelFormat_RGBA32:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1819 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1820 PixelTraits<PixelFormat_RGBA32>::PixelType pixel;
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1821 pixel.red_ = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1822 pixel.green_ = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1823 pixel.blue_ = blue;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1824 pixel.alpha_ = alpha;
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1825
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1826 BresenhamPixelWriter<PixelFormat_RGBA32> writer(image, pixel);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1827 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1828 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1829 }
2783
65699fcb4e99 PixelTraits<PixelFormat_RGBA32>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2650
diff changeset
1830
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1831 case PixelFormat_RGB24:
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1832 {
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1833 PixelTraits<PixelFormat_RGB24>::PixelType pixel;
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1834 pixel.red_ = red;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1835 pixel.green_ = green;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1836 pixel.blue_ = blue;
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1837
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1838 BresenhamPixelWriter<PixelFormat_RGB24> writer(image, pixel);
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1839 writer.DrawSegment(x0, y0, x1, y1);
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1840 break;
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1841 }
3227
53bb1f4b3844 added Invert for Grayscale16
am@osimis.io
parents: 3060
diff changeset
1842
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
1843 default:
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
1844 throw OrthancException(ErrorCode_NotImplemented);
2489
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1845 }
e91bab2d8c75 Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 2488
diff changeset
1846 }
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1847
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1848 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
1849 {
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1850 left = std::numeric_limits<int32_t>::max();
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1851 right = std::numeric_limits<int32_t>::min();
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1852 top = std::numeric_limits<int32_t>::max();
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1853 bottom = std::numeric_limits<int32_t>::min();
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1854
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1855 for (size_t i = 0; i < points.size(); i++)
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1856 {
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1857 const ImageProcessing::ImagePoint& p = points[i];
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1858 left = std::min(p.GetX(), left);
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1859 right = std::max(p.GetX(), right);
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1860 bottom = std::max(p.GetY(), bottom);
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1861 top = std::min(p.GetY(), top);
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1862 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1863 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1864
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1865
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1866 namespace
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1867 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1868 #define USE_POLYGON_FRACTIONS 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1869
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1870 class PolygonEdge
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1871 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1872 private:
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1873 int yUpper;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1874
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1875 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1876 int x;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1877 int xOffset;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1878 int dxPerScanNumerator;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1879 int dxPerScanDenominator;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1880 #else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1881 float xIntersect;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1882 float dxPerScan;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1883 #endif
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1884
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1885 public:
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1886 PolygonEdge(const ImageProcessing::ImagePoint& lower,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1887 const ImageProcessing::ImagePoint& upper,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1888 int yComp)
3365
9345710bbf12 check limits in fillpolygon
Alain Mazy <alain@mazy.be>
parents: 3323
diff changeset
1889 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1890 // cf. "makeEdgeRec()" in textbook
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1891
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1892 assert(upper.GetY() != lower.GetY());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1893
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1894 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1895 x = lower.GetX();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1896 xOffset = 0;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1897 dxPerScanNumerator = upper.GetX() - lower.GetX();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1898 dxPerScanDenominator = upper.GetY() - lower.GetY();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1899 #else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1900 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
1901 static_cast<float>(upper.GetY() - lower.GetY()));
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1902 xIntersect = lower.GetX();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1903 #endif
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1904
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1905 if (upper.GetY() < yComp)
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 yUpper = upper.GetY() - 1;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1908 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1909 else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1910 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1911 yUpper = upper.GetY();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1912 }
3365
9345710bbf12 check limits in fillpolygon
Alain Mazy <alain@mazy.be>
parents: 3323
diff changeset
1913 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1914
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1915 void NextScanLine()
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1916 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1917 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1918 xOffset += dxPerScanNumerator;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1919
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1920 while (xOffset >= dxPerScanDenominator)
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1921 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1922 x++;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1923 xOffset -= dxPerScanDenominator;
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1924 }
4872
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 while (xOffset < 0)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1927 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1928 x--;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1929 xOffset += dxPerScanDenominator;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1930 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1931
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1932 #else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1933 xIntersect += dxPerScan;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1934 #endif
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1935 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1936
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1937
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1938 int GetEnterX() const
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1939 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1940 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1941 assert(xOffset >= 0 && xOffset < dxPerScanDenominator);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1942 if (xOffset == 0)
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1943 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1944 return x;
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1945 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1946 else
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
1947 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1948 return x + 1;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1949 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1950 #else
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1951 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
1952 #endif
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1953 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1954
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1955 int GetExitX() const
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1956 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1957 #if USE_POLYGON_FRACTIONS == 1
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1958 assert(xOffset >= 0 && xOffset < dxPerScanDenominator);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1959 return x;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1960 #else
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1961 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
1962 #endif
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
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1965 int GetUpperY() const
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1966 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1967 return yUpper;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1968 }
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 bool operator< (const PolygonEdge& other) const
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1971 {
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1972 #if USE_POLYGON_FRACTIONS == 1
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1973 assert(xOffset >= 0 && xOffset < dxPerScanDenominator);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1974 assert(other.xOffset >= 0 && other.xOffset < other.dxPerScanDenominator);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1975 return x < other.x;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1976 #else
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1977 // cf. "insertEdge()" in textbook
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1978 return (xIntersect < other.xIntersect);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1979 #endif
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1980 }
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 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1983
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1984
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1985 // 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
1986 static int GetPolygonNextY(const std::vector<ImageProcessing::ImagePoint>& points,
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
1987 size_t k)
4872
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 // cf. "yNext()" in textbook
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1990 size_t j = k;
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1991
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1992 for (;;)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
1993 {
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1994 j++;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
1995 if (j == points.size())
4872
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 j = 0;
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
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2000 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
2001 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2002 return points[j].GetY();
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2003 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2004 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2005 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2006
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2007
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2008 static int GetPolygonPreviousY(const std::vector<ImageProcessing::ImagePoint>& points,
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2009 size_t k)
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2010 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2011 size_t j = k;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2012
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2013 for (;;)
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2014 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2015 if (j > 0)
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2016 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2017 j --;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2018 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2019 else
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2020 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2021 j = points.size() - 1;
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2022 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2023
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2024 if (points[k].GetY() != points[j].GetY())
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2025 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2026 return points[j].GetY();
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2027 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2028 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2029 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2030
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2031
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2032
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2033 void ImageProcessing::FillPolygon(IPolygonFiller& filler,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2034 const std::vector<ImagePoint>& points)
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 * 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
2038 * 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
2039 * Edition)" by Hearn and Baker, 1997.
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2040 **/
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2041
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2042 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
2043
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2044 if (points.size() < 2)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2045 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2046 return;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2047 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2048
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2049 bool onlyHorizontalSegments = true;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2050 for (size_t i = 1; i < points.size(); i++)
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2051 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2052 if (points[0].GetY() != points[i].GetY())
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2053 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2054 onlyHorizontalSegments = false;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2055 break;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2056 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2057 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2058
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2059 if (onlyHorizontalSegments)
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2060 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2061 // Degenerate case: There are only horizontal lines. If this is
4889
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2062 // the case, "GetPolygonPreviousY()" would be an infinite loop
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2063 int x1 = points[0].GetX();
4889
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2064 int x2 = x1;
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2065 for (size_t i = 1; i < points.size(); i++)
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2066 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2067 assert(points[i].GetY() == points[0].GetY());
4889
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2068
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2069 const int x = points[i].GetX();
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2070 x1 = std::min(x1, x);
d8c8274d4e41 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4875
diff changeset
2071 x2 = std::max(x2, x);
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2072 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2073 filler.Fill(points[0].GetY(), x1, x2);
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2074 return;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2075 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2076
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2077 EdgeTable globalEdgeTable;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2078
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2079 // cf. "buildEdgeList()" in textbook
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2080
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2081 // 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
2082 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
2083 ImagePoint v1(points[points.size() - 1]);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2084
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2085 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
2086 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2087 ImagePoint v2(points[i]);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2088
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2089 if (v1.GetY() != v2.GetY())
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2090 {
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2091 // Non-horizontal line
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2092 if (v1.GetY() < v2.GetY())
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2093 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2094 // Up-going edge
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2095 PolygonEdge edge(v1, v2, GetPolygonNextY(points, i));
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2096 globalEdgeTable[v1.GetY()].push_back(edge);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2097 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2098 else if (v1.GetY() > v2.GetY())
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2099 {
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2100 // Down-going edge
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2101 PolygonEdge edge(v2, v1, yPrev);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2102 globalEdgeTable[v2.GetY()].push_back(edge);
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2103 }
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2104
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2105 // 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
2106 yPrev = v1.GetY();
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2107 }
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2108
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2109 v1 = v2;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2110 }
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2111
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2112 assert(!globalEdgeTable.empty());
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2113
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2114 std::vector<PolygonEdge> activeEdges;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2115
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2116 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
2117 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2118 // cf. "buildActiveList()" in textbook
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2119 activeEdges.reserve(activeEdges.size() + it->second.size());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2120 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
2121 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2122 activeEdges.push_back(*it2);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2123 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2124
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2125 assert(!activeEdges.empty());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2126
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2127 EdgeTable::const_iterator next = it;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2128 ++next;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2129
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2130 int rampEnd;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2131 if (next == globalEdgeTable.end())
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2132 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2133 rampEnd = activeEdges[0].GetUpperY() + 1;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2134
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2135 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
2136 {
4873
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4872
diff changeset
2137 rampEnd = std::max(rampEnd, activeEdges[i].GetUpperY() + 1);
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2138 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2139 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2140 else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2141 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2142 rampEnd = next->first;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2143 }
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2144
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2145 for (int y = it->first; y < rampEnd; y++)
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2146 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2147 // cf. "updateActiveList()" in textbook
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2148 std::vector<PolygonEdge> stillActive;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2149 stillActive.reserve(activeEdges.size());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2150
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2151 for (size_t i = 0; i < activeEdges.size(); i++)
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2152 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2153 if (y <= activeEdges[i].GetUpperY())
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2154 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2155 stillActive.push_back(activeEdges[i]);
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2156 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2157 }
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 activeEdges.swap(stillActive);
4874
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2160
bee6da5155dc fix polygon filling
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4873
diff changeset
2161 assert(activeEdges.size() % 2 == 0);
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2162 std::sort(activeEdges.begin(), activeEdges.end());
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2163
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2164 // cf. "fillScan()" in textbook
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2165 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
2166 {
4873
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4872
diff changeset
2167 int a = activeEdges[k].GetExitX();
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4872
diff changeset
2168 int b = activeEdges[k + 1].GetEnterX();
4875
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2169
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2170 // Fix wrt. the textbook: merge overlapping segments
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2171 k += 2;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2172 while (k + 1 < activeEdges.size() &&
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2173 activeEdges[k].GetExitX() == b)
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2174 {
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2175 assert(a <= b);
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2176 b = activeEdges[k + 1].GetEnterX();
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2177 k += 2;
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2178 }
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2179
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2180 assert(a <= b);
5dae41084ab7 fix filling polygons
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4874
diff changeset
2181 filler.Fill(y, a, b);
4872
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
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2184 // cf. "updateActiveList()" in textbook
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2185 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
2186 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2187 activeEdges[k].NextScanLine();
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2188 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2189 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2190 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2191 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2192
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2193
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2194 void ImageProcessing::FillPolygon(ImageAccessor& image,
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2195 const std::vector<ImagePoint>& points,
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2196 int64_t value)
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2197 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2198 class Filler : public IPolygonFiller
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2199 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2200 private:
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2201 ImageAccessor& image_;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2202 int64_t value_;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2203
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2204 public:
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2205 Filler(ImageAccessor& image,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2206 int64_t value) :
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2207 image_(image),
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2208 value_(value)
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
2209 {
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
2210 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2211
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2212 virtual void Fill(int y,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2213 int x1,
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2214 int x2) ORTHANC_OVERRIDE
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
2215 {
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2216 assert(x1 <= x2);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2217
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2218 if (x1 < static_cast<int>(image_.GetWidth()) &&
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2219 x2 >= 0 &&
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2220 y >= 0 &&
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2221 y < static_cast<int>(image_.GetHeight()))
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2222 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2223 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
2224 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
2225 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
2226
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2227 assert(a <= b);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2228
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2229 ImageAccessor region;
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2230 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
2231 Set(region, value_);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2232 }
3499
d8f7c3970e25 more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3431
diff changeset
2233 }
4872
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2234 };
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
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2237 if (image.GetFormat() == PixelFormat_Grayscale8 ||
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2238 image.GetFormat() == PixelFormat_Grayscale16 ||
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2239 image.GetFormat() == PixelFormat_SignedGrayscale16)
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2240 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2241 Filler filler(image, value);
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2242 FillPolygon(filler, points);
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 else
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2245 {
b1556cefa5c6 reimplementation from scratch of ImageProcessing::FillPolygon()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4870
diff changeset
2246 throw OrthancException(ErrorCode_NotImplemented);
3258
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2247 }
6f652c7bfc85 ImageProcessing::FillPolygon
Alain Mazy <alain@mazy.be>
parents: 3234
diff changeset
2248 }
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2249
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2250
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2251 template <PixelFormat Format>
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2252 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
2253 const ImageAccessor& source)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2254 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2255 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
2256 target.GetFormat() == Format);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2257
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2258 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
2259 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
2260 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
2261 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
2262
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2263 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
2264 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2265 return;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2266 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2267
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2268 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
2269 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2270 // 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
2271 ImageProcessing::Set(target, 0);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2272 return;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2273 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2274
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2275 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
2276 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
2277
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2278
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2279 /**
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2280 * 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
2281 * 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
2282 * image.
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2283 **/
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2284
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2285 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
2286
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2287 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
2288 {
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
2289 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
2290 if (sourceX < 0)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2291 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2292 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
2293 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2294 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
2295 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2296 sourceX = sourceWidth - 1;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2297 }
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 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
2300 }
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 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
2303
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2304 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
2305 {
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
2306 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
2307 if (sourceY < 0)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2308 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2309 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
2310 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2311 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
2312 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2313 sourceY = sourceHeight - 1;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2314 }
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 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
2317 }
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
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2320 /**
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2321 * Actual resizing
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2322 **/
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 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
2325 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2326 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
2327
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2328 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
2329 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2330 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
2331
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2332 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
2333 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
2334 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
2335 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2336 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2337 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2338
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2339
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 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
2342 const ImageAccessor& source)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2343 {
4201
2d5209153b32 cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4119
diff changeset
2344 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
2345 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2346 throw OrthancException(ErrorCode_IncompatibleImageFormat);
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
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2349 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
2350 source.GetHeight() == target.GetHeight())
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 Copy(target, source);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2353 return;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2354 }
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2355
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2356 switch (source.GetFormat())
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 case PixelFormat_Grayscale8:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2359 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
2360 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2361
3548
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2362 case PixelFormat_Float32:
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2363 ResizeInternal<PixelFormat_Float32>(target, source);
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2364 break;
e1ce68692069 ImageProcessing::ApplyWindowing
Alain Mazy <alain@mazy.be>
parents: 3547
diff changeset
2365
3502
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2366 case PixelFormat_RGB24:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2367 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
2368 break;
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 default:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2371 throw OrthancException(ErrorCode_NotImplemented);
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 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2374
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2375
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2376 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
2377 bool forceMinimalPitch)
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2378 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3690
diff changeset
2379 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
2380 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
2381 Resize(*target, source);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2382 return target.release();
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
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2385
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2386 template <PixelFormat Format>
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2387 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
2388 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2389 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
2390 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
2391
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2392 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
2393 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2394 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
2395 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2396 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
2397
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2398 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
2399 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
2400 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
2401 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
2402 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
2403 }
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 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
2409 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2410 switch (image.GetFormat())
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2411 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2412 case PixelFormat_Grayscale8:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2413 FlipXInternal<PixelFormat_Grayscale8>(image);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2414 break;
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 case PixelFormat_RGB24:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2417 FlipXInternal<PixelFormat_RGB24>(image);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2418 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2419
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2420 default:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2421 throw OrthancException(ErrorCode_NotImplemented);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2422 }
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
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 template <PixelFormat Format>
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2427 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
2428 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2429 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
2430 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
2431
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2432 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
2433 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2434 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
2435
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2436 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
2437 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2438 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
2439 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
2440 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
2441 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
2442 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
2443 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2444 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2445 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2446
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 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
2449 {
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2450 switch (image.GetFormat())
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 case PixelFormat_Grayscale8:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2453 FlipYInternal<PixelFormat_Grayscale8>(image);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2454 break;
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 case PixelFormat_RGB24:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2457 FlipYInternal<PixelFormat_RGB24>(image);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2458 break;
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2459
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2460 default:
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2461 throw OrthancException(ErrorCode_NotImplemented);
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2462 }
c160eafc42a9 new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3499
diff changeset
2463 }
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2464
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2465
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2466 // This is a slow implementation of horizontal convolution on one
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2467 // individual channel, that checks for out-of-image values
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2468 template <typename RawPixel, unsigned int ChannelsCount>
4297
785a2713323e abi continued
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4278
diff changeset
2469 static float GetHorizontalConvolutionFloatSecure(const ImageAccessor& source,
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2470 const std::vector<float>& horizontal,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2471 size_t horizontalAnchor,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2472 unsigned int x,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2473 unsigned int y,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2474 float leftBorder,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2475 float rightBorder,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2476 unsigned int channel)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2477 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2478 const RawPixel* row = reinterpret_cast<const RawPixel*>(source.GetConstRow(y)) + channel;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2479
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2480 float p = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2481
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2482 for (unsigned int k = 0; k < horizontal.size(); k++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2483 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2484 float value;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2485
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2486 if (x + k < horizontalAnchor) // Negation of "x - horizontalAnchor + k >= 0"
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2487 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2488 value = leftBorder;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2489 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2490 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
2491 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2492 value = rightBorder;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2493 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2494 else
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2495 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2496 // The value lies within the image
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2497 value = row[(x - horizontalAnchor + k) * ChannelsCount];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2498 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2499
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2500 p += value * horizontal[k];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2501 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2502
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2503 return p;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2504 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2505
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2506
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2507
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2508 // This is an implementation of separable convolution that uses
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2509 // floating-point arithmetics, and an intermediate Float32
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2510 // image. The out-of-image values are taken as the border
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2511 // 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
2512 template <typename RawPixel, unsigned int ChannelsCount, bool UseRound>
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2513 static void SeparableConvolutionFloat(ImageAccessor& image /* inplace */,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2514 const std::vector<float>& horizontal,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2515 size_t horizontalAnchor,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2516 const std::vector<float>& vertical,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2517 size_t verticalAnchor,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2518 float normalization)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2519 {
3525
8c66c9c2257b fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3505
diff changeset
2520 // 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
2521 // 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
2522 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
2523
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2524 const unsigned int width = image.GetWidth();
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2525 const unsigned int height = image.GetHeight();
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2526
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 /**
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2529 * Horizontal convolution
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 Image tmp(PixelFormat_Float32, ChannelsCount * width, height, false);
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 for (unsigned int y = 0; y < height; y++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2535 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2536 const RawPixel* row = reinterpret_cast<const RawPixel*>(image.GetConstRow(y));
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 float leftBorder[ChannelsCount], rightBorder[ChannelsCount];
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 for (unsigned int c = 0; c < ChannelsCount; c++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2541 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2542 leftBorder[c] = row[c];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2543 rightBorder[c] = row[ChannelsCount * (width - 1) + c];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2544 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2545
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2546 float* p = static_cast<float*>(tmp.GetRow(y));
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2547
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2548 if (width < horizontal.size())
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2549 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2550 // 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
2551 for (unsigned int x = 0; x < width; x++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2552 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2553 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2554 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2555 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount>
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2556 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2557 }
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 else
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2561 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2562 // Deal with the left border
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2563 for (unsigned int x = 0; x < horizontalAnchor; x++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2564 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2565 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2566 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2567 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount>
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2568 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c);
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 }
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 // Deal with the central portion of the image (all pixel values
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2573 // scanned by the kernel lie inside the image)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2574
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2575 for (unsigned int x = 0; x < width - horizontal.size() + 1; x++)
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 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2578 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2579 *p = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2580 for (unsigned int k = 0; k < horizontal.size(); k++)
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 *p += static_cast<float>(row[(x + k) * ChannelsCount + c]) * horizontal[k];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2583 }
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 }
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 // 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
2588 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
2589 horizontalAnchor + width - horizontal.size() + 1); x < width; x++)
3503
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 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2592 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2593 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount>
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2594 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2595 }
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 }
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
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2600
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 * Vertical convolution
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
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2605 std::vector<const float*> rows(vertical.size());
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 y = 0; y < height; y++)
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 k = 0; k < vertical.size(); k++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2610 {
3504
18566f9e1831 unit testing new functions in ImageProcessing
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3503
diff changeset
2611 if (y + k < verticalAnchor)
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2612 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2613 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
2614 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2615 else if (y + k >= height + verticalAnchor)
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 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
2618 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2619 else
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2620 {
3585
113a9643e8bb Suppressed a few warnings when building with emscripten (clang) + numeric truncation warnings
Benjamin Golinvaux <bgo@osimis.io>
parents: 3565
diff changeset
2621 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
2622 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2623 }
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 RawPixel* p = reinterpret_cast<RawPixel*>(image.GetRow(y));
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2626
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2627 for (unsigned int x = 0; x < width; x++)
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 for (unsigned int c = 0; c < ChannelsCount; c++, p++)
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 float accumulator = 0;
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 for (unsigned int k = 0; k < vertical.size(); k++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2634 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2635 accumulator += rows[k][ChannelsCount * x + c] * vertical[k];
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
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2638 accumulator *= normalization;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2639
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2640 if (accumulator <= static_cast<float>(std::numeric_limits<RawPixel>::min()))
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2641 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2642 *p = std::numeric_limits<RawPixel>::min();
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2643 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2644 else if (accumulator >= static_cast<float>(std::numeric_limits<RawPixel>::max()))
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2645 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2646 *p = std::numeric_limits<RawPixel>::max();
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2647 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2648 else
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2649 {
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2650 if (UseRound)
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2651 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2652 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
2653 *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
2654 }
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2655 else
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2656 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2657 *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
2658 }
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2659 }
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 }
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 }
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
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2666 void ImageProcessing::SeparableConvolution(ImageAccessor& image /* inplace */,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2667 const std::vector<float>& horizontal,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2668 size_t horizontalAnchor,
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2669 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
2670 size_t verticalAnchor,
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2671 bool useRound)
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2672 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2673 if (horizontal.size() == 0 ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2674 vertical.size() == 0 ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2675 horizontalAnchor >= horizontal.size() ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2676 verticalAnchor >= vertical.size())
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 throw OrthancException(ErrorCode_ParameterOutOfRange);
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
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2681 if (image.GetWidth() == 0 ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2682 image.GetHeight() == 0)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2683 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2684 return;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2685 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2686
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2687 /**
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2688 * Compute normalization
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2689 **/
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2690
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2691 float sumHorizontal = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2692 for (size_t i = 0; i < horizontal.size(); i++)
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 sumHorizontal += horizontal[i];
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 float sumVertical = 0;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2698 for (size_t i = 0; i < vertical.size(); i++)
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2699 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2700 sumVertical += vertical[i];
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2701 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2702
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2703 if (fabsf(sumHorizontal) <= std::numeric_limits<float>::epsilon() ||
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2704 fabsf(sumVertical) <= std::numeric_limits<float>::epsilon())
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2705 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2706 throw OrthancException(ErrorCode_ParameterOutOfRange, "Singular convolution kernel");
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2707 }
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2708
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2709 const float normalization = 1.0f / (sumHorizontal * sumVertical);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2710
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2711 switch (image.GetFormat())
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 case PixelFormat_Grayscale8:
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2714 if (useRound)
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2715 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2716 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
2717 (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
2718 }
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2719 else
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2720 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2721 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
2722 (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
2723 }
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2724 break;
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 case PixelFormat_RGB24:
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2727 if (useRound)
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2728 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2729 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
2730 (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
2731 }
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2732 else
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2733 {
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2734 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
2735 (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
2736 }
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2737 break;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2738
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2739 default:
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2740 throw OrthancException(ErrorCode_NotImplemented);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2741 }
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
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2744
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2745 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
2746 bool useRound)
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2747 {
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2748 std::vector<float> kernel(5);
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2749 kernel[0] = 1;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2750 kernel[1] = 4;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2751 kernel[2] = 6;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2752 kernel[3] = 4;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2753 kernel[4] = 1;
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2754
4065
d6362b2c4b61 export dcmdata in shared library, rounding in convolution tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4056
diff changeset
2755 SeparableConvolution(image, kernel, 2, kernel, 2, useRound);
3503
46cf170ba121 ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3502
diff changeset
2756 }
3549
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2757
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2758
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2759 void ImageProcessing::FitSize(ImageAccessor& target,
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2760 const ImageAccessor& source)
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2761 {
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2762 if (target.GetWidth() == 0 ||
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2763 target.GetHeight() == 0)
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2764 {
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2765 return;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2766 }
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2767
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2768 if (source.GetWidth() == target.GetWidth() &&
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2769 source.GetHeight() == target.GetHeight())
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2770 {
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2771 Copy(target, source);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2772 return;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2773 }
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2774
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2775 Set(target, 0);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2776
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2777 // Preserve the aspect ratio
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2778 float cw = static_cast<float>(source.GetWidth());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2779 float ch = static_cast<float>(source.GetHeight());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2780 float r = std::min(
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2781 static_cast<float>(target.GetWidth()) / cw,
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2782 static_cast<float>(target.GetHeight()) / ch);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2783
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2784 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
2785 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
2786
3549
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2787 Image resized(target.GetFormat(), sw, sh, false);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2788
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2789 //ImageProcessing::SmoothGaussian5x5(source);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2790 ImageProcessing::Resize(resized, source);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2791
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2792 assert(target.GetWidth() >= resized.GetWidth() &&
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2793 target.GetHeight() >= resized.GetHeight());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2794 unsigned int offsetX = (target.GetWidth() - resized.GetWidth()) / 2;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2795 unsigned int offsetY = (target.GetHeight() - resized.GetHeight()) / 2;
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 ImageAccessor region;
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2798 target.GetRegion(region, offsetX, offsetY, resized.GetWidth(), resized.GetHeight());
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2799 ImageProcessing::Copy(region, resized);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2800 }
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2801
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 ImageAccessor* ImageProcessing::FitSize(const ImageAccessor& source,
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2804 unsigned int width,
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2805 unsigned int height)
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2806 {
3712
2a170a8f1faf replacing std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3690
diff changeset
2807 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
2808 FitSize(*target, source);
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2809 return target.release();
fab5777f4dd4 ImageProcessing::FitSize()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 3547
diff changeset
2810 }
4082
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2811
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2812
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2813 ImageAccessor* ImageProcessing::FitSizeKeepAspectRatio(const ImageAccessor& source,
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2814 unsigned int width,
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2815 unsigned int height)
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2816 {
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2817 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
2818 Set(*target, 0);
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2819
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2820 if (width != 0 &&
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2821 height != 0 &&
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2822 source.GetWidth() != 0 &&
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2823 source.GetHeight() != 0)
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2824 {
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2825 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
2826 static_cast<float>(height) / static_cast<float>(source.GetHeight()));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2827
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2828 unsigned int resizedWidth = static_cast<unsigned int>(
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2829 boost::math::iround(ratio * static_cast<float>(source.GetWidth())));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2830
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2831 unsigned int resizedHeight = static_cast<unsigned int>(
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2832 boost::math::iround(ratio * static_cast<float>(source.GetHeight())));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2833
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2834 std::unique_ptr<ImageAccessor> resized(FitSize(source, resizedWidth, resizedHeight));
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2835
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2836 ImageAccessor region;
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2837 target->GetRegion(region, (width - resizedWidth) / 2,
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2838 (height - resizedHeight) / 2, resizedWidth, resizedHeight);
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2839 Copy(region, *resized);
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2840 }
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2841
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2842 return target.release();
2e1564f57542 ImageProcessing::FitSizeKeepAspectRatio()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4080
diff changeset
2843 }
4443
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2844
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2845
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2846 void ImageProcessing::ConvertJpegYCbCrToRgb(ImageAccessor& image)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2847 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2848 // 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
2849 // https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2850
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2851 // TODO - Check out the outcome of Mathieu's discussion about
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2852 // truncation of YCbCr-to-RGB conversion:
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2853 // https://groups.google.com/forum/#!msg/comp.protocols.dicom/JHuGeyWbTz8/ARoTWrJzAQAJ
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2854
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2855 const unsigned int width = image.GetWidth();
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2856 const unsigned int height = image.GetHeight();
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2857 const unsigned int pitch = image.GetPitch();
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2858 uint8_t* buffer = reinterpret_cast<uint8_t*>(image.GetBuffer());
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2859
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2860 if (image.GetFormat() != PixelFormat_RGB24 ||
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2861 pitch < 3 * width)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2862 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2863 throw OrthancException(ErrorCode_IncompatibleImageFormat);
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2864 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2865
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2866 for (unsigned int y = 0; y < height; y++)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2867 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2868 uint8_t* p = buffer + y * pitch;
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2869
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2870 for (unsigned int x = 0; x < width; x++, p += 3)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2871 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2872 const float Y = p[0];
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2873 const float Cb = p[1];
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2874 const float Cr = p[2];
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2875
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2876 const float result[3] = {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2877 Y + 1.402f * (Cr - 128.0f),
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2878 Y - 0.344136f * (Cb - 128.0f) - 0.714136f * (Cr - 128.0f),
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2879 Y + 1.772f * (Cb - 128.0f)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2880 };
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2881
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2882 for (uint8_t i = 0; i < 3 ; i++)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2883 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2884 if (result[i] < 0)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2885 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2886 p[i] = 0;
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2887 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2888 else if (result[i] > 255)
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2889 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2890 p[i] = 255;
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 else
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2893 {
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2894 p[i] = static_cast<uint8_t>(result[i]);
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2895 }
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 }
fd958175c5b9 ImageProcessing::ConvertJpegYCbCrToRgb()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4437
diff changeset
2899 }
4529
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2900
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2901
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2902 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
2903 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2904 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
2905 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
2906
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2907 switch (image.GetFormat())
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2908 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2909 case PixelFormat_Grayscale8:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2910 case PixelFormat_RGB24:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2911 case PixelFormat_RGBA32:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2912 case PixelFormat_BGRA32:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2913 // No swapping required
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2914 break;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2915
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2916 case PixelFormat_Grayscale16:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2917 case PixelFormat_SignedGrayscale16:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2918 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
2919 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2920 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
2921 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
2922 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2923 uint8_t a = t[0];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2924 t[0] = t[1];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2925 t[1] = a;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2926 t += 2;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2927 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2928 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2929 break;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2930
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2931 case PixelFormat_Grayscale32:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2932 case PixelFormat_Float32:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2933 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
2934 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2935 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
2936 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
2937 {
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2938 uint8_t a = t[0];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2939 uint8_t b = t[1];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2940 t[0] = t[3];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2941 t[1] = t[2];
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2942 t[2] = b;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2943 t[3] = a;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2944 t += 4;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2945 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2946 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2947 break;
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2948
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2949 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
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 < 3 * 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 default:
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2964 throw OrthancException(ErrorCode_NotImplemented);
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2965 }
5774fe497ff2 fix decoding of images on big-endian architectures
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 4447
diff changeset
2966 }
853
839be3022203 DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2967 }