From 3b22256085ecfcb19e1e4a59cdc766f65e713a35 Mon Sep 17 00:00:00 2001 From: Joseph Davies Date: Fri, 10 Feb 2012 00:12:52 -0800 Subject: [PATCH] Add python function glue to allow loading PNG images from disk. --- .../Plasma/FeatureLib/pfPython/pyImage.cpp | 45 +++++++++++++++++++ Sources/Plasma/FeatureLib/pfPython/pyImage.h | 1 + .../FeatureLib/pfPython/pyImageGlue.cpp | 38 +++++++++++++++- 3 files changed, 83 insertions(+), 1 deletion(-) diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp b/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp index 3a7c3bd6..70edf630 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp @@ -230,4 +230,49 @@ PyObject* pyImage::LoadJPEGFromDisk(const wchar_t* filename, uint16_t width, uin PYTHON_RETURN_NONE; } +PyObject* pyImage::LoadPNGFromDisk(const wchar_t* filename, uint16_t width, uint16_t height) +{ + plMipmap* theMipmap = plPNG::Instance().ReadFromFile(filename); + if (theMipmap) + { + if (width > 0 && height > 0) + { + if (!theMipmap->ResizeNicely(width, height, plMipmap::kDefaultFilter)) + { + delete theMipmap; + PYTHON_RETURN_NONE; + } + } + + // 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; + + hsgResMgr::ResMgr()->NewKey(name.c_str(), theMipmap, plLocation::kGlobalFixedLoc); + + return pyImage::New( theMipmap ); + } + else + PYTHON_RETURN_NONE; +} + #endif diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImage.h b/Sources/Plasma/FeatureLib/pfPython/pyImage.h index 86e9337f..95c2a810 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImage.h +++ b/Sources/Plasma/FeatureLib/pfPython/pyImage.h @@ -168,6 +168,7 @@ public: void SaveAsJPEG(const wchar_t* fileName, uint8_t quality = 75); void SaveAsPNG(const wchar_t* fileName); static PyObject* LoadJPEGFromDisk(const wchar_t* filename, uint16_t width, uint16_t height); // returns pyImage + static PyObject* LoadPNGFromDisk(const wchar_t* filename, uint16_t width, uint16_t height); // returns pyImage #endif }; diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp b/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp index bf0c1c01..dbd3f178 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp @@ -306,7 +306,43 @@ PYTHON_GLOBAL_METHOD_DEFINITION(PtLoadJPEGFromDisk, args, "Params: filename,widt } else { - PyErr_SetString(PyExc_TypeError, "saveAsJPEG expects a string and a unsigned 8-bit int"); + PyErr_SetString(PyExc_TypeError, "PtLoadJPEGFromDisk expects a string and two unsigned shorts"); + PYTHON_RETURN_ERROR; + } +} + +PYTHON_GLOBAL_METHOD_DEFINITION(PtLoadPNGFromDisk, args, "Params: filename,width,height\nThe image will be resized to fit the width and height arguments. Set to 0 if resizing is not desired.\nReturns a pyImage of the specified file.") +{ + PyObject* filenameObj; + unsigned short width, height; + if (!PyArg_ParseTuple(args, "Ohh", &filenameObj, &width, &height)) + { + PyErr_SetString(PyExc_TypeError, "PtLoadPNGFromDisk expects a string and two unsigned shorts"); + 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::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); + wchar_t* wText = hsStringToWString(text); + PyObject* ret = pyImage::LoadPNGFromDisk(wText, width, height); + delete [] wText; + return ret; + } + else + { + PyErr_SetString(PyExc_TypeError, "PtLoadPNGFromDisk expects a string and two unsigned shorts"); PYTHON_RETURN_ERROR; } }