lsst.afw  13.0-92-g8ecf155+1
 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::FormatterStorage;
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 } // namespace
118 
119 template <typename ImagePixelT>
120 void ImageFormatter<ImagePixelT>::write(Persistable const* persistable,
121  std::shared_ptr<FormatterStorage> storage,
122  std::shared_ptr<lsst::daf::base::PropertySet>) {
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
129  auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
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 
147  ip->writeFits(fits->getPath());
148  // @todo Do something with these fields?
149  // int _X0;
150  // int _Y0;
151  LOGL_DEBUG(_log, "ImageFormatter write end");
152  return;
153  }
154  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Image");
155 }
156 
157 template <typename ImagePixelT>
158 Persistable* ImageFormatter<ImagePixelT>::read(std::shared_ptr<FormatterStorage> storage,
159  std::shared_ptr<lsst::daf::base::PropertySet> additionalData) {
160  LOGL_DEBUG(_log, "ImageFormatter read start");
161  // TODO: Replace this with something better in DM-10776
162  auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
163  if (boost) {
164  LOGL_DEBUG(_log, "ImageFormatter read BoostStorage");
166  boost->getIArchive() & *ip;
167  LOGL_DEBUG(_log, "ImageFormatter read end");
168  return ip;
169  }
170  auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
171  if (xml) {
172  LOGL_DEBUG(_log, "ImageFormatter read XmlStorage");
174  xml->getIArchive() & make_nvp("img", *ip);
175  LOGL_DEBUG(_log, "ImageFormatter read end");
176  return ip;
177  }
178  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
179  if (fits) {
180  LOGL_DEBUG(_log, "ImageFormatter read FitsStorage");
181  geom::Box2I box;
182  if (additionalData->exists("llcX")) {
183  int llcX = additionalData->get<int>("llcX");
184  int llcY = additionalData->get<int>("llcY");
185  int width = additionalData->get<int>("width");
186  int height = additionalData->get<int>("height");
187  box = geom::Box2I(geom::Point2I(llcX, llcY), geom::Extent2I(width, height));
188  }
190  if (additionalData->exists("imageOrigin")) {
191  std::string originStr = additionalData->get<std::string>("imageOrigin");
192  if (originStr == "LOCAL") {
193  origin = afwImg::LOCAL;
194  } else if (originStr == "PARENT") {
195  origin = afwImg::PARENT;
196  } else {
197  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
198  (boost::format("Unknown ImageOrigin type %s specified in additional"
199  "data for retrieving Image from fits") %
200  originStr)
201  .str());
202  }
203  }
204  std::shared_ptr<lsst::daf::base::PropertySet> metadata;
205 
206  Image<ImagePixelT>* ip =
207  new Image<ImagePixelT>(fits->getPath(), fits->getHdu(),
208  std::shared_ptr<lsst::daf::base::PropertySet>(), box, origin);
209  // @note We're throwing away the metadata
210  // @todo Do something with these fields?
211  // int _X0;
212  // int _Y0;
213  LOGL_DEBUG(_log, "ImageFormatter read end");
214  return ip;
215  }
216  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Image");
217 }
218 
219 template <typename ImagePixelT>
220 void ImageFormatter<ImagePixelT>::update(Persistable*, std::shared_ptr<FormatterStorage>,
221  std::shared_ptr<lsst::daf::base::PropertySet>) {
222  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Image");
223 }
224 
225 template <typename ImagePixelT>
226 template <class Archive>
227 void ImageFormatter<ImagePixelT>::delegateSerialize(Archive& ar, int const, Persistable* persistable) {
228  LOGL_DEBUG(_log, "ImageFormatter delegateSerialize start");
229  Image<ImagePixelT>* ip = dynamic_cast<Image<ImagePixelT>*>(persistable);
230  if (ip == 0) {
231  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Image");
232  }
233  int width, height;
234  if (Archive::is_saving::value) {
235  width = ip->getWidth();
236  height = ip->getHeight();
237  }
238  ar& make_nvp("width", width) & make_nvp("height", height);
239  if (Archive::is_loading::value) {
240  std::unique_ptr<Image<ImagePixelT> > ni(new Image<ImagePixelT>(geom::Extent2I(width, height)));
241  typename Image<ImagePixelT>::Array array = ni->getArray();
242  ar& make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
243  ip->swap(*ni);
244  } else {
245  ndarray::Array<ImagePixelT, 2, 2> array = ndarray::dynamic_dimension_cast<2>(ip->getArray());
246  if (array.empty()) array = ndarray::copy(ip->getArray());
247  ar& make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
248  }
249 }
250 
251 template <typename ImagePixelT>
252 std::shared_ptr<lsst::daf::persistence::Formatter> ImageFormatter<ImagePixelT>::createInstance(
253  std::shared_ptr<lsst::pex::policy::Policy> policy) {
254  return std::shared_ptr<lsst::daf::persistence::Formatter>(new ImageFormatter<ImagePixelT>(policy));
255 }
256 
257 #define InstantiateFormatter(ImagePixelT) \
258  template class ImageFormatter<ImagePixelT>; \
259  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, int const, \
260  Persistable*); \
261  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, int const, \
262  Persistable*); \
263  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_oarchive&, int const, \
264  Persistable*); \
265  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_iarchive&, int const, \
266  Persistable*); \
267  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_oarchive&, \
268  int const, Persistable*); \
269  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_iarchive&, \
270  int const, Persistable*);
271 
272 InstantiateFormatter(std::uint16_t);
274 InstantiateFormatter(float);
275 InstantiateFormatter(double);
276 InstantiateFormatter(std::uint64_t);
277 
278 #undef InstantiateSerializer
279 } // namespace formatters
280 } // namespace afw
281 } // namespace lsst
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)
#define __attribute__(x)
#define InstantiateFormatter(ImagePixelT)
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)
An integer coordinate rectangle.
Definition: Box.h:55
virtual lsst::daf::base::Persistable * read(std::shared_ptr< lsst::daf::persistence::FormatterStorage > storage, std::shared_ptr< lsst::daf::base::PropertySet > additionalData)
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.
table::Key< std::string > name
Definition: ApCorrMap.cc:71
int getHeight() const
Return the number of rows in the image.
Definition: Image.h:309
Class implementing persistence and retrieval for Images.
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