Diff of /pteditor.c [000000] .. [2a97a8] Maximize Restore

  Switch to unified view

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