annotate Core/Images/ImageProcessing.cpp @ 3548:e1ce68692069

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