Skip to content

Commit b04ad4a

Browse files
authored
Merge pull request #291 from Hoikas/image_roundtrip
Add partial bindings for `plJPEG` and `plPNG`.
2 parents d1386cf + 84ae5fa commit b04ad4a

File tree

11 files changed

+305
-82
lines changed

11 files changed

+305
-82
lines changed

Python/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,10 +684,14 @@ set(SYS_HEADERS
684684
set(UTIL_SOURCES
685685
Util/pyBitVector.cpp
686686
Util/pyDDSurface.cpp
687+
Util/pyJPEG.cpp
688+
Util/pyPNG.cpp
687689
)
688690
set(UTIL_HEADERS
689691
Util/pyBitVector.h
690692
Util/pyDDSurface.h
693+
Util/pyJPEG.h
694+
Util/pyPNG.h
691695
)
692696

693697
set(VAULT_SOURCES

Python/Module.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "Sys/pyUnifiedTime.h"
3030
#include "Util/pyBitVector.h"
3131
#include "Util/pyDDSurface.h"
32+
#include "Util/pyJPEG.h"
33+
#include "Util/pyPNG.h"
3234
#include "Vault/pyServerGuid.h"
3335
#include "Vault/pyVaultNode.h"
3436
#include "Vault/pyVaultStore.h"
@@ -487,6 +489,8 @@ PyMODINIT_FUNC PyInit_PyHSPlasma()
487489
/* Util */
488490
PyModule_AddObject(module, "hsBitVector", Init_pyBitVector_Type());
489491
PyModule_AddObject(module, "plDDSurface", Init_pyDDSurface_Type());
492+
PyModule_AddObject(module, "plJPEG", Init_pyJPEG_Type());
493+
PyModule_AddObject(module, "plPNG", Init_pyPNG_Type());
490494

491495
/* Vault */
492496
PyModule_AddObject(module, "plServerGuid", Init_pyServerGuid_Type());

Python/PyHSPlasma.pyi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3757,6 +3757,11 @@ class plInterfaceInfoModifier(plSingleModifier):
37573757
def clearIntfKeys(self) -> None: ...
37583758
def delIntfKey(self, idx: int) -> None: ...
37593759

3760+
class plJPEG:
3761+
@staticmethod
3762+
def DecompressJPEG(stream: hsStream) -> plMipmap:
3763+
...
3764+
37603765
class plKey(Generic[KeyedT]):
37613766
id: int = ...
37623767
location: plLocation = ...
@@ -4482,6 +4487,11 @@ class plPhysicalSndGroup(hsKeyedObject):
44824487
class plPickingDetector(plDetectorModifier):
44834488
...
44844489

4490+
class plPNG:
4491+
@staticmethod
4492+
def DecompressPNG(stream: hsStream) -> plMipmap:
4493+
...
4494+
44854495
class plPoint3Controller(plLeafController):
44864496
...
44874497

Python/Util/pyJPEG.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* This file is part of HSPlasma.
2+
*
3+
* HSPlasma is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* HSPlasma is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with HSPlasma. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include "pyJPEG.h"
18+
19+
#include "PRP/Surface/pyBitmap.h"
20+
#include "Stream/pyStream.h"
21+
22+
#include <Util/plJPEG.h>
23+
24+
PY_PLASMA_EMPTY_INIT(JPEG)
25+
PY_PLASMA_NEW_MSG(JPEG, "plJPEG cannot be constructed")
26+
27+
PY_METHOD_STATIC_VA(JPEG, DecompressJPEG,
28+
"Params: stream\n"
29+
"Read JPEG file from stream directly into a plMipmap")
30+
{
31+
PyObject* streamObj;
32+
if (!PyArg_ParseTuple(args, "O", &streamObj)) {
33+
PyErr_SetString(PyExc_TypeError, "DecompressJPEG expects an hsStream");
34+
return nullptr;
35+
}
36+
if (!pyStream_Check(streamObj)) {
37+
PyErr_SetString(PyExc_TypeError, "DecompressJPEG expects an hsStream");
38+
return nullptr;
39+
}
40+
41+
plMipmap* mm = plJPEG::DecompressJPEG(((pyStream*)streamObj)->fThis);
42+
43+
// We're doing this manually because the new Mipmap object is being
44+
// released to Python code.
45+
pyMipmap* mmObj = nullptr;
46+
try {
47+
mmObj = PyObject_New(pyMipmap, &pyMipmap_Type);
48+
} catch (const hsJPEGException& ex) {
49+
PyErr_SetString(PyExc_RuntimeError, ex.what());
50+
return nullptr;
51+
}
52+
mmObj->fPyOwned = true;
53+
mmObj->fThis = mm;
54+
return (PyObject*)mmObj;
55+
}
56+
57+
static PyMethodDef pyJPEG_Methods[] = {
58+
pyJPEG_DecompressJPEG_method,
59+
PY_METHOD_TERMINATOR,
60+
};
61+
62+
PY_PLASMA_TYPE(JPEG, plJPEG, "plJPEG wrapper")
63+
64+
PY_PLASMA_TYPE_INIT(JPEG)
65+
{
66+
pyJPEG_Type.tp_new = pyJPEG_new;
67+
pyJPEG_Type.tp_methods = pyJPEG_Methods;
68+
if (PyType_CheckAndReady(&pyJPEG_Type) < 0)
69+
return nullptr;
70+
71+
Py_INCREF(&pyJPEG_Type);
72+
return (PyObject*)&pyJPEG_Type;
73+
}

Python/Util/pyJPEG.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* This file is part of HSPlasma.
2+
*
3+
* HSPlasma is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* HSPlasma is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with HSPlasma. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#ifndef _PYJPEG_H
18+
#define _PYJPEG_H
19+
20+
#include "PyPlasma.h"
21+
22+
PY_WRAP_PLASMA(JPEG, class plJPEG);
23+
24+
#endif

Python/Util/pyPNG.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* This file is part of HSPlasma.
2+
*
3+
* HSPlasma is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* HSPlasma is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with HSPlasma. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#include "pyPNG.h"
18+
19+
#include "PRP/Surface/pyBitmap.h"
20+
#include "Stream/pyStream.h"
21+
22+
#include <Util/plPNG.h>
23+
24+
PY_PLASMA_EMPTY_INIT(PNG)
25+
PY_PLASMA_NEW_MSG(PNG, "plPNG cannot be constructed")
26+
27+
PY_METHOD_STATIC_VA(PNG, DecompressPNG,
28+
"Params: stream\n"
29+
"Read PNG file from stream directly into a plMipmap")
30+
{
31+
PyObject* streamObj;
32+
if (!PyArg_ParseTuple(args, "O", &streamObj)) {
33+
PyErr_SetString(PyExc_TypeError, "DecompressPNG expects an hsStream");
34+
return nullptr;
35+
}
36+
if (!pyStream_Check(streamObj)) {
37+
PyErr_SetString(PyExc_TypeError, "DecompressPNG expects an hsStream");
38+
return nullptr;
39+
}
40+
41+
plMipmap* mm = nullptr;
42+
try {
43+
mm = plPNG::DecompressPNG(((pyStream*)streamObj)->fThis);
44+
} catch (const hsPNGException& ex) {
45+
PyErr_SetString(PyExc_RuntimeError, ex.what());
46+
return nullptr;
47+
}
48+
49+
// We're doing this manually because the new Mipmap object is being
50+
// released to Python code.
51+
pyMipmap* mmObj = PyObject_New(pyMipmap, &pyMipmap_Type);
52+
mmObj->fPyOwned = true;
53+
mmObj->fThis = mm;
54+
return (PyObject*)mmObj;
55+
}
56+
57+
static PyMethodDef pyPNG_Methods[] = {
58+
pyPNG_DecompressPNG_method,
59+
PY_METHOD_TERMINATOR,
60+
};
61+
62+
PY_PLASMA_TYPE(PNG, plPNG, "plPNG wrapper")
63+
64+
PY_PLASMA_TYPE_INIT(PNG)
65+
{
66+
pyPNG_Type.tp_new = pyPNG_new;
67+
pyPNG_Type.tp_methods = pyPNG_Methods;
68+
if (PyType_CheckAndReady(&pyPNG_Type) < 0)
69+
return nullptr;
70+
71+
Py_INCREF(&pyPNG_Type);
72+
return (PyObject*)&pyPNG_Type;
73+
}

Python/Util/pyPNG.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* This file is part of HSPlasma.
2+
*
3+
* HSPlasma is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* HSPlasma is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with HSPlasma. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
17+
#ifndef _PYPNG_H
18+
#define _PYPNG_H
19+
20+
#include "PyPlasma.h"
21+
22+
PY_WRAP_PLASMA(PNG, class plPNG);
23+
24+
#endif

0 commit comments

Comments
 (0)