Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Wrappers/CairoContext.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 "CairoContext.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> |
0 | 27 |
339 | 28 |
0 | 29 namespace OrthancStone |
30 { | |
339 | 31 CairoContext::CairoContext(CairoSurface& surface) : |
32 width_(surface.GetWidth()), | |
33 height_(surface.GetHeight()) | |
0 | 34 { |
35 context_ = cairo_create(surface.GetObject()); | |
36 if (!context_) | |
37 { | |
38 LOG(ERROR) << "Cannot create Cairo drawing context"; | |
39 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
40 } | |
41 } | |
42 | |
43 | |
44 CairoContext::~CairoContext() | |
45 { | |
46 if (context_ != NULL) | |
47 { | |
48 cairo_destroy(context_); | |
49 context_ = NULL; | |
50 } | |
51 } | |
52 | |
53 | |
54 void CairoContext::SetSourceColor(uint8_t red, | |
55 uint8_t green, | |
56 uint8_t blue) | |
57 { | |
58 cairo_set_source_rgb(context_, | |
59 static_cast<float>(red) / 255.0f, | |
60 static_cast<float>(green) / 255.0f, | |
61 static_cast<float>(blue) / 255.0f); | |
62 } | |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
63 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
64 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
65 class CairoContext::AlphaSurface : public boost::noncopyable |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
66 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
67 private: |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
68 cairo_surface_t *surface_; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
69 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
70 public: |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
71 AlphaSurface(unsigned int width, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
72 unsigned int height) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
73 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
74 surface_ = cairo_image_surface_create(CAIRO_FORMAT_A8, width, height); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
75 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
76 if (!surface_) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
77 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
78 // Should never occur |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
79 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
80 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
81 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
82 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
83 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
84 LOG(ERROR) << "Cannot create a Cairo surface"; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
85 cairo_surface_destroy(surface_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
86 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
87 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
88 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
89 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
90 ~AlphaSurface() |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
91 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
92 cairo_surface_destroy(surface_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
93 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
94 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
95 void GetAccessor(Orthanc::ImageAccessor& target) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
96 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
97 target.AssignWritable(Orthanc::PixelFormat_Grayscale8, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
98 cairo_image_surface_get_width(surface_), |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
99 cairo_image_surface_get_height(surface_), |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
100 cairo_image_surface_get_stride(surface_), |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
101 cairo_image_surface_get_data(surface_)); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
102 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
103 |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
104 void Blit(cairo_t* cr, |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
105 double x, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
106 double y) |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
107 { |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
108 cairo_surface_mark_dirty(surface_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
109 cairo_mask_surface(cr, surface_, x, y); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
110 cairo_fill(cr); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
111 } |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
112 }; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
113 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
114 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
115 void CairoContext::DrawText(const Orthanc::Font& font, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
116 const std::string& text, |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
117 double x, |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
118 double y, |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
119 BitmapAnchor anchor) |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
120 { |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
121 // Render a bitmap containing the text |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
122 unsigned int width, height; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
123 font.ComputeTextExtent(width, height, text); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
124 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
125 AlphaSurface surface(width, height); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
126 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
127 Orthanc::ImageAccessor accessor; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
128 surface.GetAccessor(accessor); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
129 font.Draw(accessor, text, 0, 0, 255); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
130 |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
131 // Correct the text location given the anchor location |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
132 double deltaX, deltaY; |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
133 ComputeAnchorTranslation(deltaX, deltaY, anchor, width, height); |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
134 |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
135 // Cancel zoom/rotation before blitting the text onto the surface |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
136 double pixelX = x; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
137 double pixelY = y; |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
138 cairo_user_to_device(context_, &pixelX, &pixelY); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
139 |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
140 cairo_save(context_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
141 cairo_identity_matrix(context_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
142 |
366
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
143 // Blit the text bitmap |
a7de01c8fd29
new enum BitmapAnchor
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
365
diff
changeset
|
144 surface.Blit(context_, pixelX + deltaX, pixelY + deltaY); |
365
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
145 cairo_restore(context_); |
ef31240a73f6
no automatic call to moc and uic, CircleMeasureTracker using Orthanc fonts
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
339
diff
changeset
|
146 } |
0 | 147 } |