Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Wrappers/CairoSurface.cpp @ 1770:073484e33bee
fix offset of textures
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 May 2021 10:53:37 +0200 |
parents | 9ac2a65d4172 |
children | 3889ae96d2e9 |
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 | |
1739
9ac2a65d4172
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
0 | 6 * |
7 * 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
|
8 * modify it under the terms of the GNU Lesser General Public License |
47 | 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 | |
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
|
14 * 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
|
15 * Lesser General Public License for more details. |
1596
4fb8fdf03314
removed annoying whitespace
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
0 | 20 **/ |
21 | |
22 | |
23 #include "CairoSurface.h" | |
24 | |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
25 #include <Logging.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
26 #include <OrthancException.h> |
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1270
diff
changeset
|
27 #include <Images/ImageProcessing.h> |
0 | 28 |
29 namespace OrthancStone | |
30 { | |
31 void CairoSurface::Release() | |
32 { | |
33 if (surface_) | |
34 { | |
35 cairo_surface_destroy(surface_); | |
36 surface_ = NULL; | |
37 } | |
38 } | |
39 | |
40 | |
41 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
|
42 unsigned int height, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
43 bool hasAlpha) |
0 | 44 { |
45 Release(); | |
46 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
47 hasAlpha_ = hasAlpha; |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
48 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
49 surface_ = cairo_image_surface_create |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
50 (hasAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, width, height); |
0 | 51 if (!surface_) |
52 { | |
53 // Should never occur | |
54 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
55 } | |
56 | |
57 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
58 { | |
59 LOG(ERROR) << "Cannot create a Cairo surface"; | |
60 cairo_surface_destroy(surface_); | |
61 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
62 } | |
63 | |
64 width_ = width; | |
65 height_ = height; | |
66 pitch_ = cairo_image_surface_get_stride(surface_); | |
67 buffer_ = cairo_image_surface_get_data(surface_); | |
68 } | |
69 | |
70 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
71 CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
72 bool hasAlpha) : |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
73 hasAlpha_(hasAlpha) |
0 | 74 { |
75 if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) | |
76 { | |
77 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
78 } | |
79 | |
80 width_ = accessor.GetWidth(); | |
81 height_ = accessor.GetHeight(); | |
82 pitch_ = accessor.GetPitch(); | |
83 buffer_ = accessor.GetBuffer(); | |
84 | |
85 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
|
86 (reinterpret_cast<unsigned char*>(buffer_), |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
87 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
|
88 width_, height_, pitch_); |
0 | 89 if (!surface_) |
90 { | |
91 // Should never occur | |
92 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
93 } | |
94 | |
95 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
96 { | |
97 LOG(ERROR) << "Bad pitch for a Cairo surface"; | |
98 cairo_surface_destroy(surface_); | |
99 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
100 } | |
101 } | |
102 | |
103 | |
104 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
|
105 unsigned int height, |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
106 bool hasAlpha) |
0 | 107 { |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
108 if (hasAlpha_ != hasAlpha || |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
109 width_ != width || |
0 | 110 height_ != height) |
111 { | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
112 Allocate(width, height, hasAlpha); |
0 | 113 } |
114 } | |
115 | |
116 | |
117 void CairoSurface::Copy(const CairoSurface& other) | |
118 { | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
119 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
|
120 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
121 Orthanc::ImageAccessor source, target; |
318
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
122 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
123 other.GetReadOnlyAccessor(source); |
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
124 GetWriteableAccessor(target); |
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
125 |
0 | 126 Orthanc::ImageProcessing::Copy(target, source); |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
127 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
128 cairo_surface_mark_dirty(surface_); |
0 | 129 } |
130 | |
131 | |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
132 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
|
133 bool hasAlpha) |
556
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
134 { |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
135 SetSize(source.GetWidth(), source.GetHeight(), hasAlpha); |
556
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
136 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
137 Orthanc::ImageAccessor target; |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
138 GetWriteableAccessor(target); |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
139 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
140 Orthanc::ImageProcessing::Convert(target, source); |
560
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
141 |
aaeec7be8fb7
add support for alpha channel in CairoSurface
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
556
diff
changeset
|
142 cairo_surface_mark_dirty(surface_); |
556
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 |
b1e1eccee214
CairoSurface::Copy()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
439
diff
changeset
|
145 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
146 void CairoSurface::GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const |
0 | 147 { |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
148 target.AssignReadOnly(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); |
0 | 149 } |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
150 |
0 | 151 |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
152 void CairoSurface::GetWriteableAccessor(Orthanc::ImageAccessor& target) |
0 | 153 { |
316
ce48c3b3b0e9
fix for new ImageAccessor API
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
212
diff
changeset
|
154 target.AssignWritable(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); |
0 | 155 } |
156 } |