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" 54 LOG_LOGGER _log = LOG_GET(
"afw.ImageFormatter");
57 using boost::serialization::make_nvp;
58 using lsst::daf::base::Persistable;
59 using lsst::daf::persistence::BoostStorage;
60 using lsst::daf::persistence::XmlStorage;
61 using lsst::daf::persistence::FitsStorage;
62 using lsst::daf::persistence::FormatterStorage;
70 namespace formatters {
72 template <
typename ImagePixelT>
75 static std::string
name();
80 static std::string
name =
"ImageU";
85 static std::string
name =
"ImageI";
90 static std::string
name =
"ImageF";
95 static std::string
name =
"ImageD";
100 static std::string
name =
"ImageL";
104 template <
typename ImagePixelT>
108 template <
typename ImagePixelT>
110 : lsst::daf::persistence::Formatter(
typeid(
this)) {}
112 template <
typename ImagePixelT>
120 template <
typename ImagePixelT>
122 std::shared_ptr<FormatterStorage> storage,
123 std::shared_ptr<lsst::daf::base::PropertySet> additionalData) {
124 LOGL_DEBUG(_log,
"ImageFormatter write start");
127 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Persisting non-Image");
130 auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
132 LOGL_DEBUG(_log,
"ImageFormatter write BoostStorage");
133 boost->getOArchive() & *ip;
134 LOGL_DEBUG(_log,
"ImageFormatter write end");
137 auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
139 LOGL_DEBUG(_log,
"ImageFormatter write XmlStorage");
140 xml->getOArchive() & make_nvp(
"img", *ip);
141 LOGL_DEBUG(_log,
"ImageFormatter write end");
144 auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
146 LOGL_DEBUG(_log,
"ImageFormatter write FitsStorage");
149 if (additionalData) {
152 }
catch (std::exception
const& exc) {
153 LOGLS_WARN(_log,
"Unable to construct image write options (" << exc.what() <<
154 "); writing with default options");
162 LOGL_DEBUG(_log,
"ImageFormatter write end");
165 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Image");
168 template <
typename ImagePixelT>
170 std::shared_ptr<lsst::daf::base::PropertySet> additionalData) {
171 LOGL_DEBUG(_log,
"ImageFormatter read start");
173 auto boost = std::dynamic_pointer_cast<BoostStorage>(storage);
175 LOGL_DEBUG(_log,
"ImageFormatter read BoostStorage");
177 boost->getIArchive() & *ip;
178 LOGL_DEBUG(_log,
"ImageFormatter read end");
181 auto xml = std::dynamic_pointer_cast<XmlStorage>(storage);
183 LOGL_DEBUG(_log,
"ImageFormatter read XmlStorage");
185 xml->getIArchive() & make_nvp(
"img", *ip);
186 LOGL_DEBUG(_log,
"ImageFormatter read end");
189 auto fits = std::dynamic_pointer_cast<FitsStorage>(storage);
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");
201 if (additionalData->exists(
"imageOrigin")) {
202 std::string originStr = additionalData->get<std::string>(
"imageOrigin");
203 if (originStr ==
"LOCAL") {
205 }
else if (originStr ==
"PARENT") {
208 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
209 (boost::format(
"Unknown ImageOrigin type %s specified in additional" 210 "data for retrieving Image from fits") %
215 std::shared_ptr<lsst::daf::base::PropertySet> metadata;
219 std::shared_ptr<lsst::daf::base::PropertySet>(), box, origin);
224 LOGL_DEBUG(_log,
"ImageFormatter read end");
227 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unrecognized FormatterStorage for Image");
230 template <
typename ImagePixelT>
232 std::shared_ptr<lsst::daf::base::PropertySet>) {
233 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Unexpected call to update for Image");
236 template <
typename ImagePixelT>
237 template <
class Archive>
239 LOGL_DEBUG(_log,
"ImageFormatter delegateSerialize start");
242 throw LSST_EXCEPT(lsst::pex::exceptions::RuntimeError,
"Serializing non-Image");
245 if (Archive::is_saving::value) {
249 ar& make_nvp(
"width", width) & make_nvp(
"height", height);
250 if (Archive::is_loading::value) {
253 ar& make_nvp(
"array", boost::serialization::make_array(array.getData(), array.getNumElements()));
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()));
262 template <
typename ImagePixelT>
264 std::shared_ptr<lsst::pex::policy::Policy> policy) {
268 #define InstantiateFormatter(ImagePixelT) \ 269 template class ImageFormatter<ImagePixelT>; \ 270 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_oarchive&, int const, \ 272 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::text_iarchive&, int const, \ 274 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_oarchive&, int const, \ 276 template void ImageFormatter<ImagePixelT>::delegateSerialize(boost::archive::xml_iarchive&, int const, \ 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*); 289 #undef InstantiateSerializer
int getHeight() const
Return the number of rows in the image.
Options for writing an image to FITS.
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.
int getWidth() const
Return the number of columns in the image.
A class to represent a 2-dimensional array of pixels.