-
Notifications
You must be signed in to change notification settings - Fork 1
/
wxImageDataObject.cpp
156 lines (135 loc) · 4.55 KB
/
wxImageDataObject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************
* Plugin: XPMEditor for Code::Blocks
* Name: wxImageDataObject.cpp
* Purpose: a class for storing a wxImage in the clipboard - code
* Author: Seb ([email protected])
* Created: 2009-04-23
* Copyright: Seb ()
* License: GPL 3.0
**************************************************************/
#include <wx/wx.h>
#include "wxImageDataObject.h"
/** Constructor
* \param image: the wxImage to copy in the clipboard
*/
wxImageDataObject::wxImageDataObject(const wxImage& image)
: wxDataObjectSimple(wxDF_INVALID)
{
wxDataFormat format;
format.SetId(_("wxImage"));
SetFormat(format);
m_Image = image;
}
/** Return the wxImage currently stored in the object
* \return : the image
*/
wxImage wxImageDataObject::GetImage(void) const
{
return(m_Image);
}
/** Set the image
* \param image : the new image. The old data is deleted
* the image passed in parameters is copied
*/
void wxImageDataObject::SetImage(const wxImage& image)
{
m_Image = image;
}
/** return the size in bytes of the image
* \return the size of the object
*/
size_t wxImageDataObject::GetDataSize(void) const
{
//each pixel is stored using 3 bytes (1 for Red, 1 for Green, 1 for Blus)
//optionally an alpha channel can be used (1 byte / pixel)
size_t sSize;
sSize = sizeof(wxImage);
if (!m_Image.IsOk()) return(0);
/*
sSize = sSize + 3 * m_Image.GetWidth() * m_Image.GetHeight();
if (m_Image.HasAlpha()) sSize = sSize + m_Image.GetWidth() * m_Image.GetHeight();
*/
return(sSize);
}
/** copy the data in the buffer
* \param buf : the buffer to which the data has to be copied
* buf is assumed to be a pointer to a wxImage
* \return true on success, false on failure
*/
bool wxImageDataObject::GetDataHere(void *buf) const
{
if (!buf) return(false);
wxImage *image;
image = (wxImage *) buf;
*image = m_Image;
return(true);
/*
//initialize the image
int iHeight, iWidth;
iHeight = m_Image.GetHeight();
iWidth = m_Image.GetWidth();
image->Create(iWidth, iHeight, true);
//copy the pixel data
unsigned char *SrcData, *DestData;
SrcData = m_Image.GetData();
if (!SrcData) return(false);
DestData = (unsigned char*) malloc(iWidth * iHeight * 3 * sizeof(unsigned char)); //3 bytes per pixels
if (!DestData) return(false);
memcpy(DestData, SrcData, iWidth * iHeight * 3 * sizeof(unsigned char));
image->SetData(DestData);
//copy the alpha channel data if needed
unsigned char *SrcAlpha, *DestAlpha;
if (m_Image.HasAlpha())
{
SrcAlpha = m_Image.GetAlpha();
if (!SrcAlpha) return(false);
DestAlpha = (unsigned char*) malloc(iWidth * iHeight * sizeof(unsigned char)); //1 byte per pixels
if (!DestAlpha) return(false);
memcpy(DestAlpha, SrcAlpha, iWidth * iHeight * sizeof(unsigned char));
image->SetAlpha(DestAlpha);
}
return(true);
*/
}
/** Set the data in the object
* \param len : the size of the buffer buf, in bytes
* \param buf : the buffer containing the data to copy in the object
* the buffer is assumed to be a pointer to a wxImage
* \return true on success, false on failure
*/
bool wxImageDataObject::SetData(size_t WXUNUSED(len), const void *buf)
{
if (!buf) return(false);
wxImage *image;
image = (wxImage *) buf;
m_Image = *image;
return(true);
}
/** return the size in bytes of the image
* \return the size of the object
*/
size_t wxImageDataObject::GetDataSize(const wxDataFormat& WXUNUSED(format)) const
{
return(GetDataSize());
}
/** copy the data in the buffer
* \param format: the format in which the data must be copied (ignored)
* \param buf : the buffer to which the data has to be copied
* \return true on success, false on failure
*/
bool wxImageDataObject::GetDataHere(const wxDataFormat& WXUNUSED(format),
void *buf) const
{
return(GetDataHere(buf));
}
/** Set the data in the object
* \param format: the format in which the data must be copied (ignored)
* \param len : the size of the buffer buf, in bytes
* \param buf : the buffer containing the data to copy in the object
* \return true on success, false on failure
*/
bool wxImageDataObject::SetData(const wxDataFormat& WXUNUSED(format),
size_t len, const void *buf)
{
return(SetData(len, buf));
}