Mercurial > hg > orthanc
annotate OrthancFramework/Sources/Images/ImageBuffer.cpp @ 4706:dbee8bd8ee1f
display OpenSSL version in the logs
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 Jun 2021 09:13:03 +0200 |
parents | d9473bd5ed43 |
children | 7053502fbf97 |
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 |
4437
d9473bd5ed43
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4304
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
801 | 6 * |
7 * 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
|
8 * 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
|
9 * 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
|
10 * the License, or (at your option) any later version. |
801 | 11 * |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * 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
|
15 * Lesser General Public License for more details. |
801 | 16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
801 | 20 **/ |
21 | |
22 | |
824
a811bdf8b8eb
precompiled headers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
801
diff
changeset
|
23 #include "../PrecompiledHeaders.h" |
801 | 24 #include "ImageBuffer.h" |
25 | |
863 | 26 #include "../OrthancException.h" |
27 | |
4304 | 28 #include <boost/lexical_cast.hpp> |
860 | 29 #include <stdio.h> |
863 | 30 #include <stdlib.h> |
860 | 31 |
801 | 32 namespace Orthanc |
33 { | |
34 void ImageBuffer::Allocate() | |
35 { | |
36 if (changed_) | |
37 { | |
863 | 38 Deallocate(); |
39 | |
844 | 40 /* |
41 if (forceMinimalPitch_) | |
42 { | |
43 TODO: Align pitch and memory buffer to optimal size for SIMD. | |
44 } | |
45 */ | |
46 | |
801 | 47 pitch_ = GetBytesPerPixel() * width_; |
863 | 48 size_t size = pitch_ * height_; |
801 | 49 |
863 | 50 if (size == 0) |
801 | 51 { |
863 | 52 buffer_ = NULL; |
801 | 53 } |
54 else | |
55 { | |
863 | 56 buffer_ = malloc(size); |
57 if (buffer_ == NULL) | |
58 { | |
3348 | 59 throw OrthancException(ErrorCode_NotEnoughMemory, |
60 "Failed to allocate an image buffer of size " + boost::lexical_cast<std::string>(width_) + "x" + boost::lexical_cast<std::string>(height_)); | |
863 | 61 } |
801 | 62 } |
63 | |
64 changed_ = false; | |
65 } | |
66 } | |
67 | |
68 | |
863 | 69 void ImageBuffer::Deallocate() |
70 { | |
71 if (buffer_ != NULL) | |
72 { | |
73 free(buffer_); | |
74 buffer_ = NULL; | |
75 changed_ = true; | |
76 } | |
77 } | |
78 | |
79 | |
1608
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
80 ImageBuffer::ImageBuffer(PixelFormat format, |
adc6a5704cdb
OrthancPluginConvertPixelFormat
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1288
diff
changeset
|
81 unsigned int width, |
2107 | 82 unsigned int height, |
83 bool forceMinimalPitch) : | |
84 forceMinimalPitch_(forceMinimalPitch) | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
85 { |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
86 Initialize(); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
87 SetWidth(width); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
88 SetHeight(height); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
89 SetFormat(format); |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
90 } |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
91 |
4300 | 92 ImageBuffer::ImageBuffer() |
93 { | |
94 Initialize(); | |
95 } | |
96 | |
97 ImageBuffer::~ImageBuffer() | |
98 { | |
99 Deallocate(); | |
100 } | |
101 | |
102 PixelFormat ImageBuffer::GetFormat() const | |
103 { | |
104 return format_; | |
105 } | |
106 | |
853
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
107 |
839be3022203
DicomImageInformation
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
844
diff
changeset
|
108 void ImageBuffer::Initialize() |
801 | 109 { |
110 changed_ = false; | |
844 | 111 forceMinimalPitch_ = true; |
801 | 112 format_ = PixelFormat_Grayscale8; |
113 width_ = 0; | |
114 height_ = 0; | |
115 pitch_ = 0; | |
116 buffer_ = NULL; | |
117 } | |
118 | |
119 | |
120 void ImageBuffer::SetFormat(PixelFormat format) | |
121 { | |
863 | 122 if (format != format_) |
123 { | |
124 changed_ = true; | |
125 format_ = format; | |
126 } | |
801 | 127 } |
128 | |
4300 | 129 unsigned int ImageBuffer::GetWidth() const |
130 { | |
131 return width_; | |
132 } | |
133 | |
801 | 134 |
135 void ImageBuffer::SetWidth(unsigned int width) | |
136 { | |
863 | 137 if (width != width_) |
138 { | |
139 changed_ = true; | |
140 width_ = width; | |
141 } | |
801 | 142 } |
143 | |
4300 | 144 unsigned int ImageBuffer::GetHeight() const |
145 { | |
146 return height_; | |
147 } | |
148 | |
801 | 149 |
150 void ImageBuffer::SetHeight(unsigned int height) | |
151 { | |
863 | 152 if (height != height_) |
153 { | |
154 changed_ = true; | |
155 height_ = height; | |
156 } | |
801 | 157 } |
158 | |
4300 | 159 unsigned int ImageBuffer::GetBytesPerPixel() const |
160 { | |
161 return ::Orthanc::GetBytesPerPixel(format_); | |
162 } | |
163 | |
2861
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
164 |
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
165 void ImageBuffer::GetReadOnlyAccessor(ImageAccessor& accessor) |
801 | 166 { |
167 Allocate(); | |
2861
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
168 accessor.AssignReadOnly(format_, width_, height_, pitch_, buffer_); |
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
169 } |
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
170 |
801 | 171 |
2861
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
172 void ImageBuffer::GetWriteableAccessor(ImageAccessor& accessor) |
801 | 173 { |
174 Allocate(); | |
2861
9b4251721f22
ImageAccessor now non-copyable
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
175 accessor.AssignWritable(format_, width_, height_, pitch_, buffer_); |
801 | 176 } |
844 | 177 |
4300 | 178 bool ImageBuffer::IsMinimalPitchForced() const |
179 { | |
180 return forceMinimalPitch_; | |
181 } | |
182 | |
844 | 183 |
863 | 184 void ImageBuffer::AcquireOwnership(ImageBuffer& other) |
185 { | |
186 // Remove the content of the current image | |
187 Deallocate(); | |
188 | |
189 // Force the allocation of the other image (if not already | |
190 // allocated) | |
191 other.Allocate(); | |
192 | |
193 // Transfer the content of the other image | |
194 changed_ = false; | |
195 forceMinimalPitch_ = other.forceMinimalPitch_; | |
196 format_ = other.format_; | |
197 width_ = other.width_; | |
198 height_ = other.height_; | |
199 pitch_ = other.pitch_; | |
200 buffer_ = other.buffer_; | |
201 | |
202 // Force the reinitialization of the other image | |
203 other.Initialize(); | |
844 | 204 } |
801 | 205 } |