Mercurial > hg > orthanc-stone
annotate Framework/Viewport/CairoSurface.cpp @ 161:197a5ddaf68c wasm
FiniteProjectiveCamera
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 14 Feb 2018 11:29:26 +0100 |
parents | e2fe9352f240 |
children | fccffbf99ba1 |
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 | |
135
e2fe9352f240
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
47 | 8 * modify it under the terms of the GNU Affero General Public License |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
0 | 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 | |
47 | 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
0 | 18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 **/ | |
20 | |
21 | |
22 #include "CairoSurface.h" | |
23 | |
113
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
24 #include <Core/Logging.h> |
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
25 #include <Core/OrthancException.h> |
2eca030792aa
using the Orthanc Framework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
26 #include <Core/Images/ImageProcessing.h> |
0 | 27 |
28 namespace OrthancStone | |
29 { | |
30 void CairoSurface::Release() | |
31 { | |
32 if (surface_) | |
33 { | |
34 cairo_surface_destroy(surface_); | |
35 surface_ = NULL; | |
36 } | |
37 } | |
38 | |
39 | |
40 void CairoSurface::Allocate(unsigned int width, | |
41 unsigned int height) | |
42 { | |
43 Release(); | |
44 | |
45 surface_ = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); | |
46 if (!surface_) | |
47 { | |
48 // Should never occur | |
49 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
50 } | |
51 | |
52 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
53 { | |
54 LOG(ERROR) << "Cannot create a Cairo surface"; | |
55 cairo_surface_destroy(surface_); | |
56 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
57 } | |
58 | |
59 width_ = width; | |
60 height_ = height; | |
61 pitch_ = cairo_image_surface_get_stride(surface_); | |
62 buffer_ = cairo_image_surface_get_data(surface_); | |
63 } | |
64 | |
65 | |
66 CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor) | |
67 { | |
68 if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) | |
69 { | |
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
71 } | |
72 | |
73 width_ = accessor.GetWidth(); | |
74 height_ = accessor.GetHeight(); | |
75 pitch_ = accessor.GetPitch(); | |
76 buffer_ = accessor.GetBuffer(); | |
77 | |
78 surface_ = cairo_image_surface_create_for_data | |
79 (reinterpret_cast<unsigned char*>(buffer_), CAIRO_FORMAT_RGB24, width_, height_, pitch_); | |
80 if (!surface_) | |
81 { | |
82 // Should never occur | |
83 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
84 } | |
85 | |
86 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
87 { | |
88 LOG(ERROR) << "Bad pitch for a Cairo surface"; | |
89 cairo_surface_destroy(surface_); | |
90 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
91 } | |
92 } | |
93 | |
94 | |
95 void CairoSurface::SetSize(unsigned int width, | |
96 unsigned int height) | |
97 { | |
98 if (width_ != width || | |
99 height_ != height) | |
100 { | |
101 Allocate(width, height); | |
102 } | |
103 } | |
104 | |
105 | |
106 void CairoSurface::Copy(const CairoSurface& other) | |
107 { | |
108 Orthanc::ImageAccessor source = other.GetConstAccessor(); | |
109 Orthanc::ImageAccessor target = GetAccessor(); | |
110 Orthanc::ImageProcessing::Copy(target, source); | |
111 } | |
112 | |
113 | |
114 Orthanc::ImageAccessor CairoSurface::GetConstAccessor() const | |
115 { | |
116 Orthanc::ImageAccessor accessor; | |
117 accessor.AssignReadOnly(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); | |
118 return accessor; | |
119 } | |
120 | |
121 | |
122 Orthanc::ImageAccessor CairoSurface::GetAccessor() | |
123 { | |
124 Orthanc::ImageAccessor accessor; | |
125 accessor.AssignWritable(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); | |
126 return accessor; | |
127 } | |
128 } |