Developer documentation
Version 3.0.3-105-gd3941f44
png.h
Go to the documentation of this file.
1/* Copyright (c) 2008-2022 the MRtrix3 contributors.
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 *
7 * Covered Software is provided under this License on an "as is"
8 * basis, without warranty of any kind, either expressed, implied, or
9 * statutory, including, without limitation, warranties that the
10 * Covered Software is free of defects, merchantable, fit for a
11 * particular purpose or non-infringing.
12 * See the Mozilla Public License v. 2.0 for more details.
13 *
14 * For more details, see http://www.mrtrix.org/.
15 */
16
17#ifndef __file_png_h__
18#define __file_png_h__
19
20#ifdef MRTRIX_PNG_SUPPORT
21
22#include <png.h>
23
24#include "header.h"
25#include "raw.h"
26#include "types.h"
28
29namespace MR
30{
31 namespace File
32 {
33 namespace PNG
34 {
35
36 class Reader
37 { MEMALIGN(Reader)
38 public:
39 Reader (const std::string& filename);
40 ~Reader();
41
42 uint32_t get_width() const { return width; }
43 uint32_t get_height() const { return height; }
44 int get_bitdepth() const { return bit_depth; }
45 int get_colortype() const { return color_type; }
46 int get_channels() const { return channels; }
47
48 size_t get_size() const { return png_get_rowbytes(png_ptr, info_ptr) * height; }
49 int get_output_bitdepth() const { return output_bitdepth; }
50 bool has_transparency() const { return png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS); }
51
52 void set_expand();
53
54 void load (uint8_t*);
55
56 private:
57 png_structp png_ptr;
58 png_infop info_ptr;
59 png_uint_32 width, height;
60 int bit_depth, color_type;
61 uint8_t channels;
62
63 int output_bitdepth;
64
65 };
66
67
68
69 class Writer
70 { MEMALIGN(Writer)
71 public:
72 Writer (const Header&, const std::string&);
73 ~Writer();
74
75 size_t get_size() const { return png_get_rowbytes(png_ptr, info_ptr) * height;}
76
77 void save (uint8_t*);
78
79 private:
80 png_structp png_ptr;
81 png_infop info_ptr;
82 uint32_t width, height;
83 int color_type, bit_depth;
84 std::string filename;
85 DataType data_type;
86 FILE* outfile;
87
88 static void error_handler (png_struct_def*, const char*);
89 static jmp_buf jmpbuf;
90
91 template <typename T>
92 void fill (uint8_t* in_ptr, uint8_t* out_ptr, const DataType data_type, const size_t num_elements);
93
94 };
95
96
97 template <typename T>
98 void Writer::fill (uint8_t* in_ptr, uint8_t* out_ptr, const DataType data_type, const size_t num_elements)
99 {
100 std::function<default_type(const void*,size_t,default_type,default_type)> fetch_func;
101 std::function<void(default_type,void*,size_t,default_type,default_type)> store_func;
102 __set_fetch_store_functions<default_type> (fetch_func, store_func, data_type);
103 default_type multiplier = 1.0;
104 switch (data_type() & DataType::Type) {
105 case DataType::Float32: multiplier = std::numeric_limits<uint8_t>::max(); break;
106 case DataType::Float64: multiplier = std::numeric_limits<uint16_t>::max(); break;
107 }
108 for (size_t i = 0; i != num_elements; ++i) {
109 Raw::store_BE<T> (std::min (default_type(std::numeric_limits<T>::max()), std::max (0.0, std::round(multiplier * fetch_func (in_ptr, 0, 0.0, 1.0)))), out_ptr);
110 in_ptr += data_type.bytes();
111 out_ptr += sizeof(T);
112 }
113 };
114
115
116
117 }
118 }
119}
120
121#endif
122#endif
123
124
125
static constexpr uint8_t Float32
Definition: datatype.h:147
static constexpr uint8_t Float64
Definition: datatype.h:148
static constexpr uint8_t Type
Definition: datatype.h:134
constexpr I round(const T x)
Definition: math.h:64
void load(Header &H, const std::string &path)
Definition: base.h:24
double default_type
the default type used throughout MRtrix
Definition: types.h:228
std::enable_if< is_adapter_type< typenamestd::remove_reference< ImageType >::type >::value, std::string >::type save(ImageType &&x, const std::string &filename, bool use_multi_threading=true)
save contents of an existing image to file (for debugging only)
Definition: image.h:523
#define MEMALIGN(...)
Definition: types.h:185