Mercurial > hg > orthanc-stone
annotate Framework/Layers/FrameRenderer.cpp @ 40:7207a407bcd8
shared copyright with osimis
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Jan 2017 16:37:42 +0100 |
parents | 517c46f527cd |
children | 28956ed68280 |
rev | line source |
---|---|
0 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
40
7207a407bcd8
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
32
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "FrameRenderer.h" | |
35 | |
36 #include "GrayscaleFrameRenderer.h" | |
37 #include "ColorFrameRenderer.h" | |
38 | |
16 | 39 #include "../../Resources/Orthanc/Core/OrthancException.h" |
0 | 40 |
41 namespace OrthancStone | |
42 { | |
43 static bool ComputePixelTransform(cairo_matrix_t& target, | |
44 const SliceGeometry& viewportSlice, | |
45 const SliceGeometry& frameSlice, | |
46 double pixelSpacingX, | |
47 double pixelSpacingY) | |
48 { | |
49 bool isOpposite; | |
50 if (!GeometryToolbox::IsParallelOrOpposite(isOpposite, viewportSlice.GetNormal(), frameSlice.GetNormal())) | |
51 { | |
52 return false; | |
53 } | |
54 else | |
55 { | |
56 double x0, y0, x1, y1, x2, y2; | |
57 viewportSlice.ProjectPoint(x0, y0, frameSlice.GetOrigin() | |
58 - 0.5 * pixelSpacingX * frameSlice.GetAxisX() | |
59 - 0.5 * pixelSpacingY * frameSlice.GetAxisY()); | |
60 viewportSlice.ProjectPoint(x1, y1, frameSlice.GetOrigin() | |
61 + 0.5 * pixelSpacingX * frameSlice.GetAxisX() | |
62 - 0.5 * pixelSpacingY * frameSlice.GetAxisY()); | |
63 viewportSlice.ProjectPoint(x2, y2, frameSlice.GetOrigin() | |
64 - 0.5 * pixelSpacingX * frameSlice.GetAxisX() | |
65 + 0.5 * pixelSpacingY * frameSlice.GetAxisY()); | |
66 | |
67 /** | |
68 * Now we solve the system of linear equations Ax + b = x', given: | |
69 * A [0 ; 0] + b = [x0 ; y0] | |
70 * A [1 ; 0] + b = [x1 ; y1] | |
71 * A [0 ; 1] + b = [x2 ; y2] | |
72 * <=> | |
73 * b = [x0 ; y0] | |
74 * A [1 ; 0] = [x1 ; y1] - b = [x1 - x0 ; y1 - y0] | |
75 * A [0 ; 1] = [x2 ; y2] - b = [x2 - x0 ; y2 - y0] | |
76 * <=> | |
77 * b = [x0 ; y0] | |
78 * [a11 ; a21] = [x1 - x0 ; y1 - y0] | |
79 * [a12 ; a22] = [x2 - x0 ; y2 - y0] | |
80 **/ | |
81 | |
82 cairo_matrix_init(&target, x1 - x0, y1 - y0, x2 - x0, y2 - y0, x0, y0); | |
83 | |
84 return true; | |
85 } | |
86 } | |
87 | |
88 | |
89 FrameRenderer::FrameRenderer(const SliceGeometry& viewportSlice, | |
90 const SliceGeometry& frameSlice, | |
91 double pixelSpacingX, | |
92 double pixelSpacingY, | |
93 bool isFullQuality) : | |
94 viewportSlice_(viewportSlice), | |
95 frameSlice_(frameSlice), | |
96 pixelSpacingX_(pixelSpacingX), | |
97 pixelSpacingY_(pixelSpacingY), | |
98 isFullQuality_(isFullQuality) | |
99 { | |
100 } | |
101 | |
102 | |
103 bool FrameRenderer::ComputeFrameExtent(double& x1, | |
104 double& y1, | |
105 double& x2, | |
106 double& y2, | |
107 const SliceGeometry& viewportSlice, | |
108 const SliceGeometry& frameSlice, | |
109 unsigned int frameWidth, | |
110 unsigned int frameHeight, | |
111 double pixelSpacingX, | |
112 double pixelSpacingY) | |
113 { | |
114 bool isOpposite; | |
115 if (!GeometryToolbox::IsParallelOrOpposite(isOpposite, viewportSlice.GetNormal(), frameSlice.GetNormal())) | |
116 { | |
117 return false; | |
118 } | |
119 else | |
120 { | |
121 cairo_matrix_t transform; | |
122 if (!ComputePixelTransform(transform, viewportSlice, frameSlice, pixelSpacingX, pixelSpacingY)) | |
123 { | |
124 return true; | |
125 } | |
126 | |
127 x1 = 0; | |
128 y1 = 0; | |
129 cairo_matrix_transform_point(&transform, &x1, &y1); | |
130 | |
131 x2 = frameWidth; | |
132 y2 = frameHeight; | |
133 cairo_matrix_transform_point(&transform, &x2, &y2); | |
134 | |
135 if (x1 > x2) | |
136 { | |
137 std::swap(x1, x2); | |
138 } | |
139 | |
140 if (y1 > y2) | |
141 { | |
142 std::swap(y1, y2); | |
143 } | |
144 | |
145 return true; | |
146 } | |
147 } | |
148 | |
149 | |
150 bool FrameRenderer::RenderLayer(CairoContext& context, | |
151 const ViewportGeometry& view) | |
152 { | |
153 if (!style_.visible_) | |
154 { | |
155 return true; | |
156 } | |
157 | |
158 if (display_.get() == NULL) | |
159 { | |
160 if (!ComputePixelTransform(transform_, viewportSlice_, frameSlice_, pixelSpacingX_, pixelSpacingY_)) | |
161 { | |
162 return true; | |
163 } | |
164 | |
165 display_.reset(GenerateDisplay(style_)); | |
166 } | |
167 | |
168 assert(display_.get() != NULL); | |
169 | |
170 cairo_t *cr = context.GetObject(); | |
171 | |
172 cairo_save(cr); | |
173 | |
174 cairo_transform(cr, &transform_); | |
175 //cairo_set_operator(cr, CAIRO_OPERATOR_OVER); | |
176 cairo_set_source_surface(cr, display_->GetObject(), 0, 0); | |
177 | |
178 switch (style_.interpolation_) | |
179 { | |
180 case ImageInterpolation_Nearest: | |
181 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_NEAREST); | |
182 break; | |
183 | |
184 case ImageInterpolation_Linear: | |
185 cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_BILINEAR); | |
186 break; | |
187 | |
188 default: | |
189 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
190 } | |
191 | |
192 cairo_paint_with_alpha(cr, style_.alpha_); | |
193 | |
194 if (style_.drawGrid_) | |
195 { | |
196 context.SetSourceColor(style_.drawColor_); | |
197 cairo_set_line_width(cr, 0.5 / view.GetZoom()); | |
198 | |
199 for (unsigned int x = 0; x <= display_->GetWidth(); x++) | |
200 { | |
201 cairo_move_to(cr, x, 0); | |
202 cairo_line_to(cr, x, display_->GetHeight()); | |
203 } | |
204 | |
205 for (unsigned int y = 0; y <= display_->GetHeight(); y++) | |
206 { | |
207 cairo_move_to(cr, 0, y); | |
208 cairo_line_to(cr, display_->GetWidth(), y); | |
209 } | |
210 | |
211 cairo_stroke(cr); | |
212 } | |
213 | |
214 cairo_restore(cr); | |
215 | |
216 return true; | |
217 } | |
218 | |
219 | |
220 void FrameRenderer::SetLayerStyle(const RenderStyle& style) | |
221 { | |
222 style_ = style; | |
223 display_.reset(NULL); | |
224 } | |
225 | |
226 | |
227 ILayerRenderer* FrameRenderer::CreateRenderer(Orthanc::ImageAccessor* frame, | |
228 const SliceGeometry& viewportSlice, | |
229 const SliceGeometry& frameSlice, | |
32 | 230 const OrthancPlugins::IDicomDataset& dicom, |
0 | 231 double pixelSpacingX, |
232 double pixelSpacingY, | |
233 bool isFullQuality) | |
234 { | |
235 std::auto_ptr<Orthanc::ImageAccessor> protect(frame); | |
236 | |
237 if (frame->GetFormat() == Orthanc::PixelFormat_RGB24) | |
238 { | |
239 return new ColorFrameRenderer(protect.release(), viewportSlice, frameSlice, | |
240 pixelSpacingX, pixelSpacingY, isFullQuality); | |
241 } | |
242 else | |
243 { | |
244 DicomFrameConverter converter; | |
245 converter.ReadParameters(dicom); | |
246 return new GrayscaleFrameRenderer(protect.release(), converter, viewportSlice, frameSlice, | |
247 pixelSpacingX, pixelSpacingY, isFullQuality); | |
248 } | |
249 } | |
250 } |