Skip to content

Commit

Permalink
Add PtFindImage() for looking up images by name.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoikas committed Nov 11, 2024
1 parent 3f37d22 commit 88591ed
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Scripts/Python/plasma/Plasma.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@ def PtFileExists(filename):
"""Returns true if the specified file exists"""
pass

def PtFindImage(name: str) -> Tuple[ptImage]:
"""Find an already loaded image by name"""
...

def PtFindSceneobject(name,ageName):
"""This will try to find a sceneobject based on its name and what age its in
- it will return a ptSceneObject if found- if not found then a NameError exception will happen"""
Expand Down
21 changes: 21 additions & 0 deletions Sources/Plasma/FeatureLib/pfPython/pyImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ You can contact Cyan Worlds, Inc. by email [email protected]

#include "pyImage.h"

#include <algorithm>
#include <vector>

#include <string_theory/format>

#include "hsResMgr.h"
Expand All @@ -52,6 +55,7 @@ You can contact Cyan Worlds, Inc. by email [email protected]
#include "plGImage/plJPEG.h"
#include "plGImage/plMipmap.h"
#include "plGImage/plPNG.h"
#include "plResMgr/plKeyFinder.h"

#include "pyColor.h"
#include "pyGeometry3.h"
Expand Down Expand Up @@ -201,6 +205,23 @@ void pyImage::SaveAsPNG(const plFileName& fileName, const std::multimap<ST::stri
plPNG::Instance().WriteToFile(fileName, this->GetImage(), textFields);
}

PyObject* pyImage::Find(const ST::string& name)
{
std::vector<plKey> foundKeys;
plKeyFinder::Instance().ReallyStupidSubstringSearch(name, plMipmap::Index(), foundKeys);
// Remove anything that isn't loaded - they aren't useful in Python code
std::remove_if(
foundKeys.begin(),
foundKeys.end(),
[](const plKey& key) { return key->ObjectIsLoaded() == nullptr; }
);

PyObject* tup = PyTuple_New(foundKeys.size());
for (size_t i = 0; i < foundKeys.size(); ++i)
PyTuple_SET_ITEM(tup, i, pyImage::New(foundKeys[i]));
return tup;
}

PyObject* pyImage::LoadJPEGFromDisk(const plFileName& filename, uint16_t width, uint16_t height)
{
plMipmap* theMipmap = plJPEG::Instance().ReadFromFile(filename);
Expand Down
2 changes: 2 additions & 0 deletions Sources/Plasma/FeatureLib/pfPython/pyImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ class pyImage
uint32_t GetHeight(); // returns the height of the image
void SaveAsJPEG(const plFileName& fileName, uint8_t quality = 75);
void SaveAsPNG(const plFileName& fileName, const std::multimap<ST::string, ST::string>& textFields = std::multimap<ST::string, ST::string>());

static PyObject* Find(const ST::string& name);
static PyObject* LoadJPEGFromDisk(const plFileName& filename, uint16_t width, uint16_t height); // returns pyImage
static PyObject* LoadPNGFromDisk(const plFileName& filename, uint16_t width, uint16_t height); // returns pyImage
#endif
Expand Down
12 changes: 12 additions & 0 deletions Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,17 @@ void pyImage::AddPlasmaClasses(PyObject *m)
}

#ifndef BUILDING_PYPLASMA
PYTHON_GLOBAL_METHOD_DEFINITION(PtFindImage, args, "Params: name\nFind an already loaded image by by name.")
{
ST::string name;
if (!PyArg_ParseTuple(args, "O&", PyUnicode_STStringConverter, &name)) {
PyErr_SetString(PyExc_TypeError, "PtFindImage expects a string");
PYTHON_RETURN_ERROR;
}

return pyImage::Find(name);
}

PYTHON_GLOBAL_METHOD_DEFINITION(PtLoadJPEGFromDisk, 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.")
{
plFileName filename;
Expand Down Expand Up @@ -283,6 +294,7 @@ void pyImage::AddPlasmaMethods(PyObject* m)
{
#ifndef BUILDING_PYPLASMA
PYTHON_START_GLOBAL_METHOD_TABLE(ptImage)
PYTHON_GLOBAL_METHOD(PtFindImage)
PYTHON_GLOBAL_METHOD(PtLoadJPEGFromDisk)
PYTHON_GLOBAL_METHOD(PtLoadPNGFromDisk)
PYTHON_END_GLOBAL_METHOD_TABLE(m, ptImage)
Expand Down

0 comments on commit 88591ed

Please sign in to comment.