30 #define __attribute__(x) 37 #include "boost/serialization/shared_ptr.hpp" 38 #include "boost/serialization/binary_object.hpp" 39 #include "boost/serialization/nvp.hpp" 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> 46 #include "lsst/daf/base.h" 47 #include "lsst/daf/persistence.h" 48 #include "lsst/log/Log.h" 53 LOG_LOGGER _log = LOG_GET(
"afw.ImageFormatter");
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;
69 namespace formatters {
71 template <
typename ImagePixelT>
74 static std::string
name();
79 static std::string
name =
"ImageU";
84 static std::string
name =
"ImageI";
89 static std::string
name =
"ImageF";
94 static std::string
name =
"ImageD";
99 static std::string
name =
"ImageL";
103 template <
typename ImagePixelT>
107 template <
typename ImagePixelT>
109 : lsst::daf::persistence::Formatter(
typeid(
this)) {}
111 template <
typename ImagePixelT>
119 template <
typename ImagePixelT>
121 std::shared_ptr<FormatterStorage> storage,
122 std::shared_ptr<lsst::daf::base::PropertySet>) {
123 LOGL_DEBUG(_log,
"ImageFormatter write start");
126 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Persisting non-Image");
129 auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
131 LOGL_DEBUG(_log,
"ImageFormatter write BoostStorage");
132 boost->getOArchive() & *ip;
133 LOGL_DEBUG(_log,
"ImageFormatter write end");
136 auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
138 LOGL_DEBUG(_log,
"ImageFormatter write XmlStorage");
139 xml->getOArchive() & make_nvp(
"img", *ip);
140 LOGL_DEBUG(_log,
"ImageFormatter write end");
143 auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
145 LOGL_DEBUG(_log,
"ImageFormatter write FitsStorage");
151 LOGL_DEBUG(_log,
"ImageFormatter write end");
154 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Image");
157 template <
typename ImagePixelT>
159 std::shared_ptr<lsst::daf::base::PropertySet> additionalData) {
160 LOGL_DEBUG(_log,
"ImageFormatter read start");
162 auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
164 LOGL_DEBUG(_log,
"ImageFormatter read BoostStorage");
166 boost->getIArchive() & *ip;
167 LOGL_DEBUG(_log,
"ImageFormatter read end");
170 auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
172 LOGL_DEBUG(_log,
"ImageFormatter read XmlStorage");
174 xml->getIArchive() & make_nvp(
"img", *ip);
175 LOGL_DEBUG(_log,
"ImageFormatter read end");
178 auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
180 LOGL_DEBUG(_log,
"ImageFormatter read FitsStorage");
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");
190 if (additionalData->exists(
"imageOrigin")) {
191 std::string originStr = additionalData->get<std::string>(
"imageOrigin");
192 if (originStr ==
"LOCAL") {
194 }
else if (originStr ==
"PARENT") {
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") %
204 std::shared_ptr<lsst::daf::base::PropertySet> metadata;
208 std::shared_ptr<lsst::daf::base::PropertySet>(), box, origin);
213 LOGL_DEBUG(_log,
"ImageFormatter read end");
216 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Image");
219 template <
typename ImagePixelT>
221 std::shared_ptr<lsst::daf::base::PropertySet>) {
222 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unexpected call to update for Image");
225 template <
typename ImagePixelT>
226 template <
class Archive>
228 LOGL_DEBUG(_log,
"ImageFormatter delegateSerialize start");
231 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Serializing non-Image");
234 if (Archive::is_saving::value) {
238 ar& make_nvp(
"width", width) & make_nvp(
"height", height);
239 if (Archive::is_loading::value) {
242 ar& make_nvp(
"array", boost::serialization::make_array(array.getData(), array.getNumElements()));
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()));
251 template <
typename ImagePixelT>
253 std::shared_ptr<lsst::pex::policy::Policy> policy) {
257 #define InstantiateFormatter(ImagePixelT) \ 258 template class ImageFormatter<ImagePixelT>; \ 259 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, int const, \ 261 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, int const, \ 263 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_oarchive&, int const, \ 265 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_iarchive&, int const, \ 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*); 278 #undef InstantiateSerializer
int getHeight() const
Return the number of rows in the image.
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.
An integer coordinate rectangle.
A base class for image defects.
table::Key< std::string > name
int getWidth() const
Return the number of columns in the image.
A class to represent a 2-dimensional array of pixels.