summaryrefslogtreecommitdiffstats
path: root/tksao/frame/frcommand.C
blob: 1c9e8c811728c25fa7c2cb09b11aa46f8a48c05c (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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// Copyright (C) 1999-2017
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <fstream>
#include "fdstream.hpp"

#include <tk.h>

#include "framebase.h"
#include "fitsimage.h"
#include "context.h"
#include "marker.h"
#include "outfile.h"
#include "outchannel.h"
#include "outsocket.h"

void FrameBase::binToFitCmd()
{
  if (currentContext->fits) {
    double bf = 1/calcZoom(currentContext->fits->getHistDim(), 
			   Vector(options->width,options->height));

    // round up to next power of 2
    if (bf < 1)
      currentContext->setBinToFactor(Vector(1,1));
    else {
      int p=1;
      while (p<bf)
	p*=2;
      currentContext->setBinToFactor(Vector(p,p));
    }
  }

  Matrix mm = currentContext->binCursor();
  updateBin(mm);
}

void FrameBase::blockToFitCmd()
{
  if (currentContext->fits) {
    double bf = 1/calcZoom(currentContext->fits->osize(), 
			   Vector(options->width,options->height));

    // round up to next power of 2
    Vector vv;
    if (bf < 1)
      vv = currentContext->setBlockToFactor(Vector(1,1));
    else {
      int p=1;
      while (p<bf)
	p*=2;
      vv = currentContext->setBlockToFactor(Vector(p,p));
    }
  
    currentContext->block();
    currentContext->analysis();
    updateBlock(vv);
  }
}

void FrameBase::get3dBorderCmd()
{
  Tcl_AppendResult(interp, "1", NULL);
}

void FrameBase::get3dBorderColorCmd()
{
  Tcl_AppendResult(interp, "blue", NULL);
}

void FrameBase::get3dCompassCmd()
{
  Tcl_AppendResult(interp, "1", NULL);
}

void FrameBase::get3dCompassColorCmd()
{
  Tcl_AppendResult(interp, "green", NULL);
}

void FrameBase::get3dHighliteCmd()
{
  Tcl_AppendResult(interp, "1", NULL);
}

void FrameBase::get3dHighliteColorCmd()
{
  Tcl_AppendResult(interp, "cyan", NULL);
}

void FrameBase::get3dRenderMethodCmd()
{
  Tcl_AppendResult(interp, "mip", NULL);
}

void FrameBase::get3dScaleCmd()
{
  Tcl_AppendResult(interp, "1", NULL);
}

void FrameBase::get3dViewCmd()
{
  Tcl_AppendResult(interp, "0 0", NULL);
}

void FrameBase::get3dViewPointCmd()
{
  Tcl_AppendResult(interp, "0 0 0 0 0", NULL);
}

void FrameBase::get3dRenderBackgroundCmd()
{
  Tcl_AppendResult(interp, "none", NULL);
}

void FrameBase::getCursorCmd(Coord::InternalSystem sys)
{
  ostringstream str;
  str << mapFromRef(cursor, sys) << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
} 

void FrameBase::getCursorCmd(Coord::CoordSystem sys, Coord::SkyFrame sky,
			     Coord::SkyFormat format)
{
  if (currentContext->cfits)
    printFromRef(currentContext->cfits, cursor, sys, sky, format);
  else
    Tcl_AppendResult(interp, "0 0", NULL);
} 

void FrameBase::gridCmd(Coord::CoordSystem sys, Coord::SkyFrame sky, 
			Coord::SkyFormat format, Grid::GridType type, 
			const char* ops, const char* vars)
{
  if (grid)
    delete grid;
  grid = new Grid2d(this, sys, sky, format, type, ops, vars);

  update(PIXMAP);
}

void FrameBase::iisCursorModeCmd(int state)
{
  if (state) {
    // set iisLastCursor if first time thru
    if (iisLastCursor[0] == 0 && iisLastCursor[1] == 0)
      iisLastCursor = Vector(options->width,options->height)/2;

    // and move to last cursor position
    Vector rr = iisLastCursor * widgetToWindow;
    warpTo(rr);
  }
}

void FrameBase::iisGetCursorCmd()
{
  ostringstream str;
  str << iisLastCursor << ends;
  Tcl_AppendResult(interp, str.str().c_str(), NULL);
}

void FrameBase::iisGetFileNameCmd()
{
  FitsImage* ptr = currentContext->fits;
  while (ptr && ptr->nextMosaic())
    ptr = ptr->nextMosaic();

  if (ptr)
    Tcl_AppendResult(interp, ptr->iisGetFileName(), NULL);
}

void FrameBase::iisGetFileNameCmd(int which)
{
  FitsImage* ptr = currentContext->fits;
  for (int i=0; i<(which-1); i++) {
    if (ptr)
      ptr = ptr->nextMosaic();
  }

  if (ptr)
    Tcl_AppendResult(interp, ptr->iisGetFileName(), NULL);
}

void FrameBase::iisGetFileNameCmd(const Vector& vv)
{
  if (FitsImage* ptr = isInCFits(vv, Coord::CANVAS, NULL))
    Tcl_AppendResult(interp, ptr->iisGetFileName(), NULL);
}

void FrameBase::iisMessageCmd(const char* ptr)
{
  if (!currentContext->cfits)
    return;

  // do we have something?
  if (!(ptr && *ptr))
    return;

  // filename
  // note: the file name is the second value passed
  char fn[PATH_MAX];
  string x(ptr);
  istringstream str(x);
  str >> fn >> fn;
  currentContext->cfits->setFileName(fn);

  // object name
  // first go to end
  char* sptr = (char*)ptr;
  while (*sptr)
    sptr++;
  // now work backwards to last '-'
  while (sptr != ptr) {
    if (*sptr == '-')
      break;
    sptr--;
  }
  // is it good?
  if (sptr != ptr) {
    // go forward 2
    sptr += 2;
    currentContext->cfits->setObjectKeyword(sptr);
  }
}

void FrameBase::iisSetCursorCmd(const Vector& v, Coord::InternalSystem sys)
{
  // assume canvas
  if (!currentContext->cfits)
    return;

  iisLastCursor = v * canvasToWidget;

  Vector rr = iisLastCursor * widgetToWindow;
  warpTo(rr);
}

void FrameBase::iisSetCursorCmd(const Vector& v, Coord::CoordSystem sys)
{
  if (!currentContext->cfits)
    return;

  iisLastCursor = currentContext->cfits->mapToRef(v,sys) * refToWidget;

  Vector rr = iisLastCursor * widgetToWindow;
  warpTo(rr);
}

void FrameBase::iisSetFileNameCmd(const char* fn)
{
  FitsImage* ptr = currentContext->fits;
  while (ptr && ptr->nextMosaic())
    ptr = ptr->nextMosaic();

  if (ptr)
    ptr->iisSetFileName(fn);
}

void FrameBase::iisSetFileNameCmd(const char* fn, int which)
{
  FitsImage* ptr = currentContext->fits;
  for (int i=0; i<(which-1); i++) {
    if (ptr)
      ptr = ptr->nextMosaic();
  }

  if (ptr)
    ptr->iisSetFileName(fn);
}

void FrameBase::panCmd(const Vector& v1, const Vector& v2)
{
  Vector start = mapToRef(v1,Coord::CANVAS);
  Vector stop = mapToRef(v2,Coord::CANVAS);
  cursor -= stop - start;

  setBinCursor();
  update(MATRIX);
}

void FrameBase::panCmd(const Vector& vv)
{
  Vector uu = mapFromRef(cursor,Coord::CANVAS);
  uu += vv;
  cursor = mapToRef(uu,Coord::CANVAS);

  setBinCursor();
  update(MATRIX);
}

void FrameBase::panCmd(const Vector& vv, Coord::CoordSystem sys, Coord::SkyFrame sky)
{
  if (!currentContext->cfits)
    return;

  Vector uu = currentContext->cfits->mapFromRef(cursor, sys, sky);
  uu += vv;
  cursor = currentContext->cfits->mapToRef(uu, sys, sky);
  
  setBinCursor();
  update(MATRIX);
}

void FrameBase::panToCmd(const Vector& vv)
{
  cursor = mapToRef(vv,Coord::CANVAS);

  setBinCursor();
  update(MATRIX);
}

void FrameBase::panToCmd(const Vector& vv, Coord::CoordSystem sys, Coord::SkyFrame sky)
{
  if (!currentContext->cfits)
    return;

  cursor = currentContext->cfits->mapToRef(vv, sys, sky);

  setBinCursor();
  update(MATRIX);
}

void FrameBase::panBBoxCmd(const Vector& vv)
{
  if (!currentContext->cfits)
    return;

  // we want to round to nearest pixel center
  Vector rr = vv * currentContext->cfits->pannerToData;
  cursor = (rr.floor() + Vector(.5,.5)) * currentContext->cfits->dataToRef;

  setBinCursor();
  update(MATRIX);
}

void FrameBase::panEndCmd(const Vector& vv)
{
  // vv and panCursor are in Coord::CANVAS coords
  // delete tmp pixmap
  if (panPM)
    Tk_FreePixmap(display, panPM);
  panPM = 0;

  // use matrix, not map() for 3d?
  Vector start = panCursor * canvasToRef;
  Vector stop = vv * canvasToRef;
  cursor -= stop - start;

  setBinCursor();
  update(MATRIX);
}

void FrameBase::rotateBeginCmd()
{
  // save the current rotation
  rotateRotation = rotation;

  // Create src XImage
  if (!(rotateSrcXM = XGetImage(display, pixmap, 0, 0,
				options->width, options->height, 
				AllPlanes, ZPixmap))) {
    internalError("Unable to Create Rotate XImage");
    return;
  }

  // Create dest XImage
  if (!(rotateDestXM = XGetImage(display, pixmap, 0, 0,
				 options->width, options->height, 
				 AllPlanes, ZPixmap))) {
    internalError("Unable to Create Rotate XImage");
    return;
  }

  // Create dest Pixmap
  rotatePM = Tk_GetPixmap(display, Tk_WindowId(tkwin), 
			  options->width, options->height, depth);
  if (!rotatePM) {
    internalError("Unable to Create Rotate Motion Pixmap");
    return;
  }
}

void FrameBase::rotateMotionCmd(double angle)
{
  rotation = rotateRotation + angle;
  rotateMotion();
}

void FrameBase::rotateEndCmd()
{
  // Clean up
  if (rotateSrcXM)
    XDestroyImage(rotateSrcXM);
 
  if (rotateDestXM)
    XDestroyImage(rotateDestXM);

  if (rotatePM)
    Tk_FreePixmap(display, rotatePM);

  update(MATRIX);
}

void FrameBase::saveFitsResampleFileCmd(const char* fn)
{
  OutFitsFile str(fn);
  if (str.valid())
    saveFitsResample(str);
}

void FrameBase::saveFitsResampleChannelCmd(const char* ch)
{
  OutFitsChannel str(interp, ch);
  if (str.valid())
    saveFitsResample(str);
}

void FrameBase::saveFitsResampleSocketCmd(int ss)
{
  OutFitsSocket str(ss);
  if (str.valid())
    saveFitsResample(str);
}

void FrameBase::zoomAboutCmd(const Vector& zz, const Vector& vv)
{
  Vector az = ((Vector&)zz).abs();
  zoom_[0] *= az[0];
  zoom_[1] *= az[1];

  cursor = mapToRef(vv,Coord::CANVAS);

  setBinCursor();
  update(MATRIX);
}

void FrameBase::zoomAboutCmd(const Vector& z, const Vector& vv, 
			     Coord::CoordSystem sys, Coord::SkyFrame sky)
{
  Vector az = ((Vector&)z).abs();
  zoom_[0] *= az[0];
  zoom_[1] *= az[1];

  if (currentContext->cfits) {
    cursor = currentContext->cfits->mapToRef(vv, sys, sky);
    setBinCursor();
  }
  update(MATRIX);
}

void FrameBase::zoomToAboutCmd(const Vector& z, const Vector& vv)
{
  zoom_ = ((Vector&)z).abs();

  cursor = mapToRef(vv,Coord::CANVAS);

  setBinCursor();
  update(MATRIX);
}

void FrameBase::zoomToAboutCmd(const Vector& z, const Vector& vv, 
			       Coord::CoordSystem sys, Coord::SkyFrame sky)
{
  zoom_ = ((Vector&)z).abs();

  if (currentContext->cfits) {
    cursor = currentContext->cfits->mapToRef(vv, sys, sky);
    setBinCursor();
  }
  update(MATRIX);
}

void FrameBase::zoomToFitCmd(double ss)
{
  // will center image on center of pixel
  // with even number pixels, it will be shifted

  if (keyContext->fits) {
    centerImage();

    Vector tt = imageSize(keyContext->secMode());

    // adjust image size to be odd (see above)
    double dd; // dummy
    if (!modf(tt[0]/2,&dd))
      tt[0] +=1;
    if (!modf(tt[1]/2,&dd))
      tt[1] +=1;

    double zz = calcZoom(tt,Vector(options->width,options->height)) * ss;
    zoom_ = Vector(zz,zz);
    update(MATRIX);
  }
}