/usr/include/CCfits/Image.h is in libccfits-dev 2.5+dfsg-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | // Astrophysics Science Division,
// NASA/ Goddard Space Flight Center
// HEASARC
// http://heasarc.gsfc.nasa.gov
// e-mail: ccfits@legacy.gsfc.nasa.gov
//
// Original author: Ben Dorman
#ifndef IMAGE_H
#define IMAGE_H 1
// functional
#include <functional>
// valarray
#include <valarray>
// vector
#include <vector>
// numeric
#include <numeric>
#include <sstream>
#ifdef _MSC_VER
#include "MSconfig.h" //form std::min
#endif
#include "CCfits.h"
#include "FitsError.h"
#include "FITSUtil.h"
namespace CCfits {
template <typename T>
class Image
{
public:
Image(const Image< T > &right);
Image (const std::valarray<T>& imageArray = std::valarray<T>());
~Image();
Image< T > & operator=(const Image< T > &right);
const std::valarray<T>& readImage (fitsfile* fPtr, long first, long nElements, T* nullValue, const std::vector<long>& naxes, bool& nulls);
const std::valarray<T>& readImage (fitsfile* fPtr, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, T* nullValue, const std::vector<long>& naxes, bool& nulls);
// If write operation causes an expansion of the image's outer-most dimension, newNaxisN will be set to the new value. Else it will be 0.
void writeImage (fitsfile* fPtr, long first, long nElements, const std::valarray<T>& inData, const std::vector<long>& naxes, long& newNaxisN, T* nullValue = 0);
void writeImage (fitsfile* fPtr, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, const std::valarray<T>& inData, const std::vector<long>& naxes, long& newNaxisN);
void writeImage (fitsfile* fPtr, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::valarray<T>& inData, const std::vector<long>& naxes, long& newNaxisN);
bool isRead () const;
// This allows higher level classes to notify Image that a user-input
// scaling value has changed. Image can then decide how this
// should affect reading from cache vs. disk.
void scalingHasChanged();
// Give the user (via higher level classes) a way to explicitly set the m_isRead flag
// to false, thus providing a fail-safe override of reading from the cache.
void resetRead();
const std::valarray< T >& image () const;
// Additional Public Declarations
protected:
// Additional Protected Declarations
private:
std::valarray<T>& image ();
void prepareForSubset (const std::vector<long>& naxes, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, const std::valarray<T>& inData, std::valarray<T>& subset);
void loop (size_t iDim, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, size_t iPos, const std::vector<size_t>& incr, const std::valarray<T>& inData, size_t& iDat, const std::vector<size_t>& subIncr, std::valarray<T>& subset, size_t iSub);
bool isNullValChanged(T* newNull) const;
void setLastNullInfo(T* newNull);
// Additional Private Declarations
private: //## implementation
// Data Members for Class Attributes
// When m_isRead = true, assume m_fullImageCache contains the full image from the file.
bool m_isRead;
// Information regarding the usage of null values for the
// most recent read operation.
bool m_usingNullVal;
T m_lastNullVal;
// Data Members for Associations
std::valarray< T > m_fullImageCache;
std::valarray<T> m_currentRead;
// Additional Implementation Declarations
};
// Parameterized Class CCfits::Image
template <typename T>
inline bool Image<T>::isRead () const
{
return m_isRead;
}
template <typename T>
inline const std::valarray< T >& Image<T>::image () const
{
return m_fullImageCache;
}
// Parameterized Class CCfits::Image
template <typename T>
Image<T>::Image(const Image<T> &right)
: m_isRead(right.m_isRead),
m_usingNullVal(right.m_usingNullVal),
m_lastNullVal(right.m_lastNullVal),
m_fullImageCache(right.m_fullImageCache),
m_currentRead(right.m_currentRead)
{
}
template <typename T>
Image<T>::Image (const std::valarray<T>& imageArray)
: m_isRead(false),
m_usingNullVal(false),
m_lastNullVal(0),
m_fullImageCache(imageArray),
m_currentRead()
{
}
template <typename T>
Image<T>::~Image()
{
}
template <typename T>
Image<T> & Image<T>::operator=(const Image<T> &right)
{
// all stack allocated.
m_isRead = right.m_isRead;
m_usingNullVal = right.m_usingNullVal,
m_lastNullVal = right.m_lastNullVal,
m_fullImageCache.resize(right.m_fullImageCache.size());
m_fullImageCache = right.m_fullImageCache;
m_currentRead.resize(right.m_currentRead.size());
m_currentRead = right.m_currentRead;
return *this;
}
template <typename T>
const std::valarray<T>& Image<T>::readImage (fitsfile* fPtr, long first, long nElements, T* nullValue, const std::vector<long>& naxes, bool& nulls)
{
if (!naxes.size())
{
m_currentRead.resize(0);
return m_currentRead;
}
unsigned long init(1);
unsigned long nTotalElements(std::accumulate(naxes.begin(),naxes.end(),init,
std::multiplies<long>()));
if (first <= 0)
{
string errMsg("*** CCfits Error: For image read, lowest allowed value for first element is 1.\n");
bool silent = false;
throw FitsException(errMsg, silent);
}
// 0-based index for slice
unsigned long start = (unsigned long)first - 1;
if (start >= nTotalElements)
{
string errMsg("*** CCfits Error: For image read, starting element is out of range.\n");
bool silent = false;
throw FitsException(errMsg, silent);
}
if (nElements < 0)
{
string errMsg("*** CCfits Error: Negative nElements value specified for image read.\n");
bool silent = false;
throw FitsException(errMsg, silent);
}
const unsigned long elementsRequested = (unsigned long)nElements;
int status(0);
int any (0);
FITSUtil::MatchType<T> imageType;
// truncate to valid array size if too much data asked for.
unsigned long elementsToRead(std::min(elementsRequested,
nTotalElements - start));
if ( elementsToRead < elementsRequested)
{
std::cerr <<
"***CCfits Warning: data request exceeds image size, truncating\n";
}
const bool isFullRead = (elementsToRead == nTotalElements);
const bool isDifferentNull = isNullValChanged(nullValue);
if (!m_isRead || isDifferentNull)
{
// Must perform a read from disk.
m_isRead = false;
if (isFullRead)
{
m_fullImageCache.resize(elementsToRead);
if (fits_read_img(fPtr,imageType(),first,elementsToRead,
nullValue,&m_fullImageCache[0],&any,&status) != 0) throw FitsError(status);
m_isRead = true;
// For this case only, we'll pass m_fullImageCache back up (to be
// copied into user-supplied array). This spares having to do
// what may be a very large copy into m_currentRead.
}
else
{
m_fullImageCache.resize(0);
m_currentRead.resize(elementsToRead);
if (fits_read_img(fPtr,imageType(),first,elementsToRead,
nullValue,&m_currentRead[0],&any,&status) != 0) throw FitsError(status);
}
nulls = (any != 0);
setLastNullInfo(nullValue);
}
else
{
if (!isFullRead)
{
m_currentRead.resize((size_t)elementsToRead);
// This may be a costly copy, though should still be faster
// than disk read.
m_currentRead = m_fullImageCache[std::slice((size_t)start, (size_t)elementsToRead,1)];
}
}
if (isFullRead)
return m_fullImageCache;
return m_currentRead;
}
template <typename T>
const std::valarray<T>& Image<T>::readImage (fitsfile* fPtr, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, T* nullValue, const std::vector<long>& naxes, bool& nulls)
{
const size_t N = naxes.size();
if (!N)
{
m_currentRead.resize(0);
return m_currentRead;
}
if (N != firstVertex.size() || N != lastVertex.size() || N != stride.size())
{
string errMsg("*** CCfits Error: Image read function requires that naxes, firstVertex,");
errMsg += " \nlastVertex, and stride vectors all be the same size.\n";
bool silent = false;
throw FitsException(errMsg, silent);
}
FITSUtil::CVarray<long> carray;
int any(0);
int status(0);
long requestedSize=1;
long nTotalSize=1;
for (size_t j = 0; j < N; ++j)
{
// Intentional truncation during division.
requestedSize *= ((lastVertex[j] - firstVertex[j])/stride[j] + 1);
nTotalSize *= naxes[j];
if (firstVertex[j] < 1 || lastVertex[j] > naxes[j])
{
string errMsg("*** CCfits Error: Out-of-bounds vertex value.\n");
bool silent=false;
throw FitsException(errMsg,silent);
}
if (firstVertex[j] > lastVertex[j])
{
string errMsg("*** CCfits Error: firstVertex values must not be larger than corresponding lastVertex values.\n");
bool silent = false;
throw FitsException(errMsg,silent);
}
}
const bool isFullRead = (requestedSize == nTotalSize);
const bool isDifferentNull = isNullValChanged(nullValue);
if (!m_isRead || isDifferentNull)
{
// Must perform a read from disk.
FITSUtil::auto_array_ptr<long> pFpixel(carray(firstVertex));
FITSUtil::auto_array_ptr<long> pLpixel(carray(lastVertex));
FITSUtil::auto_array_ptr<long> pStride(carray(stride));
FITSUtil::MatchType<T> imageType;
m_isRead = false;
if (isFullRead)
{
m_fullImageCache.resize(requestedSize);
if (fits_read_subset(fPtr,imageType(),
pFpixel.get(),pLpixel.get(),
pStride.get(),nullValue,&m_fullImageCache[0],&any,&status) != 0)
throw FitsError(status);
m_isRead = true;
}
else
{
m_currentRead.resize(requestedSize);
if (fits_read_subset(fPtr,imageType(),
pFpixel.get(),pLpixel.get(),
pStride.get(),nullValue,&m_currentRead[0],&any,&status) != 0)
throw FitsError(status);
}
nulls = (any != 0);
setLastNullInfo(nullValue);
}
else
{
if (!isFullRead)
{
// Must convert firstVertex,lastVertex,stride to gslice parameters.
// Note that in cfitsio, the NAXIS1 dimension varies the fastest
// when laid out in an array in memory (ie. Fortran style). Therefore NAXISn
// ordering must be reversed to C style before passing to gslice.
size_t startPos=0;
std::valarray<size_t> gsliceLength(size_t(0),N);
std::valarray<size_t> gsliceStride(size_t(0),N);
std::vector<long> naxesProducts(N);
long accum=1;
for (size_t i=0; i<N; ++i)
{
naxesProducts[i] = accum;
accum *= naxes[i];
}
for (size_t i=0; i<N; ++i)
{
startPos += static_cast<size_t>((firstVertex[i]-1)*naxesProducts[i]);
// Here's where we reverse the order:
const size_t gsPos = N-1-i;
// Division truncation is intentional.
gsliceLength[gsPos] = static_cast<size_t>((1 + (lastVertex[i]-firstVertex[i])/stride[i]));
gsliceStride[gsPos] = static_cast<size_t>(stride[i]*naxesProducts[i]);
}
m_currentRead.resize(requestedSize);
m_currentRead = m_fullImageCache[std::gslice(startPos, gsliceLength, gsliceStride)];
}
}
if (isFullRead)
return m_fullImageCache;
return m_currentRead;
}
template <typename T>
void Image<T>::writeImage (fitsfile* fPtr, long first, long nElements, const std::valarray<T>& inData, const std::vector<long>& naxes, long& newNaxisN, T* nullValue)
{
int status(0);
if (first < 1 || nElements < 1)
{
string errMsg("*** CCfits Error: first and nElements values must be > 0\n");
bool silent = false;
throw FitsException(errMsg, silent);
}
FITSUtil::CAarray<T> convert;
FITSUtil::auto_array_ptr<T> pArray(convert(inData));
T* array = pArray.get();
m_isRead = false;
newNaxisN = 0;
FITSUtil::MatchType<T> imageType;
long type(imageType());
if (fits_write_imgnull(fPtr,type,first,nElements,array,
nullValue,&status)!= 0)
{
throw FitsError(status);
}
const size_t nDim=naxes.size();
long origTotSize=1;
for (size_t i=0; i<nDim; ++i)
origTotSize *= naxes[i];
const long highestOutputElem = first + nElements - 1;
if (highestOutputElem > origTotSize)
{
// NAXIS(nDIM) may have increased.
std::ostringstream oss;
oss <<"NAXIS" << nDim;
string keyname(oss.str());
long newVal = 1 + (highestOutputElem-1)/(origTotSize/naxes[nDim-1]);
if (newVal != naxes[nDim-1])
{
if (fits_update_key(fPtr,TLONG,(char *)keyname.c_str(),&newVal,0,&status) != 0)
{
throw FitsError(status);
}
newNaxisN = newVal;
}
}
if (fits_flush_file(fPtr,&status) != 0)
throw FitsError(status);
}
template <typename T>
void Image<T>::writeImage (fitsfile* fPtr, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, const std::valarray<T>& inData, const std::vector<long>& naxes, long& newNaxisN)
{
// input vectors' size equality will be verified in prepareForSubset.
const size_t nDim = naxes.size();
FITSUtil::auto_array_ptr<long> pFPixel(new long[nDim]);
FITSUtil::auto_array_ptr<long> pLPixel(new long[nDim]);
std::valarray<T> subset;
m_isRead = false;
newNaxisN = 0;
prepareForSubset(naxes,firstVertex,lastVertex,stride,inData,subset);
long* fPixel = pFPixel.get();
long* lPixel = pLPixel.get();
for (size_t i=0; i<nDim; ++i)
{
fPixel[i] = firstVertex[i];
lPixel[i] = lastVertex[i];
}
FITSUtil::CAarray<T> convert;
FITSUtil::auto_array_ptr<T> pArray(convert(subset));
T* array = pArray.get();
FITSUtil::MatchType<T> imageType;
int status(0);
if ( fits_write_subset(fPtr,imageType(),fPixel,lPixel,array,&status) )
throw FitsError(status);
if (lPixel[nDim-1] > naxes[nDim-1])
{
std::ostringstream oss;
oss << "NAXIS" << nDim;
string keyname(oss.str());
long newVal = lPixel[nDim-1];
if (fits_update_key(fPtr,TLONG,(char *)keyname.c_str(),&newVal,0,&status) != 0)
{
throw FitsError(status);
}
newNaxisN = lPixel[nDim-1];
}
if (fits_flush_file(fPtr,&status) != 0)
throw FitsError(status);
}
template <typename T>
std::valarray<T>& Image<T>::image ()
{
return m_fullImageCache;
}
template <typename T>
void Image<T>::prepareForSubset (const std::vector<long>& naxes, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, const std::valarray<T>& inData, std::valarray<T>& subset)
{
// naxes, firstVertex, lastVertex, and stride must all be the same size.
const size_t N = naxes.size();
if (N != firstVertex.size() || N != lastVertex.size() || N != stride.size())
{
string errMsg("*** CCfits Error: Image write function requires that naxes, firstVertex,");
errMsg += " \nlastVertex, and stride vectors all be the same size.\n";
bool silent = false;
throw FitsException(errMsg, silent);
}
for (size_t i=0; i<N; ++i)
{
if (naxes[i] < 1)
{
bool silent = false;
throw FitsException("*** CCfits Error: Invalid naxes value sent to image write function.\n", silent);
}
string rangeErrMsg("*** CCfits Error: Out-of-range value sent to image write function in arg: ");
if (firstVertex[i] < 1 || (firstVertex[i] > naxes[i] && i != N-1))
{
bool silent = false;
rangeErrMsg += "firstVertex\n";
throw FitsException(rangeErrMsg, silent);
}
if (lastVertex[i] < firstVertex[i] || (lastVertex[i] > naxes[i] && i != N-1))
{
bool silent = false;
rangeErrMsg += "lastVertex\n";
throw FitsException(rangeErrMsg, silent);
}
if (stride[i] < 1)
{
bool silent = false;
rangeErrMsg += "stride\n";
throw FitsException(rangeErrMsg, silent);
}
}
// nPoints refers to the subset of the image INCLUDING the zero'ed elements
// resulting from the stride parameter.
// subSizeWithStride refers to the same subset, not counting the zeros.
size_t subSizeWithStride = 1;
size_t nPoints = 1;
std::vector<size_t> subIncr(N);
for (size_t i=0; i<N; ++i)
{
subIncr[i] = nPoints;
nPoints *= static_cast<size_t>(1+lastVertex[i]-firstVertex[i]);
subSizeWithStride *= static_cast<size_t>(1+(lastVertex[i]-firstVertex[i])/stride[i]);
}
subset.resize(nPoints, 0);
if (subSizeWithStride != inData.size())
{
bool silent = false;
string errMsg("*** CCfits Error: Data array size is not consistent with the values");
errMsg += "\n in range and stride vectors sent to the image write function.\n";
throw FitsException(errMsg, silent);
}
size_t startPoint = 0;
size_t dimMult = 1;
std::vector<size_t> incr(N);
for (size_t j = 0; j < N; ++j)
{
startPoint += dimMult*(firstVertex[j]-1);
incr[j] = dimMult;
dimMult *= static_cast<size_t>(naxes[j]);
}
size_t inDataPos = 0;
size_t iSub = 0;
loop(N-1, firstVertex, lastVertex, stride, startPoint, incr, inData, inDataPos, subIncr, subset, iSub);
}
template <typename T>
void Image<T>::loop (size_t iDim, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::vector<long>& stride, size_t iPos, const std::vector<size_t>& incr, const std::valarray<T>& inData, size_t& iDat, const std::vector<size_t>& subIncr, std::valarray<T>& subset, size_t iSub)
{
size_t start = static_cast<size_t>(firstVertex[iDim]);
size_t stop = static_cast<size_t>(lastVertex[iDim]);
size_t skip = static_cast<size_t>(stride[iDim]);
if (iDim == 0)
{
size_t length = stop - start + 1;
for (size_t i=0; i<length; i+=skip)
{
subset[i+iSub] = inData[iDat++];
}
}
else
{
size_t jump = incr[iDim]*skip;
size_t subJump = subIncr[iDim]*skip;
for (size_t i=start; i<=stop; i+=skip)
{
loop(iDim-1, firstVertex, lastVertex, stride, iPos, incr, inData, iDat, subIncr, subset, iSub);
iPos += jump;
iSub += subJump;
}
}
}
template <typename T>
bool Image<T>::isNullValChanged(T* newNull) const
{
bool isChanged = false;
if (m_usingNullVal)
{
// If m_usingNullVal is true, we can assume m_lastNullVal != 0.
if (newNull)
{
T newVal = *newNull;
if (newVal != m_lastNullVal)
isChanged = true;
}
else
isChanged = true;
}
else
{
if (newNull && (*newNull != 0))
isChanged = true;
}
return isChanged;
}
template <typename T>
void Image<T>::setLastNullInfo(T* newNull)
{
if (!newNull || *newNull == 0)
{
m_usingNullVal = false;
m_lastNullVal = 0;
}
else
{
m_usingNullVal = true;
m_lastNullVal = *newNull;
}
}
template <typename T>
void Image<T>::writeImage (fitsfile* fPtr, const std::vector<long>& firstVertex, const std::vector<long>& lastVertex, const std::valarray<T>& inData, const std::vector<long>& naxes, long& newNaxisN)
{
std::vector<long> stride(firstVertex.size(), 1);
writeImage(fPtr, firstVertex, lastVertex, stride, inData, naxes, newNaxisN);
}
template <typename T>
void Image<T>::scalingHasChanged()
{
m_isRead = false;
}
template <typename T>
void Image<T>::resetRead()
{
m_isRead = false;
}
// Additional Declarations
} // namespace CCfits
#endif
|