File: tiff.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 (138 lines) | stat: -rw-r--r-- 3,756 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* 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/.
 */

#ifdef MRTRIX_TIFF_SUPPORT

#include "file/config.h"
#include "file/path.h"
#include "formats/list.h"
#include "header.h"
#include "file/tiff.h"
#include "image_io/tiff.h"


namespace MR
{
  namespace Formats
  {


    std::unique_ptr<ImageIO::Base> TIFF::read (Header& H) const
    {
      if (! (Path::has_suffix (H.name(), ".tiff") ||
            Path::has_suffix (H.name(), ".tif") ||
            Path::has_suffix (H.name(), ".TIFF") ||
            Path::has_suffix (H.name(), ".TIF")))
        return std::unique_ptr<ImageIO::Base>();

      File::TIFF tif (H.name());

      uint32_t width(0), height(0);
      uint16_t bpp(0), sampleformat(0), samplesperpixel(0), config (0);
      size_t ndir = 0;

      do {
        tif.read_and_check (TIFFTAG_IMAGEWIDTH, width);
        tif.read_and_check (TIFFTAG_IMAGELENGTH, height);
        tif.read_and_check (TIFFTAG_BITSPERSAMPLE, bpp);
        tif.read_and_check (TIFFTAG_SAMPLEFORMAT, sampleformat);
        tif.read_and_check (TIFFTAG_SAMPLESPERPIXEL, samplesperpixel);
        tif.read_and_check (TIFFTAG_PLANARCONFIG, config);

        ++ndir;
      } while (tif.read_directory() != 0);

      H.ndim() = samplesperpixel > 1 ? 4 : 3;

      H.size(0) = width;
      H.stride(0) = 2;

      H.size(1) = height;
      H.stride(1) = 3;

      H.size(2) = ndir;
      H.stride(2) = 4;

      if (samplesperpixel > 1) {
        H.size(3) = samplesperpixel;
        H.stride(3) = (config == PLANARCONFIG_CONTIG ? 1 : 5);
      }

      H.datatype() = DataType::Undefined;
      switch (bpp) {
        case 8:
          switch (sampleformat) {
            case 1: H.datatype() = DataType::UInt8; break;
            case 2: H.datatype() = DataType::Int8; break;
          }
          break;
        case 16:
          switch (sampleformat) {
            case 1: H.datatype() = DataType::UInt16; break;
            case 2: H.datatype() = DataType::Int16; break;
          }
          break;
        case 32:
          switch (sampleformat) {
            case 1: H.datatype() = DataType::UInt32; break;
            case 2: H.datatype() = DataType::Int32; break;
            case 3: H.datatype() = DataType::Float32; break;
          }
          break;
      }

      if (H.datatype() == DataType::Undefined)
        throw Exception ("unrecognised or unsupported data type in TIFF file \"" + H.name() + "\"");

      H.datatype().set_byte_order_native();

      std::unique_ptr<ImageIO::Base> io_handler (new ImageIO::TIFF (H));
      io_handler->files.push_back (File::Entry (H.name(), 0));

      return io_handler;
    }





    bool TIFF::check (Header& H, size_t num_axes) const
    {
      if (Path::has_suffix (H.name(), ".tiff") ||
          Path::has_suffix (H.name(), ".tif") ||
          Path::has_suffix (H.name(), ".TIFF") ||
          Path::has_suffix (H.name(), ".TIF"))
        throw Exception ("TIFF format not supported for output");

      return false;
    }







    std::unique_ptr<ImageIO::Base> TIFF::create (Header& H) const
    {
      return std::unique_ptr<ImageIO::Base>();
    }

  }
}

#endif