Browse Source

Add python function glue to allow loading PNG images from disk.

Joseph Davies 13 years ago
parent
commit
3b22256085
  1. 45
      Sources/Plasma/FeatureLib/pfPython/pyImage.cpp
  2. 1
      Sources/Plasma/FeatureLib/pfPython/pyImage.h
  3. 38
      Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp

45
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

1
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
};

38
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;
}
}

Loading…
Cancel
Save