Mercurial > hg > orthanc-stone
annotate Framework/Viewport/CairoSurface.cpp @ 767:dce5b067d040
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 23 May 2019 16:57:33 +0200 |
parents | aaeec7be8fb7 |
children |
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 | |
439 | 5 * Copyright (C) 2017-2019 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 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
24 #include <Core/Logging.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
25 #include <Core/OrthancException.h> |
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
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, | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
41 unsigned int height, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
42 bool hasAlpha) |
0 | 43 { |
44 Release(); | |
45 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
46 hasAlpha_ = hasAlpha; |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
47 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
48 surface_ = cairo_image_surface_create |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
49 (hasAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, width, height); |
0 | 50 if (!surface_) |
51 { | |
52 // Should never occur | |
53 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
54 } | |
55 | |
56 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
57 { | |
58 LOG(ERROR) << "Cannot create a Cairo surface"; | |
59 cairo_surface_destroy(surface_); | |
60 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
61 } | |
62 | |
63 width_ = width; | |
64 height_ = height; | |
65 pitch_ = cairo_image_surface_get_stride(surface_); | |
66 buffer_ = cairo_image_surface_get_data(surface_); | |
67 } | |
68 | |
69 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
70 CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
71 bool hasAlpha) : |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
72 hasAlpha_(hasAlpha) |
0 | 73 { |
74 if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) | |
75 { | |
76 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
77 } | |
78 | |
79 width_ = accessor.GetWidth(); | |
80 height_ = accessor.GetHeight(); | |
81 pitch_ = accessor.GetPitch(); | |
82 buffer_ = accessor.GetBuffer(); | |
83 | |
84 surface_ = cairo_image_surface_create_for_data | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
85 (reinterpret_cast<unsigned char*>(buffer_), |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
86 hasAlpha_ ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
87 width_, height_, pitch_); |
0 | 88 if (!surface_) |
89 { | |
90 // Should never occur | |
91 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
92 } | |
93 | |
94 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
95 { | |
96 LOG(ERROR) << "Bad pitch for a Cairo surface"; | |
97 cairo_surface_destroy(surface_); | |
98 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
99 } | |
100 } | |
101 | |
102 | |
103 void CairoSurface::SetSize(unsigned int width, | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
104 unsigned int height, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
105 bool hasAlpha) |
0 | 106 { |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
107 if (hasAlpha_ != hasAlpha || |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
108 width_ != width || |
0 | 109 height_ != height) |
110 { | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
111 Allocate(width, height, hasAlpha); |
0 | 112 } |
113 } | |
114 | |
115 | |
116 void CairoSurface::Copy(const CairoSurface& other) | |
117 { | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
118 SetSize(other.GetWidth(), other.GetHeight(), other.HasAlpha()); |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
119 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
120 Orthanc::ImageAccessor source, target; |
318
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
121 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
122 other.GetReadOnlyAccessor(source); |
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
123 GetWriteableAccessor(target); |
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
124 |
0 | 125 Orthanc::ImageProcessing::Copy(target, source); |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
126 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
127 cairo_surface_mark_dirty(surface_); |
0 | 128 } |
129 | |
130 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
131 void CairoSurface::Copy(const Orthanc::ImageAccessor& source, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
132 bool hasAlpha) |
556
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
133 { |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
134 SetSize(source.GetWidth(), source.GetHeight(), hasAlpha); |
556
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
135 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
136 Orthanc::ImageAccessor target; |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
137 GetWriteableAccessor(target); |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
138 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
139 Orthanc::ImageProcessing::Convert(target, source); |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
140 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
141 cairo_surface_mark_dirty(surface_); |
556
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
142 } |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
143 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
144 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
145 void CairoSurface::GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const |
0 | 146 { |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
147 target.AssignReadOnly(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); |
0 | 148 } |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
149 |
0 | 150 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
151 void CairoSurface::GetWriteableAccessor(Orthanc::ImageAccessor& target) |
0 | 152 { |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
153 target.AssignWritable(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); |
0 | 154 } |
155 } |