Welcome to mirror list, hosted at ThFree Co, Russian Federation.

verse_image.c « src « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe9e6137091b4f2d1e025f0e8a70549a1cddd033 (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
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
/**
 * $Id$
 *
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Contributor(s): Jiri Hnidek.
 *
 * ***** END GPL LICENSE BLOCK *****
 */

#ifdef WITH_VERSE

#include <string.h>

#include "mydevice.h"

#include "BKE_verse.h"
#include "BKE_image.h"

#include "MEM_guardedalloc.h"

#include "DNA_image_types.h"

#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"

#include "BDR_drawmesh.h"

#include "BIF_verse.h"
#include "BIF_space.h"

/*
 * unsubscribe from verse bitmap
 */
void unsubscribe_from_bitmap_node(VNode *vnode)
{
	if(vnode->type != V_NT_BITMAP) return;
	
	/* TODO */
}

/*
 * upload image to verse server
 */
void push_image_to_verse_server(VerseSession *session, Image *image)
{
	ImBuf *ibuf= BKE_image_get_ibuf(image, NULL);
	struct VNode *vnode;

	if(!session) return;

	if(!(session->flag & VERSE_CONNECTED)) return;

	/* create "my" new object VerseNode */
	vnode= create_verse_node(session, -1 , V_NT_BITMAP, VN_OWNER_MINE);
	/* create object data */
	vnode->data = create_bitmap_data();

	/* set up name of VerseNode */
	vnode->name = (char*)MEM_mallocN(sizeof(char*)*(strlen(image->id.name)-1), "object node name");
	vnode->name[0] = '\0';
	strcat(vnode->name, image->id.name+2);

	/* set up dimension of image */
	if(ibuf) {
		((VBitmapData*)vnode->data)->width = ibuf->x;
		((VBitmapData*)vnode->data)->height = ibuf->y;
	}
	else {
		((VBitmapData*)vnode->data)->width = 0;
		((VBitmapData*)vnode->data)->height = 0;
	}
	((VBitmapData*)(vnode->data))->height = 1;

	/* set up pointers between Object and VerseNode */
	((VBitmapData*)vnode->data)->image = (void*)image;
	image->vnode = (void*)vnode;

	/* add node to sending queue */
	add_item_to_send_queue(&(session->queue), vnode, VERSE_NODE);
}

/*
 * synchronize blender image channel (R,G,B,A) with verse bitmap layer
 */
void sync_blender_image_channel_with_verse_layer(VNode *vnode, VBitmapLayer *vblayer)
{
	struct Image *image = (Image*)((VBitmapData*)(vnode->data))->image;
	struct ImBuf *ibuf= BKE_image_get_ibuf(image, NULL);
	unsigned char *rect;
	int x, y, height, t_width, i, channel=0;

	if(!image) return;

	if(!ibuf) return;

	rect = (unsigned char*)ibuf->rect;

	/* select channel due to verse layer name */
	if(strcmp(vblayer->name,"col_r")==0)
		channel = 0;
	else if(strcmp(vblayer->name,"col_g")==0)
		channel = 1;
	else if(strcmp(vblayer->name, "col_b")==0)
		channel = 2;
	else if(strcmp(vblayer->name,"alpha")==0)
		channel = 3;

#ifdef VERSE_DEBUG_PRINT
	printf(" %s:%d\n", vblayer->name, channel);
#endif

	height = ((VBitmapData*)(vnode->data))->height;
	t_width = ((VBitmapData*)(vnode->data))->t_width;

	i = (height-1)*t_width;

#ifdef VERSE_DEBUG_PRINT
	printf("\ti:%d\n", i);
#endif

	if(vblayer->type==VN_B_LAYER_UINT8) {
		unsigned char *vuint8 = (unsigned char*)vblayer->data;
		for(y=height-1; y>=0; y--, i=y*t_width)
			for(x=0; x<ibuf->x; x++, rect+=4, i++)
				rect[channel] = (char)vuint8[i];
	}

	allqueue(REDRAWIMAGE, 0);
	allqueue(REDRAWVIEW3D, 0);
}

/*
 * synchronize blender image with verse image
 */
void sync_blender_image_with_verse_bitmap_node(VNode *vnode)
{
	struct VBitmapLayer *vblayer;

	vblayer = ((VBitmapData*)(vnode->data))->layers.lb.first;

	while(vblayer) {
#ifdef VERSE_DEBUG_PRINT
		printf("\tsyncing layer:");
#endif
		sync_blender_image_channel_with_verse_layer(vnode, vblayer);
		vblayer = vblayer->next;
	}
}

/*
 * This function is called, when some other verse client change dimension of image.
 * It is neccesary to reallocate blender image too, when dimension of verse image
 * is different from blender image.
 */
void post_bitmap_dimension_set(VNode *vnode)
{
	struct Image *image = (Image*)((VBitmapData*)(vnode->data))->image;
	struct ImBuf *ibuf= BKE_image_get_ibuf(image, NULL);

	if(!image) return;

	if(!ibuf) return;

	if(vnode->owner_id == VN_OWNER_MINE) {
		if( ((VBitmapData*)vnode->data)->layers.lb.first == NULL ) {
			/* send all verse bitmap layers (RGBA) to verse server */
			printf("\tsending all bitmap layers to verse server\n");
			verse_send_b_layer_create(vnode->id, -1, "col_r", VN_B_LAYER_UINT8);
			verse_send_b_layer_create(vnode->id, -1, "col_g", VN_B_LAYER_UINT8);
			verse_send_b_layer_create(vnode->id, -1, "col_b", VN_B_LAYER_UINT8);
			verse_send_b_layer_create(vnode->id, -1, "alpha", VN_B_LAYER_UINT8);

			return;
		}
	}

	if((ibuf->x!=((VBitmapData*)vnode->data)->width) || (ibuf->y!=((VBitmapData*)vnode->data)->height)) {
		struct VBitmapLayer *vblayer;
		struct ImBuf *new_ibuf;

		/* allocate new ibuf */
		new_ibuf= IMB_allocImBuf(((VBitmapData*)vnode->data)->width,
				((VBitmapData*)vnode->data)->height, 24, IB_rect, 0);
		/* free old ibuf */
		BKE_image_signal(image, NULL, IMA_SIGNAL_FREE);
		/* set up pointer at new ibuf */
		BKE_image_assign_ibuf(image, ibuf);
		
		/* sync blender image with all verse layers */
		vblayer = ((VBitmapData*)(vnode->data))->layers.lb.first;
		while(vblayer) {
			sync_blender_image_channel_with_verse_layer(vnode, vblayer);
			vblayer = vblayer->next;
		}
	}
}

/*
 * when blender tries to upload image to verse server, then it is neccessary
 * to push coresponding channel data to verse server, when verse bitmap layer
 * was created
 */
void post_bitmap_layer_create(VBitmapLayer *vblayer)
{
	struct VNode *vnode = vblayer->vnode;
	struct Image *image = (Image*)((VBitmapData*)(vnode->data))->image;
	struct ImBuf *ibuf= BKE_image_get_ibuf(image, NULL);
	unsigned char *rect;
	short channel;
/*	VNBTile tile[VN_B_TILE_SIZE*VN_B_TILE_SIZE];
	unsigned int width, t_width, height, t_height, x, y, i, j; */

	/* if this application doesn't try to upload this image to verse
	 * server then do nothing */
	if(vnode->owner_id != VN_OWNER_MINE) return;
	
	if(!image) return;

	if(!ibuf) return;

	rect = (unsigned char*)ibuf->rect;

	if(strncmp(vblayer->name, "col_r", 5))
		channel = 0;
	else if(strncmp(vblayer->name, "col_g", 5))
		channel = 1;
	else if(strncmp(vblayer->name, "col_b", 5))
		channel = 2;
	else if(strncmp(vblayer->name, "alpha", 5))
		channel = 3;

	/* TODO: send all data of channel to verse server */
}

/*
 * dummy function now
 */
void post_bitmap_layer_destroy(VBitmapLayer *vblayer)
{
}

/*
 * this function is executed, when some image changed tile comes from verse server,
 * it is neccessary to do some crazy transformation here, because blender uses
 * different (very unstandard) image coordinate system (begining of coordinate
 * system is in bottom left corner) ... all other programs (including verse) has
 * begining of image coordinate system in left top corner
 */
void post_bitmap_tile_set(VBitmapLayer *vblayer, unsigned int xs, unsigned int ys)
{
	struct VNode *vnode = vblayer->vnode;
	struct Image *image = (Image*)((VBitmapData*)(vnode->data))->image;
	struct ImBuf *ibuf= BKE_image_get_ibuf(image, NULL);
	unsigned char *rect, *i_rect;
	unsigned int x, y, t_width, t_height, height, m_ys, m_y, d, i, j, channel=0;

	if(!image) return;

	if(!ibuf) return;

	/* select channel due to verse layer name */
	if(strcmp(vblayer->name,"color_r")==0)
		channel = 0;
	else if(strcmp(vblayer->name,"color_g")==0)
		channel = 1;
	else if(strcmp(vblayer->name, "color_b")==0)
		channel = 2;
	else if(strcmp(vblayer->name,"transparency")==0)
		channel = 3;

	i_rect = rect = (unsigned char*)ibuf->rect;

	/* width of verse image including all tiles */
	t_width =((VBitmapData*)vnode->data)->t_width;
	/* height of verse image including all tiles */
	t_height =((VBitmapData*)vnode->data)->t_height;
	/* height of blender image */
	height = ((VBitmapData*)vnode->data)->height;

	/* if the bitmap's dimensions are not integer multiples of the tile
	 * side length, eight, then d will not be zero (height of "uncomplete
	 * tile") */
	d = VN_B_TILE_SIZE - (t_height - height);
	/* mirrored coordination of received tile */
	m_ys = t_height - ys - VN_B_TILE_SIZE;

	/* ys and m_ys are y axis, where we will do some changes */
	if(ys + VN_B_TILE_SIZE > height) {
		m_ys = 0;
		ys = ys + d - 1;
	}
	else {
		m_ys = m_ys - VN_B_TILE_SIZE + d;
		ys = ys + VN_B_TILE_SIZE - 1;
	}

	/* "index" of blender image */
	j = m_ys*ibuf->x + xs;
	/* index of verse image */
	i = ys*t_width + xs;

	/* pointer at image data, that will be changed in following loop */
	rect = i_rect + 4*j;

	/* it seems hackish, but I didn't find better solution :-/ */
	if(vblayer->type==VN_B_LAYER_UINT8) {
		unsigned char *vuint8 = (unsigned char*)vblayer->data;
		for(y=ys, m_y = m_ys;
			(m_y<m_ys+VN_B_TILE_SIZE) && (m_y<ibuf->y);
			y--, m_y++, i=y*t_width+xs, j=m_y*ibuf->x+xs, rect=i_rect+4*j)
			for(x=xs; (x<xs+VN_B_TILE_SIZE) && (x<ibuf->x); x++, rect+=4, i++, j++)
				rect[channel] = (char)vuint8[i];
	}

	free_realtime_image(image);

	/* redraw preview of image ... uncommented, because rendering
	 * was computed too often */
/*	BIF_preview_changed(ID_TE); */
	allqueue(REDRAWIMAGE, 0);
	allqueue(REDRAWVIEW3D, 0);
}

#endif