2
* Pure Data Packet module.
3
* Copyright (c) by Tom Schouten <pdp@zzz.kotnet.org>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
/* this module converts a greyscale image or the luma channel of a colour image
23
to a colour image intensity mask, usable for multiplication */
26
#include "pdp_resample.h"
28
typedef struct pdp_grey2mask_struct
44
static void pdp_grey2mask_process_grey(t_pdp_grey2mask *x)
46
t_pdp *header = pdp_packet_header(x->x_packet0);
47
short int *data = (short int *)pdp_packet_data (x->x_packet0);
49
short int *newdata = 0;
52
unsigned int w = header->info.image.width;
53
unsigned int h = header->info.image.height;
55
unsigned int size = w*h;
56
unsigned int totalnbpixels = size;
57
unsigned int u_offset = size;
58
unsigned int v_offset = size + (size>>2);
60
unsigned int row, col;
62
newpacket = pdp_packet_new_image_YCrCb(w, h);
63
newheader = pdp_packet_header(newpacket);
64
newdata = (short int *)pdp_packet_data(newpacket);
66
/* copy luma channel */
67
memcpy(newdata, data, size * sizeof(s16));
69
/* subsample luma -> chroma channel */
70
pdp_resample_halve(data, newdata+u_offset, w, h);
72
/* copy this to the other chroma channel */
73
memcpy(newdata+v_offset, newdata+u_offset, (size>>2)*sizeof(s16));
75
/* delete source packet and replace with new packet */
76
pdp_packet_mark_unused(x->x_packet0);
77
x->x_packet0 = newpacket;
81
static void pdp_grey2mask_process_yv12(t_pdp_grey2mask *x)
83
/* process only the luminance channel */
84
pdp_grey2mask_process_grey(x);
89
static void pdp_grey2mask_process(t_pdp_grey2mask *x)
94
/* check if image data packets are compatible */
95
if ( (header = pdp_packet_header(x->x_packet0))
96
&& (PDP_IMAGE == header->type)){
98
/* pdp_grey2mask_process inputs and write into active inlet */
99
switch(pdp_packet_header(x->x_packet0)->info.image.encoding){
102
pdp_grey2mask_process_yv12(x);
106
pdp_grey2mask_process_grey(x);
110
/* don't know the type, so dont pdp_grey2mask_process */
117
static void pdp_grey2mask_sendpacket(t_pdp_grey2mask *x)
119
/* unregister and propagate if valid packet */
120
pdp_packet_pass_if_valid(x->x_outlet0, &x->x_packet0);
123
static void pdp_grey2mask_input_0(t_pdp_grey2mask *x, t_symbol *s, t_floatarg f)
128
if (s== gensym("register_ro")) x->x_dropped = pdp_packet_copy_ro_or_drop(&x->x_packet0, p);
131
if ((s == gensym("process")) && (-1 != x->x_packet0) && (!x->x_dropped)){
134
/* add the process method and callback to the process queue */
136
//pdp_queue_add(x, pdp_grey2mask_process, pdp_grey2mask_sendpacket, &x->x_queue_id);
137
// since the process method creates a packet, this is not processed in the thread
139
pdp_grey2mask_process(x);
140
pdp_grey2mask_sendpacket(x);
147
static void pdp_grey2mask_free(t_pdp_grey2mask *x)
149
t_pdp_procqueue *q = pdp_queue_get_queue();
150
pdp_procqueue_finish(q, x->x_queue_id);
151
pdp_packet_mark_unused(x->x_packet0);
155
t_class *pdp_grey2mask_class;
159
void *pdp_grey2mask_new(void)
163
t_pdp_grey2mask *x = (t_pdp_grey2mask *)pd_new(pdp_grey2mask_class);
165
x->x_outlet0 = outlet_new(&x->x_obj, &s_anything);
180
void pdp_grey2mask_setup(void)
184
pdp_grey2mask_class = class_new(gensym("pdp_grey2mask"), (t_newmethod)pdp_grey2mask_new,
185
(t_method)pdp_grey2mask_free, sizeof(t_pdp_grey2mask), 0, A_NULL);
188
class_addmethod(pdp_grey2mask_class, (t_method)pdp_grey2mask_input_0, gensym("pdp"), A_SYMBOL, A_DEFFLOAT, A_NULL);