3
3
#include " Runtime/Engine/Public/ImageUtils.h"
4
4
#include " Runtime/Engine/Classes/Engine/Texture.h"
5
5
6
- PyObject *py_ue_texture_update_resource (ue_PyUObject *self, PyObject * args) {
6
+ PyObject *py_ue_texture_update_resource (ue_PyUObject *self, PyObject * args)
7
+ {
7
8
8
9
ue_py_check (self);
9
10
@@ -15,7 +16,8 @@ PyObject *py_ue_texture_update_resource(ue_PyUObject *self, PyObject * args) {
15
16
Py_RETURN_NONE;
16
17
}
17
18
18
- PyObject *py_ue_texture_get_width (ue_PyUObject *self, PyObject * args) {
19
+ PyObject *py_ue_texture_get_width (ue_PyUObject *self, PyObject * args)
20
+ {
19
21
20
22
ue_py_check (self);
21
23
@@ -26,7 +28,8 @@ PyObject *py_ue_texture_get_width(ue_PyUObject *self, PyObject * args) {
26
28
return PyLong_FromLong (texture->GetSizeX ());
27
29
}
28
30
29
- PyObject *py_ue_texture_get_height (ue_PyUObject *self, PyObject * args) {
31
+ PyObject *py_ue_texture_get_height (ue_PyUObject *self, PyObject * args)
32
+ {
30
33
31
34
ue_py_check (self);
32
35
@@ -37,13 +40,15 @@ PyObject *py_ue_texture_get_height(ue_PyUObject *self, PyObject * args) {
37
40
return PyLong_FromLong (texture->GetSizeY ());
38
41
}
39
42
40
- PyObject *py_ue_texture_get_data (ue_PyUObject *self, PyObject * args) {
43
+ PyObject *py_ue_texture_get_data (ue_PyUObject *self, PyObject * args)
44
+ {
41
45
42
46
ue_py_check (self);
43
47
44
48
int mipmap = 0 ;
45
49
46
- if (!PyArg_ParseTuple (args, " |i:texture_get_data" , &mipmap)) {
50
+ if (!PyArg_ParseTuple (args, " |i:texture_get_data" , &mipmap))
51
+ {
47
52
return NULL ;
48
53
}
49
54
@@ -61,13 +66,15 @@ PyObject *py_ue_texture_get_data(ue_PyUObject *self, PyObject * args) {
61
66
}
62
67
63
68
#if WITH_EDITOR
64
- PyObject *py_ue_texture_get_source_data (ue_PyUObject *self, PyObject * args) {
69
+ PyObject *py_ue_texture_get_source_data (ue_PyUObject *self, PyObject * args)
70
+ {
65
71
66
72
ue_py_check (self);
67
73
68
74
int mipmap = 0 ;
69
75
70
- if (!PyArg_ParseTuple (args, " |i:texture_get_data" , &mipmap)) {
76
+ if (!PyArg_ParseTuple (args, " |i:texture_get_data" , &mipmap))
77
+ {
71
78
return NULL ;
72
79
}
73
80
@@ -87,13 +94,15 @@ PyObject *py_ue_texture_get_source_data(ue_PyUObject *self, PyObject * args) {
87
94
}
88
95
#endif
89
96
90
- PyObject *py_ue_render_target_get_data (ue_PyUObject *self, PyObject * args) {
97
+ PyObject *py_ue_render_target_get_data (ue_PyUObject *self, PyObject * args)
98
+ {
91
99
92
100
ue_py_check (self);
93
101
94
102
int mipmap = 0 ;
95
103
96
- if (!PyArg_ParseTuple (args, " |i:render_target_get_data" , &mipmap)) {
104
+ if (!PyArg_ParseTuple (args, " |i:render_target_get_data" , &mipmap))
105
+ {
97
106
return NULL ;
98
107
}
99
108
@@ -102,26 +111,68 @@ PyObject *py_ue_render_target_get_data(ue_PyUObject *self, PyObject * args) {
102
111
return PyErr_Format (PyExc_Exception, " object is not a TextureRenderTarget" );
103
112
104
113
FTextureRenderTarget2DResource *resource = (FTextureRenderTarget2DResource *)tex->Resource ;
105
- if (!resource) {
114
+ if (!resource)
115
+ {
106
116
return PyErr_Format (PyExc_Exception, " cannot get render target resource" );
107
117
}
108
118
109
119
TArray<FColor> pixels;
110
- if (!resource->ReadPixels (pixels)) {
120
+ if (!resource->ReadPixels (pixels))
121
+ {
111
122
return PyErr_Format (PyExc_Exception, " unable to read pixels" );
112
123
}
113
124
114
125
return PyByteArray_FromStringAndSize ((const char *)pixels.GetData (), (Py_ssize_t)(tex->GetSurfaceWidth () * 4 * tex->GetSurfaceHeight ()));
115
126
}
116
127
117
- PyObject *py_ue_texture_set_data (ue_PyUObject *self, PyObject * args) {
128
+ PyObject *py_ue_render_target_get_data_to_buffer (ue_PyUObject *self, PyObject * args)
129
+ {
130
+
131
+ ue_py_check (self);
132
+ Py_buffer py_buf;
133
+ int mipmap = 0 ;
134
+
135
+ if (!PyArg_ParseTuple (args, " z*|i:render_target_get_data_to_buffer" , &py_buf, &mipmap))
136
+ {
137
+ return NULL ;
138
+ }
139
+
140
+ UTextureRenderTarget2D *tex = ue_py_check_type<UTextureRenderTarget2D>(self);
141
+ if (!tex)
142
+ return PyErr_Format (PyExc_Exception, " object is not a TextureRenderTarget" );
143
+
144
+ FTextureRenderTarget2DResource *resource = (FTextureRenderTarget2DResource *)tex->Resource ;
145
+ if (!resource)
146
+ {
147
+ return PyErr_Format (PyExc_Exception, " cannot get render target resource" );
148
+ }
149
+
150
+ Py_ssize_t data_len = (Py_ssize_t)(tex->GetSurfaceWidth () * 4 * tex->GetSurfaceHeight ());
151
+ if (py_buf.len < data_len)
152
+ {
153
+ return PyErr_Format (PyExc_Exception, " buffer is not big enough" );
154
+ }
155
+
156
+ TArray<FColor> pixels;
157
+ if (!resource->ReadPixels (pixels))
158
+ {
159
+ return PyErr_Format (PyExc_Exception, " unable to read pixels" );
160
+ }
161
+
162
+ FMemory::Memcpy (py_buf.buf , pixels.GetData (), data_len);
163
+ Py_RETURN_NONE;
164
+ }
165
+
166
+ PyObject *py_ue_texture_set_data (ue_PyUObject *self, PyObject * args)
167
+ {
118
168
119
169
ue_py_check (self);
120
170
121
171
Py_buffer py_buf;
122
172
int mipmap = 0 ;
123
173
124
- if (!PyArg_ParseTuple (args, " z*|i:texture_set_data" , &py_buf, &mipmap)) {
174
+ if (!PyArg_ParseTuple (args, " z*|i:texture_set_data" , &py_buf, &mipmap))
175
+ {
125
176
return NULL ;
126
177
}
127
178
@@ -140,7 +191,8 @@ PyObject *py_ue_texture_set_data(ue_PyUObject *self, PyObject * args) {
140
191
int32 len = tex->PlatformData ->Mips [mipmap].BulkData .GetBulkDataSize ();
141
192
int32 wanted_len = py_buf.len ;
142
193
// avoid making mess
143
- if (wanted_len > len) {
194
+ if (wanted_len > len)
195
+ {
144
196
UE_LOG (LogPython, Warning, TEXT (" truncating buffer to %d bytes" ), len);
145
197
wanted_len = len;
146
198
}
@@ -158,22 +210,26 @@ PyObject *py_ue_texture_set_data(ue_PyUObject *self, PyObject * args) {
158
210
return Py_None;
159
211
}
160
212
161
- PyObject *py_unreal_engine_compress_image_array (PyObject * self, PyObject * args) {
213
+ PyObject *py_unreal_engine_compress_image_array (PyObject * self, PyObject * args)
214
+ {
162
215
int width;
163
216
int height;
164
217
Py_buffer py_buf;
165
- if (!PyArg_ParseTuple (args, " iiz*:compress_image_array" , &width, &height, &py_buf)) {
218
+ if (!PyArg_ParseTuple (args, " iiz*:compress_image_array" , &width, &height, &py_buf))
219
+ {
166
220
return NULL ;
167
221
}
168
222
169
- if (py_buf.buf == nullptr || py_buf.len <= 0 ) {
223
+ if (py_buf.buf == nullptr || py_buf.len <= 0 )
224
+ {
170
225
PyBuffer_Release (&py_buf);
171
226
return PyErr_Format (PyExc_Exception, " invalid image data" );
172
227
}
173
228
174
229
TArray<FColor> colors;
175
230
uint8 *buf = (uint8 *)py_buf.buf ;
176
- for (int32 i = 0 ; i < py_buf.len ; i += 4 ) {
231
+ for (int32 i = 0 ; i < py_buf.len ; i += 4 )
232
+ {
177
233
colors.Add (FColor (buf[i], buf[1 + 1 ], buf[i + 2 ], buf[i + 3 ]));
178
234
}
179
235
@@ -184,11 +240,13 @@ PyObject *py_unreal_engine_compress_image_array(PyObject * self, PyObject * args
184
240
return PyBytes_FromStringAndSize ((char *)output.GetData (), output.Num ());
185
241
}
186
242
187
- PyObject *py_unreal_engine_create_checkerboard_texture (PyObject * self, PyObject * args) {
243
+ PyObject *py_unreal_engine_create_checkerboard_texture (PyObject * self, PyObject * args)
244
+ {
188
245
PyObject *py_color_one;
189
246
PyObject *py_color_two;
190
247
int checker_size;
191
- if (!PyArg_ParseTuple (args, " OOi:create_checkboard_texture" , &py_color_one, &py_color_two, &checker_size)) {
248
+ if (!PyArg_ParseTuple (args, " OOi:create_checkboard_texture" , &py_color_one, &py_color_two, &checker_size))
249
+ {
192
250
return NULL ;
193
251
}
194
252
@@ -209,11 +267,13 @@ PyObject *py_unreal_engine_create_checkerboard_texture(PyObject * self, PyObject
209
267
return (PyObject *)ret;
210
268
}
211
269
212
- PyObject *py_unreal_engine_create_transient_texture (PyObject * self, PyObject * args) {
270
+ PyObject *py_unreal_engine_create_transient_texture (PyObject * self, PyObject * args)
271
+ {
213
272
int width;
214
273
int height;
215
274
int format = PF_B8G8R8A8;
216
- if (!PyArg_ParseTuple (args, " ii|i:create_transient_texture" , &width, &height, &format)) {
275
+ if (!PyArg_ParseTuple (args, " ii|i:create_transient_texture" , &width, &height, &format))
276
+ {
217
277
return NULL ;
218
278
}
219
279
@@ -231,12 +291,14 @@ PyObject *py_unreal_engine_create_transient_texture(PyObject * self, PyObject *
231
291
return (PyObject *)ret;
232
292
}
233
293
234
- PyObject *py_unreal_engine_create_transient_texture_render_target2d (PyObject * self, PyObject * args) {
294
+ PyObject *py_unreal_engine_create_transient_texture_render_target2d (PyObject * self, PyObject * args)
295
+ {
235
296
int width;
236
297
int height;
237
298
int format = PF_B8G8R8A8;
238
299
PyObject *py_linear = nullptr ;
239
- if (!PyArg_ParseTuple (args, " ii|iO:create_transient_texture_render_target2d" , &width, &height, &format, &py_linear)) {
300
+ if (!PyArg_ParseTuple (args, " ii|iO:create_transient_texture_render_target2d" , &width, &height, &format, &py_linear))
301
+ {
240
302
return NULL ;
241
303
}
242
304
@@ -254,24 +316,29 @@ PyObject *py_unreal_engine_create_transient_texture_render_target2d(PyObject * s
254
316
}
255
317
256
318
#if WITH_EDITOR
257
- PyObject *py_unreal_engine_create_texture (PyObject * self, PyObject * args) {
319
+ PyObject *py_unreal_engine_create_texture (PyObject * self, PyObject * args)
320
+ {
258
321
PyObject *py_package;
259
322
char *name;
260
323
int width;
261
324
int height;
262
325
Py_buffer py_buf;
263
326
264
- if (!PyArg_ParseTuple (args, " Osiiz*:create_texture" , &py_package, &name, &width, &height, &py_buf)) {
327
+ if (!PyArg_ParseTuple (args, " Osiiz*:create_texture" , &py_package, &name, &width, &height, &py_buf))
328
+ {
265
329
return nullptr ;
266
330
}
267
331
268
332
UPackage *u_package = nullptr ;
269
- if (py_package == Py_None) {
333
+ if (py_package == Py_None)
334
+ {
270
335
u_package = GetTransientPackage ();
271
336
}
272
- else {
337
+ else
338
+ {
273
339
u_package = ue_py_check_type<UPackage>(py_package);
274
- if (!u_package) {
340
+ if (!u_package)
341
+ {
275
342
return PyErr_Format (PyExc_Exception, " argument is not a UPackage" );
276
343
}
277
344
}
@@ -286,7 +353,7 @@ PyObject *py_unreal_engine_create_texture(PyObject * self, PyObject * args) {
286
353
wanted_len = py_buf.len ;
287
354
288
355
FMemory::Memcpy (colors.GetData (), py_buf.buf , wanted_len);
289
-
356
+
290
357
UTexture2D *texture = FImageUtils::CreateTexture2D (width, height, colors, u_package, UTF8_TO_TCHAR (name), RF_Public | RF_Standalone, params);
291
358
if (!texture)
292
359
return PyErr_Format (PyExc_Exception, " unable to create texture" );
0 commit comments