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;
65 namespace afwGeom = lsst::afw::geom;
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>
115 namespace dafBase = lsst::daf::base;
119 template <
typename ImagePixelT>
121 std::shared_ptr<lsst::daf::base::PropertySet>) {
122 LOGL_DEBUG(_log,
"ImageFormatter write start");
125 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Persisting non-Image");
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");
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");
139 }
else if (
typeid(*storage) ==
typeid(FitsStorage)) {
140 LOGL_DEBUG(_log,
"ImageFormatter write FitsStorage");
141 FitsStorage* fits =
dynamic_cast<FitsStorage*
>(storage.get());
148 LOGL_DEBUG(_log,
"ImageFormatter write end");
151 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Image");
154 template <
typename ImagePixelT>
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");
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");
172 }
else if (
typeid(*storage) ==
typeid(FitsStorage)) {
173 LOGL_DEBUG(_log,
"ImageFormatter read FitsStorage");
174 FitsStorage* fits =
dynamic_cast<FitsStorage*
>(storage.get());
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");
184 if (additionalData->exists(
"imageOrigin")) {
185 std::string originStr = additionalData->get<std::string>(
"imageOrigin");
186 if (originStr ==
"LOCAL") {
188 }
else if (originStr ==
"PARENT") {
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") %
199 std::shared_ptr<lsst::daf::base::PropertySet> metadata;
203 std::shared_ptr<lsst::daf::base::PropertySet>(), box, origin);
208 LOGL_DEBUG(_log,
"ImageFormatter read end");
211 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Image");
214 template <
typename ImagePixelT>
216 std::shared_ptr<lsst::daf::base::PropertySet>) {
217 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unexpected call to update for Image");
220 template <
typename ImagePixelT>
221 template <
class Archive>
223 LOGL_DEBUG(_log,
"ImageFormatter delegateSerialize start");
226 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Serializing non-Image");
229 if (Archive::is_saving::value) {
233 ar& make_nvp(
"width", width) & make_nvp(
"height", height);
234 if (Archive::is_loading::value) {
237 ar& make_nvp(
"array", boost::serialization::make_array(array.getData(), array.getNumElements()));
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()));
246 template <
typename ImagePixelT>
248 std::shared_ptr<lsst::pex::policy::Policy> policy) {
252 #define InstantiateFormatter(ImagePixelT) \
253 template class ImageFormatter<ImagePixelT>; \
254 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, int const, \
256 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, int const, \
258 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_oarchive&, int const, \
260 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_iarchive&, int const, \
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*);
273 #undef InstantiateSerializer
An integer coordinate rectangle.
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
int getHeight() const
Return the number of rows in the image.
int getWidth() const
Return the number of columns in the image.
A class to represent a 2-dimensional array of pixels.