This repository has been archived by the owner on Feb 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ROP_CopGif.C
583 lines (452 loc) · 13.7 KB
/
ROP_CopGif.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include "ROP_CopGif.h"
#include <ROP/ROP_Error.h>
#include <ROP/ROP_Templates.h>
#include <COP2/COP2_Node.h>
#include <OP/OP_Director.h>
#include <OP/OP_OperatorTable.h>
#include <PRM/PRM_Include.h>
#include <PRM/PRM_SpareData.h>
#include <CH/CH_LocalVariable.h>
#include <UT/UT_DSOVersion.h>
#include <PXL/PXL_Raster.h>
#include <TIL/TIL_Raster.h>
#include <TIL/TIL_CopResolver.h>
#define ROP_COP_GIF_NAME "copgif"
#define ROP_COP_GIF_DESCRIPTION "COP Gif"
#define ROP_COP_GIF_DEFAULT_COP_PLANE "C"
#define ROP_COP_GIF_COPYRIGHT_TEXT "(c) 2016, Mykola Konyk, https://github.com/ttvd"
#define ROP_COP_GIF_COP_PATH "cop2_path"
#define ROP_COP_GIF_OUTPUT_FILE "output_file"
#define SOP_COP_GIF_PLANE "cop2_plane"
#define SOP_COP_GIF_DITHER "gif_dither"
#define SOP_COP_GIF_COPYRIGHT "rop_cop_gif_copyright"
#define ROP_COP_GIF_SEPARATOR "rop_cop_gif_separator"
#define ROP_COP_GIF_SEPARATOR2 "rop_cop_gif_separator2"
#define ROP_COP_GIF_SPACE "rop_cop_gif_space"
static PRM_Name s_name_file_output(ROP_COP_GIF_OUTPUT_FILE, "Output File");
static PRM_Name s_name_cop_path(ROP_COP_GIF_COP_PATH, "COP2 Path");
static PRM_Name s_name_cop_plane(SOP_COP_GIF_PLANE, "COP2 Plane");
static PRM_Name s_name_dither(SOP_COP_GIF_DITHER, "Dither");
static PRM_Name s_name_separator(ROP_COP_GIF_SEPARATOR, "");
static PRM_Name s_name_separator2(ROP_COP_GIF_SEPARATOR2, "");
static PRM_Name s_name_copyright(SOP_COP_GIF_COPYRIGHT, "");
static PRM_Name s_name_space(ROP_COP_GIF_SPACE, "");
static PRM_Default s_default_name_cop_plane(0.0f, ROP_COP_GIF_DEFAULT_COP_PLANE);
static PRM_Default s_default_copyright(0.0f, ROP_COP_GIF_COPYRIGHT_TEXT);
static PRM_Default s_default_dither(false);
static PRM_SpareData s_spare_file_output(PRM_SpareArgs()
<< PRM_SpareToken(PRM_SpareData::getFileChooserModeToken(), PRM_SpareData::getFileChooserModeValRead())
<< PRM_SpareToken(PRM_SpareData::getFileChooserPatternToken(), "*.gif"));
PRM_Template*
ROP_CopGif::ms_template = nullptr;
OP_TemplatePair*
ROP_CopGif::ms_template_pair = nullptr;
OP_VariablePair*
ROP_CopGif::ms_variable_pair = nullptr;
static
int
callbackCopyright(void* data, int x, fpreal t, const PRM_Template* prm_template)
{
ROP_CopGif* rop_node = (ROP_CopGif*) data;
if(rop_node)
{
rop_node->setString(ROP_COP_GIF_COPYRIGHT_TEXT, CH_STRING_LITERAL, SOP_COP_GIF_COPYRIGHT, 0, t);
}
return 0;
}
static
PRM_Template*
getTemplates()
{
if(ROP_CopGif::ms_template)
{
return ROP_CopGif::ms_template;
}
const int template_count = 9;
ROP_CopGif::ms_template = new PRM_Template[template_count];
int template_idx = 0;
ROP_CopGif::ms_template[template_idx++] = PRM_Template(PRM_SEPARATOR, 0, &s_name_separator);
ROP_CopGif::ms_template[template_idx++] =
PRM_Template(PRM_STRING, PRM_TYPE_DYNAMIC_PATH, 1, &s_name_cop_path, 0, 0, 0, 0, &PRM_SpareData::cop2Path),
ROP_CopGif::ms_template[template_idx++] =
PRM_Template(PRM_STRING, 1, &s_name_cop_plane, &s_default_name_cop_plane);
ROP_CopGif::ms_template[template_idx++] =
PRM_Template(PRM_TOGGLE, 1, &s_name_dither, &s_default_dither);
ROP_CopGif::ms_template[template_idx++] =
PRM_Template(PRM_FILE, 1, &s_name_file_output, 0, 0, 0, 0, &s_spare_file_output);
ROP_CopGif::ms_template[template_idx++] = PRM_Template(PRM_LABEL, 0, &s_name_space);
ROP_CopGif::ms_template[template_idx++] = PRM_Template(PRM_SEPARATOR, 0, &s_name_separator2);
ROP_CopGif::ms_template[template_idx++] = PRM_Template(PRM_STRING_E, PRM_TYPE_NO_LABEL, 1, &s_name_copyright,
&s_default_copyright, 0, 0, callbackCopyright);
ROP_CopGif::ms_template[template_idx++] = PRM_Template();
return ROP_CopGif::ms_template;
}
OP_TemplatePair*
ROP_CopGif::getTemplatePair()
{
if(!ROP_CopGif::ms_template_pair)
{
OP_TemplatePair* base = new OP_TemplatePair(getTemplates());
ROP_CopGif::ms_template_pair = new OP_TemplatePair(ROP_Node::getROPbaseTemplate(), base);
}
return ROP_CopGif::ms_template_pair;
}
OP_VariablePair*
ROP_CopGif::getVariablePair()
{
if(!ROP_CopGif::ms_variable_pair)
{
ROP_CopGif::ms_variable_pair = new OP_VariablePair(ROP_Node::myVariableList);
}
return ROP_CopGif::ms_variable_pair;
}
OP_Node*
ROP_CopGif::myConstructor(OP_Network* net, const char* name, OP_Operator* op)
{
return new ROP_CopGif(net, name, op);
}
ROP_CopGif::ROP_CopGif(OP_Network* net, const char* name, OP_Operator* entry) :
ROP_Node(net, name, entry),
m_render_time_start(0.0f),
m_render_time_end(0.0f)
{
}
ROP_CopGif::~ROP_CopGif()
{
}
bool
ROP_CopGif::updateParmsFlags()
{
bool changed = ROP_Node::updateParmsFlags();
return changed;
}
int
ROP_CopGif::startRender(int num_frames, fpreal time_start, fpreal time_end)
{
if(error() < UT_ERROR_ABORT)
{
executePreRenderScript(time_start);
}
// Record frames start and end.
m_render_time_start = time_start;
m_render_time_end = time_end;
// Reset frame sizes.
m_width = 0u;
m_height = 0u;
// Clear previously stored frames.
m_frames.clear();
return 1;
}
ROP_RENDER_CODE
ROP_CopGif::renderFrame(fpreal t, UT_Interrupt* boss)
{
// Execute the pre-render script.
if(!executePreFrameScript(t))
{
return ROP_ABORT_RENDER;
}
UT_String cop_image_plane;
getParamImagePlaneName(t, cop_image_plane);
UT_String cop_relative_path;
getParamCopPath(t, cop_relative_path);
UT_String cop_full_path;
if(!getFullCopPath(cop_relative_path, cop_full_path))
{
addError(ROP_MESSAGE, "Cop Gif: Invalid COP2 path.");
return ROP_ABORT_RENDER;
}
// Make sure COP node exists and we are able to look it up.
COP2_Node* cop_node = getCOP2Node(cop_full_path, 1);
if(!cop_node)
{
addError(ROP_MESSAGE, "Cop Gif: Unable to find the COP2 node.");
return ROP_ABORT_RENDER;
}
// Get COP2 resolver.
if(!m_cop_resolver)
{
m_cop_resolver = TIL_CopResolver::getResolver();
if(!m_cop_resolver)
{
addError(ROP_MESSAGE, "Cop Gif: Unable to retrieve COP2 resolver.");
return ROP_ABORT_RENDER;
}
}
// Get COP2 raster.
TIL_Raster* raster = m_cop_resolver->getNodeRaster(cop_full_path, cop_image_plane, "");
if(!raster)
{
addError(ROP_MESSAGE, "Cop Gif: Unable to retrieve COP2 node raster.");
return ROP_ABORT_RENDER;
}
// Make sure raster is valid.
if(!raster->isValid())
{
addError(ROP_MESSAGE, "Cop Gif: Invalid COP2 node raster.");
return ROP_ABORT_RENDER;
}
// Make sure we support raster format.
if(!isCopDataFloat(raster->getFormat()))
{
addError(ROP_MESSAGE, "Cop Gif: Non floating raster formats are not supported.");
return ROP_ABORT_RENDER;
}
// Process raster.
if(!processFrameRaster(t, raster))
{
addError(ROP_MESSAGE, "Cop Gif: Unable to process COP2 node raster.");
return ROP_ABORT_RENDER;
}
// Return raster back to resolver.
m_cop_resolver->returnRaster(raster);
// Execute the post-render script.
if(error() < UT_ERROR_ABORT)
{
executePostFrameScript(t);
}
return ROP_CONTINUE_RENDER;
}
ROP_RENDER_CODE
ROP_CopGif::endRender()
{
if(error() < UT_ERROR_ABORT)
{
executePostRenderScript(m_render_time_end);
}
// Get output path.
UT_String output_file;
getParamOutputFileName(0, output_file);
// Get dither setting.
bool gif_dither = getParamDither(0);
// Library we are using supports only 8-bit.
const int bit_depth = 8;
// We set frame delay to 0 (this is mostly ignored by all implementations).
const int frame_delay = 0;
if(!output_file || !output_file.isstring())
{
addError(ROP_MESSAGE, "Cop Gif: Invalid output gif file specified.");
return ROP_ABORT_RENDER;
}
GifWriter gif_writer;
if(!GifBegin(&gif_writer, output_file.c_str(), m_width, m_height, frame_delay, bit_depth, gif_dither))
{
addError(ROP_MESSAGE, "Cop Gif: Unable to initialize gif writer.");
return ROP_ABORT_RENDER;
}
for(int frame_idx = 0; frame_idx < m_frames.size(); ++frame_idx)
{
const UT_Array<unsigned char>& frame_data = m_frames(frame_idx);
if(!frame_data.size())
{
addError(ROP_MESSAGE, "Cop Gif: Invalid raster frame data, zero size.");
return ROP_ABORT_RENDER;
}
if(frame_data.size() != m_width * m_height * 4u)
{
addError(ROP_MESSAGE, "Cop Gif: Invalid raster frame data, invalid size.");
return ROP_ABORT_RENDER;
}
const unsigned char* frame_start = &frame_data(0);
if(!GifWriteFrame(&gif_writer, (const uint8_t*) frame_start, m_width, m_height, frame_delay, bit_depth,
gif_dither))
{
addError(ROP_MESSAGE, "Cop Gif: Unable to write a gif frame.");
return ROP_ABORT_RENDER;
}
}
if(!GifEnd(&gif_writer))
{
addError(ROP_MESSAGE, "Cop Gif: Unable to finalize gif writer.");
return ROP_ABORT_RENDER;
}
return ROP_CONTINUE_RENDER;
}
bool
ROP_CopGif::getFullCopPath(const UT_String& relative_path, UT_String& full_path) const
{
OP_Node* node = findNode(relative_path);
if(!node)
{
return false;
}
// If this is not a cop2 node.
if(COP2_OPTYPE_ID != node->getOpTypeID())
{
return false;
}
node = (OP_Node*) CAST_COP2NODE(node);
if(!node)
{
return false;
}
node->getFullPath(full_path);
return true;
}
void
ROP_CopGif::getParamCopPath(fpreal t, UT_String& cop_path) const
{
evalString(cop_path, ROP_COP_GIF_COP_PATH, 0, t);
}
void
ROP_CopGif::getParamOutputFileName(fpreal t, UT_String& output_file) const
{
evalString(output_file, ROP_COP_GIF_OUTPUT_FILE, 0, t);
}
void
ROP_CopGif::getParamImagePlaneName(fpreal t, UT_String& image_plane_name) const
{
evalString(image_plane_name, SOP_COP_GIF_PLANE, 0, t);
}
bool
ROP_CopGif::getParamDither(fpreal t) const
{
return evalInt(SOP_COP_GIF_DITHER, 0, t) != 0;
}
unsigned int
ROP_CopGif::getCopDataChannelCount(PXL_Packing packing) const
{
switch(packing)
{
case PACK_SINGLE:
{
return 1u;
}
case PACK_DUAL:
{
return 2u;
}
case PACK_RGB:
{
return 3u;
}
case PACK_RGBA:
{
return 4u;
}
default:
{
break;
}
}
return 0u;
}
bool
ROP_CopGif::isCopDataFloat(PXL_DataFormat data_format) const
{
switch(data_format)
{
case PXL_FLOAT16:
case PXL_FLOAT32:
{
return true;
}
default:
{
break;
}
}
return false;
}
void
ROP_CopGif::getFramePixel(const unsigned char* pixel_data, PXL_Packing packing, PXL_DataFormat data_format,
UT_Array<unsigned char>& frame_data) const
{
unsigned int num_channels = getCopDataChannelCount(packing);
for(unsigned int channel_idx = 0; channel_idx < 4; ++channel_idx)
{
if(channel_idx < num_channels)
{
fpreal value = 0.0f;
switch(data_format)
{
case PXL_FLOAT16:
{
const fpreal16* pixel_cast = (const fpreal16*) pixel_data;
value = (fpreal)(*(pixel_cast + channel_idx));
break;
}
case PXL_FLOAT32:
{
const float* pixel_cast = (const float*) pixel_data;
value = *(pixel_cast + channel_idx);
break;
}
default:
{
break;
}
}
value = SYSclamp(value, 0.0f, 1.0f);
if(value == 1.0f)
{
frame_data.append(255u);
}
else
{
unsigned int pixel_value = SYSfloor(256.0f * value);
frame_data.append(pixel_value);
}
}
else
{
if(3 == channel_idx)
{
frame_data.append(255u);
}
else
{
frame_data.append(0u);
}
}
}
}
bool
ROP_CopGif::processFrameRaster(fpreal t, TIL_Raster* raster) const
{
if(!raster || !raster->isValid())
{
return false;
}
if(!m_width)
{
m_width = raster->getXres();
}
if(!m_height)
{
m_height = raster->getYres();
}
if(!m_width || !m_height)
{
return false;
}
if(m_width != raster->getXres() || m_height != raster->getYres())
{
return false;
}
// Get raster data format and packing.
PXL_DataFormat pixel_format = raster->getFormat();
PXL_Packing packing = raster->getPacking();
// We only support floating point formats.
if(!isCopDataFloat(pixel_format))
{
return false;
}
UT_Array<unsigned char> frame_data;
for(int idx_height = m_height - 1; idx_height >= 0; --idx_height)
{
for(int idx_width = 0; idx_width < m_width; ++idx_width)
{
const unsigned char* pixel = (const unsigned char*) raster->getPixel(idx_width, idx_height);
getFramePixel(pixel, packing, pixel_format, frame_data);
}
}
m_frames.append(frame_data);
return true;
}
void
newDriverOperator(OP_OperatorTable* table)
{
table->addOperator(new OP_Operator(ROP_COP_GIF_NAME, ROP_COP_GIF_DESCRIPTION,
ROP_CopGif::myConstructor, ROP_CopGif::getTemplatePair(), 0, 0,
ROP_CopGif::getVariablePair(), OP_FLAG_GENERATOR));
}