From 036a1d38fc0868becdecaa883021f947911715c0 Mon Sep 17 00:00:00 2001 From: Joseph Davies Date: Fri, 10 Feb 2012 13:53:04 -0800 Subject: [PATCH] Clean up python binding for image-loading functions. --- .../Plasma/FeatureLib/pfPython/pyImage.cpp | 46 +++---------------- .../FeatureLib/pfPython/pyImageGlue.cpp | 38 ++++----------- 2 files changed, 15 insertions(+), 69 deletions(-) diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp b/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp index 70edf630..bd815f4f 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp @@ -201,26 +201,9 @@ PyObject* pyImage::LoadJPEGFromDisk(const wchar_t* filename, uint16_t width, uin // let's create a nice name for this thing based on the filename std::string name = "PtImageFromDisk_"; - const wchar_t* i = filename; - int charsChecked = 0; - - while (*i != '\\' && *i != '\0' && charsChecked < 1024) - { - i++; - charsChecked++; - } - - if (*i == '\0') - { - i = filename; - } - else - { - i++; - } - - char* cName = hsWStringToString(i); - name = name + cName; + char* filetext = hsWStringToString(filename); + name = name + filetext; + delete[] filetext; hsgResMgr::ResMgr()->NewKey(name.c_str(), theMipmap, plLocation::kGlobalFixedLoc); @@ -246,26 +229,9 @@ PyObject* pyImage::LoadPNGFromDisk(const wchar_t* filename, uint16_t width, uint // let's create a nice name for this thing based on the filename std::string name = "PtImageFromDisk_"; - const wchar_t* i = filename; - int charsChecked = 0; - - while (*i != '\\' && *i != '\0' && charsChecked < 1024) - { - i++; - charsChecked++; - } - - if (*i == '\0') - { - i = filename; - } - else - { - i++; - } - - char* cName = hsWStringToString(i); - name = name + cName; + char* filetext = hsWStringToString(filename); + name = name + filetext; + delete[] filetext; hsgResMgr::ResMgr()->NewKey(name.c_str(), theMipmap, plLocation::kGlobalFixedLoc); diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp b/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp index dbd3f178..7670b48c 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp @@ -285,23 +285,13 @@ PYTHON_GLOBAL_METHOD_DEFINITION(PtLoadJPEGFromDisk, args, "Params: filename,widt PYTHON_RETURN_ERROR; } - if (PyUnicode_Check(filenameObj)) - { - int strLen = PyUnicode_GetSize(filenameObj); - wchar_t* text = new wchar_t[strLen + 1]; - PyUnicode_AsWideChar((PyUnicodeObject*)filenameObj, text, strLen); - text[strLen] = L'\0'; - PyObject* ret = pyImage::LoadJPEGFromDisk(text, width, height); - delete [] text; - return ret; - } - else if (PyString_Check(filenameObj)) + if (PyString_CheckEx(filenameObj)) { - // we'll allow this, just in case something goes weird - char* text = PyString_AsString(filenameObj); + char* text = PyString_AsStringEx(filenameObj); wchar_t* wText = hsStringToWString(text); PyObject* ret = pyImage::LoadJPEGFromDisk(wText, width, height); - delete [] wText; + delete[] wText; + delete[] text; return ret; } else @@ -320,24 +310,13 @@ PYTHON_GLOBAL_METHOD_DEFINITION(PtLoadPNGFromDisk, args, "Params: filename,width PyErr_SetString(PyExc_TypeError, "PtLoadPNGFromDisk expects a string and two unsigned shorts"); PYTHON_RETURN_ERROR; } - - if (PyUnicode_Check(filenameObj)) + if (PyString_CheckEx(filenameObj)) { - int strLen = PyUnicode_GetSize(filenameObj); - wchar_t* text = new wchar_t[strLen + 1]; - PyUnicode_AsWideChar((PyUnicodeObject*)filenameObj, text, strLen); - text[strLen] = L'\0'; - PyObject* ret = pyImage::LoadPNGFromDisk(text, width, height); - delete [] text; - return ret; - } - else if (PyString_Check(filenameObj)) - { - // we'll allow this, just in case something goes weird - char* text = PyString_AsString(filenameObj); + char* text = PyString_AsStringEx(filenameObj); wchar_t* wText = hsStringToWString(text); PyObject* ret = pyImage::LoadPNGFromDisk(wText, width, height); - delete [] wText; + delete[] wText; + delete[] text; return ret; } else @@ -352,5 +331,6 @@ void pyImage::AddPlasmaMethods(std::vector &methods) { #ifndef BUILDING_PYPLASMA PYTHON_GLOBAL_METHOD(methods, PtLoadJPEGFromDisk); + PYTHON_GLOBAL_METHOD(methods, PtLoadPNGFromDisk); #endif } \ No newline at end of file