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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
// Copyright (C) 1999-2018
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"
#include <tkInt.h>
#include "frame.h"
#include "fitsimage.h"
#include "ps.h"
#include "sigbus.h"
// Frame Member Functions
Frame::Frame(Tcl_Interp* i, Tk_Canvas c, Tk_Item* item)
: FrameBase(i,c,item)
{
context = new Context();
context->parent(this);
currentContext = context;
keyContext = context;
keyContextSet =1;
colormapData =NULL;
cmapID = 1;
bias = 0.5;
contrast = 1.0;
colorCount = 0;
colorScale = NULL;
colorCells = NULL;
}
Frame::~Frame()
{
if (context)
delete context;
if (colorScale)
delete colorScale;
if (colorCells)
delete [] colorCells;
if (colormapData)
delete [] colormapData;
}
unsigned char* Frame::blend(unsigned char* src, unsigned char* msk,
int width, int height)
{
unsigned char* sptr = src; // 3 component
unsigned char* mptr = msk; // 4 component, premultiplied
for (int jj=0; jj<height; jj++)
for (int ii=0; ii<width; ii++) {
if (*(mptr+3)) {
float aa = 1-maskAlpha;
*sptr = *mptr++ + *sptr * aa;
sptr++;
*sptr = *mptr++ + *sptr * aa;
sptr++;
*sptr = *mptr++ + *sptr * aa;
sptr++;
mptr++;
}
else {
mptr+=4;
sptr+=3;
}
}
return src;
}
unsigned char* Frame::fillImage(int width, int height,
Coord::InternalSystem sys)
{
// img
unsigned char* img = new unsigned char[width*height*3];
{
unsigned char* ptr = img;
for (int jj=0; jj<height; jj++)
for (int ii=0; ii<width; ii++) {
*ptr++ = (unsigned char)bgColor->red;
*ptr++ = (unsigned char)bgColor->green;
*ptr++ = (unsigned char)bgColor->blue;
}
}
if (!context->cfits)
return img;
// basics
int length = colorScale->size() - 1;
const unsigned char* table = colorScale->psColors();
FitsImage* sptr = context->cfits;
int mosaic = isMosaic();
// variable
double* mm = sptr->matrixToData(sys).mm();
FitsBound* params = sptr->getDataParams(context->secMode());
int srcw = sptr->width();
double ll = sptr->low();
double hh = sptr->high();
double diff = hh - ll;
// main loop
unsigned char* dest = img;
SETSIGBUS
for (long jj=0; jj<height; jj++) {
for (long ii=0; ii<width; ii++, dest+=3) {
if (mosaic) {
sptr = context->cfits;
mm = sptr->matrixToData(sys).mm();
params = sptr->getDataParams(context->secMode());
srcw = sptr->width();
ll = sptr->low();
hh = sptr->high();
diff = hh - ll;
}
do {
double xx = ii*mm[0] + jj*mm[3] + mm[6];
double yy = ii*mm[1] + jj*mm[4] + mm[7];
if (xx>=params->xmin && xx<params->xmax &&
yy>=params->ymin && yy<params->ymax) {
double value = sptr->getValueDouble(long(yy)*srcw + long(xx));
if (isfinite(diff) && isfinite(value)) {
if (value <= ll) {
*(dest+2) = table[0];
*(dest+1) = table[1];
*dest = table[2];
}
else if (value >= hh) {
*(dest+2) = table[length*3];
*(dest+1) = table[length*3+1];
*dest = table[length*3+2];
}
else {
int l = (int)(((value - ll)/diff * length) + .5);
*(dest+2) = table[l*3];
*(dest+1) = table[l*3+1];
*dest = table[l*3+2];
}
}
else {
*(dest+2) = nanColor->blue;
*(dest+1) = nanColor->green;
*dest = nanColor->red;
}
break;
}
else {
if (mosaic) {
sptr = sptr->nextMosaic();
if (sptr) {
mm = sptr->matrixToData(sys).mm();
params = sptr->getDataParams(context->secMode());
srcw = sptr->width();
ll = sptr->low();
hh = sptr->high();
diff = hh - ll;
}
}
}
}
while (mosaic && sptr);
}
}
CLEARSIGBUS
if (img) {
if (context->mask.head()) {
FitsMask* mptr = context->mask.tail();
while (mptr) {
unsigned char* msk = fillMask(mptr, width, height, sys);
blend(img,msk,width,height);
delete [] msk;
mptr = mptr->previous();
}
}
}
return img;
}
unsigned char* Frame::fillMask(FitsMask* msk, int width, int height,
Coord::InternalSystem sys)
{
FitsImage* currentMsk = msk->current();
XColor* maskColor = msk->color();
int mark = msk->mark();
// img
unsigned char* img = new unsigned char[width*height*4];
memset(img,0,width*height*4);
if (!currentMsk)
return img;
// basics
FitsImage* sptr = currentMsk;
int mosaic = isMosaic();
// variable
double* mm = sptr->matrixToData(sys).mm();
FitsBound* params = sptr->getDataParams(context->secMode());
int srcw = sptr->width();
// main loop
unsigned char* dest = img;
SETSIGBUS
for (long jj=0; jj<height; jj++) {
for (long ii=0; ii<width; ii++, dest+=4) {
if (mosaic) {
sptr = currentMsk;
mm = sptr->matrixToData(sys).mm();
params = sptr->getDataParams(context->secMode());
srcw = sptr->width();
}
do {
double xx = ii*mm[0] + jj*mm[3] + mm[6];
double yy = ii*mm[1] + jj*mm[4] + mm[7];
if (xx>=params->xmin && xx<params->xmax &&
yy>=params->ymin && yy<params->ymax) {
int value = sptr->getValueMask(long(yy)*srcw + long(xx));
if ((mark && value) || (!mark && !value)) {
*dest = ((unsigned char)maskColor->red)*maskAlpha;
*(dest+1) = ((unsigned char)maskColor->green)*maskAlpha;
*(dest+2) = ((unsigned char)maskColor->blue)*maskAlpha;
*(dest+3) = 1;
}
break;
}
else {
if (mosaic) {
sptr = sptr->nextMosaic();
if (sptr) {
mm = sptr->matrixToData(sys).mm();
params = sptr->getDataParams(context->secMode());
srcw = sptr->width();
}
}
}
}
while (mosaic && sptr);
}
}
CLEARSIGBUS
return img;
}
int Frame::isIIS()
{
return context->cfits && context->cfits->isIIS();
}
void Frame::pushMatrices()
{
Base::pushMatrices();
// alway identity
Matrix rgbToRef;
// now any masks
FitsMask* msk = currentContext->mask.tail();
while (msk) {
FitsImage* mskimg = msk->mask();
while (mskimg) {
FitsImage* sptr = mskimg;
while (sptr) {
sptr->updateMatrices(rgbToRef, refToUser, userToWidget,
widgetToCanvas, canvasToWindow);
sptr = sptr->nextSlice();
}
mskimg = mskimg->nextMosaic();
}
msk = msk->previous();
}
}
void Frame::pushMagnifierMatrices()
{
Base::pushMagnifierMatrices();
FitsMask* msk = context->mask.tail();
while (msk) {
FitsImage* mskimg = msk->mask();
while (mskimg) {
FitsImage* sptr = mskimg;
while (sptr) {
sptr->updateMagnifierMatrices(refToMagnifier);
sptr = sptr->nextSlice();
}
mskimg = mskimg->nextMosaic();
}
msk = msk->previous();
}
}
void Frame::pushPannerMatrices()
{
Base::pushPannerMatrices();
FitsMask* msk = context->mask.tail();
while (msk) {
FitsImage* mskimg = msk->mask();
while (mskimg) {
FitsImage* sptr = mskimg;
while (sptr) {
sptr->updatePannerMatrices(refToPanner);
sptr = sptr->nextSlice();
}
mskimg = mskimg->nextMosaic();
}
msk = msk->previous();
}
}
void Frame::pushPSMatrices(float scale, int width, int height)
{
Base::pushPSMatrices(scale, width, height);
Matrix mx = psMatrix(scale, width, height);
FitsMask* msk = context->mask.tail();
while (msk) {
FitsImage* ptr = msk->current();
while (ptr) {
ptr->updatePS(mx);
ptr = ptr->nextMosaic();
}
msk = msk->previous();
}
}
void Frame::reset()
{
cmapID = 1;
bias = 0.5;
contrast = 1.0;
context->resetSecMode();
context->updateClip();
Base::reset();
}
void Frame::updateColorCells(unsigned char* cells, int cnt)
{
colorCount = cnt;
if (colorCells)
delete [] colorCells;
colorCells = new unsigned char[cnt*3];
if (!colorCells) {
internalError("Unable to Alloc colorCells");
return;
}
memcpy(colorCells, cells, cnt*3);
}
void Frame::unloadFits()
{
if (DebugPerf)
cerr << "Frame::unloadFits()" << endl;
// clean up from iis if needed
if (isIIS())
context->resetIIS();
context->unload();
FrameBase::unloadFits();
}
// Commands
void Frame::colormapCmd(int id, float b, float c, int i,
unsigned char* cells, int cnt)
{
cmapID = id;
bias = b;
contrast = c;
invert = i;
updateColorCells(cells, cnt);
updateColorScale();
update(BASE);
}
void Frame::colormapBeginCmd()
{
// we need a colorScale before we can render
if (!validColorScale())
return;
// we need some fits data
// we assume the colorScale length will not change during motion calls
if (!context->cfits)
return;
int width = options->width;
int height = options->height;
// Create XImage
if (!(colormapXM = XGetImage(display, pixmap, 0, 0,
width, height, AllPlanes, ZPixmap))) {
internalError("Unable to Create Colormap XImage");
return;
}
// Create Pixmap
colormapPM =
Tk_GetPixmap(display, Tk_WindowId(tkwin), width, height, depth);
if (!colormapPM) {
internalError("Unable to Create Colormap Pixmap");
return;
}
// colormapGCXOR
colormapGCXOR = XCreateGC(display, Tk_WindowId(tkwin), 0, NULL);
// Create table index array
if (colormapData)
delete [] colormapData;
colormapData = new long[width*height];
if (!colormapData) {
internalError("Unable to alloc tmp data array");
return;
}
// fill data array
// basics
int bytesPerPixel = colormapXM->bits_per_pixel/8;
int length = colorScale->size() - 1;
int last = length * bytesPerPixel;
FitsImage* sptr = context->cfits;
int mosaic = isMosaic();
long* dest = colormapData;
// variable
double* mm = sptr->matrixToData(Coord::WIDGET).mm();
FitsBound* params = sptr->getDataParams(context->secMode());
int srcw = sptr->width();
double ll = sptr->low();
double hh = sptr->high();
double diff = hh - ll;
// main loop
SETSIGBUS
for (long jj=0; jj<height; jj++) {
for (long ii=0; ii<width; ii++, dest++) {
// default is bg
*dest = -2;
if (mosaic) {
sptr = context->cfits;
mm = sptr->matrixToData(Coord::WIDGET).mm();
params = sptr->getDataParams(context->secMode());
srcw = sptr->width();
ll = sptr->low();
hh = sptr->high();
diff = hh - ll;
}
do {
double xx = ii*mm[0] + jj*mm[3] + mm[6];
double yy = ii*mm[1] + jj*mm[4] + mm[7];
if (xx>=params->xmin && xx<params->xmax &&
yy>=params->ymin && yy<params->ymax) {
double value = sptr->getValueDouble(long(yy)*srcw + long(xx));
if (isfinite(diff) && isfinite(value)) {
if (value <= ll)
*dest = 0;
else if (value >= hh)
*dest = last;
else
*dest = (int)(((value - ll)/diff * length) + .5)*bytesPerPixel;
}
else
*dest = -1;
break;
}
else {
if (mosaic) {
sptr = sptr->nextMosaic();
if (sptr) {
mm = sptr->matrixToData(Coord::WIDGET).mm();
params = sptr->getDataParams(context->secMode());
srcw = sptr->width();
ll = sptr->low();
hh = sptr->high();
diff = hh - ll;
}
}
}
}
while (mosaic && sptr);
}
}
CLEARSIGBUS
}
void Frame::colormapMotionCmd(int id, float b, float c, int i,
unsigned char* cells, int cnt)
{
// we need a colorScale before we can render
if (!validColorScale())
return;
// first check for change
if (cmapID == id && bias == b && contrast == c && invert == i && colorCells)
return;
// we got a change
cmapID = id;
bias = b;
contrast = c;
invert = i;
updateColorCells(cells, cnt);
updateColorScale();
// if we have no data, stop now
if (!context->cfits)
return;
// clear ximage
int& width = colormapXM->width;
int& height = colormapXM->height;
char* data = colormapXM->data;
int bytesPerPixel = colormapXM->bits_per_pixel/8;
int& bytesPerLine = colormapXM->bytes_per_line;
const unsigned char* table = colorScale->colors();
long* src = colormapData;
for (long jj=0; jj<height; jj++) {
// line may be padded at end
char* dest = data + jj*bytesPerLine;
for (long ii=0; ii<width; ii++, src++, dest+=bytesPerPixel)
switch (*src) {
case -1:
memcpy(dest, nanTrueColor_, bytesPerPixel);
break;
case -2:
memcpy(dest, bgTrueColor_, bytesPerPixel);
break;
default:
memcpy(dest, table+(*src), bytesPerPixel);
break;
}
}
// XImage to Pixmap
TkPutImage(NULL, 0, display, colormapPM, widgetGC, colormapXM,
0, 0, 0, 0, width, height);
// Display Pixmap
Vector dd = Vector() * widgetToWindow;
XCopyArea(display, colormapPM, Tk_WindowId(tkwin), colormapGCXOR, 0, 0,
width, height, dd[0], dd[1]);
// update panner
updatePanner();
}
void Frame::colormapEndCmd()
{
if (colormapXM) {
XDestroyImage(colormapXM);
colormapXM = NULL;
}
if (colormapPM) {
Tk_FreePixmap(display, colormapPM);
colormapPM = 0;
}
if (colormapGCXOR) {
XFreeGC(display, colormapGCXOR);
colormapGCXOR = 0;
}
if (colormapData) {
delete [] colormapData;
colormapData = NULL;
}
update(BASE); // always update
}
void Frame::getColorbarCmd()
{
ostringstream str;
str << cmapID << ' ' << bias << ' ' << contrast << ' ' << invert << ends;
Tcl_AppendResult(interp, str.str().c_str(), NULL);
}
void Frame::getRGBChannelCmd()
{
Tcl_AppendResult(interp, "red", NULL);
}
void Frame::getRGBViewCmd()
{
Tcl_AppendResult(interp, "1 1 1", NULL);
}
void Frame::getRGBSystemCmd()
{
Tcl_AppendResult(interp, "image", NULL);
}
void Frame::getTypeCmd()
{
Tcl_AppendResult(interp, "base", NULL);
}
void Frame::iisCmd(int width, int height)
{
unloadAllFits();
context->setIIS();
FitsImage* img = new FitsImageIIS(currentContext, interp, width, height);
loadDone(currentContext->load(ALLOC, "", img, IMG),IMG);
}
void Frame::iisEraseCmd()
{
if (context->cfits)
((FitsImageIIS*)context->cfits)->iisErase();
}
void Frame::iisGetCmd(char* dest, int xx, int yy, int dx, int dy)
{
if (context->cfits) {
char* buf = ((FitsImageIIS*)context->cfits)->iisGet(xx,yy,dx,dy);
memcpy(dest, buf, dx*dy);
delete [] buf;
}
}
void Frame::iisSetCmd(const char* src, int xx, int yy, int dx, int dy)
{
if (context->cfits)
((FitsImageIIS*)context->cfits)->iisSet(src, xx, yy, dx, dy);
}
void Frame::iisWCSCmd(const Matrix& mx, const Vector& z, int zt)
{
if (context->cfits)
((FitsImageIIS*)context->cfits)->iisWCS(mx, z, zt);
}
void Frame::savePhotoCmd(const char* ph)
{
FitsImage* fits = currentContext->cfits;
if (!fits)
return;
// basics
int length = colorScale->size() - 1;
const unsigned char* table = colorScale->psColors();
// variable
FitsBound* params = fits->getDataParams(context->secMode());
double ll = fits->low();
double hh = fits->high();
double diff = hh - ll;
int width = params->xmax - params->xmin;
int height = params->ymax - params->ymin;
// photo
if (*ph == '\0') {
Tcl_AppendResult(interp, "bad image name ", NULL);
return;
}
Tk_PhotoHandle photo = Tk_FindPhoto(interp, ph);
if (!photo) {
Tcl_AppendResult(interp, "bad image handle ", NULL);
return;
}
if (Tk_PhotoSetSize(interp, photo, width, height) != TCL_OK) {
Tcl_AppendResult(interp, "bad photo set size ", NULL);
return;
}
Tk_PhotoBlank(photo);
Tk_PhotoImageBlock block;
if (!Tk_PhotoGetImage(photo,&block)) {
Tcl_AppendResult(interp, "bad image block ", NULL);
return;
}
if (block.pixelSize<4) {
Tcl_AppendResult(interp, "bad pixel size ", NULL);
return;
}
// main loop
SETSIGBUS
unsigned char* dest = block.pixelPtr;
for (long jj=params->ymax-1; jj>=params->ymin; jj--) {
for (long ii=params->xmin; ii<params->xmax; ii++, dest += block.pixelSize) {
double value = fits->getValueDouble(Vector(ii,jj));
if (isfinite(diff) && isfinite(value)) {
if (value <= ll) {
*(dest+block.offset[0]) = table[2];
*(dest+block.offset[1]) = table[1];
*(dest+block.offset[2]) = table[0];
*(dest+block.offset[3]) = 255;
}
else if (value >= hh) {
*(dest+block.offset[0]) = table[length*3+2];
*(dest+block.offset[1]) = table[length*3+1];
*(dest+block.offset[2]) = table[length*3];
*(dest+block.offset[3]) = 255;
}
else {
int l = (int)(((value - ll)/diff * length) + .5);
*(dest+block.offset[0]) = table[l*3+2];
*(dest+block.offset[1]) = table[l*3+1];
*(dest+block.offset[2]) = table[l*3];
*(dest+block.offset[3]) = 255;
}
}
else {
*(dest+block.offset[0]) = nanColor->red;
*(dest+block.offset[1]) = nanColor->green;
*(dest+block.offset[2]) = nanColor->blue;
*(dest+block.offset[3]) = 255;
}
}
}
CLEARSIGBUS
if (Tk_PhotoPutBlock(interp, photo, &block, 0, 0, width, height,
TK_PHOTO_COMPOSITE_SET) != TCL_OK) {
Tcl_AppendResult(interp, "bad put block ", NULL);
return;
}
}
|