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

#include "iis.h"

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

  // alloc memory
  size_t size = (size_t)width*height;
  data_ = new char[size];
  if (!data_)
    return;

  dataSize_ = size;
  dataSkip_ = 0;

  // clear memory
  memset(data_, '\0', size);

  // made it this far, must be valid
  valid_ = 1;
}

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

void FitsIIS::erase()
{
  // clear memory
  FitsImageHDU* hdu = (FitsImageHDU*)head_->hdu();
  memset(data_, '\0', hdu->realbytes());
}

char* FitsIIS::get(int xx, int yy, int dx, int dy)
{
  // fill-in in reverse order
  int ll = dx*dy;
  char* dest = new char[ll];
  int ww = head_->naxis(0);
  int hh = head_->naxis(1);

  char* dptr = dest;
  char* sptr = (char*)data_ + ((hh-1)-yy)*ww + xx;
  while (ll) {
    memcpy(dptr, sptr, ww);
    sptr -= ww;
    dptr += ww;
    ll -= ww;
  }

  return dest;
}

void FitsIIS::set(const char* src, int xx, int yy, int dx, int dy)
{
  // fill-in in reverse order
  int ll = dx*dy;
  int ww = head_->naxis(0);
  int hh = head_->naxis(1);

  char* sptr = (char*)src;
  char* dptr = (char*)data_ + ((hh-1)-yy)*ww + xx;
  while (ll) {
    memcpy(dptr, sptr, ww);
    sptr += ww;
    dptr -= ww;
    ll -= ww;
  }
}