File: default.cpp

package info (click to toggle)
mrtrix3 3.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,712 kB
  • sloc: cpp: 129,776; python: 9,494; sh: 593; makefile: 234; xml: 47
file content (118 lines) | stat: -rw-r--r-- 3,742 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* Copyright (c) 2008-2022 the MRtrix3 contributors.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * Covered Software is provided under this License on an "as is"
 * basis, without warranty of any kind, either expressed, implied, or
 * statutory, including, without limitation, warranties that the
 * Covered Software is free of defects, merchantable, fit for a
 * particular purpose or non-infringing.
 * See the Mozilla Public License v. 2.0 for more details.
 *
 * For more details, see http://www.mrtrix.org/.
 */

#include <limits>

#include "app.h"
#include "header.h"
#include "file/ofstream.h"
#include "image_io/default.h"

namespace MR
{
  namespace ImageIO
  {

    void Default::load (const Header& header, size_t)
    {
      if (files.empty())
        throw Exception ("no files specified in header for image \"" + header.name() + "\"");

      segsize /= files.size();

      if (header.datatype().bits() == 1) {
        bytes_per_segment = segsize/8;
        if (bytes_per_segment*8 < int64_t (segsize))
          ++bytes_per_segment;
      }
      else bytes_per_segment = header.datatype().bytes() * segsize;

      if (files.size() * double (bytes_per_segment) >= double (std::numeric_limits<size_t>::max()))
        throw Exception ("image \"" + header.name() + "\" is larger than maximum accessible memory");

      if (files.size() > MAX_FILES_PER_IMAGE)
        copy_to_mem (header);
      else
        map_files (header);
    }




    void Default::unload (const Header& header)
    {
      if (mmaps.empty() && addresses.size()) {
        assert (addresses[0].get());

        if (writable) {
          for (size_t n = 0; n < files.size(); n++) {
            File::OFStream out (files[n].name, std::ios::in | std::ios::out | std::ios::binary);
            out.seekp (files[n].start, out.beg);
            out.write ((char*) (addresses[0].get() + n*bytes_per_segment), bytes_per_segment);
            if (!out.good())
              throw Exception ("error writing back contents of file \"" + files[n].name + "\": " + strerror(errno));
          }
        }
      }
      else {
        for (size_t n = 0; n < addresses.size(); ++n)
          addresses[n].release();
        mmaps.clear();
      }
    }



    void Default::map_files (const Header& header)
    {
      mmaps.resize (files.size());
      addresses.resize (mmaps.size());
      for (size_t n = 0; n < files.size(); n++) {
        mmaps[n].reset (new File::MMap (files[n], writable, !is_new, bytes_per_segment));
        addresses[n].reset (mmaps[n]->address());
      }
    }





    void Default::copy_to_mem (const Header& header)
    {
      DEBUG ("loading image \"" + header.name() + "\"...");
      addresses.resize (files.size() > 1 && header.datatype().bits() *segsize != 8*size_t (bytes_per_segment) ? files.size() : 1);
      addresses[0].reset (new uint8_t [files.size() * bytes_per_segment]);
      if (!addresses[0])
        throw Exception ("failed to allocate memory for image \"" + header.name() + "\"");

      if (is_new) memset (addresses[0].get(), 0, files.size() * bytes_per_segment);
      else {
        for (size_t n = 0; n < files.size(); n++) {
          File::MMap file (files[n], false, false, bytes_per_segment);
          memcpy (addresses[0].get() + n*bytes_per_segment, file.address(), bytes_per_segment);
        }
      }

      if (addresses.size() > 1)
        for (size_t n = 1; n < addresses.size(); n++)
          addresses[n].reset (addresses[0].get() + n*bytes_per_segment);
      else segsize = std::numeric_limits<size_t>::max();
    }

  }
}