annotate Resources/Orthanc/Core/Images/ImageProcessing.cpp @ 16:ff1e935768e7

reorganization
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 09 Nov 2016 20:06:52 +0100
parents Framework/Orthanc/Core/Images/ImageProcessing.cpp@2dbe613f6c93
children 7207a407bcd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
5 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 * This program is free software: you can redistribute it and/or
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * modify it under the terms of the GNU General Public License as
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * published by the Free Software Foundation, either version 3 of the
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * License, or (at your option) any later version.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * In addition, as a special exception, the copyright holders of this
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * program give permission to link the code of its release with the
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * OpenSSL project's "OpenSSL" library (or with modified versions of it
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * that use the same license as the "OpenSSL" library), and distribute
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * the linked executables. You must obey the GNU General Public License
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * in all respects for all of the code used other than "OpenSSL". If you
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * modify file(s) with this exception, you may extend this exception to
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * your version of the file(s), but you are not obligated to do so. If
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * you do not wish to do so, delete this exception statement from your
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * version. If you delete this exception statement from all source files
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 * in the program, then also delete it here.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23 * This program is distributed in the hope that it will be useful, but
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 * WITHOUT ANY WARRANTY; without even the implied warranty of
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
26 * General Public License for more details.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27 *
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 * You should have received a copy of the GNU General Public License
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 **/
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 #include "../PrecompiledHeaders.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 #include "ImageProcessing.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 #include "../OrthancException.h"
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 #include <boost/math/special_functions/round.hpp>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 #include <cassert>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include <string.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 #include <limits>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 #include <stdint.h>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 namespace Orthanc
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 template <typename TargetType, typename SourceType>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 static void ConvertInternal(ImageAccessor& target,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 const ImageAccessor& source)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 const TargetType minValue = std::numeric_limits<TargetType>::min();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 const TargetType maxValue = std::numeric_limits<TargetType>::max();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
57 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
58
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
59 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
61 if (static_cast<int32_t>(*s) < static_cast<int32_t>(minValue))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
62 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63 *t = minValue;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 else if (static_cast<int32_t>(*s) > static_cast<int32_t>(maxValue))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 *t = maxValue;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
69 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
70 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
71 *t = static_cast<TargetType>(*s);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
72 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
73 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
74 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
75 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
76
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
77
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
78 template <typename SourceType>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
79 static void ConvertGrayscaleToFloat(ImageAccessor& target,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
80 const ImageAccessor& source)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
81 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
82 assert(sizeof(float) == 4);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
83
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
84 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
85 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
86 float* t = reinterpret_cast<float*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
87 const SourceType* s = reinterpret_cast<const SourceType*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
88
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
89 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
90 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 *t = static_cast<float>(*s);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 template <typename TargetType>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 static void ConvertColorToGrayscale(ImageAccessor& target,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
99 const ImageAccessor& source)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 assert(source.GetFormat() == PixelFormat_RGB24);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 const TargetType minValue = std::numeric_limits<TargetType>::min();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 const TargetType maxValue = std::numeric_limits<TargetType>::max();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 TargetType* t = reinterpret_cast<TargetType*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109 const uint8_t* s = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
111 for (unsigned int x = 0; x < source.GetWidth(); x++, t++, s += 3)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
112 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
113 // Y = 0.2126 R + 0.7152 G + 0.0722 B
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
114 int32_t v = (2126 * static_cast<int32_t>(s[0]) +
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
115 7152 * static_cast<int32_t>(s[1]) +
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
116 0722 * static_cast<int32_t>(s[2])) / 1000;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
117
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118 if (static_cast<int32_t>(v) < static_cast<int32_t>(minValue))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
119 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
120 *t = minValue;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
121 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
122 else if (static_cast<int32_t>(v) > static_cast<int32_t>(maxValue))
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 *t = maxValue;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 *t = static_cast<TargetType>(v);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 template <typename PixelType>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 static void SetInternal(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 int64_t constant)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 for (unsigned int y = 0; y < image.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 for (unsigned int x = 0; x < image.GetWidth(); x++, p++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 *p = static_cast<PixelType>(constant);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
148 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 template <typename PixelType>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 static void GetMinMaxValueInternal(PixelType& minValue,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 PixelType& maxValue,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154 const ImageAccessor& source)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 // Deal with the special case of empty image
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 if (source.GetWidth() == 0 ||
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 source.GetHeight() == 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 minValue = 0;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
161 maxValue = 0;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
162 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
163 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
164
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
165 minValue = std::numeric_limits<PixelType>::max();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
166 maxValue = std::numeric_limits<PixelType>::min();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
167
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
168 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
169 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
170 const PixelType* p = reinterpret_cast<const PixelType*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
171
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
172 for (unsigned int x = 0; x < source.GetWidth(); x++, p++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
173 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
174 if (*p < minValue)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
175 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
176 minValue = *p;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
177 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
179 if (*p > maxValue)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
180 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
181 maxValue = *p;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
182 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
183 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
184 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
185 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
186
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189 template <typename PixelType>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 static void AddConstantInternal(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 int64_t constant)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 if (constant == 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 const int64_t minValue = std::numeric_limits<PixelType>::min();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 const int64_t maxValue = std::numeric_limits<PixelType>::max();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 for (unsigned int y = 0; y < image.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 for (unsigned int x = 0; x < image.GetWidth(); x++, p++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 int64_t v = static_cast<int64_t>(*p) + constant;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 if (v > maxValue)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
211 *p = std::numeric_limits<PixelType>::max();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
213 else if (v < minValue)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
214 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
215 *p = std::numeric_limits<PixelType>::min();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
216 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
217 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
218 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
219 *p = static_cast<PixelType>(v);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
220 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
221 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
222 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
223 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
224
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
225
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
226
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
227 template <typename PixelType>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
228 void MultiplyConstantInternal(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
229 float factor)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
230 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
231 if (std::abs(factor - 1.0f) <= std::numeric_limits<float>::epsilon())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
232 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
233 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
234 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
235
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
236 const int64_t minValue = std::numeric_limits<PixelType>::min();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
237 const int64_t maxValue = std::numeric_limits<PixelType>::max();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
238
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
239 for (unsigned int y = 0; y < image.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
240 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
241 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
242
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
243 for (unsigned int x = 0; x < image.GetWidth(); x++, p++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
244 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
245 int64_t v = boost::math::llround(static_cast<float>(*p) * factor);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
246
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
247 if (v > maxValue)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
248 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249 *p = std::numeric_limits<PixelType>::max();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
250 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
251 else if (v < minValue)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
252 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
253 *p = std::numeric_limits<PixelType>::min();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
254 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
255 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
257 *p = static_cast<PixelType>(v);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
258 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
259 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
260 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
261 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
262
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
264 template <typename PixelType>
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 void ShiftScaleInternal(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 float offset,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267 float scaling)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
268 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
269 const float minValue = static_cast<float>(std::numeric_limits<PixelType>::min());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
270 const float maxValue = static_cast<float>(std::numeric_limits<PixelType>::max());
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
271
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
272 for (unsigned int y = 0; y < image.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
273 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 PixelType* p = reinterpret_cast<PixelType*>(image.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
275
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
276 for (unsigned int x = 0; x < image.GetWidth(); x++, p++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
277 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
278 float v = (static_cast<float>(*p) + offset) * scaling;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
279
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
280 if (v > maxValue)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
281 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
282 *p = std::numeric_limits<PixelType>::max();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
283 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
284 else if (v < minValue)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
285 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 *p = std::numeric_limits<PixelType>::min();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
287 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 else
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
289 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
290 *p = static_cast<PixelType>(boost::math::iround(v));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
291 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
292 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
293 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
294 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
295
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
296
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
297 void ImageProcessing::Copy(ImageAccessor& target,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
298 const ImageAccessor& source)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
299 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
300 if (target.GetWidth() != source.GetWidth() ||
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
301 target.GetHeight() != source.GetHeight())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
302 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
303 throw OrthancException(ErrorCode_IncompatibleImageSize);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
304 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
305
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
306 if (target.GetFormat() != source.GetFormat())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
307 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
308 throw OrthancException(ErrorCode_IncompatibleImageFormat);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311 unsigned int lineSize = GetBytesPerPixel(source.GetFormat()) * source.GetWidth();
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
313 assert(source.GetPitch() >= lineSize && target.GetPitch() >= lineSize);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
314
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
315 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
316 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
317 memcpy(target.GetRow(y), source.GetConstRow(y), lineSize);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
318 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
319 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
320
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
321
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
322 void ImageProcessing::Convert(ImageAccessor& target,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
323 const ImageAccessor& source)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
324 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
325 if (target.GetWidth() != source.GetWidth() ||
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
326 target.GetHeight() != source.GetHeight())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
327 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
328 throw OrthancException(ErrorCode_IncompatibleImageSize);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
329 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
330
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
331 if (source.GetFormat() == target.GetFormat())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
332 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
333 Copy(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
334 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
335 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
336
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
337 if (target.GetFormat() == PixelFormat_Grayscale16 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
338 source.GetFormat() == PixelFormat_Grayscale8)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
339 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
340 ConvertInternal<uint16_t, uint8_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
341 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
342 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
343
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
344 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
345 source.GetFormat() == PixelFormat_Grayscale8)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
346 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
347 ConvertInternal<int16_t, uint8_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
348 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
349 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
350
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
351 if (target.GetFormat() == PixelFormat_Grayscale8 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
352 source.GetFormat() == PixelFormat_Grayscale16)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
353 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
354 ConvertInternal<uint8_t, uint16_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
355 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
356 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
357
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
358 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
359 source.GetFormat() == PixelFormat_Grayscale16)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
360 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
361 ConvertInternal<int16_t, uint16_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
362 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
363 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
364
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
365 if (target.GetFormat() == PixelFormat_Grayscale8 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
366 source.GetFormat() == PixelFormat_SignedGrayscale16)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
367 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
368 ConvertInternal<uint8_t, int16_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
369 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
370 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
371
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
372 if (target.GetFormat() == PixelFormat_Grayscale16 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
373 source.GetFormat() == PixelFormat_SignedGrayscale16)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
374 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
375 ConvertInternal<uint16_t, int16_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
376 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
377 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
378
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
379 if (target.GetFormat() == PixelFormat_Grayscale8 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
380 source.GetFormat() == PixelFormat_RGB24)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
381 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
382 ConvertColorToGrayscale<uint8_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
383 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
384 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
385
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
386 if (target.GetFormat() == PixelFormat_Grayscale16 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
387 source.GetFormat() == PixelFormat_RGB24)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
388 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
389 ConvertColorToGrayscale<uint16_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
390 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
391 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
392
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
393 if (target.GetFormat() == PixelFormat_SignedGrayscale16 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
394 source.GetFormat() == PixelFormat_RGB24)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
395 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
396 ConvertColorToGrayscale<int16_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
397 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
398 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
399
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
400 if (target.GetFormat() == PixelFormat_Float32 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
401 source.GetFormat() == PixelFormat_Grayscale8)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
402 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
403 ConvertGrayscaleToFloat<uint8_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
404 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 if (target.GetFormat() == PixelFormat_Float32 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 source.GetFormat() == PixelFormat_Grayscale16)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 ConvertGrayscaleToFloat<uint16_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
413
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
414 if (target.GetFormat() == PixelFormat_Float32 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
415 source.GetFormat() == PixelFormat_SignedGrayscale16)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
416 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
417 ConvertGrayscaleToFloat<int16_t>(target, source);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 if (target.GetFormat() == PixelFormat_Grayscale8 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422 source.GetFormat() == PixelFormat_RGBA32)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428 for (unsigned int x = 0; x < source.GetWidth(); x++, q++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
429 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 *q = static_cast<uint8_t>((2126 * static_cast<uint32_t>(p[0]) +
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 7152 * static_cast<uint32_t>(p[1]) +
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 0722 * static_cast<uint32_t>(p[2])) / 10000);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 p += 4;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
437 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
438 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
439
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
440 if (target.GetFormat() == PixelFormat_RGB24 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
441 source.GetFormat() == PixelFormat_RGBA32)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
442 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
443 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
444 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
445 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
446 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
447 for (unsigned int x = 0; x < source.GetWidth(); x++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
448 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
449 q[0] = p[0];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
450 q[1] = p[1];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
451 q[2] = p[2];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
452 p += 4;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
453 q += 3;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
454 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
455 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
456
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
457 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
458 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
459
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
460 if (target.GetFormat() == PixelFormat_RGBA32 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
461 source.GetFormat() == PixelFormat_RGB24)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
462 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
463 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
464 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
465 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
466 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
467 for (unsigned int x = 0; x < source.GetWidth(); x++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
468 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
469 q[0] = p[0];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
470 q[1] = p[1];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
471 q[2] = p[2];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
472 q[3] = 255; // Set the alpha channel to full opacity
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
473 p += 3;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
474 q += 4;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
475 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
476 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
477
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
478 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
479 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
480
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
481 if (target.GetFormat() == PixelFormat_RGB24 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
482 source.GetFormat() == PixelFormat_Grayscale8)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
483 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
484 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
485 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
486 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
487 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
488 for (unsigned int x = 0; x < source.GetWidth(); x++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
489 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
490 q[0] = *p;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
491 q[1] = *p;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
492 q[2] = *p;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
493 p += 1;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
494 q += 3;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
495 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
496 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
497
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
498 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 if (target.GetFormat() == PixelFormat_RGBA32 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 source.GetFormat() == PixelFormat_Grayscale8)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
505 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508 for (unsigned int x = 0; x < source.GetWidth(); x++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510 q[0] = *p;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511 q[1] = *p;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 q[2] = *p;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513 q[3] = 255;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514 p += 1;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 q += 4;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
517 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
518
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
519 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
520 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
521
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522 if (target.GetFormat() == PixelFormat_BGRA32 &&
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 source.GetFormat() == PixelFormat_RGB24)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 for (unsigned int y = 0; y < source.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 const uint8_t* p = reinterpret_cast<const uint8_t*>(source.GetConstRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528 uint8_t* q = reinterpret_cast<uint8_t*>(target.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529 for (unsigned int x = 0; x < source.GetWidth(); x++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 q[0] = p[2];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 q[1] = p[1];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 q[2] = p[0];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 q[3] = 255;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 p += 3;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536 q += 4;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
541 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
542
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
543 throw OrthancException(ErrorCode_NotImplemented);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
544 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
545
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
546
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
547
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
548 void ImageProcessing::Set(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
549 int64_t value)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
550 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
551 switch (image.GetFormat())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
552 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
553 case PixelFormat_Grayscale8:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
554 SetInternal<uint8_t>(image, value);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
555 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
556
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
557 case PixelFormat_Grayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
558 SetInternal<uint16_t>(image, value);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
559 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
560
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
561 case PixelFormat_SignedGrayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
562 SetInternal<int16_t>(image, value);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
563 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
564
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
565 case PixelFormat_Float32:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
566 assert(sizeof(float) == 4);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
567 SetInternal<float>(image, value);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
568 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
569
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
570 default:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
571 throw OrthancException(ErrorCode_NotImplemented);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
572 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
573 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
574
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
575
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
576 void ImageProcessing::Set(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
577 uint8_t red,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
578 uint8_t green,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
579 uint8_t blue,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
580 uint8_t alpha)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
581 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
582 uint8_t p[4];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
583 unsigned int size;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
584
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
585 switch (image.GetFormat())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
586 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587 case PixelFormat_RGBA32:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
588 p[0] = red;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
589 p[1] = green;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
590 p[2] = blue;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
591 p[3] = alpha;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
592 size = 4;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
593 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
594
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
595 case PixelFormat_BGRA32:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
596 p[0] = blue;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
597 p[1] = green;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
598 p[2] = red;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
599 p[3] = alpha;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
600 size = 4;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
601 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
602
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
603 case PixelFormat_RGB24:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
604 p[0] = red;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
605 p[1] = green;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
606 p[2] = blue;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
607 size = 3;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 default:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
611 throw OrthancException(ErrorCode_NotImplemented);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
612 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
613
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
614 for (unsigned int y = 0; y < image.GetHeight(); y++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
615 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
616 uint8_t* q = reinterpret_cast<uint8_t*>(image.GetRow(y));
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
617
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
618 for (unsigned int x = 0; x < image.GetWidth(); x++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
619 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
620 for (unsigned int i = 0; i < size; i++)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
621 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
622 q[i] = p[i];
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
623 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
624
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
625 q += size;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
626 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
627 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
628 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
629
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
630
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
631 void ImageProcessing::ShiftRight(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
632 unsigned int shift)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
633 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
634 if (image.GetWidth() == 0 ||
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
635 image.GetHeight() == 0 ||
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
636 shift == 0)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
637 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
638 // Nothing to do
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
639 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
640 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
641
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
642 throw OrthancException(ErrorCode_NotImplemented);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
643 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
644
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
645
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
646 void ImageProcessing::GetMinMaxValue(int64_t& minValue,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
647 int64_t& maxValue,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
648 const ImageAccessor& image)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
649 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
650 switch (image.GetFormat())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
651 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
652 case PixelFormat_Grayscale8:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
653 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
654 uint8_t a, b;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
655 GetMinMaxValueInternal<uint8_t>(a, b, image);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
656 minValue = a;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
657 maxValue = b;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
658 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
659 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
660
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
661 case PixelFormat_Grayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
662 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
663 uint16_t a, b;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
664 GetMinMaxValueInternal<uint16_t>(a, b, image);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
665 minValue = a;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
666 maxValue = b;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
667 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
668 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
669
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
670 case PixelFormat_SignedGrayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
671 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
672 int16_t a, b;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
673 GetMinMaxValueInternal<int16_t>(a, b, image);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
674 minValue = a;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
675 maxValue = b;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
676 break;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
677 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
678
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
679 default:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
680 throw OrthancException(ErrorCode_NotImplemented);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
681 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
682 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
683
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
684
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
685
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
686 void ImageProcessing::AddConstant(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
687 int64_t value)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
688 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
689 switch (image.GetFormat())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
690 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
691 case PixelFormat_Grayscale8:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
692 AddConstantInternal<uint8_t>(image, value);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
693 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
694
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
695 case PixelFormat_Grayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
696 AddConstantInternal<uint16_t>(image, value);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
697 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
698
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
699 case PixelFormat_SignedGrayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
700 AddConstantInternal<int16_t>(image, value);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
701 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
702
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
703 default:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
704 throw OrthancException(ErrorCode_NotImplemented);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
705 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
706 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
707
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
708
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
709 void ImageProcessing::MultiplyConstant(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
710 float factor)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
711 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
712 switch (image.GetFormat())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
713 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
714 case PixelFormat_Grayscale8:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
715 MultiplyConstantInternal<uint8_t>(image, factor);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
716 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
717
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
718 case PixelFormat_Grayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
719 MultiplyConstantInternal<uint16_t>(image, factor);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
720 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
721
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
722 case PixelFormat_SignedGrayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
723 MultiplyConstantInternal<int16_t>(image, factor);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
724 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
725
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
726 default:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
727 throw OrthancException(ErrorCode_NotImplemented);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
728 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
729 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
730
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
731
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
732 void ImageProcessing::ShiftScale(ImageAccessor& image,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
733 float offset,
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
734 float scaling)
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
735 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
736 switch (image.GetFormat())
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
737 {
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
738 case PixelFormat_Grayscale8:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
739 ShiftScaleInternal<uint8_t>(image, offset, scaling);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
740 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
741
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
742 case PixelFormat_Grayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
743 ShiftScaleInternal<uint16_t>(image, offset, scaling);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
744 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
745
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
746 case PixelFormat_SignedGrayscale16:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
747 ShiftScaleInternal<int16_t>(image, offset, scaling);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
748 return;
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
749
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
750 default:
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
751 throw OrthancException(ErrorCode_NotImplemented);
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
752 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
753 }
2dbe613f6c93 add orthanc core
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
754 }