Skip to content

Commit

Permalink
Merge pull request #291 from Hoikas/image_roundtrip
Browse files Browse the repository at this point in the history
Add partial bindings for `plJPEG` and `plPNG`.
  • Loading branch information
zrax authored Dec 6, 2024
2 parents d1386cf + 84ae5fa commit b04ad4a
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 82 deletions.
4 changes: 4 additions & 0 deletions Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -684,10 +684,14 @@ set(SYS_HEADERS
set(UTIL_SOURCES
Util/pyBitVector.cpp
Util/pyDDSurface.cpp
Util/pyJPEG.cpp
Util/pyPNG.cpp
)
set(UTIL_HEADERS
Util/pyBitVector.h
Util/pyDDSurface.h
Util/pyJPEG.h
Util/pyPNG.h
)

set(VAULT_SOURCES
Expand Down
4 changes: 4 additions & 0 deletions Python/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#include "Sys/pyUnifiedTime.h"
#include "Util/pyBitVector.h"
#include "Util/pyDDSurface.h"
#include "Util/pyJPEG.h"
#include "Util/pyPNG.h"
#include "Vault/pyServerGuid.h"
#include "Vault/pyVaultNode.h"
#include "Vault/pyVaultStore.h"
Expand Down Expand Up @@ -487,6 +489,8 @@ PyMODINIT_FUNC PyInit_PyHSPlasma()
/* Util */
PyModule_AddObject(module, "hsBitVector", Init_pyBitVector_Type());
PyModule_AddObject(module, "plDDSurface", Init_pyDDSurface_Type());
PyModule_AddObject(module, "plJPEG", Init_pyJPEG_Type());
PyModule_AddObject(module, "plPNG", Init_pyPNG_Type());

/* Vault */
PyModule_AddObject(module, "plServerGuid", Init_pyServerGuid_Type());
Expand Down
10 changes: 10 additions & 0 deletions Python/PyHSPlasma.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3757,6 +3757,11 @@ class plInterfaceInfoModifier(plSingleModifier):
def clearIntfKeys(self) -> None: ...
def delIntfKey(self, idx: int) -> None: ...

class plJPEG:
@staticmethod
def DecompressJPEG(stream: hsStream) -> plMipmap:
...

class plKey(Generic[KeyedT]):
id: int = ...
location: plLocation = ...
Expand Down Expand Up @@ -4482,6 +4487,11 @@ class plPhysicalSndGroup(hsKeyedObject):
class plPickingDetector(plDetectorModifier):
...

class plPNG:
@staticmethod
def DecompressPNG(stream: hsStream) -> plMipmap:
...

class plPoint3Controller(plLeafController):
...

Expand Down
73 changes: 73 additions & 0 deletions Python/Util/pyJPEG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* This file is part of HSPlasma.
*
* HSPlasma is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HSPlasma is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HSPlasma. If not, see <http://www.gnu.org/licenses/>.
*/

#include "pyJPEG.h"

#include "PRP/Surface/pyBitmap.h"
#include "Stream/pyStream.h"

#include <Util/plJPEG.h>

PY_PLASMA_EMPTY_INIT(JPEG)
PY_PLASMA_NEW_MSG(JPEG, "plJPEG cannot be constructed")

PY_METHOD_STATIC_VA(JPEG, DecompressJPEG,
"Params: stream\n"
"Read JPEG file from stream directly into a plMipmap")
{
PyObject* streamObj;
if (!PyArg_ParseTuple(args, "O", &streamObj)) {
PyErr_SetString(PyExc_TypeError, "DecompressJPEG expects an hsStream");
return nullptr;
}
if (!pyStream_Check(streamObj)) {
PyErr_SetString(PyExc_TypeError, "DecompressJPEG expects an hsStream");
return nullptr;
}

plMipmap* mm = plJPEG::DecompressJPEG(((pyStream*)streamObj)->fThis);

// We're doing this manually because the new Mipmap object is being
// released to Python code.
pyMipmap* mmObj = nullptr;
try {
mmObj = PyObject_New(pyMipmap, &pyMipmap_Type);
} catch (const hsJPEGException& ex) {
PyErr_SetString(PyExc_RuntimeError, ex.what());
return nullptr;
}
mmObj->fPyOwned = true;
mmObj->fThis = mm;
return (PyObject*)mmObj;
}

static PyMethodDef pyJPEG_Methods[] = {
pyJPEG_DecompressJPEG_method,
PY_METHOD_TERMINATOR,
};

PY_PLASMA_TYPE(JPEG, plJPEG, "plJPEG wrapper")

PY_PLASMA_TYPE_INIT(JPEG)
{
pyJPEG_Type.tp_new = pyJPEG_new;
pyJPEG_Type.tp_methods = pyJPEG_Methods;
if (PyType_CheckAndReady(&pyJPEG_Type) < 0)
return nullptr;

Py_INCREF(&pyJPEG_Type);
return (PyObject*)&pyJPEG_Type;
}
24 changes: 24 additions & 0 deletions Python/Util/pyJPEG.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* This file is part of HSPlasma.
*
* HSPlasma is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HSPlasma is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HSPlasma. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _PYJPEG_H
#define _PYJPEG_H

#include "PyPlasma.h"

PY_WRAP_PLASMA(JPEG, class plJPEG);

#endif
73 changes: 73 additions & 0 deletions Python/Util/pyPNG.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* This file is part of HSPlasma.
*
* HSPlasma is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HSPlasma is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HSPlasma. If not, see <http://www.gnu.org/licenses/>.
*/

#include "pyPNG.h"

#include "PRP/Surface/pyBitmap.h"
#include "Stream/pyStream.h"

#include <Util/plPNG.h>

PY_PLASMA_EMPTY_INIT(PNG)
PY_PLASMA_NEW_MSG(PNG, "plPNG cannot be constructed")

PY_METHOD_STATIC_VA(PNG, DecompressPNG,
"Params: stream\n"
"Read PNG file from stream directly into a plMipmap")
{
PyObject* streamObj;
if (!PyArg_ParseTuple(args, "O", &streamObj)) {
PyErr_SetString(PyExc_TypeError, "DecompressPNG expects an hsStream");
return nullptr;
}
if (!pyStream_Check(streamObj)) {
PyErr_SetString(PyExc_TypeError, "DecompressPNG expects an hsStream");
return nullptr;
}

plMipmap* mm = nullptr;
try {
mm = plPNG::DecompressPNG(((pyStream*)streamObj)->fThis);
} catch (const hsPNGException& ex) {
PyErr_SetString(PyExc_RuntimeError, ex.what());
return nullptr;
}

// We're doing this manually because the new Mipmap object is being
// released to Python code.
pyMipmap* mmObj = PyObject_New(pyMipmap, &pyMipmap_Type);
mmObj->fPyOwned = true;
mmObj->fThis = mm;
return (PyObject*)mmObj;
}

static PyMethodDef pyPNG_Methods[] = {
pyPNG_DecompressPNG_method,
PY_METHOD_TERMINATOR,
};

PY_PLASMA_TYPE(PNG, plPNG, "plPNG wrapper")

PY_PLASMA_TYPE_INIT(PNG)
{
pyPNG_Type.tp_new = pyPNG_new;
pyPNG_Type.tp_methods = pyPNG_Methods;
if (PyType_CheckAndReady(&pyPNG_Type) < 0)
return nullptr;

Py_INCREF(&pyPNG_Type);
return (PyObject*)&pyPNG_Type;
}
24 changes: 24 additions & 0 deletions Python/Util/pyPNG.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* This file is part of HSPlasma.
*
* HSPlasma is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HSPlasma is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HSPlasma. If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef _PYPNG_H
#define _PYPNG_H

#include "PyPlasma.h"

PY_WRAP_PLASMA(PNG, class plPNG);

#endif
Loading

0 comments on commit b04ad4a

Please sign in to comment.