summaryrefslogtreecommitdiffstats
path: root/tksao/fitsy++/photo.C
blob: 88592e4e7c129e5be98aa626e080debbd1d6a3ad (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
// Copyright (C) 1999-2016
// Smithsonian Astrophysical Observatory, Cambridge, MA, USA
// For conditions of distribution and use, see copyright notice in "copyright"

#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

#include <tk.h>

#include "photo.h"

FitsPhoto::FitsPhoto(Tcl_Interp* interp, const char* ph)
{
  // reset
  valid_ = 0;
  
  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;
  }
  Tk_PhotoImageBlock block;
  if (!Tk_PhotoGetImage(photo,&block)) {
    Tcl_AppendResult(interp, "bad image block ", NULL);
    return;
  }
  int width =0;
  int height =0;
  Tk_PhotoGetSize(photo, &width, &height);

  // new header
  head_ = new FitsHead(width, height, 1, 8);
  if (!head_->isValid())
    return;

  size_t size = (size_t)width*height;
  unsigned char* dest  = new unsigned char[size];
  data_ = dest;

  dataSize_ = size;
  dataSkip_ = 0;

  unsigned char* src = block.pixelPtr;
  for (int jj=height-1; jj>=0; jj--)
    for (int ii=0; ii<width; ii++) {
      int pp = (jj*width+ii)*block.pixelSize;
      unsigned char rr = src[pp+block.offset[0]];
      unsigned char gg = src[pp+block.offset[1]];
      unsigned char bb = src[pp+block.offset[2]];
      //unsigned char vv = (rr+gg+bb)/3.;
      //unsigned char vv = .2126*rr + .7152*gg + .0722*bb;
      // must round
      unsigned char vv = (.299*rr + .587*gg + .114*bb)+.5;
      *dest++ = vv;
    }

  // made it this far, must be valid
  byteswap_ = 0;
  endian_ = lsb() ? LITTLE : BIG;
  valid_ = 1;
}

FitsPhoto::~FitsPhoto()
{
  if (data_)
    delete [] (unsigned char*)data_;
}

FitsPhotoCube::FitsPhotoCube(Tcl_Interp* interp, const char* ph)
{
  // reset
  valid_ = 0;
  
  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;
  }
  Tk_PhotoImageBlock block;
  if (!Tk_PhotoGetImage(photo,&block)) {
    Tcl_AppendResult(interp, "bad image block ", NULL);
    return;
  }
  int width =0;
  int height =0;
  int depth =3;
  Tk_PhotoGetSize(photo, &width, &height);

  // new header
  head_ = new FitsHead(width, height, depth, 8);
  if (!head_->isValid())
    return;

  size_t size = (size_t)width*height*depth;
  unsigned char* dest  = new unsigned char[size];
  data_ = dest;

  dataSize_ = size;
  dataSkip_ = 0;

  unsigned char* src = block.pixelPtr;
  for (int kk=0; kk<depth; kk++) {
    for (int jj=height-1; jj>=0; jj--) {
      for (int ii=0; ii<width; ii++) {
	int pp = (jj*width+ii)*block.pixelSize;
	*dest++ = src[pp+block.offset[kk]];
      }
    }
  }

  // made it this far, must be valid
  byteswap_ = 0;
  endian_ = lsb() ? LITTLE : BIG;
  valid_ = 1;
}

FitsPhotoCube::~FitsPhotoCube()
{
  if (data_)
    delete [] (unsigned char*)data_;
}

FitsPhotoCubeNext::FitsPhotoCubeNext(FitsFile* prev)
{
  primary_ = prev->primary();
  managePrimary_ = 0;

  head_ = prev->head();
  manageHead_ = 0;
  FitsImageHDU* hdu = (FitsImageHDU*)head_->hdu();

  data_ = (char*)prev->data() + hdu->imgbytes();
  dataSize_ = 0;
  dataSkip_ = 0;

  ext_ = prev->ext();
  inherit_ = head_->inherit();
  byteswap_ = prev->byteswap();
  endian_ = prev->endian();
  valid_ = 1;

  return;
}