psddl_hdf2psana/src/camera.cpp

Go to the documentation of this file.
00001 //--------------------------------------------------------------------------
00002 // File and Version Information:
00003 //      $Id: camera.cpp 7484 2013-12-24 07:52:02Z salnikov@SLAC.STANFORD.EDU $
00004 //
00005 // Description:
00006 //      Hand-written supporting types for DDL-HDF5 mapping.
00007 //
00008 //------------------------------------------------------------------------
00009 
00010 //-----------------------
00011 // This Class's Header --
00012 //-----------------------
00013 #include "psddl_hdf2psana/camera.h"
00014 
00015 //-----------------
00016 // C/C++ Headers --
00017 //-----------------
00018 
00019 //-------------------------------
00020 // Collaborating Class Headers --
00021 //-------------------------------
00022 #include "hdf5pp/CompoundType.h"
00023 #include "hdf5pp/Utils.h"
00024 
00025 //-----------------------------------------------------------------------
00026 // Local Macros, Typedefs, Structures, Unions and Forward Declarations --
00027 //-----------------------------------------------------------------------
00028 
00029 //              ----------------------------------------
00030 //              -- Public Function Member Definitions --
00031 //              ----------------------------------------
00032 
00033 namespace psddl_hdf2psana {
00034 namespace Camera {
00035 
00036 hdf5pp::Type ns_FrameV1_v0_dataset_data_stored_type()
00037 {
00038   typedef ns_FrameV1_v0::dataset_data DsType;
00039   hdf5pp::CompoundType type = hdf5pp::CompoundType::compoundType<DsType>();
00040   type.insert("width", offsetof(DsType, width), hdf5pp::TypeTraits<uint32_t>::stored_type());
00041   type.insert("height", offsetof(DsType, height), hdf5pp::TypeTraits<uint32_t>::stored_type());
00042   type.insert("depth", offsetof(DsType, depth), hdf5pp::TypeTraits<uint32_t>::stored_type());
00043   type.insert("offset", offsetof(DsType, offset), hdf5pp::TypeTraits<uint32_t>::stored_type());
00044   return type;
00045 }
00046 
00047 hdf5pp::Type ns_FrameV1_v0::dataset_data::stored_type()
00048 {
00049   static hdf5pp::Type type = ns_FrameV1_v0_dataset_data_stored_type();
00050   return type;
00051 }
00052 
00053 hdf5pp::Type ns_FrameV1_v0_dataset_data_native_type()
00054 {
00055   typedef ns_FrameV1_v0::dataset_data DsType;
00056   hdf5pp::CompoundType type = hdf5pp::CompoundType::compoundType<DsType>();
00057   type.insert("width", offsetof(DsType, width), hdf5pp::TypeTraits<uint32_t>::native_type());
00058   type.insert("height", offsetof(DsType, height), hdf5pp::TypeTraits<uint32_t>::native_type());
00059   type.insert("depth", offsetof(DsType, depth), hdf5pp::TypeTraits<uint32_t>::native_type());
00060   type.insert("offset", offsetof(DsType, offset), hdf5pp::TypeTraits<uint32_t>::native_type());
00061   return type;
00062 }
00063 
00064 hdf5pp::Type ns_FrameV1_v0::dataset_data::native_type()
00065 {
00066   static hdf5pp::Type type = ns_FrameV1_v0_dataset_data_native_type();
00067   return type;
00068 }
00069 
00070 ns_FrameV1_v0::dataset_data::dataset_data()
00071 {
00072 }
00073 
00074 ns_FrameV1_v0::dataset_data::dataset_data(const Psana::Camera::FrameV1& psanaobj)
00075   : width(psanaobj.width())
00076   , height(psanaobj.height())
00077   , depth(psanaobj.depth())
00078   , offset(psanaobj.offset())
00079 {
00080 }
00081 
00082 ns_FrameV1_v0::dataset_data::~dataset_data()
00083 {
00084 }
00085 uint32_t FrameV1_v0::width() const {
00086   if (not m_ds_data.get()) read_ds_data();
00087   return uint32_t(m_ds_data->width);
00088 }
00089 uint32_t FrameV1_v0::height() const {
00090   if (not m_ds_data.get()) read_ds_data();
00091   return uint32_t(m_ds_data->height);
00092 }
00093 uint32_t FrameV1_v0::depth() const {
00094   if (not m_ds_data.get()) read_ds_data();
00095   return uint32_t(m_ds_data->depth);
00096 }
00097 uint32_t FrameV1_v0::offset() const {
00098   if (not m_ds_data.get()) read_ds_data();
00099   return uint32_t(m_ds_data->offset);
00100 }
00101 
00102 ndarray<const uint8_t, 1> FrameV1_v0::_int_pixel_data() const {
00103   if (m_ds_image.empty()) read_ds_image();
00104   return m_ds_image;
00105 }
00106 
00107 ndarray<const uint8_t, 2>
00108 FrameV1_v0::data8() const
00109 {
00110   if (this->depth() > 8) return ndarray<const uint8_t, 2>();
00111   if (m_ds_image.empty()) read_ds_image();
00112   return make_ndarray(m_ds_image.data_ptr(), height(), width());
00113 }
00114 
00115 ndarray<const uint16_t, 2>
00116 FrameV1_v0::data16() const
00117 {
00118   if (this->depth() <= 8) return ndarray<const uint16_t, 2>();
00119   if (m_ds_image.empty()) read_ds_image();
00120   boost::shared_ptr<const uint16_t> tptr(m_ds_image.data_ptr(), (const uint16_t*)m_ds_image.data_ptr().get());
00121   return make_ndarray(tptr, height(), width());
00122 }
00123 
00124 /** Number of bytes per pixel. */
00125 uint32_t 
00126 FrameV1_v0::depth_bytes() const
00127 {
00128   return (depth() + 7) / 8;
00129 }
00130 
00131 
00132 void FrameV1_v0::read_ds_data() const
00133 {
00134   m_ds_data = hdf5pp::Utils::readGroup<Camera::ns_FrameV1_v0::dataset_data>(m_group, "data", m_idx);
00135 }
00136 
00137 void FrameV1_v0::read_ds_image() const
00138 {
00139   // Image in HDF5 is stored as rank-2 array of either 8-bit or 16-bit data
00140 
00141   // open dataset and check the type
00142   hdf5pp::DataSet ds = m_group.openDataSet("image");
00143   if (ds.type().super().size() == 1) {
00144     // single-byte
00145     ndarray<const uint8_t, 2> img = hdf5pp::Utils::readNdarray<uint8_t, 2>(ds, m_idx);
00146     m_ds_image = make_ndarray(img.data_ptr(), img.size());
00147   } else {
00148     // otherwise 16-bit
00149     ndarray<const uint16_t, 2> img = hdf5pp::Utils::readNdarray<uint16_t, 2>(m_group, "image", m_idx);
00150     boost::shared_ptr<const uint8_t> tptr(img.data_ptr(), (const uint8_t*)img.data_ptr().get());
00151     m_ds_image = make_ndarray(tptr, img.size()*2);
00152   }
00153 }
00154 
00155 void make_datasets_FrameV1_v0(const Psana::Camera::FrameV1& obj,
00156       hdf5pp::Group group, const ChunkPolicy& chunkPolicy, int deflate, bool shuffle)
00157 {
00158   {
00159     hdf5pp::Type dstype = ns_FrameV1_v0::dataset_data::stored_type();
00160     hdf5pp::Utils::createDataset(group, "data", dstype, chunkPolicy.chunkSize(dstype), chunkPolicy.chunkCacheSize(dstype), deflate, shuffle);
00161   }
00162 
00163   {
00164     // image can be either 8-bit or 16-bit
00165     hsize_t dims[2];
00166     hdf5pp::Type basetype;
00167     if (obj.depth() > 8) {
00168       const ndarray<const uint16_t, 2>& psana_array = obj.data16();
00169       std::copy(psana_array.shape(), psana_array.shape()+2, dims);
00170       basetype = hdf5pp::TypeTraits<uint16_t>::stored_type();
00171     } else {
00172       const ndarray<const uint8_t, 2>& psana_array = obj.data8();
00173       std::copy(psana_array.shape(), psana_array.shape()+2, dims);
00174       basetype = hdf5pp::TypeTraits<uint8_t>::stored_type();
00175     }
00176     hdf5pp::Type dstype = hdf5pp::ArrayType::arrayType(basetype, 2, dims);
00177     hdf5pp::Utils::createDataset(group, "image", dstype, chunkPolicy.chunkSize(dstype), chunkPolicy.chunkCacheSize(dstype), deflate, shuffle);
00178   }
00179 }
00180 
00181 void store_FrameV1_v0(const Psana::Camera::FrameV1* obj, hdf5pp::Group group, long index, bool append)
00182 {
00183   if (not obj) {
00184     if (append) {
00185       hdf5pp::Utils::resizeDataset(group, "data", index < 0 ? index : index + 1);
00186       hdf5pp::Utils::resizeDataset(group, "image", index < 0 ? index : index + 1);
00187     }
00188     return;
00189   }
00190 
00191   // store metadata dataset
00192   if (append) {
00193     hdf5pp::Utils::storeAt(group, "data", ns_FrameV1_v0::dataset_data(*obj), index);
00194   } else {
00195     hdf5pp::Utils::storeScalar(group, "data", ns_FrameV1_v0::dataset_data(*obj));
00196   }
00197 
00198   // store image, it can be either 8-bit or 16-bit
00199   if (obj->depth() > 8) {
00200 
00201     ndarray<const uint16_t, 2> img = obj->data16();
00202     if (append) {
00203       hdf5pp::Utils::storeNDArrayAt(group, "image", img, index);
00204     } else {
00205       hdf5pp::Utils::storeNDArray(group, "image", img);
00206     }
00207 
00208   } else {
00209 
00210     ndarray<const uint8_t, 2> img = obj->data8();
00211     if (append) {
00212       hdf5pp::Utils::storeNDArrayAt(group, "image", img, index);
00213     } else {
00214       hdf5pp::Utils::storeNDArray(group, "image", img);
00215     }
00216 
00217   }
00218 
00219 }
00220 
00221 } // namespace Camera
00222 } // namespace psddl_hdf2psana

Generated on 19 Dec 2016 for PSDMSoftware by  doxygen 1.4.7