1 #include "filter.h"
2 #include "pteditor.h"
3 
4 #if _MSC_VER > 1000
5 #pragma warning(disable: 4100) // disable unreferenced formal parameter warning
6 #endif
7 
8 //defined in ptpicker.c
9 int jpathTofullPath( const char* jpath, fullPath *fp );
10 
11 static Image *pano = NULL;
12 
13 
Java_pteditor_CLoadImage(JNIEnv * env,jobject obj PT_UNUSED,jstring path)14 JNIEXPORT void JNICALL Java_pteditor_CLoadImage
15 	(JNIEnv *env, jobject obj PT_UNUSED, jstring path){
16 	fullPath fp;
17 	const char *jpath = (*env)->GetStringUTFChars(env, path, 0);
18 
19 #ifdef __Mac__
20 	setLibToResFile();
21 #endif
22 	if( strlen(jpath) == 0 ) return;
23 
24 	if( jpathTofullPath( jpath, &fp ) != 0 ){
25 		PrintError("Could not create Path from %s", jpath);
26 		return;
27 	}
28 
29 	(*env)->ReleaseStringUTFChars(env, path, jpath);
30 
31 	if( pano != NULL ){
32 		if( pano->data != NULL ){
33 			myfree((void**)pano->data);
34 			pano->data = NULL;
35 		}
36 	}else
37 		pano = (Image*)malloc(sizeof(Image));
38 
39 	SetImageDefaults(pano);
40 
41 	if( panoImageRead( pano, &fp ) == 0 ){
42 		PrintError("Could not read image");
43 		return;
44 	}
45 #ifdef __Mac__
46 	unsetLibToResFile();
47 #endif
48 
49 }
50 
51 
Java_pteditor_CSaveImage(JNIEnv * env,jobject obj PT_UNUSED,jstring path)52 JNIEXPORT void JNICALL Java_pteditor_CSaveImage
53 	(JNIEnv *env, jobject obj PT_UNUSED, jstring path){
54 	fullPath fp;
55 	const char *jpath = (*env)->GetStringUTFChars(env, path, 0);
56 
57 #ifdef __Mac__
58 	setLibToResFile();
59 #endif
60 
61 	if( strlen(jpath) > 0 ){
62 		if( jpathTofullPath( jpath, &fp ) != 0 ){
63 			PrintError("Could not create Path from %s", jpath);
64 			return;
65 		}
66 	}
67 	(*env)->ReleaseStringUTFChars(env, path, jpath);
68 
69 	mycreate( &fp, '8BIM', 'TIFF' );
70 	if(pano != NULL)
71 		writeTIFF( pano, &fp );
72 #ifdef __Mac__
73 	unsetLibToResFile();
74 #endif
75 
76 }
77 
78 
Java_pteditor_CGetImageWidth(JNIEnv * env PT_UNUSED,jobject obj PT_UNUSED)79 JNIEXPORT jint JNICALL Java_pteditor_CGetImageWidth
80   (JNIEnv *env PT_UNUSED, jobject obj PT_UNUSED){
81 	if(pano != NULL)
82 		return pano->width;
83 	else
84 		return 0;
85 }
86 
Java_pteditor_CGetImageHeight(JNIEnv * env PT_UNUSED,jobject obj PT_UNUSED)87 JNIEXPORT jint JNICALL Java_pteditor_CGetImageHeight
88   (JNIEnv *env PT_UNUSED, jobject obj PT_UNUSED){
89 	if(pano != NULL)
90 		return pano->height;
91 	else
92 		return 0;
93 }
94 
Java_pteditor_CGetImageRow(JNIEnv * env,jobject obj PT_UNUSED,jintArray jdata,jint nrow)95 JNIEXPORT void JNICALL Java_pteditor_CGetImageRow
96   (JNIEnv *env, jobject obj PT_UNUSED, jintArray jdata, jint nrow){
97 	if(pano == NULL) return;
98   	if(pano->data != NULL){
99 		jint *pix = (jint*)malloc(pano->width * sizeof(jint));
100 		if( pix == NULL ) return;
101 		if( pano->bitsPerPixel == 64 ){
102 			int x;
103 			unsigned char *p = *(pano->data) + pano->bytesPerLine * nrow,*q = (unsigned char*)pix;
104 			for(x=0; x<pano->width; x++, p+=8, q+=4){
105 #ifdef PT_BIGENDIAN
106 				q[0] = p[0]; q[1] = p[2]; q[2] = p[4]; q[3] = p[6];
107 #else
108 				q[0] = p[7]; q[1] = p[5]; q[2] = p[3]; q[3] = p[1];
109 #endif
110 			}
111 		}else{
112 #ifdef PT_BIGENDIAN
113 			memcpy( pix, *(pano->data) + pano->bytesPerLine * nrow, pano->width * sizeof(jint));
114 #else
115 			int x;
116 			unsigned char *p = *(pano->data) + pano->bytesPerLine * nrow,*q = (unsigned char*)pix;
117 			for(x=0; x<pano->width; x++, p+=4, q+=4){
118 				q[0] = p[3];q[1] = p[2];q[2] = p[1];q[3] = p[0];
119 			}
120 #endif
121 		}
122 		(*env)->SetIntArrayRegion( env, jdata, 0, pano->width , pix ) ;
123 		free( pix );
124 	}
125 }
126 
127 
Java_pteditor_CExtract(JNIEnv * env,jobject obj PT_UNUSED,jstring path,jdouble yaw,jdouble pitch,jdouble hfov,jdouble aspect,jint format,jdouble phfov)128 JNIEXPORT void JNICALL Java_pteditor_CExtract
129   (JNIEnv *env, jobject obj PT_UNUSED, jstring path, jdouble yaw, jdouble pitch, jdouble hfov, jdouble aspect, jint format, jdouble phfov ){
130 	aPrefs				ap;
131 	TrformStr  			Tr;
132 	Image				dest;
133 	struct size_Prefs 	spref;
134 	fullPath fp;
135 
136 	const char *jpath = (*env)->GetStringUTFChars(env, path, 0);
137 #ifdef __Mac__
138 	setLibToResFile();
139 #endif
140 
141 	if( pano == NULL ) return;
142 
143 	SetImageDefaults(&dest);
144 	SetAdjustDefaults( &ap );
145 
146 	ap.mode = _extract;
147 
148 	memcpy( &ap.pano, pano, sizeof(Image) );
149 
150 	ap.pano.format 	= format;
151 	ap.pano.hfov	= phfov;
152 
153 	if( ap.pano.format == _equirectangular ){ // extract image from panorama
154 		ap.im.width = (int32_t)((hfov / ap.pano.hfov) * 2 * ap.pano.width);
155 	}else{ // extract image from fisheye source
156 		ap.im.width = (int32_t)((hfov / ap.pano.hfov) * ap.pano.width);
157 	}
158 	ap.im.height	= (int32_t)((double)ap.im.width / aspect);
159 	ap.im.format	= _rectilinear;
160 	ap.im.yaw		= yaw;
161 	ap.im.pitch		= pitch;
162 	ap.im.hfov		= hfov;
163 
164 	if( readPrefs( (char*) &spref, _sizep ) != 0 )
165 		SetSizeDefaults	( &spref );
166 
167 	memset(&Tr, 0, sizeof(TrformStr));
168 	Tr.src 	= pano;
169 	Tr.dest	= &dest;
170 
171 	Tr.tool					= _adjust ;
172 	Tr.mode					= _usedata + _show_progress + _honor_valid;
173 	Tr.interpolator			= spref.interpolator;
174 	Tr.gamma				= spref.gamma;
175     Tr.fastStep             = spref.fastStep;
176 	Tr.data					= (void*) &ap;
177 
178 	Tr.success = 1;
179 
180 	filter_main( &Tr, &spref);
181 
182 	// Save image
183 	if( Tr.success ){
184 		if( strlen(jpath) > 0 ){
185 			if( jpathTofullPath( jpath, &fp ) != 0 ){
186 				PrintError("Could not create Path from %s", jpath);
187 				return;
188 			}
189 		}
190 		(*env)->ReleaseStringUTFChars(env, path, jpath);
191 
192 		mycreate( &fp, '8BIM', 'TIFF' );
193 
194 		writeTIFF( &dest, &fp );
195 		myfree((void**)dest.data);
196 	}
197 #ifdef __Mac__
198 	unsetLibToResFile();
199 #endif
200 
201 }
202 
Java_pteditor_CInsert(JNIEnv * env,jobject obj PT_UNUSED,jstring path,jdouble yaw,jdouble pitch,jdouble rot,jdouble hfov,jint format)203 JNIEXPORT void JNICALL Java_pteditor_CInsert
204   (JNIEnv *env, jobject obj PT_UNUSED, jstring path, jdouble yaw, jdouble pitch, jdouble rot, jdouble hfov, jint format){
205 	aPrefs				ap;
206 	TrformStr  			Tr;
207 	struct size_Prefs 	spref;
208 	fullPath fp;
209 	Image	src, dest;
210 
211 	const char *jpath = (*env)->GetStringUTFChars(env, path, 0);
212 #ifdef __Mac__
213 	setLibToResFile();
214 #endif
215 
216 	if(pano == NULL) return;
217 	SetAdjustDefaults( &ap );
218 	SetImageDefaults(&src);
219 	SetImageDefaults(&dest);
220 
221 	ap.mode = _insert;
222 
223 	memcpy( &ap.pano, pano, sizeof(Image) );
224 	ap.pano.format 	= _equirectangular;
225 	ap.pano.hfov	= 360.0;
226 
227 	// Set destination image here
228 
229 	memcpy( &dest, pano, sizeof(Image) );
230 	dest.data = (unsigned char**)mymalloc(dest.dataSize);
231 	if(dest.data == NULL){
232 		PrintError("Not enough memory");
233 		return;
234 	}
235 
236 
237 	if( strlen(jpath) > 0 ){
238 		if( jpathTofullPath( jpath, &fp ) != 0 ){
239 			PrintError("Could not create Path from %s", jpath);
240 			return;
241 		}
242 	}
243 	(*env)->ReleaseStringUTFChars(env, path, jpath);
244 
245 	if( panoImageRead( &src, &fp ) == 0 ){
246 		PrintError("Could not read image");
247 		return;
248 	}
249 
250 	ap.im.format		= format;
251 	ap.im.yaw		= yaw;
252 	ap.im.pitch		= pitch;
253 	ap.im.hfov		= hfov;
254 	ap.im.roll		= rot;
255 
256 	if( readPrefs( (char*) &spref, _sizep ) != 0 )
257 		SetSizeDefaults	( &spref );
258 
259   memset(&Tr, 0, sizeof(TrformStr));
260 	Tr.src 	= &src;
261 	Tr.dest	= &dest;
262 
263 	Tr.tool					= _adjust ;
264 	Tr.mode					= _usedata + _show_progress + _destSupplied;
265 	Tr.interpolator			= spref.interpolator;
266 	Tr.gamma				= spref.gamma;
267     Tr.fastStep             = spref.fastStep;
268 	Tr.data					= (void*) &ap;
269 
270 	Tr.success = 1;
271 
272 	filter_main( &Tr, &spref);
273 
274 	myfree((void**)src.data);
275 
276 	// Merge images
277 	if( Tr.success ){
278 		if( merge( &dest , pano, ap.sBuf.feather, Tr.mode & _show_progress, _dest ) != 0 ){
279 			PrintError( "Error merging images" );
280 			myfree((void**)dest.data);
281 		}else{
282 			myfree((void**)pano->data);
283 			pano->data = dest.data;
284 		}
285 	}
286 #ifdef __Mac__
287 	unsetLibToResFile();
288 #endif
289 
290 }
291 
292 
Java_pteditor_CSetImageWidth(JNIEnv * env PT_UNUSED,jobject obj PT_UNUSED,jint width)293 JNIEXPORT void JNICALL Java_pteditor_CSetImageWidth
294   (JNIEnv *env PT_UNUSED, jobject obj PT_UNUSED, jint width){
295 	if( pano != NULL ){
296 		if( pano->data != NULL ){
297 			myfree((void**)pano->data);
298 			pano->data = NULL;
299 		}
300 	}else{
301 		pano = (Image*)malloc(sizeof(Image));
302 		SetImageDefaults(pano);
303 	}
304 	pano->width = width;
305 }
306 
Java_pteditor_CSetImageHeight(JNIEnv * env PT_UNUSED,jobject obj PT_UNUSED,jint height)307 JNIEXPORT void JNICALL Java_pteditor_CSetImageHeight
308   (JNIEnv *env PT_UNUSED, jobject obj PT_UNUSED, jint height){
309 	if( pano != NULL ){
310 		if( pano->data != NULL ){
311 			myfree((void**)pano->data);
312 			pano->data = NULL;
313 		}
314 	}else{
315 		pano = (Image*)malloc(sizeof(Image));
316 		SetImageDefaults(pano);
317 	}
318 
319 	pano->height = height;
320 }
321 
Java_pteditor_CSetImageRow(JNIEnv * env,jobject obj PT_UNUSED,jintArray jdata,jint nrow)322 JNIEXPORT void JNICALL Java_pteditor_CSetImageRow
323   (JNIEnv *env, jobject obj PT_UNUSED, jintArray jdata, jint nrow){
324 	if( pano == NULL ) return;
325 	if( pano->width == 0 || pano->height == 0 ) return;
326 
327 	if( pano->data == NULL){
328 		pano->bitsPerPixel = 32;
329 		pano->bytesPerLine = pano->width  * pano->bitsPerPixel/8;
330 		pano->dataSize = pano->height * pano->bytesPerLine;
331 		pano->data = (unsigned char**) mymalloc( pano->dataSize * sizeof(unsigned char) );
332 	}
333 
334 	if( pano->data == NULL) return;
335 
336 	(*env)->GetIntArrayRegion( env, jdata, 0, pano->width ,
337 				(jint*)((*pano->data) + pano->bytesPerLine * nrow) ) ;
338 #ifdef PT_BIGENDIAN
339 #else
340 	{
341 		jint *row = (jint*)((*pano->data) + pano->bytesPerLine * nrow), pix;
342 		unsigned char *p,*q;
343 		int x;
344 		q = (unsigned char*) &pix;
345 		for(x=0; x<pano->width; x++){
346 			p = (unsigned char*) &(row[x]);
347 			q[0] = p[3];
348 			q[1] = p[2];
349 			q[2] = p[1];
350 			q[3] = p[0];
351 			row[x] = pix;
352 		}
353 	}
354 
355 #endif
356 }
357 
358 
359