File: writer.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (152 lines) | stat: -rw-r--r-- 4,491 bytes parent folder | download | duplicates (2)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/************************************************************************
 *
 * Copyright (C) 2023 IRCAD France
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "writer.hpp"

#include "detail/writer_impl.hxx"

#include <algorithm>
#include <fstream>

// cspell:ignore nvjpeg nvjpeg2k nppi bitstream LRCP BGRI RGBI NOLINTNEXTLINE LIBJPEG OPENJPEG

namespace sight::io::bitmap
{

writer::writer() :
    m_pimpl(std::make_unique<detail::writer_impl>(this))
{
}

// Defining the destructor here, allows us to use PImpl with a unique_ptr
writer::~writer() = default;

//------------------------------------------------------------------------------

std::string writer::extension() const
{
    try
    {
        const auto& [backend, extensions] = detail::guess_backend_or_extension(
            backend::any,
            get_file().extension().string()
        );

        return extensions.front();
    }
    catch(...)
    {
        return extensions(backend::libtiff).front();
    }
}

//------------------------------------------------------------------------------

void writer::write()
{
    write(backend::any, mode::fast);
}

//------------------------------------------------------------------------------

std::size_t writer::write(backend _backend, mode _mode)
{
    auto file = get_file();

    try
    {
        if(file.has_parent_path() && !std::filesystem::exists(file.parent_path()))
        {
            std::filesystem::create_directories(file.parent_path());
        }

        // Compute the right backend to use
        const auto& [backend_to_use, extensions_to_use] = detail::guess_backend_or_extension(_backend, extension());

        // If there is no extension
        if(!file.has_extension())
        {
            // Add the good one
            const auto& ext = extensions_to_use.front();
            file.replace_extension(ext);

            SIGHT_WARN("No extension given, using '" << ext << "'");
        }
        else
        {
            // Check that the extension is valid
            const auto& current_extension = file.extension().string();

            SIGHT_THROW_IF(
                "Unsupported image extension: '" << file.extension().string() << "'",
                std::none_of(
                    extensions_to_use.cbegin(),
                    extensions_to_use.cend(),
                    [&](const auto& extension)
                {
                    return current_extension.ends_with(extension);
                })
            );
        }

        // Open the output file
        std::ofstream output;
        output.open(file.string(), std::ios::out | std::ios::binary | std::ios::trunc);

        return write(output, backend_to_use, _mode);
    }
    catch(...)
    {
        // Cleanup the created file in case of problem
        std::filesystem::remove_all(file);
        throw;
    }
}

//------------------------------------------------------------------------------

std::size_t writer::write(std::ostream& _ostream, backend _backend, mode _mode)
{
    return m_pimpl->write(_ostream, _backend, _mode);
}

//------------------------------------------------------------------------------

std::size_t writer::write(std::uint8_t** _buffer, backend _backend, mode _mode)
{
    return m_pimpl->write(_buffer, _backend, _mode);
}

//------------------------------------------------------------------------------

std::size_t writer::write(std::uint8_t* _buffer, backend _backend, mode _mode)
{
    return m_pimpl->write(_buffer, _backend, _mode);
}

//------------------------------------------------------------------------------

std::size_t writer::write(std::vector<uint8_t>& _buffer, backend _backend, mode _mode)
{
    return m_pimpl->write(_buffer, _backend, _mode);
}

} // namespace sight::io::bitmap