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

#include "outfits.h"
#include "util.h"

OutFitsStream::OutFitsStream()
{
  valid_ = 0;
}

OutFitsStream::~OutFitsStream()
{
}

int OutFitsStream::writeSwap(char* where, int size, int bitpix)
{
  char* buf = new char[B4KB];

  // size_t size is unsigned
  long long ss = size;
  size_t rr =0;
  int r;
  do {
    r = (ss>B4KB) ? B4KB : ss;

    switch (bitpix) {
    case 8:
      memcpy(buf, where+rr, r);
      break;
    case 16:
    case -16:
      for (int ii=0; ii<r; ii+=2)
	swap2(where+rr+ii,buf+ii);
      break;
    case 32:
    case -32:
      for (int ii=0; ii<r; ii+=4)
	swap4(where+rr+ii,buf+ii);
      break;
    case 64:
    case -64:
      for (int ii=0; ii<r; ii+=8)
	swap8(where+rr+ii,buf+ii);
      break;
    }

    write(buf,r);
    ss -= r;
    rr += r;
  } while (r>0 && rr<size);

  if (buf)
    delete [] buf;

  return rr;
}