lsst.afw  14.0-58-g91bfccb15
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 namespace afwGeom = lsst::afw::geom;
67 
68 namespace lsst {
69 namespace afw {
70 namespace formatters {
71 
72 template <typename ImagePixelT>
74 public:
75  static std::string name();
76 };
77 
78 template <>
80  static std::string name = "ImageU";
81  return name;
82 }
83 template <>
85  static std::string name = "ImageI";
86  return name;
87 }
88 template <>
90  static std::string name = "ImageF";
91  return name;
92 }
93 template <>
95  static std::string name = "ImageD";
96  return name;
97 }
98 template <>
100  static std::string name = "ImageL";
101  return name;
102 }
103 
104 template <typename ImagePixelT>
107 
108 template <typename ImagePixelT>
110  : lsst::daf::persistence::Formatter(typeid(this)) {}
111 
112 template <typename ImagePixelT>
114 
115 namespace {
116 namespace dafBase = lsst::daf::base;
117 namespace afwImage = lsst::afw::image;
118 } // namespace
119 
120 template <typename ImagePixelT>
124  LOGL_DEBUG(_log, "ImageFormatter write start");
125  Image<ImagePixelT> const* ip = dynamic_cast<Image<ImagePixelT> const*>(persistable);
126  if (ip == 0) {
127  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Persisting non-Image");
128  }
129  // TODO: Replace this with something better in DM-10776
131  if (boost) {
132  LOGL_DEBUG(_log, "ImageFormatter write BoostStorage");
133  boost->getOArchive() & *ip;
134  LOGL_DEBUG(_log, "ImageFormatter write end");
135  return;
136  }
137  auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
138  if (xml) {
139  LOGL_DEBUG(_log, "ImageFormatter write XmlStorage");
140  xml->getOArchive() & make_nvp("img", *ip);
141  LOGL_DEBUG(_log, "ImageFormatter write end");
142  return;
143  }
144  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
145  if (fits) {
146  LOGL_DEBUG(_log, "ImageFormatter write FitsStorage");
147 
149  if (additionalData) {
150  try {
151  options = fits::ImageWriteOptions(*additionalData->getAsPropertySetPtr("image"));
152  } catch (std::exception const& exc) {
153  LOGLS_WARN(_log, "Unable to construct image write options (" << exc.what() <<
154  "); writing with default options");
155  }
156  }
157 
158  ip->writeFits(fits->getPath(), options);
159  // @todo Do something with these fields?
160  // int _X0;
161  // int _Y0;
162  LOGL_DEBUG(_log, "ImageFormatter write end");
163  return;
164  }
165  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Image");
166 }
167 
168 template <typename ImagePixelT>
171  LOGL_DEBUG(_log, "ImageFormatter read start");
172  // TODO: Replace this with something better in DM-10776
174  if (boost) {
175  LOGL_DEBUG(_log, "ImageFormatter read BoostStorage");
177  boost->getIArchive() & *ip;
178  LOGL_DEBUG(_log, "ImageFormatter read end");
179  return ip;
180  }
181  auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
182  if (xml) {
183  LOGL_DEBUG(_log, "ImageFormatter read XmlStorage");
185  xml->getIArchive() & make_nvp("img", *ip);
186  LOGL_DEBUG(_log, "ImageFormatter read end");
187  return ip;
188  }
189  auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
190  if (fits) {
191  LOGL_DEBUG(_log, "ImageFormatter read FitsStorage");
193  if (additionalData->exists("llcX")) {
194  int llcX = additionalData->get<int>("llcX");
195  int llcY = additionalData->get<int>("llcY");
196  int width = additionalData->get<int>("width");
197  int height = additionalData->get<int>("height");
198  box = geom::Box2I(geom::Point2I(llcX, llcY), geom::Extent2I(width, height));
199  }
201  if (additionalData->exists("imageOrigin")) {
202  std::string originStr = additionalData->get<std::string>("imageOrigin");
203  if (originStr == "LOCAL") {
204  origin = afwImg::LOCAL;
205  } else if (originStr == "PARENT") {
206  origin = afwImg::PARENT;
207  } else {
209  (boost::format("Unknown ImageOrigin type %s specified in additional"
210  "data for retrieving Image from fits") %
211  originStr)
212  .str());
213  }
214  }
216 
217  Image<ImagePixelT>* ip =
218  new Image<ImagePixelT>(fits->getPath(), fits->getHdu(),
220  // @note We're throwing away the metadata
221  // @todo Do something with these fields?
222  // int _X0;
223  // int _Y0;
224  LOGL_DEBUG(_log, "ImageFormatter read end");
225  return ip;
226  }
227  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unrecognized FormatterStorage for Image");
228 }
229 
230 template <typename ImagePixelT>
233  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Unexpected call to update for Image");
234 }
235 
236 template <typename ImagePixelT>
237 template <class Archive>
239  LOGL_DEBUG(_log, "ImageFormatter delegateSerialize start");
240  Image<ImagePixelT>* ip = dynamic_cast<Image<ImagePixelT>*>(persistable);
241  if (ip == 0) {
242  throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError, "Serializing non-Image");
243  }
244  int width, height;
245  if (Archive::is_saving::value) {
246  width = ip->getWidth();
247  height = ip->getHeight();
248  }
249  ar& make_nvp("width", width) & make_nvp("height", height);
250  if (Archive::is_loading::value) {
252  typename Image<ImagePixelT>::Array array = ni->getArray();
253  ar& make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
254  ip->swap(*ni);
255  } else {
256  ndarray::Array<ImagePixelT, 2, 2> array = ndarray::dynamic_dimension_cast<2>(ip->getArray());
257  if (array.empty()) array = ndarray::copy(ip->getArray());
258  ar& make_nvp("array", boost::serialization::make_array(array.getData(), array.getNumElements()));
259  }
260 }
261 
262 template <typename ImagePixelT>
266 }
267 
268 #define InstantiateFormatter(ImagePixelT) \
269  template class ImageFormatter<ImagePixelT>; \
270  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, int const, \
271  Persistable*); \
272  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, int const, \
273  Persistable*); \
274  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_oarchive&, int const, \
275  Persistable*); \
276  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_iarchive&, int const, \
277  Persistable*); \
278  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_oarchive&, \
279  int const, Persistable*); \
280  template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::binary_iarchive&, \
281  int const, Persistable*);
282 
285 InstantiateFormatter(float);
286 InstantiateFormatter(double);
288 
289 #undef InstantiateSerializer
290 } // namespace formatters
291 } // namespace afw
292 } // 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:220
#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...)
An integer coordinate rectangle.
Definition: Box.h:55
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:451
A class to represent a 2-dimensional array of pixels.
Definition: Image.h:69
ImageFormatter(ImageFormatter const &)=default