lsst.afw  15.0-33-g5c41f601f+4
ImageFormatter.cc
Go to the documentation of this file.
1 // -*- lsst-c++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 /*
26  * Implementation of ImageFormatter class
27  */
28 
29 #ifndef __GNUC__
30 #define __attribute__(x) /*NOTHING*/
31 #endif
32 static char const* SVNid __attribute__((unused)) = "$Id$";
33 
34 #include <cstdint>
35 #include <memory>
36 #include <string>
37 #include "boost/serialization/shared_ptr.hpp"
38 #include "boost/serialization/binary_object.hpp"
39 #include "boost/serialization/nvp.hpp"
40 
41 #include <boost/archive/text_oarchive.hpp>
42 #include <boost/archive/text_iarchive.hpp>
43 #include <boost/archive/binary_oarchive.hpp>
44 #include <boost/archive/binary_iarchive.hpp>
45 
46 #include "lsst/daf/base.h"
47 #include "lsst/daf/persistence.h"
48 #include "lsst/log/Log.h"
50 #include "lsst/afw/image/Image.h"
51 #include "lsst/afw/fits.h"
52 
53 namespace {
54 LOG_LOGGER _log = LOG_GET("afw.ImageFormatter");
55 }
56 
57 using boost::serialization::make_nvp;
64 
65 namespace afwImg = lsst::afw::image;
66 
67 namespace lsst {
68 namespace afw {
69 namespace formatters {
70 
71 template <typename ImagePixelT>
73 public:
74  static std::string name();
75 };
76 
77 template <>
79  static std::string name = "ImageU";
80  return name;
81 }
82 template <>
84  static std::string name = "ImageI";
85  return name;
86 }
87 template <>
89  static std::string name = "ImageF";
90  return name;
91 }
92 template <>
94  static std::string name = "ImageD";
95  return name;
96 }
97 template <>
99  static std::string name = "ImageL";
100  return name;
101 }
102 
103 template <typename ImagePixelT>
106 
107 template <typename ImagePixelT>
109  : lsst::daf::persistence::Formatter(typeid(this)) {}
110 
111 template <typename ImagePixelT>
113 
114 namespace {
115 namespace dafBase = lsst::daf::base;
116 namespace afwImage = lsst::afw::image;
117 } // namespace
118 
119 template <typename ImagePixelT>
123  LOGL_DEBUG(_log, "ImageFormatter write start");
124  Image<ImagePixelT> const* ip = dynamic_cast<Image<ImagePixelT> const*>(persistable);
125  if (ip == 0) {
126  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Image");
127  }
128  // TODO: Replace this with something better in DM-10776
130  if (boost) {
131  LOGL_DEBUG(_log, "ImageFormatter write BoostStorage");
132  boost->getOArchive() & *ip;
133  LOGL_DEBUG(_log, "ImageFormatter write end");
134  return;
135  }
136  auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
137  if (xml) {
138  LOGL_DEBUG(_log, "ImageFormatter write XmlStorage");
139  xml->getOArchive() & make_nvp("img", *ip);
140  LOGL_DEBUG(_log, "ImageFormatter write end");
141  return;
142  }
143  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
144  if (fits) {
145  LOGL_DEBUG(_log, "ImageFormatter write FitsStorage");
146 
148  if (additionalData) {
149  try {
150  options = fits::ImageWriteOptions(*additionalData->getAsPropertySetPtr("image"));
151  } catch (std::exception const& exc) {
152  LOGLS_WARN(_log, "Unable to construct image write options ("
153  << exc.what() << "); writing with default options");
154  }
155  }
156 
157  ip->writeFits(fits->getPath(), options);
158  // @todo Do something with these fields?
159  // int _X0;
160  // int _Y0;
161  LOGL_DEBUG(_log, "ImageFormatter write end");
162  return;
163  }
164  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Image");
165 }
166 
167 template <typename ImagePixelT>
170  LOGL_DEBUG(_log, "ImageFormatter read start");
171  // TODO: Replace this with something better in DM-10776
173  if (boost) {
174  LOGL_DEBUG(_log, "ImageFormatter read BoostStorage");
176  boost->getIArchive() & *ip;
177  LOGL_DEBUG(_log, "ImageFormatter read end");
178  return ip;
179  }
180  auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
181  if (xml) {
182  LOGL_DEBUG(_log, "ImageFormatter read XmlStorage");
184  xml->getIArchive() & make_nvp("img", *ip);
185  LOGL_DEBUG(_log, "ImageFormatter read end");
186  return ip;
187  }
188  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
189  if (fits) {
190  LOGL_DEBUG(_log, "ImageFormatter read FitsStorage");
191  lsst::geom::Box2I box;
192  if (additionalData->exists("llcX")) {
193  int llcX = additionalData->get<int>("llcX");
194  int llcY = additionalData->get<int>("llcY");
195  int width = additionalData->get<int>("width");
196  int height = additionalData->get<int>("height");
197  box = lsst::geom::Box2I(lsst::geom::Point2I(llcX, llcY), lsst::geom::Extent2I(width, height));
198  }
200  if (additionalData->exists("imageOrigin")) {
201  std::string originStr = additionalData->get<std::string>("imageOrigin");
202  if (originStr == "LOCAL") {
203  origin = afwImg::LOCAL;
204  } else if (originStr == "PARENT") {
205  origin = afwImg::PARENT;
206  } else {
208  (boost::format("Unknown ImageOrigin type %s specified in additional"
209  "data for retrieving Image from fits") %
210  originStr)
211  .str());
212  }
213  }
215 
216  Image<ImagePixelT>* ip =
217  new Image<ImagePixelT>(fits->getPath(), fits->getHdu(),
219  // @note We're throwing away the metadata
220  // @todo Do something with these fields?
221  // int _X0;
222  // int _Y0;
223  LOGL_DEBUG(_log, "ImageFormatter read end");
224  return ip;
225  }
226  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Image");
227 }
228 
229 template <typename ImagePixelT>
232  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Image");
233 }
234 
235 template <typename ImagePixelT>
236 template <class Archive>
238  LOGL_DEBUG(_log, "ImageFormatter delegateSerialize start");
239  Image<ImagePixelT>* ip = dynamic_cast<Image<ImagePixelT>*>(persistable);
240  if (ip == 0) {
241  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Image");
242  }
243  int width, height;
244  if (Archive::is_saving::value) {
245  width = ip->getWidth();
246  height = ip->getHeight();
247  }
248  ar& make_nvp("width", width) & make_nvp("height", height);
249  if (Archive::is_loading::value) {
251  typename Image<ImagePixelT>::Array array = ni->getArray();
252  ar& make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
253  ip->swap(*ni);
254  } else {
255  ndarray::Array<ImagePixelT, 2, 2> array = ndarray::dynamic_dimension_cast<2>(ip->getArray());
256  if (array.empty()) array = ndarray::copy(ip->getArray());
257  ar& make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
258  }
259 }
260 
261 template <typename ImagePixelT>
265 }
266 
267 #define InstantiateFormatter(ImagePixelT) \
268  template class ImageFormatter<ImagePixelT>; \
269  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, int const, \
270  Persistable*); \
271  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, int const, \
272  Persistable*); \
273  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_oarchive&, int const, \
274  Persistable*); \
275  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_iarchive&, int const, \
276  Persistable*); \
277  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_oarchive&, \
278  int const, Persistable*); \
279  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_iarchive&, \
280  int const, Persistable*);
281 
284 InstantiateFormatter(float);
285 InstantiateFormatter(double);
287 
288 #undef InstantiateSerializer
289 } // namespace formatters
290 } // namespace afw
291 } // namespace lsst
#define LOGLS_WARN(logger, message)
afw::table::Key< afw::table::Array< ImagePixelT > > image
virtual void write(lsst::daf::base::Persistable const *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
int getHeight() const
Return the number of rows in the image.
Definition: ImageBase.h:301
Options for writing an image to FITS.
Definition: fits.h:218
#define __attribute__(x)
void writeFits(std::string const &fileName, std::shared_ptr< lsst::daf::base::PropertySet const > metadata=std::shared_ptr< lsst::daf::base::PropertySet const >(), std::string const &mode="w") const
Write an image to a regular FITS file.
tuple options
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
#define LOGL_DEBUG(logger, message...)
Fits * fits
Definition: FitsWriter.cc:90
STL class.
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
A base class for image defects.
T what(T... args)
#define InstantiateFormatter(ImagePixelT)
T dynamic_pointer_cast(T... args)
Class implementing persistence and retrieval for Images.
STL class.
T get(T... args)
#define LSST_EXCEPT(type,...)
STL class.
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
int getWidth() const
Return the number of columns in the image.
Definition: ImageBase.h:299
#define LOG_GET(logger)
void swap(Image &rhs)
Definition: Image.cc:441
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:67
ImageFormatter(ImageFormatter const &)=default