Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9cb48c1

Browse files
author
Steve Chaplin
committed
SC
svn path=/trunk/matplotlib/; revision=2986
1 parent 1cc8238 commit 9cb48c1

File tree

4 files changed

+66
-108
lines changed

4 files changed

+66
-108
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2007-01-15 src/_image.cpp combine buffer_argb32() and buffer_bgra32() into
2+
a new method color_conv(format) - SC
3+
14
2007-01-14 backend_cairo.py: update draw_arc() so that
25
examples/arctest.py looks correct - SC
36

lib/matplotlib/backends/backend_cairo.py

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,19 @@ def _fn_name(): return sys._getframe(1).f_code.co_name
4242
from matplotlib.transforms import Bbox
4343

4444

45-
#if hasattr (cairo.ImageSurface, 'create_for_array'):
46-
# HAVE_CAIRO_NUMPY = True
47-
#else:
48-
# HAVE_CAIRO_NUMPY = False
49-
50-
5145
_debug = False
5246
#_debug = True
5347

5448
# Image formats that this backend supports - for print_figure()
5549
IMAGE_FORMAT = ['eps', 'pdf', 'png', 'ps', 'svg']
5650
IMAGE_FORMAT_DEFAULT = 'png'
5751

52+
# Image::color_conv(format) for draw_image()
53+
if sys.byteorder == 'little':
54+
BYTE_FORMAT = 0 # BGRA
55+
else:
56+
BYTE_FORMAT = 1 # ARGB
57+
5858

5959
class RendererCairo(RendererBase):
6060
fontweights = {
@@ -153,18 +153,9 @@ def draw_image(self, x, y, im, bbox):
153153

154154
im.flipud_out()
155155

156-
if sys.byteorder == 'little':
157-
rows, cols, str_ = im.buffer_bgra32()
158-
else:
159-
rows, cols, str_ = im.buffer_argb32()
160-
161-
X = numx.fromstring (str_, numx.UInt8)
162-
surface = cairo.ImageSurface.create_for_data (X, cairo.FORMAT_ARGB32,
163-
rows, cols, rows*4)
164-
# It would be simpler if im.buffer_*() returned a writeable buffer
165-
# instead of a string, we could then bypass numx completely:
166-
#surface = cairo.ImageSurface.create_for_data (
167-
# str_, cairo.FORMAT_ARGB32, rows, cols, rows*4)
156+
rows, cols, buf = im.color_conv (BYTE_FORMAT)
157+
surface = cairo.ImageSurface.create_for_data (
158+
buf, cairo.FORMAT_ARGB32, rows, cols, rows*4)
168159

169160
# function does not pass a 'gc' so use renderer.ctx
170161
ctx = self.ctx

src/_image.cpp

Lines changed: 30 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -212,78 +212,47 @@ Image::as_rgba_str(const Py::Tuple& args, const Py::Dict& kwargs) {
212212

213213
if (bufpair.second) delete [] bufpair.first;
214214
return ret;
215-
216-
217-
}
218-
219-
220-
char Image::buffer_argb32__doc__[] =
221-
"buffer = buffer_argb32()"
222-
"\n"
223-
"Return the image buffer as agbr32\n"
224-
;
225-
Py::Object
226-
Image::buffer_argb32(const Py::Tuple& args) {
227-
//"Return the image object as argb32";
228-
229-
_VERBOSE("RendererAgg::buffer_argb32");
230-
231-
args.verify_length(0);
232-
233-
int row_len = colsOut * 4;
234-
235-
unsigned char* buf_tmp = new unsigned char[row_len * rowsOut];
236-
if (buf_tmp ==NULL)
237-
throw Py::MemoryError("RendererAgg::buffer_argb32 could not allocate memory");
238-
239-
agg::rendering_buffer rtmp;
240-
rtmp.attach(buf_tmp, colsOut, rowsOut, row_len);
241-
242-
agg::color_conv(&rtmp, rbufOut, agg::color_conv_rgba32_to_argb32());
243-
244-
//todo: how to do this with native CXX
245-
//PyObject* o = Py_BuildValue("s#", buf_tmp, row_len * rowsOut);
246-
PyObject* o = Py_BuildValue("lls#", rowsOut, colsOut,
247-
buf_tmp, row_len * rowsOut);
248-
delete [] buf_tmp;
249-
return Py::asObject(o);
250-
251-
252215
}
253216

254217

255-
char Image::buffer_bgra32__doc__[] =
256-
"buffer = buffer_bgra32()"
218+
char Image::color_conv__doc__[] =
219+
"numrows, numcols, buffer = color_conv(format)"
257220
"\n"
258-
"Return the image buffer as agbr32\n"
221+
"format 0(BGRA) or 1(ARGB)\n"
222+
"Convert image to format and return in a writable buffer\n"
259223
;
260224
Py::Object
261-
Image::buffer_bgra32(const Py::Tuple& args) {
262-
//"Return the image object as bgra32";
225+
Image::color_conv(const Py::Tuple& args) {
226+
_VERBOSE("Image::color_conv");
263227

264-
_VERBOSE("RendererAgg::buffer_bgra32");
265-
266-
args.verify_length(0);
228+
args.verify_length(1);
229+
int format = Py::Int(args[0]);
267230

268231
int row_len = colsOut * 4;
269-
270-
unsigned char* buf_tmp = new unsigned char[row_len * rowsOut];
271-
if (buf_tmp ==NULL)
272-
throw Py::MemoryError("RendererAgg::buffer_bgra32 could not allocate memory");
232+
PyObject* py_buffer = PyBuffer_New(row_len * rowsOut);
233+
if (py_buffer ==NULL)
234+
throw Py::MemoryError("Image::color_conv could not allocate memory");
235+
unsigned char* buf;
236+
int buffer_len;
237+
int ret = PyObject_AsWriteBuffer(py_buffer, (void **)&buf, &buffer_len);
238+
if (ret !=0)
239+
throw Py::MemoryError("Image::color_conv could not allocate memory");
273240

274241
agg::rendering_buffer rtmp;
275-
rtmp.attach(buf_tmp, colsOut, rowsOut, row_len);
276-
277-
agg::color_conv(&rtmp, rbufOut, agg::color_conv_rgba32_to_bgra32());
278-
279-
//todo: how to do this with native CXX
280-
//PyObject* o = Py_BuildValue("s#", buf_tmp, row_len * rowsOut);
281-
PyObject* o = Py_BuildValue("lls#", rowsOut, colsOut,
282-
buf_tmp, row_len * rowsOut);
283-
delete [] buf_tmp;
242+
rtmp.attach(buf, colsOut, rowsOut, row_len);
243+
244+
switch (format) {
245+
case 0:
246+
agg::color_conv(&rtmp, rbufOut, agg::color_conv_rgba32_to_bgra32());
247+
break;
248+
case 1:
249+
agg::color_conv(&rtmp, rbufOut, agg::color_conv_rgba32_to_argb32());
250+
break;
251+
default:
252+
throw Py::ValueError("Image::color_conv unknown format");
253+
}
254+
PyObject* o = Py_BuildValue("llN", rowsOut, colsOut, py_buffer);
284255
return Py::asObject(o);
285-
286-
287256
}
288257

289258
char Image::buffer_rgba__doc__[] =
@@ -302,8 +271,6 @@ Image::buffer_rgba(const Py::Tuple& args) {
302271
PyObject* o = Py_BuildValue("lls#", rowsOut, colsOut,
303272
rbufOut, row_len * rowsOut);
304273
return Py::asObject(o);
305-
306-
307274
}
308275

309276
char Image::reset_matrix__doc__[] =
@@ -785,8 +752,7 @@ Image::init_type() {
785752
add_varargs_method( "apply_scaling", &Image::apply_scaling, Image::apply_scaling__doc__);
786753
add_varargs_method( "apply_translation", &Image::apply_translation, Image::apply_translation__doc__);
787754
add_keyword_method( "as_rgba_str", &Image::as_rgba_str, Image::as_rgba_str__doc__);
788-
add_varargs_method( "buffer_argb32", &Image::buffer_argb32, Image::buffer_argb32__doc__);
789-
add_varargs_method( "buffer_bgra32", &Image::buffer_bgra32, Image::buffer_bgra32__doc__);
755+
add_varargs_method( "color_conv", &Image::color_conv, Image::color_conv__doc__);
790756
add_varargs_method( "buffer_rgba", &Image::buffer_rgba, Image::buffer_rgba__doc__);
791757
add_varargs_method( "get_aspect", &Image::get_aspect, Image::get_aspect__doc__);
792758
add_varargs_method( "get_interpolation", &Image::get_interpolation, Image::get_interpolation__doc__);

src/_image.h

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* image.h
1+
/* image.h
22
*
33
*/
44

@@ -27,8 +27,7 @@ class Image : public Py::PythonExtension<Image> {
2727
Py::Object apply_scaling(const Py::Tuple& args);
2828
Py::Object apply_translation(const Py::Tuple& args);
2929
Py::Object as_rgba_str(const Py::Tuple& args, const Py::Dict& kwargs);
30-
Py::Object buffer_argb32(const Py::Tuple& args);
31-
Py::Object buffer_bgra32(const Py::Tuple& args);
30+
Py::Object color_conv(const Py::Tuple& args);
3231
Py::Object buffer_rgba(const Py::Tuple& args);
3332
Py::Object reset_matrix(const Py::Tuple& args);
3433
Py::Object get_matrix(const Py::Tuple& args);
@@ -54,31 +53,31 @@ class Image : public Py::PythonExtension<Image> {
5453
HANNING,
5554
HAMMING,
5655
HERMITE,
57-
KAISER,
58-
QUADRIC,
59-
CATROM,
60-
GAUSSIAN,
61-
BESSEL,
62-
MITCHELL,
63-
SINC,
64-
LANCZOS,
56+
KAISER,
57+
QUADRIC,
58+
CATROM,
59+
GAUSSIAN,
60+
BESSEL,
61+
MITCHELL,
62+
SINC,
63+
LANCZOS,
6564
BLACKMAN,};
6665

67-
//enum { BICUBIC=0, BILINEAR, BLACKMAN100, BLACKMAN256, BLACKMAN64,
66+
//enum { BICUBIC=0, BILINEAR, BLACKMAN100, BLACKMAN256, BLACKMAN64,
6867
// NEAREST, SINC144, SINC256, SINC64, SPLINE16, SPLINE36};
6968
enum { ASPECT_PRESERVE=0, ASPECT_FREE};
7069

7170
agg::int8u *bufferIn;
7271
agg::rendering_buffer *rbufIn;
73-
size_t colsIn, rowsIn;
72+
size_t colsIn, rowsIn;
7473

7574
agg::int8u *bufferOut;
7675
agg::rendering_buffer *rbufOut;
77-
size_t colsOut, rowsOut;
76+
size_t colsOut, rowsOut;
7877
unsigned BPP;
7978

8079
unsigned interpolation, aspect;
81-
agg::rgba bg;
80+
agg::rgba bg;
8281
private:
8382
Py::Dict __dict__;
8483
agg::trans_affine srcMatrix, imageMatrix;
@@ -87,8 +86,7 @@ class Image : public Py::PythonExtension<Image> {
8786
static char apply_scaling__doc__[];
8887
static char apply_translation__doc__[];
8988
static char as_rgba_str__doc__[];
90-
static char buffer_argb32__doc__[];
91-
static char buffer_bgra32__doc__[];
89+
static char color_conv__doc__[];
9290
static char buffer_rgba__doc__[];
9391
static char reset_matrix__doc__[];
9492
static char get_matrix__doc__[];
@@ -122,25 +120,25 @@ class _image_module : public Py::ExtensionModule<_image_module>
122120
{
123121
Image::init_type();
124122

125-
add_varargs_method("fromarray", &_image_module::fromarray,
123+
add_varargs_method("fromarray", &_image_module::fromarray,
126124
"fromarray");
127-
add_varargs_method("fromarray2", &_image_module::fromarray2,
125+
add_varargs_method("fromarray2", &_image_module::fromarray2,
128126
"fromarray2");
129-
add_varargs_method("frombyte", &_image_module::frombyte,
127+
add_varargs_method("frombyte", &_image_module::frombyte,
130128
"frombyte");
131-
add_varargs_method("frombuffer", &_image_module::frombuffer,
129+
add_varargs_method("frombuffer", &_image_module::frombuffer,
132130
"frombuffer");
133-
add_varargs_method("readpng", &_image_module::readpng,
131+
add_varargs_method("readpng", &_image_module::readpng,
134132
"readpng");
135-
add_varargs_method("from_images", &_image_module::from_images,
133+
add_varargs_method("from_images", &_image_module::from_images,
136134
"from_images");
137135
add_varargs_method("pcolor", &_image_module::pcolor,
138136
"pcolor");
139137
initialize( "The _image module" );
140138
}
141-
142-
~_image_module() {}
143-
139+
140+
~_image_module() {}
141+
144142
private:
145143
Py::Object frombyte (const Py::Tuple &args);
146144
Py::Object frombuffer (const Py::Tuple &args);

0 commit comments

Comments
 (0)