Class: Wiretap::NodeFrames

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wiretap.rb,
ext/nodeframes.cpp

Direct Known Subclasses

AudioFrames

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nodeObject (readonly)

Returns the value of attribute node.



222
223
224
# File 'lib/wiretap.rb', line 222

def node
  @node
end

Instance Method Details

#[](index) ⇒ Object

Get frame by index.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'ext/nodeframes.cpp', line 34

static VALUE wiretap_node_frames_get_at(VALUE self, VALUE index) {
	WireTapNodeHandle* node;
	Data_Get_Struct(self, WireTapNodeHandle, node);
	
	Check_Type(index, T_FIXNUM);
	WireTapClipFormat format;
	RUN(node->getClipFormat(format));

	VALUE frame = rb_str_buf_new(format.frameBufferSize());
	RSTRING(frame)->len = format.frameBufferSize();

	NODERUN_E(node, node->readFrame(FIX2INT(index), STR(frame), LEN(frame)));

	return frame;
}

#[]=(index, value) ⇒ Object

Set frame at index. Accepts the formatted Stone image buffer as value.



53
54
55
56
57
58
59
60
61
# File 'ext/nodeframes.cpp', line 53

static VALUE wiretap_node_frames_set_at(VALUE self, VALUE index, VALUE value) {
	WireTapNodeHandle* node;
	Data_Get_Struct(self, WireTapNodeHandle, node);
	Check_Type(index, T_STRING);
	Check_Type(value, T_STRING);

	NODERUN_E(node, node->writeFrame(FIX2INT(index), CSTR(value), LEN(value)));
	return self;
}

#countObject Also known as: size, length

Get frames count of clip



11
12
13
14
15
16
17
# File 'ext/nodeframes.cpp', line 11

static VALUE wiretap_node_frames_count(VALUE self) {
	WireTapNodeHandle* node;
	int num;
	Data_Get_Struct(self, WireTapNodeHandle, node);
	NODERUN_E(node, node->getNumFrames(num));
	return INT2FIX(num);
}

#count=(value) ⇒ Object

Set frames count in clip



22
23
24
25
26
27
28
29
# File 'ext/nodeframes.cpp', line 22

static VALUE wiretap_node_frames_set_count(VALUE self, VALUE value) {
	VALUE count = rb_funcall(value, rb_intern("to_i"), 0);

	WireTapNodeHandle* node;
	Data_Get_Struct(self, WireTapNodeHandle, node);
	NODERUN_E(node, node->setNumFrames(NUM2INT(count)));
	return count;
}

#dump(index, file) ⇒ Object

Dump one frame to file



145
146
147
148
149
150
151
152
153
154
155
156
# File 'ext/nodeframes.cpp', line 145

static VALUE wiretap_node_frame_dump(VALUE self, VALUE index, VALUE file) {
	WireTapNodeHandle* node;
	unsigned long size;
	WireTapClipFormat* format;
	frame_prepare(self, &node, &size, &format);

	std::auto_ptr<unsigned char> frame(new unsigned char[size]);
	NODERUN_E_FRAME(node, node->readFrame(NUM2INT(index), frame.get(), size));

	wiretap_write_image_frame(format->width(), format->height(), format->bitsPerPixel(), frame.get(), CSTR(file));
	return self;
}

#dump_all(file_pattern) ⇒ Object

Dump all frames into files, matched by pattern.

@clip.frames.dump_all("/tmp/clip/%d.sgi")

It is substituted via snprintf



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'ext/nodeframes.cpp', line 123

static VALUE wiretap_node_frame_dump_all(VALUE self, VALUE file_pattern) {
	WireTapNodeHandle* node;
	unsigned long size;
	WireTapClipFormat* format;
	frame_prepare(self, &node, &size, &format);

	int frame_count = NUM2INT(rb_funcall(self, rb_intern("count"), 0));
	std::auto_ptr<unsigned char> frame(new unsigned char[size]);
	for(int i = 0; i < frame_count; i++) {
		NODERUN_E_FRAME(node, node->readFrame(i, frame.get(), size));
		size_t buffer_size = PATH_MAX;
		char file_name[buffer_size];
		snprintf(file_name, buffer_size, STR(file_pattern), i);
		wiretap_write_image_frame(format->width(), format->height(), format->bitsPerPixel(), frame.get(), file_name);
	}

	return self;
}

#eachObject

Eval block on each frame



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/nodeframes.cpp', line 66

static VALUE wiretap_node_frames_each(VALUE self) {
	WireTapNodeHandle* node;
	int i, num;
	Data_Get_Struct(self, WireTapNodeHandle, node);
	RUN(node->getNumFrames(num));

	WireTapClipFormat format;
	RUN(node->getClipFormat(format));

	for(i = 0; i < num; i++) {
		VALUE frame = rb_str_buf_new(format.frameBufferSize());
		RSTRING(frame)->len = format.frameBufferSize();

		RUN(node->readFrame(i, STR(frame), LEN(frame)));
		//rb_iv_set(frame, "@path", wiretap_node_frames_get_path_at(self, INT2FIX(i)));
		rb_yield(frame);
	}
	return self;
}

#path_at(index) ⇒ Object

Get path of frame at index



89
90
91
92
93
94
95
96
97
98
99
# File 'ext/nodeframes.cpp', line 89

static VALUE wiretap_node_frames_get_path_at(VALUE self, VALUE index) {
	WireTapNodeHandle* node;
	Data_Get_Struct(self, WireTapNodeHandle, node);
	
	Check_Type(index, T_FIXNUM);

	WireTapStr path;
	RUN(node->getFrameIdPath(FIX2INT(index), path));

	return wiretap_to_str(path);
}

#write_from_file(index, filename) ⇒ Object

Read one frame from a PPM file. If this is the first frame (index 0) and the clip has no frames yet one frame will be allocated. You cannot change the number of frames afterwards. THIS METHOD SHOULD BE PRIVATE DAMN!



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
# File 'ext/nodeframes.cpp', line 163

static VALUE wiretap_node_frame_write_from_file(VALUE self, VALUE index, VALUE filename) {
	WireTapNodeHandle* node;
	WireTapClipFormat format;

	Data_Get_Struct(self, WireTapNodeHandle, node);
	NODERUN_E(node, node->getClipFormat(format));

	unsigned int bufSize = format.frameBufferSize();
	std::auto_ptr<unsigned char> frame(new unsigned char[bufSize]);

	int i = NUM2INT(index);
	
	FILE* infile = fopen(CSTR(filename), "r");
	if(!infile) {
		frame.release();
		THROW("Couldn't open file %s for reading", filename);
	}
	
	if(!ppm_read_format(infile, &format)) {
		frame.release();
		fclose(infile);
		THROW("Couldn't read PPM format from file %s", filename);
	}
	
	if(!ppm_read_image(infile, frame.get(), format.width(), format.height(), format.bitsPerPixel())) {
		frame.release();
		fclose(infile);
		THROW("Couldn't read PPM image data from file %s", filename);
	}
  
  fclose(infile);
  
	int totalFrms;
	NODERUN_E_FRAME(node, node->getNumFrames(totalFrms));
  
	/* Edge case - If we are setting the first frame and the clip has no frames */
	if ((totalFrms == i) && (i == 0)) {
	  NODERUN_E_FRAME(node, node->setNumFrames(i+1)); /* wiretapd will crash on setNumFrames(0) */
	} else if (i >= totalFrms) {
	  /* Edge case - trying to add a frame to a clip after the fact will crash wiretapd */
	  frame.release();
	  THROW("You cannot add frames to an existing clip");
	}

	NODERUN_E_FRAME(node, node->writeFrame(i, frame.get(), bufSize));
	return self;
}