@@ -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;
260224Py::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
289258char 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
309276char 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__);
0 commit comments