Mercurial > hg > orthanc-stone
annotate Framework/Viewport/CairoSurface.cpp @ 40:7207a407bcd8
shared copyright with osimis
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Jan 2017 16:37:42 +0100 |
parents | ff1e935768e7 |
children | 81e2651dca17 |
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:
16
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 "CairoSurface.h" | |
35 | |
16 | 36 #include "../../Resources/Orthanc/Core/Logging.h" |
37 #include "../../Resources/Orthanc/Core/OrthancException.h" | |
38 #include "../../Resources/Orthanc/Core/Images/ImageProcessing.h" | |
0 | 39 |
40 namespace OrthancStone | |
41 { | |
42 void CairoSurface::Release() | |
43 { | |
44 if (surface_) | |
45 { | |
46 cairo_surface_destroy(surface_); | |
47 surface_ = NULL; | |
48 } | |
49 } | |
50 | |
51 | |
52 void CairoSurface::Allocate(unsigned int width, | |
53 unsigned int height) | |
54 { | |
55 Release(); | |
56 | |
57 surface_ = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); | |
58 if (!surface_) | |
59 { | |
60 // Should never occur | |
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
62 } | |
63 | |
64 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
65 { | |
66 LOG(ERROR) << "Cannot create a Cairo surface"; | |
67 cairo_surface_destroy(surface_); | |
68 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
69 } | |
70 | |
71 width_ = width; | |
72 height_ = height; | |
73 pitch_ = cairo_image_surface_get_stride(surface_); | |
74 buffer_ = cairo_image_surface_get_data(surface_); | |
75 } | |
76 | |
77 | |
78 CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor) | |
79 { | |
80 if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) | |
81 { | |
82 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
83 } | |
84 | |
85 width_ = accessor.GetWidth(); | |
86 height_ = accessor.GetHeight(); | |
87 pitch_ = accessor.GetPitch(); | |
88 buffer_ = accessor.GetBuffer(); | |
89 | |
90 surface_ = cairo_image_surface_create_for_data | |
91 (reinterpret_cast<unsigned char*>(buffer_), CAIRO_FORMAT_RGB24, width_, height_, pitch_); | |
92 if (!surface_) | |
93 { | |
94 // Should never occur | |
95 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
96 } | |
97 | |
98 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
99 { | |
100 LOG(ERROR) << "Bad pitch for a Cairo surface"; | |
101 cairo_surface_destroy(surface_); | |
102 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
103 } | |
104 } | |
105 | |
106 | |
107 void CairoSurface::SetSize(unsigned int width, | |
108 unsigned int height) | |
109 { | |
110 if (width_ != width || | |
111 height_ != height) | |
112 { | |
113 Allocate(width, height); | |
114 } | |
115 } | |
116 | |
117 | |
118 void CairoSurface::Copy(const CairoSurface& other) | |
119 { | |
120 Orthanc::ImageAccessor source = other.GetConstAccessor(); | |
121 Orthanc::ImageAccessor target = GetAccessor(); | |
122 Orthanc::ImageProcessing::Copy(target, source); | |
123 } | |
124 | |
125 | |
126 Orthanc::ImageAccessor CairoSurface::GetConstAccessor() const | |
127 { | |
128 Orthanc::ImageAccessor accessor; | |
129 accessor.AssignReadOnly(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); | |
130 return accessor; | |
131 } | |
132 | |
133 | |
134 Orthanc::ImageAccessor CairoSurface::GetAccessor() | |
135 { | |
136 Orthanc::ImageAccessor accessor; | |
137 accessor.AssignWritable(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); | |
138 return accessor; | |
139 } | |
140 } |