annotate Core/Images/ImageProcessing.cpp @ 3901:603a7b86fa5f transcoding

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