comparison Framework/Viewport/CairoSurface.cpp @ 560:aaeec7be8fb7

add support for alpha channel in CairoSurface
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 17 Apr 2019 17:57:50 +0200
parents b1e1eccee214
children
comparison
equal deleted inserted replaced
559:9e61b0ac12f1 560:aaeec7be8fb7
36 } 36 }
37 } 37 }
38 38
39 39
40 void CairoSurface::Allocate(unsigned int width, 40 void CairoSurface::Allocate(unsigned int width,
41 unsigned int height) 41 unsigned int height,
42 bool hasAlpha)
42 { 43 {
43 Release(); 44 Release();
44 45
45 surface_ = cairo_image_surface_create(CAIRO_FORMAT_RGB24, width, height); 46 hasAlpha_ = hasAlpha;
47
48 surface_ = cairo_image_surface_create
49 (hasAlpha ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24, width, height);
46 if (!surface_) 50 if (!surface_)
47 { 51 {
48 // Should never occur 52 // Should never occur
49 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 53 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
50 } 54 }
61 pitch_ = cairo_image_surface_get_stride(surface_); 65 pitch_ = cairo_image_surface_get_stride(surface_);
62 buffer_ = cairo_image_surface_get_data(surface_); 66 buffer_ = cairo_image_surface_get_data(surface_);
63 } 67 }
64 68
65 69
66 CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor) 70 CairoSurface::CairoSurface(Orthanc::ImageAccessor& accessor,
71 bool hasAlpha) :
72 hasAlpha_(hasAlpha)
67 { 73 {
68 if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32) 74 if (accessor.GetFormat() != Orthanc::PixelFormat_BGRA32)
69 { 75 {
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat); 76 throw Orthanc::OrthancException(Orthanc::ErrorCode_IncompatibleImageFormat);
71 } 77 }
74 height_ = accessor.GetHeight(); 80 height_ = accessor.GetHeight();
75 pitch_ = accessor.GetPitch(); 81 pitch_ = accessor.GetPitch();
76 buffer_ = accessor.GetBuffer(); 82 buffer_ = accessor.GetBuffer();
77 83
78 surface_ = cairo_image_surface_create_for_data 84 surface_ = cairo_image_surface_create_for_data
79 (reinterpret_cast<unsigned char*>(buffer_), CAIRO_FORMAT_RGB24, width_, height_, pitch_); 85 (reinterpret_cast<unsigned char*>(buffer_),
86 hasAlpha_ ? CAIRO_FORMAT_ARGB32 : CAIRO_FORMAT_RGB24,
87 width_, height_, pitch_);
80 if (!surface_) 88 if (!surface_)
81 { 89 {
82 // Should never occur 90 // Should never occur
83 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 91 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
84 } 92 }
91 } 99 }
92 } 100 }
93 101
94 102
95 void CairoSurface::SetSize(unsigned int width, 103 void CairoSurface::SetSize(unsigned int width,
96 unsigned int height) 104 unsigned int height,
105 bool hasAlpha)
97 { 106 {
98 if (width_ != width || 107 if (hasAlpha_ != hasAlpha ||
108 width_ != width ||
99 height_ != height) 109 height_ != height)
100 { 110 {
101 Allocate(width, height); 111 Allocate(width, height, hasAlpha);
102 } 112 }
103 } 113 }
104 114
105 115
106 void CairoSurface::Copy(const CairoSurface& other) 116 void CairoSurface::Copy(const CairoSurface& other)
107 { 117 {
118 SetSize(other.GetWidth(), other.GetHeight(), other.HasAlpha());
119
108 Orthanc::ImageAccessor source, target; 120 Orthanc::ImageAccessor source, target;
109 121
110 other.GetReadOnlyAccessor(source); 122 other.GetReadOnlyAccessor(source);
111 GetWriteableAccessor(target); 123 GetWriteableAccessor(target);
112 124
113 Orthanc::ImageProcessing::Copy(target, source); 125 Orthanc::ImageProcessing::Copy(target, source);
126
127 cairo_surface_mark_dirty(surface_);
114 } 128 }
115 129
116 130
117 void CairoSurface::Copy(const Orthanc::ImageAccessor& source) 131 void CairoSurface::Copy(const Orthanc::ImageAccessor& source,
132 bool hasAlpha)
118 { 133 {
119 SetSize(source.GetWidth(), source.GetHeight()); 134 SetSize(source.GetWidth(), source.GetHeight(), hasAlpha);
120 135
121 Orthanc::ImageAccessor target; 136 Orthanc::ImageAccessor target;
122 GetWriteableAccessor(target); 137 GetWriteableAccessor(target);
123 138
124 Orthanc::ImageProcessing::Convert(target, source); 139 Orthanc::ImageProcessing::Convert(target, source);
140
141 cairo_surface_mark_dirty(surface_);
125 } 142 }
126 143
127 144
128 void CairoSurface::GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const 145 void CairoSurface::GetReadOnlyAccessor(Orthanc::ImageAccessor& target) const
129 { 146 {