File: pteditor.c

package info (click to toggle)
libpano13 2.9.12.dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 8,616 kB
  • ctags: 3,403
  • sloc: ansic: 31,169; sh: 8,383; cpp: 1,714; makefile: 236; perl: 202
file content (354 lines) | stat: -rw-r--r-- 7,999 bytes parent folder | download
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
#include "filter.h"
#include "pteditor.h"

#if _MSC_VER > 1000
#pragma warning(disable: 4100) // disable unreferenced formal parameter warning
#endif

//defined in ptpicker.c
int jpathTofullPath( const char* jpath, fullPath *fp );

static Image *pano = NULL;


JNIEXPORT void JNICALL Java_pteditor_CLoadImage
	(JNIEnv *env, jobject obj PT_UNUSED, jstring path){
	fullPath fp;
	const char *jpath = (*env)->GetStringUTFChars(env, path, 0);

#ifdef __Mac__
	setLibToResFile();
#endif
	if( strlen(jpath) == 0 ) return;	

	if( jpathTofullPath( jpath, &fp ) != 0 ){
		PrintError("Could not create Path from %s", jpath);
		return;
	}

	(*env)->ReleaseStringUTFChars(env, path, jpath);
	
	if( pano != NULL ){
		if( pano->data != NULL ){
			myfree((void**)pano->data);
			pano->data = NULL;
		}
	}else
		pano = (Image*)malloc(sizeof(Image));
	
	SetImageDefaults(pano);

	if( panoImageRead( pano, &fp ) == 0 ){
		PrintError("Could not read image");
		return;
	}
#ifdef __Mac__
	unsetLibToResFile();
#endif

}


JNIEXPORT void JNICALL Java_pteditor_CSaveImage
	(JNIEnv *env, jobject obj PT_UNUSED, jstring path){
	fullPath fp;
	const char *jpath = (*env)->GetStringUTFChars(env, path, 0);

#ifdef __Mac__
	setLibToResFile();
#endif

	if( strlen(jpath) > 0 ){
		if( jpathTofullPath( jpath, &fp ) != 0 ){
			PrintError("Could not create Path from %s", jpath);
			return;
		}
	}
	(*env)->ReleaseStringUTFChars(env, path, jpath);
	
	mycreate( &fp, '8BIM', 'TIFF' );
	if(pano != NULL)
		writeTIFF( pano, &fp );
#ifdef __Mac__
	unsetLibToResFile();
#endif

}


JNIEXPORT jint JNICALL Java_pteditor_CGetImageWidth
  (JNIEnv *env PT_UNUSED, jobject obj PT_UNUSED){
	if(pano != NULL)
		return pano->width;
	else
		return 0;
}

JNIEXPORT jint JNICALL Java_pteditor_CGetImageHeight
  (JNIEnv *env PT_UNUSED, jobject obj PT_UNUSED){
	if(pano != NULL)
		return pano->height;
	else
		return 0;
}

JNIEXPORT void JNICALL Java_pteditor_CGetImageRow
  (JNIEnv *env, jobject obj PT_UNUSED, jintArray jdata, jint nrow){
	if(pano == NULL) return;
  	if(pano->data != NULL){
		jint *pix = (jint*)malloc(pano->width * sizeof(jint));
		if( pix == NULL ) return;
		if( pano->bitsPerPixel == 64 ){
			int x;
			UCHAR *p = *(pano->data) + pano->bytesPerLine * nrow,*q = (UCHAR*)pix;
			for(x=0; x<pano->width; x++, p+=8, q+=4){
#ifdef PT_BIGENDIAN
				q[0] = p[0]; q[1] = p[2]; q[2] = p[4]; q[3] = p[6];
#else
				q[0] = p[7]; q[1] = p[5]; q[2] = p[3]; q[3] = p[1];
#endif
			}
		}else{
#ifdef PT_BIGENDIAN
			memcpy( pix, *(pano->data) + pano->bytesPerLine * nrow, pano->width * sizeof(jint));				
#else
			int x;
			UCHAR *p = *(pano->data) + pano->bytesPerLine * nrow,*q = (UCHAR*)pix;
			for(x=0; x<pano->width; x++, p+=4, q+=4){
				q[0] = p[3];q[1] = p[2];q[2] = p[1];q[3] = p[0];
			}
#endif
		}
		(*env)->SetIntArrayRegion( env, jdata, 0, pano->width , pix ) ;
		free( pix );
	}
}
 
 
JNIEXPORT void JNICALL Java_pteditor_CExtract
  (JNIEnv *env, jobject obj PT_UNUSED, jstring path, jdouble yaw, jdouble pitch, jdouble hfov, jdouble aspect, jint format, jdouble phfov ){
	aPrefs				ap;
	TrformStr  			Tr;
	Image				dest;
	struct size_Prefs 	spref;
	fullPath fp;

	const char *jpath = (*env)->GetStringUTFChars(env, path, 0);
#ifdef __Mac__
	setLibToResFile();
#endif

	if( pano == NULL ) return;

	SetImageDefaults(&dest);
	SetAdjustDefaults( &ap );

	ap.mode = _extract;
	
	memcpy( &ap.pano, pano, sizeof(Image) );
	
	ap.pano.format 	= format;
	ap.pano.hfov	= phfov;

	if( ap.pano.format == _equirectangular ){ // extract image from panorama	
		ap.im.width = (hfov / ap.pano.hfov) * 2 * ap.pano.width;
	}else{ // extract image from fisheye source
		ap.im.width = (hfov / ap.pano.hfov) * ap.pano.width;
	}
	ap.im.height	= (double)ap.im.width / aspect;
	ap.im.format	= _rectilinear;
	ap.im.yaw		= yaw;
	ap.im.pitch		= pitch;
	ap.im.hfov		= hfov;

	if( readPrefs( (char*) &spref, _sizep ) != 0 )
		SetSizeDefaults	( &spref );
	
	Tr.src 	= pano;
	Tr.dest	= &dest;

	Tr.tool					= _adjust ;
	Tr.mode					= _usedata + _show_progress + _honor_valid;
	Tr.interpolator			= spref.interpolator;
	Tr.gamma				= spref.gamma;
	Tr.data					= (void*) &ap;

	Tr.success = 1;
	
	filter_main( &Tr, &spref);
	
	// Save image
	if( Tr.success ){
		if( strlen(jpath) > 0 ){
			if( jpathTofullPath( jpath, &fp ) != 0 ){
				PrintError("Could not create Path from %s", jpath);
				return;
			}
		}
		(*env)->ReleaseStringUTFChars(env, path, jpath);

		mycreate( &fp, '8BIM', 'TIFF' );

		writeTIFF( &dest, &fp );
		myfree((void**)dest.data);
	}
#ifdef __Mac__
	unsetLibToResFile();
#endif

}

JNIEXPORT void JNICALL Java_pteditor_CInsert
  (JNIEnv *env, jobject obj PT_UNUSED, jstring path, jdouble yaw, jdouble pitch, jdouble rot, jdouble hfov, jint format){
	aPrefs				ap;
	TrformStr  			Tr;
	struct size_Prefs 	spref;
	fullPath fp;
	Image	src, dest;

	const char *jpath = (*env)->GetStringUTFChars(env, path, 0);
#ifdef __Mac__
	setLibToResFile();
#endif

	if(pano == NULL) return;
	SetAdjustDefaults( &ap );
	SetImageDefaults(&src);
	SetImageDefaults(&dest);

	ap.mode = _insert;
	
	memcpy( &ap.pano, pano, sizeof(Image) );
	ap.pano.format 	= _equirectangular;
	ap.pano.hfov	= 360.0;
	
	// Set destination image here
	
	memcpy( &dest, pano, sizeof(Image) );
	dest.data = (unsigned char**)mymalloc(dest.dataSize);
	if(dest.data == NULL){
		PrintError("Not enough memory");
		return;
	}


	if( strlen(jpath) > 0 ){
		if( jpathTofullPath( jpath, &fp ) != 0 ){
			PrintError("Could not create Path from %s", jpath);
			return;
		}
	}
	(*env)->ReleaseStringUTFChars(env, path, jpath);

	if( panoImageRead( &src, &fp ) == 0 ){
		PrintError("Could not read image");
		return;
	}

	ap.im.format		= format;
	ap.im.yaw		= yaw;
	ap.im.pitch		= pitch;
	ap.im.hfov		= hfov;
	ap.im.roll		= rot;

	if( readPrefs( (char*) &spref, _sizep ) != 0 )
		SetSizeDefaults	( &spref );
	
	Tr.src 	= &src;
	Tr.dest	= &dest;

	Tr.tool					= _adjust ;
	Tr.mode					= _usedata + _show_progress + _destSupplied;
	Tr.interpolator			= spref.interpolator;
	Tr.gamma				= spref.gamma;
	Tr.data					= (void*) &ap;

	Tr.success = 1;
	
	filter_main( &Tr, &spref);
	
	myfree((void**)src.data);
	
	// Merge images
	if( Tr.success ){
		if( merge( &dest , pano, ap.sBuf.feather, Tr.mode & _show_progress, _dest ) != 0 ){
			PrintError( "Error merging images" );
			myfree((void**)dest.data);
		}else{
			myfree((void**)pano->data);
			pano->data = dest.data;
		}
	}
#ifdef __Mac__
	unsetLibToResFile();
#endif

}


JNIEXPORT void JNICALL Java_pteditor_CSetImageWidth
  (JNIEnv *env PT_UNUSED, jobject obj PT_UNUSED, jint width){
	if( pano != NULL ){
		if( pano->data != NULL ){
			myfree((void**)pano->data);
			pano->data = NULL;
		}
	}else{
		pano = (Image*)malloc(sizeof(Image));
		SetImageDefaults(pano);
	}
	pano->width = width;
}

JNIEXPORT void JNICALL Java_pteditor_CSetImageHeight
  (JNIEnv *env PT_UNUSED, jobject obj PT_UNUSED, jint height){
	if( pano != NULL ){
		if( pano->data != NULL ){
			myfree((void**)pano->data);
			pano->data = NULL;
		}
	}else{
		pano = (Image*)malloc(sizeof(Image));
		SetImageDefaults(pano);
	}
	
	pano->height = height;
}

JNIEXPORT void JNICALL Java_pteditor_CSetImageRow
  (JNIEnv *env, jobject obj PT_UNUSED, jintArray jdata, jint nrow){
	if( pano == NULL ) return;
	if( pano->width == 0 || pano->height == 0 ) return;
	
	if( pano->data == NULL){
		pano->bitsPerPixel = 32;
		pano->bytesPerLine = pano->width  * pano->bitsPerPixel/8;
		pano->dataSize = pano->height * pano->bytesPerLine;
		pano->data = (UCHAR**) mymalloc( pano->dataSize * sizeof(UCHAR) );
	}

	if( pano->data == NULL) return;

	(*env)->GetIntArrayRegion( env, jdata, 0, pano->width , 
				(jint*)((*pano->data) + pano->bytesPerLine * nrow) ) ;
#ifdef PT_BIGENDIAN
#else
	{
		jint *row = (jint*)((*pano->data) + pano->bytesPerLine * nrow), pix;
		unsigned char *p,*q;
		int x;
		q = (unsigned char*) &pix;
		for(x=0; x<pano->width; x++){
			p = (unsigned char*) &(row[x]);
			q[0] = p[3];
			q[1] = p[2];
			q[2] = p[1];
			q[3] = p[0];
			row[x] = pix;
		}
	}
				
#endif
}