Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Wrappers/CairoSurface.cpp @ 1999:709b90ae0f89
starting text annotations
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 02 Nov 2022 13:44:13 +0100 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
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 | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
47 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
0 | 12 * |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
16 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
17 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
19 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1596
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
0 | 21 **/ |
22 | |
23 | |
24 #include "CairoSurface.h" | |
25 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
26 #include <Logging.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
27 #include <OrthancException.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
28 #include <Images/ImageProcessing.h> |
0 | 29 |
30 namespace OrthancStone | |
31 { | |
32 void CairoSurface::Release() | |
33 { | |
34 if (surface_) | |
35 { | |
36 cairo_surface_destroy(surface_); | |
37 surface_ = NULL; | |
38 } | |
39 } | |
40 | |
41 | |
42 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
|
43 unsigned int height, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
44 bool hasAlpha) |
0 | 45 { |
46 Release(); | |
47 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
48 hasAlpha_ = hasAlpha; |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
49 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
50 surface_ = cairo_image_surface_create |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
51 (hasAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, width, height); |
0 | 52 if (!surface_) |
53 { | |
54 // Should never occur | |
55 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
56 } | |
57 | |
58 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
59 { | |
60 LOG(ERROR) << "Cannot create a Cairo surface"; | |
61 cairo_surface_destroy(surface_); | |
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
63 } | |
64 | |
65 width_ = width; | |
66 height_ = height; | |
67 pitch_ = cairo_image_surface_get_stride(surface_); | |
68 buffer_ = cairo_image_surface_get_data(surface_); | |
69 } | |
70 | |
71 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
72 CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
73 bool hasAlpha) : |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
74 hasAlpha_(hasAlpha) |
0 | 75 { |
76 if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) | |
77 { | |
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
79 } | |
80 | |
81 width_ = accessor.GetWidth(); | |
82 height_ = accessor.GetHeight(); | |
83 pitch_ = accessor.GetPitch(); | |
84 buffer_ = accessor.GetBuffer(); | |
85 | |
86 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
|
87 (reinterpret_cast<unsigned char*>(buffer_), |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
88 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
|
89 width_, height_, pitch_); |
0 | 90 if (!surface_) |
91 { | |
92 // Should never occur | |
93 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
94 } | |
95 | |
96 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
97 { | |
98 LOG(ERROR) << "Bad pitch for a Cairo surface"; | |
99 cairo_surface_destroy(surface_); | |
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
101 } | |
102 } | |
103 | |
104 | |
105 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
|
106 unsigned int height, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
107 bool hasAlpha) |
0 | 108 { |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
109 if (hasAlpha_ != hasAlpha || |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
110 width_ != width || |
0 | 111 height_ != height) |
112 { | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
113 Allocate(width, height, hasAlpha); |
0 | 114 } |
115 } | |
116 | |
117 | |
118 void CairoSurface::Copy(const CairoSurface& other) | |
119 { | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
120 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
|
121 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
122 Orthanc::ImageAccessor source, target; |
318
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
123 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
124 other.GetReadOnlyAccessor(source); |
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
125 GetWriteableAccessor(target); |
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
126 |
0 | 127 Orthanc::ImageProcessing::Copy(target, source); |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
128 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
129 cairo_surface_mark_dirty(surface_); |
0 | 130 } |
131 | |
132 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
133 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
|
134 bool hasAlpha) |
556
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
135 { |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
136 SetSize(source.GetWidth(), source.GetHeight(), hasAlpha); |
556
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
137 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
138 Orthanc::ImageAccessor target; |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
139 GetWriteableAccessor(target); |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
140 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
141 Orthanc::ImageProcessing::Convert(target, source); |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
142 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
143 cairo_surface_mark_dirty(surface_); |
556
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
144 } |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
145 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
146 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
147 void CairoSurface::GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const |
0 | 148 { |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
149 target.AssignReadOnly(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); |
0 | 150 } |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
151 |
0 | 152 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
153 void CairoSurface::GetWriteableAccessor(Orthanc::ImageAccessor& target) |
0 | 154 { |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
155 target.AssignWritable(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); |
0 | 156 } |
157 } |