@@ -284,7 +284,7 @@ FT2Image::py_draw_rect_filled(const Py::Tuple & args)
284284PYCXX_VARARGS_METHOD_DECL (FT2Image, py_draw_rect_filled)
285285
286286char FT2Image::as_str__doc__[] =
287- "width, height, s = image_as_str ()\n"
287+ "s = image.as_str ()\n"
288288 "\n"
289289 "Return the image buffer as a string\n"
290290 "\n"
@@ -300,6 +300,70 @@ FT2Image::py_as_str(const Py::Tuple & args)
300300}
301301PYCXX_VARARGS_METHOD_DECL (FT2Image, py_as_str)
302302
303+ char FT2Image::as_rgba_str__doc__[] =
304+ "s = image.as_rgba_str()\n"
305+ "\n"
306+ "Return the image buffer as a RGBA string\n"
307+ "\n"
308+ ;
309+ Py::Object
310+ FT2Image::py_as_rgba_str (const Py::Tuple & args)
311+ {
312+ _VERBOSE (" FT2Image::as_str" );
313+ args.verify_length (0 );
314+
315+ Py_ssize_t size = _width*_height*4 ;
316+ PyObject* result = PyBytes_FromStringAndSize (NULL , size);
317+
318+ unsigned char *src = _buffer;
319+ unsigned char *src_end = src + (_width * _height);
320+ unsigned char *dst = (unsigned char *)PyBytes_AS_STRING (result);
321+
322+ while (src != src_end)
323+ {
324+ *dst++ = 0 ;
325+ *dst++ = 0 ;
326+ *dst++ = 0 ;
327+ *dst++ = *src++;
328+ }
329+
330+ return Py::asObject (result);
331+ }
332+ PYCXX_VARARGS_METHOD_DECL (FT2Image, py_as_rgba_str)
333+
334+ /* TODO: This could take a color as an argument, but for
335+ now it defaults to black on white background */
336+ char FT2Image::as_rgb_str__doc__[] =
337+ "s = image.as_rgb_str()\n"
338+ "\n"
339+ "Return the image buffer as a RGB string\n"
340+ "\n"
341+ ;
342+ Py::Object
343+ FT2Image::py_as_rgb_str (const Py::Tuple & args)
344+ {
345+ _VERBOSE (" FT2Image::as_str" );
346+ args.verify_length (0 );
347+
348+ Py_ssize_t size = _width*_height*3 ;
349+ PyObject* result = PyBytes_FromStringAndSize (NULL , size);
350+
351+ unsigned char *src = _buffer;
352+ unsigned char *src_end = src + (_width * _height);
353+ unsigned char *dst = (unsigned char *)PyBytes_AS_STRING (result);
354+
355+ while (src != src_end)
356+ {
357+ unsigned char tmp = 255 - *src++;
358+ *dst++ = tmp;
359+ *dst++ = tmp;
360+ *dst++ = tmp;
361+ }
362+
363+ return Py::asObject (result);
364+ }
365+ PYCXX_VARARGS_METHOD_DECL (FT2Image, py_as_rgb_str)
366+
303367char FT2Image::as_array__doc__[] =
304368 "x = image.as_array()\n"
305369 "\n"
@@ -1986,6 +2050,10 @@ FT2Image::init_type(void)
19862050 FT2Image::as_array__doc__);
19872051 PYCXX_ADD_VARARGS_METHOD (as_str, py_as_str,
19882052 FT2Image::as_str__doc__);
2053+ PYCXX_ADD_VARARGS_METHOD (as_rgb_str, py_as_rgb_str,
2054+ FT2Image::as_rgb_str__doc__);
2055+ PYCXX_ADD_VARARGS_METHOD (as_rgba_str, py_as_rgba_str,
2056+ FT2Image::as_rgba_str__doc__);
19892057 PYCXX_ADD_VARARGS_METHOD (get_width, py_get_width,
19902058 " Returns the width of the image" );
19912059 PYCXX_ADD_VARARGS_METHOD (get_height, py_get_height,
0 commit comments