@@ -8,74 +8,87 @@ using namespace std::experimental::drawing;
88image_surface::image_surface (image_surface&& other) : surface(move(other)) {
99 _Create_from_png_fn = move (other._Create_from_png_fn );
1010 _Create_from_png_closure = move (other._Create_from_png_closure );
11+ _Data = move (other._Data );
1112 other._Create_from_png_fn = nullptr ;
1213 other._Create_from_png_closure = nullptr ;
14+ other._Data = nullptr ;
1315}
1416
1517image_surface& image_surface::operator =(image_surface&& other) {
1618 if (this != &other) {
1719 surface::operator =(move (other));
1820 _Create_from_png_fn = move (other._Create_from_png_fn );
1921 _Create_from_png_closure = move (other._Create_from_png_closure );
22+ _Data = move (other._Data );
2023 other._Create_from_png_fn = nullptr ;
2124 other._Create_from_png_closure = nullptr ;
25+ other._Data = nullptr ;
2226 }
2327 return *this ;
2428}
2529
2630image_surface::image_surface (surface::native_handle_type nh, surface::native_handle_type map_of)
2731: surface(nullptr )
2832, _Create_from_png_fn(new ::std::function<void (void * closure, ::std::vector<unsigned char >& data)>)
29- , _Create_from_png_closure() {
33+ , _Create_from_png_closure()
34+ , _Data(nullptr ) {
3035 _Surface = shared_ptr<cairo_surface_t >(nh, [map_of](cairo_surface_t *mapped_surface) {
31- cairo_surface_unmap_image (map_of, mapped_surface); });
36+ cairo_surface_unmap_image (map_of, mapped_surface); }
37+ );
3238}
3339
3440image_surface::image_surface (format format, int width, int height)
3541: surface(nullptr )
3642, _Create_from_png_fn(new ::std::function<void (void * closure, ::std::vector<unsigned char >& data)>)
37- , _Create_from_png_closure() {
43+ , _Create_from_png_closure()
44+ , _Data(nullptr ) {
3845 _Surface = shared_ptr<cairo_surface_t >(cairo_image_surface_create (_Format_to_cairo_format_t (format), width, height), &cairo_surface_destroy);
3946 _Throw_if_failed_status (_Cairo_status_t_to_status (cairo_surface_status (_Surface.get ())));
4047}
4148
4249image_surface::image_surface (vector<unsigned char >& data, format format, int width, int height, int stride)
4350: surface(nullptr )
4451, _Create_from_png_fn(new ::std::function<void (void * closure, ::std::vector<unsigned char >& data)>)
45- , _Create_from_png_closure() {
52+ , _Create_from_png_closure()
53+ , _Data(new vector<unsigned char >) {
4654 assert (stride == format_stride_for_width (format, width));
47- _Surface = shared_ptr<cairo_surface_t >(cairo_image_surface_create_for_data (data.data (), _Format_to_cairo_format_t (format), width, height, stride), &cairo_surface_destroy);
55+ _Data->resize (height * stride);
56+ _Data->assign (begin (data), end (data));
57+ _Surface = shared_ptr<cairo_surface_t >(cairo_image_surface_create_for_data (_Data->data (), _Format_to_cairo_format_t (format), width, height, stride), &cairo_surface_destroy);
4858 _Throw_if_failed_status (_Cairo_status_t_to_status (cairo_surface_status (_Surface.get ())));
4959}
5060
5161image_surface::image_surface (surface& other, format format, int width, int height)
5262: surface(nullptr )
5363, _Create_from_png_fn(new ::std::function<void (void * closure, ::std::vector<unsigned char >& data)>)
54- , _Create_from_png_closure() {
64+ , _Create_from_png_closure()
65+ , _Data(nullptr ) {
5566 _Surface = shared_ptr<cairo_surface_t >(cairo_surface_create_similar_image (other.native_handle (), _Format_to_cairo_format_t (format), width, height), &cairo_surface_destroy);
5667 _Throw_if_failed_status (_Cairo_status_t_to_status (cairo_surface_status (_Surface.get ())));
5768}
5869
5970image_surface::image_surface (const string& filename)
6071: surface(nullptr )
6172, _Create_from_png_fn(new ::std::function<void (void * closure, ::std::vector<unsigned char >& data)>)
62- , _Create_from_png_closure() {
73+ , _Create_from_png_closure()
74+ , _Data(nullptr ) {
6375 _Surface = shared_ptr<cairo_surface_t >(cairo_image_surface_create_from_png (filename.c_str ()), &cairo_surface_destroy);
6476 _Throw_if_failed_status (_Cairo_status_t_to_status (cairo_surface_status (_Surface.get ())));
6577}
6678
6779image_surface::image_surface (::std::function<void (void * closure, vector<unsigned char >& data)> read_fn, void* closure)
6880: surface(nullptr )
6981, _Create_from_png_fn(new function<void (void *, vector<unsigned char >&)>(read_fn))
70- , _Create_from_png_closure(closure) {
82+ , _Create_from_png_closure(closure)
83+ , _Data(nullptr ) {
7184 _Surface = shared_ptr<cairo_surface_t >(cairo_image_surface_create_from_png_stream (&image_surface::_Cairo_create_from_png_stream, this ), &cairo_surface_destroy);
7285 _Throw_if_failed_status (_Cairo_status_t_to_status (cairo_surface_status (_Surface.get ())));
7386 _Create_from_png_closure = nullptr ;
7487 *_Create_from_png_fn = nullptr ;
7588}
7689
7790void image_surface::set_data (vector<unsigned char >& data) {
78- auto expected_size = get_stride () * get_height ();
91+ auto expected_size = static_cast < size_t >( get_stride () * get_height () );
7992 if (data.size () != static_cast <uint64_t >(expected_size)) {
8093 throw drawing_exception (status::invalid_stride);
8194 }
@@ -86,15 +99,14 @@ void image_surface::set_data(vector<unsigned char>& data) {
8699 if (imageData == nullptr ) {
87100 throw drawing_exception (status::null_pointer);
88101 }
89- memmove (imageData, data.data (), expected_size);
102+ memcpy (imageData, data.data (), expected_size);
90103}
91104
92105vector<unsigned char > image_surface::get_data () {
93106 auto required_size = get_stride () * get_height ();
94107 vector<unsigned char > data;
95108 auto imageData = cairo_image_surface_get_data (_Surface.get ());
96109 if (imageData == nullptr ) {
97- // throw drawing_exception(status::null_pointer);
98110 data.clear ();
99111 }
100112 else {
0 commit comments