lsst.afw  13.0-39-g3deb52d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
52 namespace {
53 LOG_LOGGER _log = LOG_GET("afw.ImageFormatter");
54 }
55 
56 using boost::serialization::make_nvp;
57 using lsst::daf::base::Persistable;
58 using lsst::daf::persistence::BoostStorage;
59 using lsst::daf::persistence::XmlStorage;
60 using lsst::daf::persistence::FitsStorage;
61 using lsst::daf::persistence::Storage;
63 
64 namespace afwImg = lsst::afw::image;
65 namespace afwGeom = lsst::afw::geom;
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>
104 lsst::daf::persistence::FormatterRegistration ImageFormatter<ImagePixelT>::registration(
106 
107 template <typename ImagePixelT>
108 ImageFormatter<ImagePixelT>::ImageFormatter(std::shared_ptr<lsst::pex::policy::Policy>)
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 }
118 
119 template <typename ImagePixelT>
120 void ImageFormatter<ImagePixelT>::write(Persistable const* persistable, std::shared_ptr<Storage> storage,
121  std::shared_ptr<lsst::daf::base::PropertySet>) {
122  LOGL_DEBUG(_log, "ImageFormatter write start");
123  Image<ImagePixelT> const* ip = dynamic_cast<Image<ImagePixelT> const*>(persistable);
124  if (ip == 0) {
125  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Image");
126  }
127  if (typeid(*storage) == typeid(BoostStorage)) {
128  LOGL_DEBUG(_log, "ImageFormatter write BoostStorage");
129  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
130  boost->getOArchive() & *ip;
131  LOGL_DEBUG(_log, "ImageFormatter write end");
132  return;
133  } else if (typeid(*storage) == typeid(XmlStorage)) {
134  LOGL_DEBUG(_log, "ImageFormatter write XmlStorage");
135  XmlStorage* boost = dynamic_cast<XmlStorage*>(storage.get());
136  boost->getOArchive() & make_nvp("img", *ip);
137  LOGL_DEBUG(_log, "ImageFormatter write end");
138  return;
139  } else if (typeid(*storage) == typeid(FitsStorage)) {
140  LOGL_DEBUG(_log, "ImageFormatter write FitsStorage");
141  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
142  typedef Image<ImagePixelT> Image;
143 
144  ip->writeFits(fits->getPath());
145  // @todo Do something with these fields?
146  // int _X0;
147  // int _Y0;
148  LOGL_DEBUG(_log, "ImageFormatter write end");
149  return;
150  }
151  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Image");
152 }
153 
154 template <typename ImagePixelT>
155 Persistable* ImageFormatter<ImagePixelT>::read(std::shared_ptr<Storage> storage,
156  std::shared_ptr<lsst::daf::base::PropertySet> additionalData) {
157  LOGL_DEBUG(_log, "ImageFormatter read start");
158  if (typeid(*storage) == typeid(BoostStorage)) {
159  LOGL_DEBUG(_log, "ImageFormatter read BoostStorage");
160  BoostStorage* boost = dynamic_cast<BoostStorage*>(storage.get());
162  boost->getIArchive() & *ip;
163  LOGL_DEBUG(_log, "ImageFormatter read end");
164  return ip;
165  } else if (typeid(*storage) == typeid(XmlStorage)) {
166  LOGL_DEBUG(_log, "ImageFormatter read XmlStorage");
167  XmlStorage* boost = dynamic_cast<XmlStorage*>(storage.get());
169  boost->getIArchive() & make_nvp("img", *ip);
170  LOGL_DEBUG(_log, "ImageFormatter read end");
171  return ip;
172  } else if (typeid(*storage) == typeid(FitsStorage)) {
173  LOGL_DEBUG(_log, "ImageFormatter read FitsStorage");
174  FitsStorage* fits = dynamic_cast<FitsStorage*>(storage.get());
175  geom::Box2I box;
176  if (additionalData->exists("llcX")) {
177  int llcX = additionalData->get<int>("llcX");
178  int llcY = additionalData->get<int>("llcY");
179  int width = additionalData->get<int>("width");
180  int height = additionalData->get<int>("height");
181  box = geom::Box2I(geom::Point2I(llcX, llcY), geom::Extent2I(width, height));
182  }
184  if (additionalData->exists("imageOrigin")) {
185  std::string originStr = additionalData->get<std::string>("imageOrigin");
186  if (originStr == "LOCAL") {
187  origin = afwImg::LOCAL;
188  } else if (originStr == "PARENT") {
189  origin = afwImg::PARENT;
190  } else {
191  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
192  (boost::format("Unknown ImageOrigin type %s specified in additional"
193  "data for retrieving Image from fits") %
194  originStr
195 
196  ).str());
197  }
198  }
199  std::shared_ptr<lsst::daf::base::PropertySet> metadata;
200 
201  Image<ImagePixelT>* ip =
202  new Image<ImagePixelT>(fits->getPath(), fits->getHdu(),
203  std::shared_ptr<lsst::daf::base::PropertySet>(), box, origin);
204  // @note We're throwing away the metadata
205  // @todo Do something with these fields?
206  // int _X0;
207  // int _Y0;
208  LOGL_DEBUG(_log, "ImageFormatter read end");
209  return ip;
210  }
211  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized Storage for Image");
212 }
213 
214 template <typename ImagePixelT>
215 void ImageFormatter<ImagePixelT>::update(Persistable*, std::shared_ptr<Storage>,
216  std::shared_ptr<lsst::daf::base::PropertySet>) {
217  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Image");
218 }
219 
220 template <typename ImagePixelT>
221 template <class Archive>
222 void ImageFormatter<ImagePixelT>::delegateSerialize(Archive& ar, int const, Persistable* persistable) {
223  LOGL_DEBUG(_log, "ImageFormatter delegateSerialize start");
224  Image<ImagePixelT>* ip = dynamic_cast<Image<ImagePixelT>*>(persistable);
225  if (ip == 0) {
226  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Image");
227  }
228  int width, height;
229  if (Archive::is_saving::value) {
230  width = ip->getWidth();
231  height = ip->getHeight();
232  }
233  ar& make_nvp("width", width) & make_nvp("height", height);
234  if (Archive::is_loading::value) {
235  std::unique_ptr<Image<ImagePixelT> > ni(new Image<ImagePixelT>(geom::Extent2I(width, height)));
236  typename Image<ImagePixelT>::Array array = ni->getArray();
237  ar& make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
238  ip->swap(*ni);
239  } else {
240  ndarray::Array<ImagePixelT, 2, 2> array = ndarray::dynamic_dimension_cast<2>(ip->getArray());
241  if (array.empty()) array = ndarray::copy(ip->getArray());
242  ar& make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
243  }
244 }
245 
246 template <typename ImagePixelT>
247 std::shared_ptr<lsst::daf::persistence::Formatter> ImageFormatter<ImagePixelT>::createInstance(
248  std::shared_ptr<lsst::pex::policy::Policy> policy) {
249  return std::shared_ptr<lsst::daf::persistence::Formatter>(new ImageFormatter<ImagePixelT>(policy));
250 }
251 
252 #define InstantiateFormatter(ImagePixelT) \
253  template class ImageFormatter<ImagePixelT>; \
254  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, int const, \
255  Persistable*); \
256  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, int const, \
257  Persistable*); \
258  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_oarchive&, int const, \
259  Persistable*); \
260  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_iarchive&, int const, \
261  Persistable*); \
262  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_oarchive&, \
263  int const, Persistable*); \
264  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_iarchive&, \
265  int const, Persistable*);
266 
267 InstantiateFormatter(std::uint16_t);
269 InstantiateFormatter(float);
270 InstantiateFormatter(double);
271 InstantiateFormatter(std::uint64_t);
272 
273 #undef InstantiateSerializer
274 }
275 }
276 } // namespace lsst::afw::formatters
#define __attribute__(x)
#define InstantiateFormatter(ImagePixelT)
static void delegateSerialize(Archive &ar, int const version, lsst::daf::base::Persistable *persistable)
An integer coordinate rectangle.
Definition: Box.h:55
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.
virtual void update(lsst::daf::base::Persistable *persistable, std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
table::Key< std::string > name
Definition: ApCorrMap.cc:71
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:309
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
Class implementing persistence and retrieval for Images.
virtual void write(lsst::daf::base::Persistable const *persistable, std::shared_ptr< lsst::daf::persistence::Storage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
static std::shared_ptr< lsst::daf::persistence::Formatter > createInstance(std::shared_ptr< lsst::pex::policy::Policy > policy)
afw::table::Key< afw::table::Array< ImagePixelT > > image
int getWidth() const
Return the number of columns in the image.
Definition: Image.h:307
void swap(Image &rhs)
Definition: Image.cc:424
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:486