Mercurial > hg > orthanc
annotate Core/Images/ImageProcessing.cpp @ 4114:cfe805d75e4b Orthanc-1.5.8
closing Orthanc-1.5.8
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 13:08:45 +0200 |
parents | 8c66c9c2257b |
children | 551945086617 |
rev | line source |
---|---|
853 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 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 |
3060
4e43e67f8ecf
preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2918
diff
changeset
|
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
853 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
3227 | 23 * |
853 | 24 * This program is distributed in the hope that it will be useful, but |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../PrecompiledHeaders.h" | |
35 #include "ImageProcessing.h" | |
36 | |
3502
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
37 #include "Image.h" |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
38 #include "ImageTraits.h" |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
39 #include "PixelTraits.h" |
2895 | 40 #include "../OrthancException.h" |
853 | 41 |
863 | 42 #include <boost/math/special_functions/round.hpp> |
43 | |
853 | 44 #include <cassert> |
45 #include <string.h> | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
46 #include <limits> |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
47 #include <stdint.h> |
3258 | 48 #include <algorithm> |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
49 |
853 | 50 namespace Orthanc |
51 { | |
3265
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
52 double ImageProcessing::ImagePoint::GetDistanceTo(const ImagePoint& other) const |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
53 { |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
54 double dx = (double)(other.GetX() - GetX()); |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
55 double dy = (double)(other.GetY() - GetY()); |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
56 return sqrt(dx * dx + dy * dy); |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
57 } |
59a184cbb596
ImagePoint::Set + GetDistanceTo
Alain Mazy <alain@mazy.be>
parents:
3259
diff
changeset
|
58 |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
59 template <typename TargetType, typename SourceType> |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
60 static void ConvertInternal(ImageAccessor& target, |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
61 const ImageAccessor& source) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
62 { |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
63 // 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
|
64 // 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
|
65 assert(sizeof(TargetType) <= 2); // Safeguard to remember about "float/double" |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
66 const TargetType minValue = std::numeric_limits<TargetType>::min(); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
67 const TargetType maxValue = std::numeric_limits<TargetType>::max(); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
68 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
69 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
70 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
71 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
72 for (unsigned int y = 0; y < height; y++) |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
73 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
74 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
75 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
76 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
77 for (unsigned int x = 0; x < width; x++, t++, s++) |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
78 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
79 if (static_cast<int32_t>(*s) < static_cast<int32_t>(minValue)) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
80 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
81 *t = minValue; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
82 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
83 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
|
84 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
85 *t = maxValue; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
86 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
87 else |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
88 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
89 *t = static_cast<TargetType>(*s); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
90 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
91 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
92 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
93 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
94 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
95 |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
96 template <typename SourceType> |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
97 static void ConvertGrayscaleToFloat(ImageAccessor& target, |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
98 const ImageAccessor& source) |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
99 { |
1994
4d099fee5eca
ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1993
diff
changeset
|
100 assert(sizeof(float) == 4); |
4d099fee5eca
ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1993
diff
changeset
|
101 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
102 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
103 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
104 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
105 for (unsigned int y = 0; y < height; y++) |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
106 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
107 float* t = reinterpret_cast<float*>(target.GetRow(y)); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
108 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y)); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
109 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
110 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
|
111 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
112 *t = static_cast<float>(*s); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
113 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
114 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
115 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
116 |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
117 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
118 template <PixelFormat TargetFormat> |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
119 static void ConvertFloatToGrayscale(ImageAccessor& target, |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
120 const ImageAccessor& source) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
121 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
122 typedef typename PixelTraits<TargetFormat>::PixelType TargetType; |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
123 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
124 assert(sizeof(float) == 4); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
125 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
126 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
127 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
128 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
129 for (unsigned int y = 0; y < height; y++) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
130 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
131 TargetType* q = reinterpret_cast<TargetType*>(target.GetRow(y)); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
132 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
|
133 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
134 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
|
135 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
136 PixelTraits<TargetFormat>::FloatToPixel(*q, *p); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
137 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
138 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
139 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
140 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
141 |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
142 template <typename TargetType> |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
143 static void ConvertColorToGrayscale(ImageAccessor& target, |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
144 const ImageAccessor& source) |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
145 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
146 assert(source.GetFormat() == PixelFormat_RGB24); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
147 |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
148 // 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
|
149 // 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
|
150 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
|
151 const TargetType minValue = std::numeric_limits<TargetType>::min(); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
152 const TargetType maxValue = std::numeric_limits<TargetType>::max(); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
153 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
154 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
155 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
156 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
157 for (unsigned int y = 0; y < height; y++) |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
158 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
159 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y)); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
160 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
|
161 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
162 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
|
163 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
164 // 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
|
165 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
|
166 7152 * static_cast<int32_t>(s[1]) + |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
167 0722 * static_cast<int32_t>(s[2])) / 10000; |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
168 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
169 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
|
170 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
171 *t = minValue; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
172 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
173 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
|
174 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
175 *t = maxValue; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
176 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
177 else |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
178 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
179 *t = static_cast<TargetType>(v); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
180 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
181 } |
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 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
184 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
185 |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
186 static void MemsetZeroInternal(ImageAccessor& image) |
3227 | 187 { |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
188 const unsigned int height = image.GetHeight(); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
189 const size_t lineSize = image.GetBytesPerPixel() * image.GetWidth(); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
190 const size_t pitch = image.GetPitch(); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
191 |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
192 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
|
193 |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
194 for (unsigned int y = 0; y < height; y++) |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
195 { |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
196 memset(p, 0, lineSize); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
197 p += pitch; |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
198 } |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
199 } |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
200 |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
201 |
863 | 202 template <typename PixelType> |
203 static void SetInternal(ImageAccessor& image, | |
204 int64_t constant) | |
205 { | |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
206 if (constant == 0 && |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
207 (image.GetFormat() == PixelFormat_Grayscale8 || |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
208 image.GetFormat() == PixelFormat_Grayscale16 || |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
209 image.GetFormat() == PixelFormat_Grayscale32 || |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
210 image.GetFormat() == PixelFormat_Grayscale64 || |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
211 image.GetFormat() == PixelFormat_SignedGrayscale16)) |
863 | 212 { |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
213 MemsetZeroInternal(image); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
214 } |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
215 else |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
216 { |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
217 const unsigned int width = image.GetWidth(); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
218 const unsigned int height = image.GetHeight(); |
863 | 219 |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
220 for (unsigned int y = 0; y < height; y++) |
863 | 221 { |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
222 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
223 |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
224 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
|
225 { |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
226 *p = static_cast<PixelType>(constant); |
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
227 } |
863 | 228 } |
229 } | |
230 } | |
231 | |
232 | |
233 template <typename PixelType> | |
234 static void GetMinMaxValueInternal(PixelType& minValue, | |
235 PixelType& maxValue, | |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
236 const ImageAccessor& source, |
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
237 const PixelType LowestValue = std::numeric_limits<PixelType>::min()) |
863 | 238 { |
239 // Deal with the special case of empty image | |
240 if (source.GetWidth() == 0 || | |
241 source.GetHeight() == 0) | |
242 { | |
243 minValue = 0; | |
244 maxValue = 0; | |
245 return; | |
246 } | |
247 | |
248 minValue = std::numeric_limits<PixelType>::max(); | |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
249 maxValue = LowestValue; |
863 | 250 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
251 const unsigned int height = source.GetHeight(); |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
252 const unsigned int width = source.GetWidth(); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
253 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
254 for (unsigned int y = 0; y < height; y++) |
863 | 255 { |
256 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y)); | |
257 | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
258 for (unsigned int x = 0; x < width; x++, p++) |
863 | 259 { |
260 if (*p < minValue) | |
261 { | |
262 minValue = *p; | |
263 } | |
264 | |
265 if (*p > maxValue) | |
266 { | |
267 maxValue = *p; | |
268 } | |
269 } | |
270 } | |
271 } | |
272 | |
273 | |
274 | |
275 template <typename PixelType> | |
276 static void AddConstantInternal(ImageAccessor& image, | |
277 int64_t constant) | |
278 { | |
279 if (constant == 0) | |
280 { | |
281 return; | |
282 } | |
283 | |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
284 // 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
|
285 // 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
|
286 assert(sizeof(PixelType) <= 2); // Safeguard to remember about "float/double" |
863 | 287 const int64_t minValue = std::numeric_limits<PixelType>::min(); |
288 const int64_t maxValue = std::numeric_limits<PixelType>::max(); | |
289 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
290 const unsigned int width = image.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
291 const unsigned int height = image.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
292 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
293 for (unsigned int y = 0; y < height; y++) |
863 | 294 { |
295 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
296 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
297 for (unsigned int x = 0; x < width; x++, p++) |
863 | 298 { |
299 int64_t v = static_cast<int64_t>(*p) + constant; | |
300 | |
301 if (v > maxValue) | |
302 { | |
876 | 303 *p = std::numeric_limits<PixelType>::max(); |
863 | 304 } |
305 else if (v < minValue) | |
306 { | |
876 | 307 *p = std::numeric_limits<PixelType>::min(); |
863 | 308 } |
309 else | |
310 { | |
311 *p = static_cast<PixelType>(v); | |
312 } | |
313 } | |
314 } | |
315 } | |
316 | |
317 | |
318 | |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
319 template <typename PixelType, |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
320 bool UseRound> |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
321 static void MultiplyConstantInternal(ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
322 float factor) |
863 | 323 { |
1334 | 324 if (std::abs(factor - 1.0f) <= std::numeric_limits<float>::epsilon()) |
863 | 325 { |
326 return; | |
327 } | |
328 | |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
329 // 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
|
330 // 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
|
331 assert(sizeof(PixelType) <= 2); // Safeguard to remember about "float/double" |
863 | 332 const int64_t minValue = std::numeric_limits<PixelType>::min(); |
333 const int64_t maxValue = std::numeric_limits<PixelType>::max(); | |
334 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
335 const unsigned int width = image.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
336 const unsigned int height = image.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
337 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
338 for (unsigned int y = 0; y < height; y++) |
863 | 339 { |
340 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
341 | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
342 for (unsigned int x = 0; x < width; x++, p++) |
863 | 343 { |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
344 int64_t v; |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
345 if (UseRound) |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
346 { |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
347 // The "round" operation is very costly |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
348 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
|
349 } |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
350 else |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
351 { |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
352 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
|
353 } |
863 | 354 |
355 if (v > maxValue) | |
356 { | |
876 | 357 *p = std::numeric_limits<PixelType>::max(); |
863 | 358 } |
359 else if (v < minValue) | |
360 { | |
876 | 361 *p = std::numeric_limits<PixelType>::min(); |
863 | 362 } |
363 else | |
364 { | |
365 *p = static_cast<PixelType>(v); | |
366 } | |
367 } | |
368 } | |
369 } | |
370 | |
371 | |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
372 template <typename PixelType, |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
373 bool UseRound> |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
374 static void ShiftScaleInternal(ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
375 float offset, |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
376 float scaling, |
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
377 const PixelType LowestValue = std::numeric_limits<PixelType>::min()) |
863 | 378 { |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
379 const PixelType minPixelValue = LowestValue; |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
380 const PixelType maxPixelValue = std::numeric_limits<PixelType>::max(); |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
381 const float minFloatValue = static_cast<float>(LowestValue); |
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
382 const float maxFloatValue = static_cast<float>(maxPixelValue); |
863 | 383 |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
384 const unsigned int height = image.GetHeight(); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
385 const unsigned int width = image.GetWidth(); |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
386 |
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
387 for (unsigned int y = 0; y < height; y++) |
863 | 388 { |
389 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y)); | |
390 | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
391 for (unsigned int x = 0; x < width; x++, p++) |
863 | 392 { |
393 float v = (static_cast<float>(*p) + offset) * scaling; | |
394 | |
3505
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
395 if (v >= maxFloatValue) |
863 | 396 { |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
397 *p = maxPixelValue; |
863 | 398 } |
3505
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
399 else if (v <= minFloatValue) |
863 | 400 { |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
401 *p = minPixelValue; |
863 | 402 } |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
403 else if (UseRound) |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
404 { |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
405 // The "round" operation is very costly |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
406 *p = static_cast<PixelType>(boost::math::iround(v)); |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
407 } |
863 | 408 else |
409 { | |
2482
509041cb57db
speedup by truncating instead of rounding
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
410 *p = static_cast<PixelType>(v); |
863 | 411 } |
412 } | |
413 } | |
414 } | |
415 | |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
416 |
853 | 417 void ImageProcessing::Copy(ImageAccessor& target, |
418 const ImageAccessor& source) | |
419 { | |
420 if (target.GetWidth() != source.GetWidth() || | |
421 target.GetHeight() != source.GetHeight()) | |
422 { | |
423 throw OrthancException(ErrorCode_IncompatibleImageSize); | |
424 } | |
425 | |
426 if (target.GetFormat() != source.GetFormat()) | |
427 { | |
428 throw OrthancException(ErrorCode_IncompatibleImageFormat); | |
429 } | |
430 | |
2902
e80b38fb22c6
fix ImageProcessing::Set() for subregions
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2895
diff
changeset
|
431 unsigned int lineSize = source.GetBytesPerPixel() * source.GetWidth(); |
853 | 432 |
433 assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize); | |
434 | |
435 for (unsigned int y = 0; y < source.GetHeight(); y++) | |
436 { | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
437 memcpy(target.GetRow(y), source.GetConstRow(y), lineSize); |
853 | 438 } |
439 } | |
440 | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
441 |
853 | 442 void ImageProcessing::Convert(ImageAccessor& target, |
443 const ImageAccessor& source) | |
444 { | |
445 if (target.GetWidth() != source.GetWidth() || | |
446 target.GetHeight() != source.GetHeight()) | |
447 { | |
448 throw OrthancException(ErrorCode_IncompatibleImageSize); | |
449 } | |
450 | |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
451 const unsigned int width = source.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
452 const unsigned int height = source.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
453 |
853 | 454 if (source.GetFormat() == target.GetFormat()) |
455 { | |
456 Copy(target, source); | |
457 return; | |
458 } | |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
459 |
859
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
460 if (target.GetFormat() == PixelFormat_Grayscale16 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
461 source.GetFormat() == PixelFormat_Grayscale8) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
462 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
463 ConvertInternal<uint16_t, uint8_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
464 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
465 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
466 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
467 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
468 source.GetFormat() == PixelFormat_Grayscale8) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
469 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
470 ConvertInternal<int16_t, uint8_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
471 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
472 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
473 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
474 if (target.GetFormat() == PixelFormat_Grayscale8 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
475 source.GetFormat() == PixelFormat_Grayscale16) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
476 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
477 ConvertInternal<uint8_t, uint16_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
478 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
479 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
480 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
481 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
482 source.GetFormat() == PixelFormat_Grayscale16) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
483 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
484 ConvertInternal<int16_t, uint16_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
485 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
486 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
487 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
488 if (target.GetFormat() == PixelFormat_Grayscale8 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
489 source.GetFormat() == PixelFormat_SignedGrayscale16) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
490 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
491 ConvertInternal<uint8_t, int16_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
492 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
493 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
494 |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
495 if (target.GetFormat() == PixelFormat_Grayscale16 && |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
496 source.GetFormat() == PixelFormat_SignedGrayscale16) |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
497 { |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
498 ConvertInternal<uint16_t, int16_t>(target, source); |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
499 return; |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
500 } |
610a9a1ed855
ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
854
diff
changeset
|
501 |
993
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
502 if (target.GetFormat() == PixelFormat_Grayscale8 && |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
503 source.GetFormat() == PixelFormat_RGB24) |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
504 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
505 ConvertColorToGrayscale<uint8_t>(target, source); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
506 return; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
507 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
508 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
509 if (target.GetFormat() == PixelFormat_Grayscale16 && |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
510 source.GetFormat() == PixelFormat_RGB24) |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
511 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
512 ConvertColorToGrayscale<uint16_t>(target, source); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
513 return; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
514 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
515 |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
516 if (target.GetFormat() == PixelFormat_SignedGrayscale16 && |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
517 source.GetFormat() == PixelFormat_RGB24) |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
518 { |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
519 ConvertColorToGrayscale<int16_t>(target, source); |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
520 return; |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
521 } |
501880d76474
improvements to GDCM plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
876
diff
changeset
|
522 |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
523 if (target.GetFormat() == PixelFormat_Float32 && |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
524 source.GetFormat() == PixelFormat_Grayscale8) |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
525 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
526 ConvertGrayscaleToFloat<uint8_t>(target, source); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
527 return; |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
528 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
529 |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
530 if (target.GetFormat() == PixelFormat_Float32 && |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
531 source.GetFormat() == PixelFormat_Grayscale16) |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
532 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
533 ConvertGrayscaleToFloat<uint16_t>(target, source); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
534 return; |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
535 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
536 |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
537 if (target.GetFormat() == PixelFormat_Float32 && |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
538 source.GetFormat() == PixelFormat_Grayscale32) |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
539 { |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
540 ConvertGrayscaleToFloat<uint32_t>(target, source); |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
541 return; |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
542 } |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
543 |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
544 if (target.GetFormat() == PixelFormat_Float32 && |
1993
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
545 source.GetFormat() == PixelFormat_SignedGrayscale16) |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
546 { |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
547 ConvertGrayscaleToFloat<int16_t>(target, source); |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
548 return; |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
549 } |
e2a3ff770b48
introducing float32 images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1992
diff
changeset
|
550 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
551 |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
552 if (target.GetFormat() == PixelFormat_Grayscale8 && |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
553 source.GetFormat() == PixelFormat_RGBA32) |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
554 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
555 for (unsigned int y = 0; y < height; y++) |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
556 { |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
557 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
|
558 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
|
559 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
|
560 { |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
561 *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
|
562 7152 * static_cast<uint32_t>(p[1]) + |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
563 0722 * static_cast<uint32_t>(p[2])) / 10000); |
1610
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
564 p += 4; |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
565 } |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
566 } |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
567 |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
568 return; |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
569 } |
2dff2bdffdb8
font support within Orthanc
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1608
diff
changeset
|
570 |
2840
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
571 if (target.GetFormat() == PixelFormat_Grayscale8 && |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
572 source.GetFormat() == PixelFormat_BGRA32) |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
573 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
574 for (unsigned int y = 0; y < height; y++) |
2840
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
575 { |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
576 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
|
577 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
|
578 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
|
579 { |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
580 *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
|
581 7152 * static_cast<uint32_t>(p[1]) + |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
582 0722 * static_cast<uint32_t>(p[0])) / 10000); |
2840
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
583 p += 4; |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
584 } |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
585 } |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
586 |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
587 return; |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
588 } |
f4c232bba1eb
bgra32 to grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2783
diff
changeset
|
589 |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
590 if (target.GetFormat() == PixelFormat_RGB24 && |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
591 source.GetFormat() == PixelFormat_RGBA32) |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
592 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
593 for (unsigned int y = 0; y < height; y++) |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
594 { |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
595 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
596 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
|
597 for (unsigned int x = 0; x < width; x++) |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
598 { |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
599 q[0] = p[0]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
600 q[1] = p[1]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
601 q[2] = p[2]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
602 p += 4; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
603 q += 3; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
604 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
605 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
606 |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
607 return; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
608 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
609 |
2252
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
610 if (target.GetFormat() == PixelFormat_RGB24 && |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
611 source.GetFormat() == PixelFormat_BGRA32) |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
612 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
613 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
|
614 { |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
615 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
|
616 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
|
617 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
|
618 { |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
619 q[0] = p[2]; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
620 q[1] = p[1]; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
621 q[2] = p[0]; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
622 p += 4; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
623 q += 3; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
624 } |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
625 } |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
626 |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
627 return; |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
628 } |
002b94046c69
colorspace conversion from BGRA32 to RGB24
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
629 |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
630 if (target.GetFormat() == PixelFormat_RGBA32 && |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
631 source.GetFormat() == PixelFormat_RGB24) |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
632 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
633 for (unsigned int y = 0; y < height; y++) |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
634 { |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
635 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y)); |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
636 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
|
637 for (unsigned int x = 0; x < width; x++) |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
638 { |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
639 q[0] = p[0]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
640 q[1] = p[1]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
641 q[2] = p[2]; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
642 q[3] = 255; // Set the alpha channel to full opacity |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
643 p += 3; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
644 q += 4; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
645 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
646 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
647 |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
648 return; |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
649 } |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1334
diff
changeset
|
650 |
1992
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
651 if (target.GetFormat() == PixelFormat_RGB24 && |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
652 source.GetFormat() == PixelFormat_Grayscale8) |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
653 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
654 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
|
655 { |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
656 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
|
657 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
|
658 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
|
659 { |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
660 q[0] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
661 q[1] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
662 q[2] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
663 p += 1; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
664 q += 3; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
665 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
666 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
667 |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
668 return; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
669 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
670 |
2650 | 671 if ((target.GetFormat() == PixelFormat_RGBA32 || |
672 target.GetFormat() == PixelFormat_BGRA32) && | |
1992
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
673 source.GetFormat() == PixelFormat_Grayscale8) |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
674 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
675 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
|
676 { |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
677 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
|
678 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
|
679 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
|
680 { |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
681 q[0] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
682 q[1] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
683 q[2] = *p; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
684 q[3] = 255; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
685 p += 1; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
686 q += 4; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
687 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
688 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
689 |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
690 return; |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
691 } |
9161e3ef0d17
new conversions in ImageProcessing::Convert
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
692 |
2100
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
693 if (target.GetFormat() == PixelFormat_BGRA32 && |
2495
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
694 source.GetFormat() == PixelFormat_Grayscale16) |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
695 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
696 for (unsigned int y = 0; y < height; y++) |
2495
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
697 { |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
698 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
|
699 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
|
700 for (unsigned int x = 0; x < width; x++) |
2495
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
701 { |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
702 uint8_t value = (*p < 256 ? *p : 255); |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
703 q[0] = value; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
704 q[1] = value; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
705 q[2] = value; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
706 q[3] = 255; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
707 p += 1; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
708 q += 4; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
709 } |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
710 } |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
711 |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
712 return; |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
713 } |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
714 |
cd7b854dbc05
convert grayscale16 to bgra32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2491
diff
changeset
|
715 if (target.GetFormat() == PixelFormat_BGRA32 && |
2496
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
716 source.GetFormat() == PixelFormat_SignedGrayscale16) |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
717 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
718 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
|
719 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
720 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
|
721 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
|
722 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
|
723 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
724 uint8_t value; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
725 if (*p < 0) |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
726 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
727 value = 0; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
728 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
729 else if (*p > 255) |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
730 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
731 value = 255; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
732 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
733 else |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
734 { |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
735 value = static_cast<uint8_t>(*p); |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
736 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
737 |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
738 q[0] = value; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
739 q[1] = value; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
740 q[2] = value; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
741 q[3] = 255; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
742 p += 1; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
743 q += 4; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
744 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
745 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
746 |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
747 return; |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
748 } |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
749 |
3d65adee289a
int16_t to rgba32 conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2495
diff
changeset
|
750 if (target.GetFormat() == PixelFormat_BGRA32 && |
2100
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
751 source.GetFormat() == PixelFormat_RGB24) |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
752 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
753 for (unsigned int y = 0; y < height; y++) |
2100
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
754 { |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
755 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
|
756 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
|
757 for (unsigned int x = 0; x < width; x++) |
2100
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
758 { |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
759 q[0] = p[2]; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
760 q[1] = p[1]; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
761 q[2] = p[0]; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
762 q[3] = 255; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
763 p += 3; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
764 q += 4; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
765 } |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
766 } |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
767 |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
768 return; |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
769 } |
1554fc153a93
conversion RGB24 to BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2089
diff
changeset
|
770 |
2423
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
771 if (target.GetFormat() == PixelFormat_RGB24 && |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
772 source.GetFormat() == PixelFormat_RGB48) |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
773 { |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
774 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
|
775 { |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
776 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
|
777 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
|
778 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
|
779 { |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
780 q[0] = p[0] >> 8; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
781 q[1] = p[1] >> 8; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
782 q[2] = p[2] >> 8; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
783 p += 3; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
784 q += 3; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
785 } |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
786 } |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
787 |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
788 return; |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
789 } |
5a7c5c541a1d
Built-in decoding of palette images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2415
diff
changeset
|
790 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
791 if (target.GetFormat() == PixelFormat_Grayscale16 && |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
792 source.GetFormat() == PixelFormat_Float32) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
793 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
794 ConvertFloatToGrayscale<PixelFormat_Grayscale16>(target, source); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
795 return; |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
796 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
797 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
798 if (target.GetFormat() == PixelFormat_Grayscale8 && |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
799 source.GetFormat() == PixelFormat_Float32) |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
800 { |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
801 ConvertFloatToGrayscale<PixelFormat_Grayscale8>(target, source); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
802 return; |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
803 } |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
804 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
805 throw OrthancException(ErrorCode_NotImplemented); |
853 | 806 } |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
807 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
808 |
863 | 809 |
810 void ImageProcessing::Set(ImageAccessor& image, | |
811 int64_t value) | |
812 { | |
813 switch (image.GetFormat()) | |
814 { | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
815 case PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
816 SetInternal<uint8_t>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
817 return; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
818 |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
819 case PixelFormat_Grayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
820 SetInternal<uint16_t>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
821 return; |
863 | 822 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
823 case PixelFormat_Grayscale32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
824 SetInternal<uint32_t>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
825 return; |
863 | 826 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
827 case PixelFormat_Grayscale64: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
828 SetInternal<uint64_t>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
829 return; |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
830 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
831 case PixelFormat_SignedGrayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
832 SetInternal<int16_t>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
833 return; |
2645
89b789366596
Grayscale64 pixel format
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2496
diff
changeset
|
834 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
835 case PixelFormat_Float32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
836 assert(sizeof(float) == 4); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
837 SetInternal<float>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
838 return; |
863 | 839 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
840 case PixelFormat_RGBA32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
841 case PixelFormat_BGRA32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
842 case PixelFormat_RGB24: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
843 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
844 uint8_t v = static_cast<uint8_t>(value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
845 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
|
846 return; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
847 } |
1994
4d099fee5eca
ImageProcessing::Set for float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1993
diff
changeset
|
848 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
849 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
850 throw OrthancException(ErrorCode_NotImplemented); |
863 | 851 } |
852 } | |
853 | |
854 | |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
855 void ImageProcessing::Set(ImageAccessor& image, |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
856 uint8_t red, |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
857 uint8_t green, |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
858 uint8_t blue, |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
859 uint8_t alpha) |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
860 { |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
861 uint8_t p[4]; |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
862 unsigned int size; |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
863 |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
864 switch (image.GetFormat()) |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
865 { |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
866 case PixelFormat_RGBA32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
867 p[0] = red; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
868 p[1] = green; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
869 p[2] = blue; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
870 p[3] = alpha; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
871 size = 4; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
872 break; |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
873 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
874 case PixelFormat_BGRA32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
875 p[0] = blue; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
876 p[1] = green; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
877 p[2] = red; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
878 p[3] = alpha; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
879 size = 4; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
880 break; |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
881 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
882 case PixelFormat_RGB24: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
883 p[0] = red; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
884 p[1] = green; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
885 p[2] = blue; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
886 size = 3; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
887 break; |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
888 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
889 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
890 throw OrthancException(ErrorCode_NotImplemented); |
3227 | 891 } |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
892 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
893 const unsigned int width = image.GetWidth(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
894 const unsigned int height = image.GetHeight(); |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
895 |
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
896 for (unsigned int y = 0; y < height; y++) |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
897 { |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
898 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y)); |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
899 |
2904
0dd54ee073db
float to integer grayscale conversion
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2902
diff
changeset
|
900 for (unsigned int x = 0; x < width; x++) |
2089
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
901 { |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
902 for (unsigned int i = 0; i < size; i++) |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
903 { |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
904 q[i] = p[i]; |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
905 } |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
906 |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
907 q += size; |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
908 } |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
909 } |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
910 } |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
911 |
7a969f235adf
PixelFormat_BGRA32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1994
diff
changeset
|
912 |
854
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
913 void ImageProcessing::ShiftRight(ImageAccessor& image, |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
914 unsigned int shift) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
915 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
916 if (image.GetWidth() == 0 || |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
917 image.GetHeight() == 0 || |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
918 shift == 0) |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
919 { |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
920 // Nothing to do |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
921 return; |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
922 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
923 |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
924 throw OrthancException(ErrorCode_NotImplemented); |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
925 } |
ff530685e46a
fast version of image copy
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
853
diff
changeset
|
926 |
863 | 927 |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
928 void ImageProcessing::GetMinMaxIntegerValue(int64_t& minValue, |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
929 int64_t& maxValue, |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
930 const ImageAccessor& image) |
863 | 931 { |
932 switch (image.GetFormat()) | |
933 { | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
934 case PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
935 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
936 uint8_t a, b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
937 GetMinMaxValueInternal<uint8_t>(a, b, image); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
938 minValue = a; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
939 maxValue = b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
940 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
941 } |
863 | 942 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
943 case PixelFormat_Grayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
944 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
945 uint16_t a, b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
946 GetMinMaxValueInternal<uint16_t>(a, b, image); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
947 minValue = a; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
948 maxValue = b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
949 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
950 } |
863 | 951 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
952 case PixelFormat_Grayscale32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
953 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
954 uint32_t a, b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
955 GetMinMaxValueInternal<uint32_t>(a, b, image); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
956 minValue = a; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
957 maxValue = b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
958 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
959 } |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
960 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
961 case PixelFormat_SignedGrayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
962 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
963 int16_t a, b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
964 GetMinMaxValueInternal<int16_t>(a, b, image); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
965 minValue = a; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
966 maxValue = b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
967 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
968 } |
863 | 969 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
970 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
971 throw OrthancException(ErrorCode_NotImplemented); |
863 | 972 } |
973 } | |
974 | |
975 | |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
976 void ImageProcessing::GetMinMaxFloatValue(float& minValue, |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
977 float& maxValue, |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
978 const ImageAccessor& image) |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
979 { |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
980 switch (image.GetFormat()) |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
981 { |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
982 case PixelFormat_Float32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
983 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
984 assert(sizeof(float) == 4); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
985 float a, b; |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
986 |
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
987 /** |
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
988 * 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
|
989 * "-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
|
990 * (as implemented by "::min()") |
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
991 * 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
|
992 **/ |
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
993 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
|
994 minValue = a; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
995 maxValue = b; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
996 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
997 } |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
998 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
999 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1000 throw OrthancException(ErrorCode_NotImplemented); |
2415
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
1001 } |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
1002 } |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
1003 |
7e217a1cc63f
PixelFormat_Float32
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2281
diff
changeset
|
1004 |
863 | 1005 |
1006 void ImageProcessing::AddConstant(ImageAccessor& image, | |
1007 int64_t value) | |
1008 { | |
1009 switch (image.GetFormat()) | |
1010 { | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1011 case PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1012 AddConstantInternal<uint8_t>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1013 return; |
863 | 1014 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1015 case PixelFormat_Grayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1016 AddConstantInternal<uint16_t>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1017 return; |
863 | 1018 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1019 case PixelFormat_SignedGrayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1020 AddConstantInternal<int16_t>(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1021 return; |
863 | 1022 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1023 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1024 throw OrthancException(ErrorCode_NotImplemented); |
863 | 1025 } |
1026 } | |
1027 | |
1028 | |
1029 void ImageProcessing::MultiplyConstant(ImageAccessor& image, | |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
1030 float factor, |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
1031 bool useRound) |
863 | 1032 { |
1033 switch (image.GetFormat()) | |
1034 { | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1035 case PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1036 if (useRound) |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1037 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1038 MultiplyConstantInternal<uint8_t, true>(image, factor); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1039 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1040 else |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1041 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1042 MultiplyConstantInternal<uint8_t, false>(image, factor); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1043 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1044 return; |
863 | 1045 |
3499
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 if (useRound) |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1048 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1049 MultiplyConstantInternal<uint16_t, true>(image, factor); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1050 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1051 else |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1052 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1053 MultiplyConstantInternal<uint16_t, false>(image, factor); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1054 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1055 return; |
863 | 1056 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1057 case PixelFormat_SignedGrayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1058 if (useRound) |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1059 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1060 MultiplyConstantInternal<int16_t, true>(image, factor); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1061 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1062 else |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1063 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1064 MultiplyConstantInternal<int16_t, false>(image, factor); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1065 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1066 return; |
863 | 1067 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1068 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1069 throw OrthancException(ErrorCode_NotImplemented); |
863 | 1070 } |
1071 } | |
1072 | |
1073 | |
1074 void ImageProcessing::ShiftScale(ImageAccessor& image, | |
1075 float offset, | |
2488
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
1076 float scaling, |
345725b9350c
back to rounding to fix integration tests
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2487
diff
changeset
|
1077 bool useRound) |
863 | 1078 { |
1079 switch (image.GetFormat()) | |
1080 { | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1081 case PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1082 if (useRound) |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1083 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1084 ShiftScaleInternal<uint8_t, true>(image, offset, scaling); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1085 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1086 else |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1087 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1088 ShiftScaleInternal<uint8_t, false>(image, offset, scaling); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1089 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1090 return; |
863 | 1091 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1092 case PixelFormat_Grayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1093 if (useRound) |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1094 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1095 ShiftScaleInternal<uint16_t, true>(image, offset, scaling); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1096 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1097 else |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1098 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1099 ShiftScaleInternal<uint16_t, false>(image, offset, scaling); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1100 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1101 return; |
863 | 1102 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1103 case PixelFormat_SignedGrayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1104 if (useRound) |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1105 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1106 ShiftScaleInternal<int16_t, true>(image, offset, scaling); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1107 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1108 else |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1109 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1110 ShiftScaleInternal<int16_t, false>(image, offset, scaling); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1111 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1112 return; |
863 | 1113 |
3505
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1114 case PixelFormat_Float32: |
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1115 if (useRound) |
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1116 { |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
1117 ShiftScaleInternal<float, true>(image, offset, scaling, -std::numeric_limits<float>::max()); |
3505
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1118 } |
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1119 else |
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1120 { |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
1121 ShiftScaleInternal<float, false>(image, offset, scaling, -std::numeric_limits<float>::max()); |
3505
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1122 } |
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1123 return; |
b2d4dd16dae8
removed C++11 primitive
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3504
diff
changeset
|
1124 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1125 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1126 throw OrthancException(ErrorCode_NotImplemented); |
863 | 1127 } |
1128 } | |
2281
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1129 |
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1130 |
3227 | 1131 void ImageProcessing::Invert(ImageAccessor& image, int64_t maxValue) |
1132 { | |
1133 const unsigned int width = image.GetWidth(); | |
1134 const unsigned int height = image.GetHeight(); | |
1135 | |
1136 switch (image.GetFormat()) | |
1137 { | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1138 case PixelFormat_Grayscale16: |
3227 | 1139 { |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1140 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
|
1141 |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1142 for (unsigned int y = 0; y < height; y++) |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1143 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1144 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
|
1145 |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1146 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
|
1147 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1148 *p = maxValueUint16 - (*p); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1149 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1150 } |
3227 | 1151 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1152 return; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1153 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1154 case PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1155 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1156 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
|
1157 |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1158 for (unsigned int y = 0; y < height; y++) |
3227 | 1159 { |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1160 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
|
1161 |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1162 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
|
1163 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1164 *p = maxValueUint8 - (*p); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1165 } |
3227 | 1166 } |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1167 |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1168 return; |
3227 | 1169 } |
1170 | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1171 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1172 throw OrthancException(ErrorCode_NotImplemented); |
3227 | 1173 } |
1174 | |
1175 } | |
1176 | |
2281
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1177 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
|
1178 { |
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1179 switch (image.GetFormat()) |
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1180 { |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1181 case PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1182 return Invert(image, 255); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1183 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1184 throw OrthancException(ErrorCode_NotImplemented); // you should use the Invert(image, maxValue) overload |
3227 | 1185 } |
2281
e002430baa41
Fix issue #44 (Bad interpretation of photometric interpretation MONOCHROME1)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2252
diff
changeset
|
1186 } |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1187 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1188 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1189 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1190 namespace |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1191 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1192 template <Orthanc::PixelFormat Format> |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1193 class BresenhamPixelWriter |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1194 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1195 private: |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1196 typedef typename PixelTraits<Format>::PixelType PixelType; |
3227 | 1197 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1198 Orthanc::ImageAccessor& image_; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1199 PixelType value_; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1200 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1201 void PlotLineLow(int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1202 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1203 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1204 int y1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1205 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1206 int dx = x1 - x0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1207 int dy = y1 - y0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1208 int yi = 1; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1209 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1210 if (dy < 0) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1211 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1212 yi = -1; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1213 dy = -dy; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1214 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1215 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1216 int d = 2 * dy - dx; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1217 int y = y0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1218 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1219 for (int x = x0; x <= x1; x++) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1220 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1221 Write(x, y); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1222 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1223 if (d > 0) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1224 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1225 y = y + yi; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1226 d = d - 2 * dx; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1227 } |
3227 | 1228 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1229 d = d + 2*dy; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1230 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1231 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1232 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1233 void PlotLineHigh(int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1234 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1235 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1236 int y1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1237 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1238 int dx = x1 - x0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1239 int dy = y1 - y0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1240 int xi = 1; |
3227 | 1241 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1242 if (dx < 0) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1243 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1244 xi = -1; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1245 dx = -dx; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1246 } |
3227 | 1247 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1248 int d = 2 * dx - dy; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1249 int x = x0; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1250 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1251 for (int y = y0; y <= y1; y++) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1252 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1253 Write(x, y); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1254 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1255 if (d > 0) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1256 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1257 x = x + xi; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1258 d = d - 2 * dy; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1259 } |
3227 | 1260 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1261 d = d + 2 * dx; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1262 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1263 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1264 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1265 public: |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1266 BresenhamPixelWriter(Orthanc::ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1267 int64_t value) : |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1268 image_(image), |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1269 value_(PixelTraits<Format>::IntegerToPixel(value)) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1270 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1271 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1272 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1273 BresenhamPixelWriter(Orthanc::ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1274 const PixelType& value) : |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1275 image_(image), |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1276 value_(value) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1277 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1278 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1279 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1280 void Write(int x, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1281 int y) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1282 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1283 if (x >= 0 && |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1284 y >= 0 && |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1285 static_cast<unsigned int>(x) < image_.GetWidth() && |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1286 static_cast<unsigned int>(y) < image_.GetHeight()) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1287 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1288 PixelType* p = reinterpret_cast<PixelType*>(image_.GetRow(y)); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1289 p[x] = value_; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1290 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1291 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1292 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1293 void DrawSegment(int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1294 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1295 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1296 int y1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1297 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1298 // This is an implementation of Bresenham's line algorithm |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1299 // https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm#All_cases |
3227 | 1300 |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1301 if (abs(y1 - y0) < abs(x1 - x0)) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1302 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1303 if (x0 > x1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1304 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1305 PlotLineLow(x1, y1, x0, y0); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1306 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1307 else |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1308 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1309 PlotLineLow(x0, y0, x1, y1); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1310 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1311 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1312 else |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1313 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1314 if (y0 > y1) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1315 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1316 PlotLineHigh(x1, y1, x0, y0); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1317 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1318 else |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1319 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1320 PlotLineHigh(x0, y0, x1, y1); |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1321 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1322 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1323 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1324 }; |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1325 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1326 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1327 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1328 void ImageProcessing::DrawLineSegment(ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1329 int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1330 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1331 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1332 int y1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1333 int64_t value) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1334 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1335 switch (image.GetFormat()) |
3227 | 1336 { |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1337 case Orthanc::PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1338 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1339 BresenhamPixelWriter<Orthanc::PixelFormat_Grayscale8> writer(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1340 writer.DrawSegment(x0, y0, x1, y1); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1341 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1342 } |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1343 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1344 case Orthanc::PixelFormat_Grayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1345 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1346 BresenhamPixelWriter<Orthanc::PixelFormat_Grayscale16> writer(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1347 writer.DrawSegment(x0, y0, x1, y1); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1348 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1349 } |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1350 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1351 case Orthanc::PixelFormat_SignedGrayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1352 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1353 BresenhamPixelWriter<Orthanc::PixelFormat_SignedGrayscale16> writer(image, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1354 writer.DrawSegment(x0, y0, x1, y1); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1355 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1356 } |
3227 | 1357 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1358 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1359 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1360 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1361 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1362 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1363 |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1364 void ImageProcessing::DrawLineSegment(ImageAccessor& image, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1365 int x0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1366 int y0, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1367 int x1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1368 int y1, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1369 uint8_t red, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1370 uint8_t green, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1371 uint8_t blue, |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1372 uint8_t alpha) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1373 { |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1374 switch (image.GetFormat()) |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1375 { |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1376 case Orthanc::PixelFormat_BGRA32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1377 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1378 PixelTraits<Orthanc::PixelFormat_BGRA32>::PixelType pixel; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1379 pixel.red_ = red; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1380 pixel.green_ = green; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1381 pixel.blue_ = blue; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1382 pixel.alpha_ = alpha; |
3227 | 1383 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1384 BresenhamPixelWriter<Orthanc::PixelFormat_BGRA32> writer(image, pixel); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1385 writer.DrawSegment(x0, y0, x1, y1); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1386 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1387 } |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1388 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1389 case Orthanc::PixelFormat_RGBA32: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1390 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1391 PixelTraits<Orthanc::PixelFormat_RGBA32>::PixelType pixel; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1392 pixel.red_ = red; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1393 pixel.green_ = green; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1394 pixel.blue_ = blue; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1395 pixel.alpha_ = alpha; |
3227 | 1396 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1397 BresenhamPixelWriter<Orthanc::PixelFormat_RGBA32> writer(image, pixel); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1398 writer.DrawSegment(x0, y0, x1, y1); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1399 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1400 } |
2783
65699fcb4e99
PixelTraits<PixelFormat_RGBA32>
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2650
diff
changeset
|
1401 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1402 case Orthanc::PixelFormat_RGB24: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1403 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1404 PixelTraits<Orthanc::PixelFormat_RGB24>::PixelType pixel; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1405 pixel.red_ = red; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1406 pixel.green_ = green; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1407 pixel.blue_ = blue; |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1408 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1409 BresenhamPixelWriter<Orthanc::PixelFormat_RGB24> writer(image, pixel); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1410 writer.DrawSegment(x0, y0, x1, y1); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1411 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1412 } |
3227 | 1413 |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1414 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1415 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
2489
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1416 } |
e91bab2d8c75
Bresenham's line algorithm
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2488
diff
changeset
|
1417 } |
3258 | 1418 |
1419 void ComputePolygonExtent(int32_t& left, int32_t& right, int32_t& top, int32_t& bottom, const std::vector<ImageProcessing::ImagePoint>& points) | |
1420 { | |
1421 left = std::numeric_limits<int32_t>::max(); | |
1422 right = std::numeric_limits<int32_t>::min(); | |
1423 top = std::numeric_limits<int32_t>::max(); | |
1424 bottom = std::numeric_limits<int32_t>::min(); | |
1425 | |
1426 for (size_t i = 0; i < points.size(); i++) | |
1427 { | |
1428 const ImageProcessing::ImagePoint& p = points[i]; | |
1429 left = std::min(p.GetX(), left); | |
1430 right = std::max(p.GetX(), right); | |
1431 bottom = std::max(p.GetY(), bottom); | |
1432 top = std::min(p.GetY(), top); | |
1433 } | |
1434 } | |
1435 | |
1436 template <PixelFormat TargetFormat> | |
1437 void FillPolygon_(ImageAccessor& image, | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1438 const std::vector<ImageProcessing::ImagePoint>& points, |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1439 int64_t value_) |
3258 | 1440 { |
1441 typedef typename PixelTraits<TargetFormat>::PixelType TargetType; | |
1442 | |
1443 TargetType value = PixelTraits<TargetFormat>::IntegerToPixel(value_); | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1444 int imageWidth = static_cast<int>(image.GetWidth()); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1445 int imageHeight = static_cast<int>(image.GetHeight()); |
3258 | 1446 int32_t left; |
1447 int32_t right; | |
1448 int32_t top; | |
1449 int32_t bottom; | |
1450 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1451 // TODO: test clipping in UT (in Trello board) |
3258 | 1452 ComputePolygonExtent(left, right, top, bottom, points); |
1453 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1454 // clip the computed extent with the target image |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1455 // L and R |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1456 left = std::max(0, left); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1457 left = std::min(imageWidth, left); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1458 right = std::max(0, right); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1459 right = std::min(imageWidth, right); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1460 if (left > right) |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1461 std::swap(left, right); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1462 |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1463 // T and B |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1464 top = std::max(0, top); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1465 top = std::min(imageHeight, top); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1466 bottom = std::max(0, bottom); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1467 bottom = std::min(imageHeight, bottom); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1468 if (top > bottom) |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1469 std::swap(top, bottom); |
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1470 |
3258 | 1471 // from http://alienryderflex.com/polygon_fill/ |
1472 | |
1473 // convert all "corner" points to double only once | |
1474 std::vector<double> cpx; | |
1475 std::vector<double> cpy; | |
1476 size_t cpSize = points.size(); | |
1477 for (size_t i = 0; i < points.size(); i++) | |
1478 { | |
3365 | 1479 if (points[i].GetX() < 0 || points[i].GetX() >= imageWidth |
1480 || points[i].GetY() < 0 || points[i].GetY() >= imageHeight) | |
1481 { | |
1482 throw Orthanc::OrthancException(ErrorCode_ParameterOutOfRange); | |
1483 } | |
3258 | 1484 cpx.push_back((double)points[i].GetX()); |
1485 cpy.push_back((double)points[i].GetY()); | |
1486 } | |
1487 | |
3431 | 1488 // Draw the lines segments |
1489 for (size_t i = 0; i < (points.size() -1); i++) | |
1490 { | |
1491 ImageProcessing::DrawLineSegment(image, points[i].GetX(), points[i].GetY(), points[i+1].GetX(), points[i+1].GetY(), value_); | |
1492 } | |
1493 ImageProcessing::DrawLineSegment(image, points[points.size() -1].GetX(), points[points.size() -1].GetY(), points[0].GetX(), points[0].GetY(), value_); | |
1494 | |
3258 | 1495 std::vector<int32_t> nodeX; |
1496 nodeX.resize(cpSize); | |
1497 int nodes, pixelX, pixelY, i, j, swap ; | |
1498 | |
1499 // Loop through the rows of the image. | |
1500 for (pixelY = top; pixelY < bottom; pixelY++) | |
1501 { | |
1502 double y = (double)pixelY; | |
1503 // Build a list of nodes. | |
1504 nodes = 0; | |
1505 j = static_cast<int>(cpSize) - 1; | |
1506 | |
3259
6f9398eb902d
unit test Toolbox.SubstituteVariables
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3258
diff
changeset
|
1507 for (i = 0; i < static_cast<int>(cpSize); i++) |
3258 | 1508 { |
1509 if ((cpy[i] < y && cpy[j] >= y) || (cpy[j] < y && cpy[i] >= y)) | |
1510 { | |
1511 nodeX[nodes++] = (int32_t)(cpx[i] + (y - cpy[i])/(cpy[j] - cpy[i]) * (cpx[j] - cpx[i])); | |
1512 } | |
1513 j=i; | |
1514 } | |
1515 | |
1516 // Sort the nodes, via a simple “Bubble” sort. | |
1517 i=0; | |
1518 while (i < nodes-1) | |
1519 { | |
1520 if (nodeX[i] > nodeX[i+1]) | |
1521 { | |
1522 swap = nodeX[i]; | |
1523 nodeX[i] = nodeX[i+1]; | |
1524 nodeX[i+1] = swap; | |
1525 if (i > 0) | |
1526 { | |
1527 i--; | |
1528 } | |
1529 } | |
1530 else | |
1531 { | |
1532 i++; | |
1533 } | |
1534 } | |
1535 | |
1536 TargetType* row = reinterpret_cast<TargetType*>(image.GetRow(pixelY)); | |
1537 // Fill the pixels between node pairs. | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1538 for (i = 0; i < nodes; i += 2) |
3258 | 1539 { |
1540 if (nodeX[i] >= right) | |
1541 break; | |
1542 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1543 if (nodeX[i + 1] >= left) |
3258 | 1544 { |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1545 if (nodeX[i] < left) |
3258 | 1546 { |
1547 nodeX[i] = left; | |
1548 } | |
1549 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1550 if (nodeX[i + 1] > right) |
3258 | 1551 { |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1552 nodeX[i + 1] = right; |
3258 | 1553 } |
1554 | |
3323
a15a4b9d8c00
Added image clipping to <T> FillPolygon_ to prevent out-of-memory access
Benjamin Golinvaux <bgo@osimis.io>
parents:
3265
diff
changeset
|
1555 for (pixelX = nodeX[i]; pixelX <= nodeX[i + 1]; pixelX++) |
3258 | 1556 { |
1557 *(row + pixelX) = value; | |
1558 } | |
1559 } | |
1560 } | |
1561 } | |
1562 } | |
1563 | |
1564 void ImageProcessing::FillPolygon(ImageAccessor& image, | |
1565 const std::vector<ImagePoint>& points, | |
1566 int64_t value) | |
1567 { | |
1568 switch (image.GetFormat()) | |
1569 { | |
3499
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1570 case Orthanc::PixelFormat_Grayscale8: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1571 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1572 FillPolygon_<Orthanc::PixelFormat_Grayscale8>(image, points, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1573 break; |
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 case Orthanc::PixelFormat_Grayscale16: |
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 FillPolygon_<Orthanc::PixelFormat_Grayscale16>(image, points, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1578 break; |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1579 } |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1580 case Orthanc::PixelFormat_SignedGrayscale16: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1581 { |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1582 FillPolygon_<Orthanc::PixelFormat_SignedGrayscale16>(image, points, value); |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1583 break; |
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 default: |
d8f7c3970e25
more tolerance in ImageProcessing::Set()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3431
diff
changeset
|
1586 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
3258 | 1587 } |
1588 } | |
3502
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1589 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1590 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1591 template <PixelFormat Format> |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1592 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
|
1593 const ImageAccessor& source) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1594 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1595 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
|
1596 target.GetFormat() == Format); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1597 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1598 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
|
1599 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
|
1600 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
|
1601 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
|
1602 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1603 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
|
1604 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1605 return; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1606 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1607 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1608 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
|
1609 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1610 // 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
|
1611 ImageProcessing::Set(target, 0); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1612 return; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1613 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1614 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1615 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
|
1616 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
|
1617 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1618 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1619 /** |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1620 * 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
|
1621 * 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
|
1622 * image. |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1623 **/ |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1624 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1625 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
|
1626 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1627 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
|
1628 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1629 int sourceX = std::floor((static_cast<float>(x) + 0.5f) * scaleX); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1630 if (sourceX < 0) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1631 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1632 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
|
1633 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1634 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
|
1635 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1636 sourceX = sourceWidth - 1; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1637 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1638 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1639 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
|
1640 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1641 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1642 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
|
1643 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1644 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
|
1645 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1646 int sourceY = std::floor((static_cast<float>(y) + 0.5f) * scaleY); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1647 if (sourceY < 0) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1648 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1649 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
|
1650 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1651 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
|
1652 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1653 sourceY = sourceHeight - 1; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1654 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1655 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1656 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
|
1657 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1658 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1659 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1660 /** |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1661 * Actual resizing |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1662 **/ |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1663 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1664 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
|
1665 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1666 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
|
1667 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1668 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
|
1669 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1670 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
|
1671 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1672 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
|
1673 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
|
1674 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
|
1675 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1676 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1677 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1678 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1679 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1680 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1681 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
|
1682 const ImageAccessor& source) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1683 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1684 if (source.GetFormat() != source.GetFormat()) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1685 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1686 throw OrthancException(ErrorCode_IncompatibleImageFormat); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1687 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1688 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1689 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
|
1690 source.GetHeight() == target.GetHeight()) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1691 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1692 Copy(target, source); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1693 return; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1694 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1695 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1696 switch (source.GetFormat()) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1697 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1698 case PixelFormat_Grayscale8: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1699 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
|
1700 break; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1701 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1702 case PixelFormat_RGB24: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1703 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
|
1704 break; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1705 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1706 default: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1707 throw OrthancException(ErrorCode_NotImplemented); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1708 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1709 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1710 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1711 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1712 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
|
1713 bool forceMinimalPitch) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1714 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1715 std::auto_ptr<Image> target(new Image(source.GetFormat(), source.GetWidth() / 2, |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1716 source.GetHeight() / 2, forceMinimalPitch)); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1717 Resize(*target, source); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1718 return target.release(); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1719 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1720 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1721 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1722 template <PixelFormat Format> |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1723 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
|
1724 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1725 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
|
1726 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
|
1727 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1728 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
|
1729 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1730 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
|
1731 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1732 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
|
1733 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1734 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
|
1735 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
|
1736 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
|
1737 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
|
1738 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
|
1739 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1740 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1741 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1742 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1743 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1744 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
|
1745 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1746 switch (image.GetFormat()) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1747 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1748 case PixelFormat_Grayscale8: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1749 FlipXInternal<PixelFormat_Grayscale8>(image); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1750 break; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1751 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1752 case PixelFormat_RGB24: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1753 FlipXInternal<PixelFormat_RGB24>(image); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1754 break; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1755 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1756 default: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1757 throw OrthancException(ErrorCode_NotImplemented); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1758 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1759 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1760 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1761 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1762 template <PixelFormat Format> |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1763 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
|
1764 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1765 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
|
1766 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
|
1767 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1768 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
|
1769 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1770 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
|
1771 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1772 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
|
1773 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1774 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
|
1775 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
|
1776 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
|
1777 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
|
1778 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
|
1779 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1780 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1781 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1782 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1783 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1784 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
|
1785 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1786 switch (image.GetFormat()) |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1787 { |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1788 case PixelFormat_Grayscale8: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1789 FlipYInternal<PixelFormat_Grayscale8>(image); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1790 break; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1791 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1792 case PixelFormat_RGB24: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1793 FlipYInternal<PixelFormat_RGB24>(image); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1794 break; |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1795 |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1796 default: |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1797 throw OrthancException(ErrorCode_NotImplemented); |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1798 } |
c160eafc42a9
new functions in ImageProcessing toolbox: FlipX/Y(), Resize(), Halve()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3499
diff
changeset
|
1799 } |
3503
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1800 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1801 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1802 // This is a slow implementation of horizontal convolution on one |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1803 // individual channel, that checks for out-of-image values |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1804 template <typename RawPixel, unsigned int ChannelsCount> |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1805 static float GetHorizontalConvolutionFloatSecure(const Orthanc::ImageAccessor& source, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1806 const std::vector<float>& horizontal, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1807 size_t horizontalAnchor, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1808 unsigned int x, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1809 unsigned int y, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1810 float leftBorder, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1811 float rightBorder, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1812 unsigned int channel) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1813 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1814 const RawPixel* row = reinterpret_cast<const RawPixel*>(source.GetConstRow(y)) + channel; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1815 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1816 float p = 0; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1817 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1818 for (unsigned int k = 0; k < horizontal.size(); k++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1819 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1820 float value; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1821 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1822 if (x + k < horizontalAnchor) // Negation of "x - horizontalAnchor + k >= 0" |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1823 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1824 value = leftBorder; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1825 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1826 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
|
1827 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1828 value = rightBorder; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1829 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1830 else |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1831 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1832 // The value lies within the image |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1833 value = row[(x - horizontalAnchor + k) * ChannelsCount]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1834 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1835 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1836 p += value * horizontal[k]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1837 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1838 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1839 return p; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1840 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1841 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1842 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1843 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1844 // This is an implementation of separable convolution that uses |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1845 // floating-point arithmetics, and an intermediate Float32 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1846 // image. The out-of-image values are taken as the border |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1847 // value. Further optimization is possible. |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1848 template <typename RawPixel, unsigned int ChannelsCount> |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1849 static void SeparableConvolutionFloat(ImageAccessor& image /* inplace */, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1850 const std::vector<float>& horizontal, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1851 size_t horizontalAnchor, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1852 const std::vector<float>& vertical, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1853 size_t verticalAnchor, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1854 float normalization) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1855 { |
3525
8c66c9c2257b
fix ImageProcessing::ShiftScale() on float images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3505
diff
changeset
|
1856 // 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
|
1857 // 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
|
1858 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
|
1859 |
3503
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1860 const unsigned int width = image.GetWidth(); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1861 const unsigned int height = image.GetHeight(); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1862 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1863 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1864 /** |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1865 * Horizontal convolution |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1866 **/ |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1867 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1868 Image tmp(PixelFormat_Float32, ChannelsCount * width, height, false); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1869 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1870 for (unsigned int y = 0; y < height; y++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1871 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1872 const RawPixel* row = reinterpret_cast<const RawPixel*>(image.GetConstRow(y)); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1873 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1874 float leftBorder[ChannelsCount], rightBorder[ChannelsCount]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1875 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1876 for (unsigned int c = 0; c < ChannelsCount; c++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1877 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1878 leftBorder[c] = row[c]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1879 rightBorder[c] = row[ChannelsCount * (width - 1) + c]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1880 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1881 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1882 float* p = static_cast<float*>(tmp.GetRow(y)); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1883 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1884 if (width < horizontal.size()) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1885 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1886 // 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
|
1887 for (unsigned int x = 0; x < width; x++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1888 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1889 for (unsigned int c = 0; c < ChannelsCount; c++, p++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1890 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1891 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount> |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1892 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1893 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1894 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1895 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1896 else |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1897 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1898 // Deal with the left border |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1899 for (unsigned int x = 0; x < horizontalAnchor; x++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1900 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1901 for (unsigned int c = 0; c < ChannelsCount; c++, p++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1902 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1903 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount> |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1904 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1905 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1906 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1907 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1908 // Deal with the central portion of the image (all pixel values |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1909 // scanned by the kernel lie inside the image) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1910 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1911 for (unsigned int x = 0; x < width - horizontal.size() + 1; x++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1912 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1913 for (unsigned int c = 0; c < ChannelsCount; c++, p++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1914 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1915 *p = 0; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1916 for (unsigned int k = 0; k < horizontal.size(); k++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1917 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1918 *p += static_cast<float>(row[(x + k) * ChannelsCount + c]) * horizontal[k]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1919 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1920 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1921 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1922 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1923 // Deal with the right border |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1924 for (unsigned int x = horizontalAnchor + width - horizontal.size() + 1; x < width; x++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1925 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1926 for (unsigned int c = 0; c < ChannelsCount; c++, p++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1927 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1928 *p = GetHorizontalConvolutionFloatSecure<RawPixel, ChannelsCount> |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1929 (image, horizontal, horizontalAnchor, x, y, leftBorder[c], rightBorder[c], c); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1930 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1931 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1932 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1933 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1934 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1935 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1936 /** |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1937 * Vertical convolution |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1938 **/ |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1939 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1940 std::vector<const float*> rows(vertical.size()); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1941 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1942 for (unsigned int y = 0; y < height; y++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1943 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1944 for (unsigned int k = 0; k < vertical.size(); k++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1945 { |
3504
18566f9e1831
unit testing new functions in ImageProcessing
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3503
diff
changeset
|
1946 if (y + k < verticalAnchor) |
3503
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1947 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1948 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
|
1949 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1950 else if (y + k >= height + verticalAnchor) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1951 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1952 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
|
1953 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1954 else |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1955 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1956 rows[k] = reinterpret_cast<const float*>(tmp.GetConstRow(y + k - verticalAnchor)); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1957 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1958 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1959 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1960 RawPixel* p = reinterpret_cast<RawPixel*>(image.GetRow(y)); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1961 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1962 for (unsigned int x = 0; x < width; x++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1963 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1964 for (unsigned int c = 0; c < ChannelsCount; c++, p++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1965 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1966 float accumulator = 0; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1967 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1968 for (unsigned int k = 0; k < vertical.size(); k++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1969 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1970 accumulator += rows[k][ChannelsCount * x + c] * vertical[k]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1971 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1972 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1973 accumulator *= normalization; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1974 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1975 if (accumulator <= static_cast<float>(std::numeric_limits<RawPixel>::min())) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1976 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1977 *p = std::numeric_limits<RawPixel>::min(); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1978 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1979 else if (accumulator >= static_cast<float>(std::numeric_limits<RawPixel>::max())) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1980 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1981 *p = std::numeric_limits<RawPixel>::max(); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1982 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1983 else |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1984 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1985 *p = static_cast<RawPixel>(accumulator); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1986 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1987 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1988 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1989 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1990 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1991 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1992 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1993 void ImageProcessing::SeparableConvolution(ImageAccessor& image /* inplace */, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1994 const std::vector<float>& horizontal, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1995 size_t horizontalAnchor, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1996 const std::vector<float>& vertical, |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1997 size_t verticalAnchor) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1998 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
1999 if (horizontal.size() == 0 || |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2000 vertical.size() == 0 || |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2001 horizontalAnchor >= horizontal.size() || |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2002 verticalAnchor >= vertical.size()) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2003 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2004 throw OrthancException(ErrorCode_ParameterOutOfRange); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2005 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2006 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2007 if (image.GetWidth() == 0 || |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2008 image.GetHeight() == 0) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2009 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2010 return; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2011 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2012 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2013 /** |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2014 * Compute normalization |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2015 **/ |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2016 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2017 float sumHorizontal = 0; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2018 for (size_t i = 0; i < horizontal.size(); i++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2019 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2020 sumHorizontal += horizontal[i]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2021 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2022 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2023 float sumVertical = 0; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2024 for (size_t i = 0; i < vertical.size(); i++) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2025 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2026 sumVertical += vertical[i]; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2027 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2028 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2029 if (fabsf(sumHorizontal) <= std::numeric_limits<float>::epsilon() || |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2030 fabsf(sumVertical) <= std::numeric_limits<float>::epsilon()) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2031 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2032 throw OrthancException(ErrorCode_ParameterOutOfRange, "Singular convolution kernel"); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2033 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2034 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2035 const float normalization = 1.0f / (sumHorizontal * sumVertical); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2036 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2037 switch (image.GetFormat()) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2038 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2039 case PixelFormat_Grayscale8: |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2040 SeparableConvolutionFloat<uint8_t, 1u> |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2041 (image, horizontal, horizontalAnchor, vertical, verticalAnchor, normalization); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2042 break; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2043 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2044 case PixelFormat_RGB24: |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2045 SeparableConvolutionFloat<uint8_t, 3u> |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2046 (image, horizontal, horizontalAnchor, vertical, verticalAnchor, normalization); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2047 break; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2048 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2049 default: |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2050 throw OrthancException(ErrorCode_NotImplemented); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2051 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2052 } |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2053 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2054 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2055 void ImageProcessing::SmoothGaussian5x5(ImageAccessor& image) |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2056 { |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2057 std::vector<float> kernel(5); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2058 kernel[0] = 1; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2059 kernel[1] = 4; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2060 kernel[2] = 6; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2061 kernel[3] = 4; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2062 kernel[4] = 1; |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2063 |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2064 SeparableConvolution(image, kernel, 2, kernel, 2); |
46cf170ba121
ImageProcessing::SeparableConvolution()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3502
diff
changeset
|
2065 } |
853 | 2066 } |