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

Skip to content

Commit 890ee59

Browse files
committed
Merge pull request #578 from mdboom/ft2font-as_rgba_str
Reinstate FT2Image.as_rgb_str() and FT2Image.as_rgba_str().
2 parents f1adc64 + e611471 commit 890ee59

2 files changed

Lines changed: 73 additions & 4 deletions

File tree

src/ft2font.cpp

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ FT2Image::py_draw_rect_filled(const Py::Tuple & args)
284284
PYCXX_VARARGS_METHOD_DECL(FT2Image, py_draw_rect_filled)
285285

286286
char 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
}
301301
PYCXX_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+
303367
char 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,

src/ft2font.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class FT2Image : public Py::PythonClass<FT2Image>
6161
Py::Object py_as_array(const Py::Tuple & args);
6262
static char as_str__doc__ [];
6363
Py::Object py_as_str(const Py::Tuple & args);
64+
static char as_rgb_str__doc__ [];
65+
Py::Object py_as_rgb_str(const Py::Tuple & args);
66+
static char as_rgba_str__doc__ [];
67+
Py::Object py_as_rgba_str(const Py::Tuple & args);
6468
Py::Object py_get_width(const Py::Tuple & args);
6569
Py::Object py_get_height(const Py::Tuple & args);
6670

@@ -70,9 +74,6 @@ class FT2Image : public Py::PythonClass<FT2Image>
7074
unsigned long _width;
7175
unsigned long _height;
7276

73-
void makeRgbCopy();
74-
void makeRgbaCopy();
75-
7677
void resize(long width, long height);
7778
};
7879

0 commit comments

Comments
 (0)