Exemple #1
0
    std::unique_ptr<ImageIO::Base> MRtrix::create (Header& H) const
    {
      File::OFStream out (H.name(), std::ios::out | std::ios::binary);

      out << "mrtrix image\n";

      write_mrtrix_header (H, out);

      bool single_file = Path::has_suffix (H.name(), ".mif");

      int64_t offset = 0;
      out << "file: ";
      if (single_file) {
        offset = out.tellp() + int64_t(18);
        offset += ((4 - (offset % 4)) % 4);
        out << ". " << offset << "\nEND\n";
      }
      else out << Path::basename (H.name().substr (0, H.name().size()-4) + ".dat") << "\n";

      out.close();

      std::unique_ptr<ImageIO::Base> io_handler (new ImageIO::Default (H));
      if (single_file) {
        File::resize (H.name(), offset + footprint(H));
        io_handler->files.push_back (File::Entry (H.name(), offset));
      }
      else {
        std::string data_file (H.name().substr (0, H.name().size()-4) + ".dat");
        File::create (data_file, footprint(H));
        io_handler->files.push_back (File::Entry (data_file));
      }

      return io_handler;
    }
      std::shared_ptr<Handler::Base> MRtrix_sparse::create (Header& H) const
      {

        Header::const_iterator name_it = H.find (Image::Sparse::name_key);
        if (name_it == H.end())
          throw Exception ("Cannot create sparse image " + H.name() + "; no knowledge of underlying data class type");

        Header::const_iterator size_it = H.find (Image::Sparse::size_key);
        if (size_it == H.end())
          throw Exception ("Cannot create sparse image " + H.name() + "; no knowledge of underlying data class size");

        H.datatype() = DataType::UInt64;
        H.datatype().set_byte_order_native();

        File::OFStream out (H.name(), std::ios::out | std::ios::binary);

        out << "mrtrix sparse image\n";

        write_mrtrix_header (H, out);

        bool single_file = Path::has_suffix (H.name(), ".msf");

        int64_t image_offset = 0, sparse_offset = 0;
        std::string image_path, sparse_path;
        if (single_file) {

          image_offset = out.tellp() + int64_t(54);
          image_offset += ((4 - (image_offset % 4)) % 4);
          sparse_offset = image_offset + Image::footprint(H);

          out << "file: . " << image_offset << "\nsparse_file: . " << sparse_offset << "\nEND\n";

          File::resize (H.name(), sparse_offset);
          image_path = H.name();
          sparse_path = H.name();

        } else {

          image_path  = Path::basename (H.name().substr (0, H.name().size()-4) + ".dat");
          sparse_path = Path::basename (H.name().substr (0, H.name().size()-4) + ".sdat");

          out << "file: " << image_path << "\nsparse_file: " << sparse_path << "\nEND\n";

          File::create (image_path, Image::footprint(H));
          File::create (sparse_path);

        }

        Handler::Default base_handler (H);
        base_handler.files.push_back (File::Entry (image_path, image_offset));

        std::shared_ptr<Handler::Base> handler (new Handler::Sparse (base_handler, name_it->second, to<size_t>(size_it->second), File::Entry (sparse_path, sparse_offset)));

        return handler;
      }