Mercurial > hg > orthanc-stone
annotate Framework/Viewport/CairoSurface.cpp @ 366:a7de01c8fd29 am-2
new enum BitmapAnchor
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 01 Nov 2018 11:55:45 +0100 |
parents | 3a4ca166fafa |
children | 557c8ff1db5c |
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 | |
134
4cff7b1ed31d
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
47
diff
changeset
|
5 * Copyright (C) 2017-2018 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, | |
41 unsigned int height) | |
42 { | |
43 Release(); | |
44 | |
45 surface_ = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); | |
46 if (!surface_) | |
47 { | |
48 // Should never occur | |
49 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
50 } | |
51 | |
52 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
53 { | |
54 LOG(ERROR) << "Cannot create a Cairo surface"; | |
55 cairo_surface_destroy(surface_); | |
56 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
57 } | |
58 | |
59 width_ = width; | |
60 height_ = height; | |
61 pitch_ = cairo_image_surface_get_stride(surface_); | |
62 buffer_ = cairo_image_surface_get_data(surface_); | |
63 } | |
64 | |
65 | |
66 CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor) | |
67 { | |
68 if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) | |
69 { | |
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); | |
71 } | |
72 | |
73 width_ = accessor.GetWidth(); | |
74 height_ = accessor.GetHeight(); | |
75 pitch_ = accessor.GetPitch(); | |
76 buffer_ = accessor.GetBuffer(); | |
77 | |
78 surface_ = cairo_image_surface_create_for_data | |
79 (reinterpret_cast<unsigned char*>(buffer_), CAIRO_FORMAT_RGB24, width_, height_, pitch_); | |
80 if (!surface_) | |
81 { | |
82 // Should never occur | |
83 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
84 } | |
85 | |
86 if (cairo_surface_status(surface_) != CAIRO_STATUS_SUCCESS) | |
87 { | |
88 LOG(ERROR) << "Bad pitch for a Cairo surface"; | |
89 cairo_surface_destroy(surface_); | |
90 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
91 } | |
92 } | |
93 | |
94 | |
95 void CairoSurface::SetSize(unsigned int width, | |
96 unsigned int height) | |
97 { | |
98 if (width_ != width || | |
99 height_ != height) | |
100 { | |
101 Allocate(width, height); | |
102 } | |
103 } | |
104 | |
105 | |
106 void CairoSurface::Copy(const CairoSurface& other) | |
107 { | |
318
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
108 Orthanc::ImageAccessor source, target; |
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
109 |
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
110 other.GetConstAccessor(source); |
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
111 GetAccessor(target); |
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
112 |
0 | 113 Orthanc::ImageProcessing::Copy(target, source); |
114 } | |
115 | |
116 | |
318
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
117 void CairoSurface::GetConstAccessor(Orthanc::ImageAccessor& target) const |
0 | 118 { |
318
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
119 target.AssignReadOnly(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); |
0 | 120 } |
121 | |
122 | |
318
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
123 void CairoSurface::GetAccessor(Orthanc::ImageAccessor& target) |
0 | 124 { |
318
3a4ca166fafa
ImageAccessor refactoring + implemented Image Cache in SmartLoader
am@osimis.io
parents:
212
diff
changeset
|
125 target.AssignWritable(Orthanc::PixelFormat_BGRA32, width_, height_, pitch_, buffer_); |
0 | 126 } |
127 } |