|
From: <md...@us...> - 2007-09-04 19:52:26
|
Revision: 3778
http://matplotlib.svn.sourceforge.net/matplotlib/?rev=3778&view=rev
Author: mdboom
Date: 2007-09-04 12:52:23 -0700 (Tue, 04 Sep 2007)
Log Message:
-----------
Simplify and fix a memory leak.
Modified Paths:
--------------
trunk/matplotlib/src/ft2font.cpp
trunk/matplotlib/src/ft2font.h
Modified: trunk/matplotlib/src/ft2font.cpp
===================================================================
--- trunk/matplotlib/src/ft2font.cpp 2007-09-04 19:29:45 UTC (rev 3777)
+++ trunk/matplotlib/src/ft2font.cpp 2007-09-04 19:52:23 UTC (rev 3778)
@@ -42,14 +42,14 @@
FT_Library _ft2Library;
-FT2Image::FT2Image() :
- _isDirty(true),
- _buffer(NULL),
- _width(0), _height(0),
- _rgbCopy(NULL),
- _rgbaCopy(NULL) {
- _VERBOSE("FT2Image::FT2Image");
-}
+// FT2Image::FT2Image() :
+// _isDirty(true),
+// _buffer(NULL),
+// _width(0), _height(0),
+// _rgbCopy(NULL),
+// _rgbaCopy(NULL) {
+// _VERBOSE("FT2Image::FT2Image");
+// }
FT2Image::FT2Image(unsigned long width, unsigned long height) :
_isDirty(true),
@@ -65,75 +65,28 @@
_VERBOSE("FT2Image::~FT2Image");
delete [] _buffer;
_buffer=NULL;
+ delete _rgbCopy;
+ delete _rgbaCopy;
}
void FT2Image::resize(unsigned long width, unsigned long height) {
size_t numBytes = width*height;
- if (_width != width || _height != height) {
+ if (width != _width || height != _height) {
+ if (numBytes > _width*_height) {
+ delete [] _buffer;
+ _buffer = new unsigned char [numBytes];
+ }
+
_width = width;
_height = height;
-
- delete [] _buffer;
- _buffer = new unsigned char [numBytes];
}
- for (size_t n=0; n<numBytes; n++)
- _buffer[n] = 0;
+ memset(_buffer, 0, numBytes);
_isDirty = true;
}
-char FT2Image::resize__doc__[] =
-"resize(width, height)\n"
-"\n"
-"Resize the dimensions of the image (it is cleared in the process).\n"
-;
-Py::Object
-FT2Image::py_resize(const Py::Tuple & args) {
- _VERBOSE("FT2Image::resize");
-
- args.verify_length(2);
-
- long x0 = Py::Int(args[0]);
- long y0 = Py::Int(args[1]);
-
- resize(x0, y0);
-
- return Py::Object();
-}
-
-void FT2Image::clear() {
- _VERBOSE("FT2Image::clear");
-
- _width = 0;
- _height = 0;
- _isDirty = true;
- delete [] _buffer;
- _buffer = NULL;
- if (_rgbCopy) {
- delete _rgbCopy;
- _rgbCopy = NULL;
- }
- if (_rgbaCopy) {
- delete _rgbaCopy;
- _rgbaCopy = NULL;
- }
-}
-char FT2Image::clear__doc__[] =
-"clear()\n"
-"\n"
-"Clear the contents of the image.\n"
-;
-Py::Object
-FT2Image::py_clear(const Py::Tuple & args) {
- args.verify_length(0);
-
- clear();
-
- return Py::Object();
-}
-
void
FT2Image::draw_bitmap( FT_Bitmap* bitmap,
FT_Int x,
@@ -345,9 +298,7 @@
unsigned char *dst = _rgbaCopy->_buffer;
while (src != src_end) {
- *dst++ = 0;
- *dst++ = 0;
- *dst++ = 0;
+ dst += 3;
*dst++ = *src++;
}
}
@@ -824,8 +775,7 @@
_VERBOSE("FT2Font::clear");
args.verify_length(0);
- if (image)
- image->clear();
+ delete image;
angle = 0.0;
@@ -1194,11 +1144,9 @@
size_t width = (string_bbox.xMax-string_bbox.xMin) / 64 + 2;
size_t height = (string_bbox.yMax-string_bbox.yMin) / 64 + 2;
- if (!image) {
- image = new FT2Image(width, height);
- } else {
- image->resize(width, height);
- }
+ Py_XDECREF(image);
+ image = NULL;
+ image = new FT2Image(width, height);
for ( size_t n = 0; n < glyphs.size(); n++ )
{
@@ -1764,10 +1712,6 @@
behaviors().name("FT2Image");
behaviors().doc("FT2Image");
- add_varargs_method("clear", &FT2Image::py_clear,
- FT2Image::clear__doc__);
- add_varargs_method("resize", &FT2Image::py_resize,
- FT2Image::resize__doc__);
add_varargs_method("write_bitmap", &FT2Image::py_write_bitmap,
FT2Image::write_bitmap__doc__);
add_varargs_method("draw_rect", &FT2Image::py_draw_rect,
Modified: trunk/matplotlib/src/ft2font.h
===================================================================
--- trunk/matplotlib/src/ft2font.h 2007-09-04 19:29:45 UTC (rev 3777)
+++ trunk/matplotlib/src/ft2font.h 2007-09-04 19:52:23 UTC (rev 3778)
@@ -22,14 +22,12 @@
// the freetype string rendered into a width, height buffer
class FT2Image : public Py::PythonExtension<FT2Image> {
public:
- FT2Image();
+ // FT2Image();
FT2Image(unsigned long width, unsigned long height);
~FT2Image();
static void init_type();
- void resize(unsigned long width, unsigned long height);
- void clear();
void draw_bitmap(FT_Bitmap* bitmap, FT_Int x, FT_Int y);
void write_bitmap(const char* filename) const;
void draw_rect(unsigned long x0, unsigned long y0,
@@ -41,10 +39,6 @@
unsigned int get_height() const { return _height; };
const unsigned char *const get_buffer() const { return _buffer; };
- static char clear__doc__ [];
- Py::Object py_clear(const Py::Tuple & args);
- static char resize__doc__ [];
- Py::Object py_resize(const Py::Tuple & args);
static char write_bitmap__doc__ [];
Py::Object py_write_bitmap(const Py::Tuple & args);
static char draw_rect__doc__ [];
@@ -71,6 +65,8 @@
void makeRgbCopy();
void makeRgbaCopy();
+
+ void resize(unsigned long width, unsigned long height);
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|