Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/ImageBuffer.cpp @ 4934:94a7b681b340 more-tags
added configuration for extra main dicom tags + save signature in metadata + show warning if inconsistent main dicom tags
author | Alain Mazy <am@osimis.io> |
---|---|
date | Thu, 10 Mar 2022 09:03:24 +0100 |
parents | 43e613a7756b |
children | 0ea402b4d901 |
rev | line source |
---|---|
801 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
863
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
801 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
10 * as published by the Free Software Foundation, either version 3 of |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
11 * the License, or (at your option) any later version. |
801 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
16 * Lesser General Public License for more details. |
801 | 17 * |
4119
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
19 * License along with this program. If not, see |
bf7b9edf6b81
re-licensing the OrthancFramework to LGPL, in order to license Stone of Orthanc under LGPL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4044
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
801 | 21 **/ |
22 | |
23 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
801
diff
changeset
|
24 #include "../PrecompiledHeaders.h" |
801 | 25 #include "ImageBuffer.h" |
26 | |
863 | 27 #include "../OrthancException.h" |
28 | |
4304 | 29 #include <boost/lexical_cast.hpp> |
860 | 30 #include <stdio.h> |
863 | 31 #include <stdlib.h> |
860 | 32 |
801 | 33 namespace Orthanc |
34 { | |
35 void ImageBuffer::Allocate() | |
36 { | |
37 if (changed_) | |
38 { | |
863 | 39 Deallocate(); |
40 | |
844 | 41 /* |
42 if (forceMinimalPitch_) | |
43 { | |
44 TODO: Align pitch and memory buffer to optimal size for SIMD. | |
45 } | |
46 */ | |
47 | |
801 | 48 pitch_ = GetBytesPerPixel() * width_; |
863 | 49 size_t size = pitch_ * height_; |
801 | 50 |
863 | 51 if (size == 0) |
801 | 52 { |
863 | 53 buffer_ = NULL; |
801 | 54 } |
55 else | |
56 { | |
863 | 57 buffer_ = malloc(size); |
58 if (buffer_ == NULL) | |
59 { | |
3348 | 60 throw OrthancException(ErrorCode_NotEnoughMemory, |
61 "Failed to allocate an image buffer of size " + boost::lexical_cast<std::string>(width_) + "x" + boost::lexical_cast<std::string>(height_)); | |
863 | 62 } |
801 | 63 } |
64 | |
65 changed_ = false; | |
66 } | |
67 } | |
68 | |
69 | |
863 | 70 void ImageBuffer::Deallocate() |
71 { | |
72 if (buffer_ != NULL) | |
73 { | |
74 free(buffer_); | |
75 buffer_ = NULL; | |
76 changed_ = true; | |
77 } | |
78 } | |
79 | |
80 | |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
81 ImageBuffer::ImageBuffer(PixelFormat format, |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
82 unsigned int width, |
2107 | 83 unsigned int height, |
84 bool forceMinimalPitch) : | |
85 forceMinimalPitch_(forceMinimalPitch) | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
86 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
87 Initialize(); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
88 SetWidth(width); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
89 SetHeight(height); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
90 SetFormat(format); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
91 } |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
92 |
4300 | 93 ImageBuffer::ImageBuffer() |
94 { | |
95 Initialize(); | |
96 } | |
97 | |
98 ImageBuffer::~ImageBuffer() | |
99 { | |
100 Deallocate(); | |
101 } | |
102 | |
103 PixelFormat ImageBuffer::GetFormat() const | |
104 { | |
105 return format_; | |
106 } | |
107 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
108 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
109 void ImageBuffer::Initialize() |
801 | 110 { |
111 changed_ = false; | |
844 | 112 forceMinimalPitch_ = true; |
801 | 113 format_ = PixelFormat_Grayscale8; |
114 width_ = 0; | |
115 height_ = 0; | |
116 pitch_ = 0; | |
117 buffer_ = NULL; | |
118 } | |
119 | |
120 | |
121 void ImageBuffer::SetFormat(PixelFormat format) | |
122 { | |
863 | 123 if (format != format_) |
124 { | |
125 changed_ = true; | |
126 format_ = format; | |
127 } | |
801 | 128 } |
129 | |
4300 | 130 unsigned int ImageBuffer::GetWidth() const |
131 { | |
132 return width_; | |
133 } | |
134 | |
801 | 135 |
136 void ImageBuffer::SetWidth(unsigned int width) | |
137 { | |
863 | 138 if (width != width_) |
139 { | |
140 changed_ = true; | |
141 width_ = width; | |
142 } | |
801 | 143 } |
144 | |
4300 | 145 unsigned int ImageBuffer::GetHeight() const |
146 { | |
147 return height_; | |
148 } | |
149 | |
801 | 150 |
151 void ImageBuffer::SetHeight(unsigned int height) | |
152 { | |
863 | 153 if (height != height_) |
154 { | |
155 changed_ = true; | |
156 height_ = height; | |
157 } | |
801 | 158 } |
159 | |
4300 | 160 unsigned int ImageBuffer::GetBytesPerPixel() const |
161 { | |
162 return ::Orthanc::GetBytesPerPixel(format_); | |
163 } | |
164 | |
2861
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
165 |
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
166 void ImageBuffer::GetReadOnlyAccessor(ImageAccessor& accessor) |
801 | 167 { |
168 Allocate(); | |
2861
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
169 accessor.AssignReadOnly(format_, width_, height_, pitch_, buffer_); |
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
170 } |
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
171 |
801 | 172 |
2861
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
173 void ImageBuffer::GetWriteableAccessor(ImageAccessor& accessor) |
801 | 174 { |
175 Allocate(); | |
2861
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
176 accessor.AssignWritable(format_, width_, height_, pitch_, buffer_); |
801 | 177 } |
844 | 178 |
4300 | 179 bool ImageBuffer::IsMinimalPitchForced() const |
180 { | |
181 return forceMinimalPitch_; | |
182 } | |
183 | |
844 | 184 |
863 | 185 void ImageBuffer::AcquireOwnership(ImageBuffer& other) |
186 { | |
187 // Remove the content of the current image | |
188 Deallocate(); | |
189 | |
190 // Force the allocation of the other image (if not already | |
191 // allocated) | |
192 other.Allocate(); | |
193 | |
194 // Transfer the content of the other image | |
195 changed_ = false; | |
196 forceMinimalPitch_ = other.forceMinimalPitch_; | |
197 format_ = other.format_; | |
198 width_ = other.width_; | |
199 height_ = other.height_; | |
200 pitch_ = other.pitch_; | |
201 buffer_ = other.buffer_; | |
202 | |
203 // Force the reinitialization of the other image | |
204 other.Initialize(); | |
844 | 205 } |
801 | 206 } |