aboutsummaryrefslogtreecommitdiff
path: root/Patches/vsnapshot~.c
blob: ba5e980a4caba587bb4c128af14f773871bccbd7 (plain)
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
#include "m_pd.h"
#include "math.h"
#include "sharemem.h"
#include "vframe.h"

/* -------------------------- vsnapshot~ ------------------------------ */
static t_class *vsnapshot_class;

typedef struct _vsnapshot
{
	t_object x_obj;
	float x_f;
	int id;
	HANDLE hvf, hbits;
	LPVOID memvf, membits;
	unsigned long pos, size;
} t_vsnapshot;

static t_int *vsnapshot_perform(t_int *w)
{
	t_vsnapshot *x = (t_vsnapshot *)(w[1]);
	t_float *in = (t_sample *)(w[2]);
	t_float *sync = (t_sample *)(w[3]);
	int n = (int)(w[4]);
	short *p16;
	long *p32;
	struct vframeimage *vfp=x->memvf;
	unsigned long bytespp;

	if(!vfp || !x->membits) return(w+5);

	bytespp = vfp->f.pixelformat / 8;

	switch(vfp->f.pixelformat)
	{
	case 16:
		p16 = x->membits;
		while (n--)
		{
			if(*sync>0 && *sync<1)
				x->pos=*sync * (float)(x->size / bytespp);
			else
				if(*sync==1) x->pos=(x->size / bytespp)-1;

			p16[x->pos++] = sampletocolor16(*in);
			in++;
			sync++;
			if(x->pos*bytespp >= x->size)
			{
				x->pos=0;
				outlet_float(x->x_obj.ob_outlet, x->id);
			}
		}
		break;
	case 32:
		p32 = x->membits;
		while (n--)
		{
			if(*sync>0 && *sync<1)
				x->pos=*sync * (float)(x->size / bytespp);
			else
				if(*sync==1) x->pos=(x->size / bytespp)-1;

			p32[x->pos++] = sampletocolor32(*in);
			in++;
			sync++;
			if(x->pos*bytespp >= x->size)
			{
				x->pos=0;
				outlet_float(x->x_obj.ob_outlet, x->id);
			}
		}
		break;
	}
	return(w+5);
}

static void vsnapshot_dsp(t_vsnapshot *x, t_signal **sp)
{
	dsp_add(vsnapshot_perform, 4, x, sp[0]->s_vec, sp[1]->s_vec, sp[0]->s_n);
}

static void vsnapshot_float(t_vsnapshot *x, t_float f)
{
	struct vframeimage *vfp;

	if(x->id==f && x->membits!=NULL)
	{
		outlet_float(x->x_obj.ob_outlet, x->id);
		return;
	}

	x->id = x->x_f = f;

	openframedatabyid(x->id,
	 &x->hvf, &x->hbits, &x->memvf, &x->membits, &vfp);
	if(!x->membits)
	{
		post("vsnapshot~: no memory at %d", x->id);
		return;
	} else
	{
		x->size = vfp->f.height*vfp->f.lpitch;
		x->pos = 0;
		outlet_float(x->x_obj.ob_outlet, x->id);
	}
}

static void *vsnapshot_new(t_floatarg f)
{
	t_vsnapshot *x = (t_vsnapshot *)pd_new(vsnapshot_class);
	x->x_f = f;
	x->pos=0;
	x->hvf=NULL;
	x->memvf=NULL;
	x->hbits=NULL;
	x->membits=NULL;
	inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("signal"), gensym("signal"));
	inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("float"), gensym("ft1"));
	outlet_new(&x->x_obj, gensym("float"));
	return (x);
}

static void vsnapshot_destroy(t_vsnapshot *x)
{
	if(x->membits!=NULL) smfree(&x->hbits, x->membits);
	if(x->memvf!=NULL) smfree(&x->hvf, x->memvf);
}

void vsnapshot_tilde_setup(void)
{
	vsnapshot_class = class_new(gensym("vsnapshot~"),
	 (t_newmethod)vsnapshot_new, (t_method)vsnapshot_destroy,
	 sizeof(t_vsnapshot), 0, A_DEFFLOAT, 0);
	CLASS_MAINSIGNALIN(vsnapshot_class, t_vsnapshot, x_f);
	class_addmethod(vsnapshot_class, (t_method)vsnapshot_float,
	 gensym("ft1"), A_FLOAT, 0);
	class_addmethod(vsnapshot_class, (t_method)vsnapshot_dsp, gensym("dsp"), 0);
}