1
2//
3// This source file is part of appleseed.
4// Visit http://appleseedhq.net/ for additional information and resources.
5//
6// This software is released under the MIT license.
7//
8// Copyright (c) 2010-2013 Francois Beaune, Jupiter Jazz Limited
9// Copyright (c) 2014-2017 Francois Beaune, The appleseedhq Organization
10//
11// Permission is hereby granted, free of charge, to any person obtaining a copy
12// of this software and associated documentation files (the "Software"), to deal
13// in the Software without restriction, including without limitation the rights
14// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15// copies of the Software, and to permit persons to whom the Software is
16// furnished to do so, subject to the following conditions:
17//
18// The above copyright notice and this permission notice shall be included in
19// all copies or substantial portions of the Software.
20//
21// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27// THE SOFTWARE.
28//
29
30// Interface header.
31#include "continuoussavingtilecallback.h"
32
33// appleseed.cli headers.
34#include "progresstilecallback.h"
35
36// appleseed.renderer headers.
37#include "renderer/api/frame.h"
38
39// appleseed.foundation headers.
40#include "foundation/image/image.h"
41#include "foundation/image/imageattributes.h"
42#include "foundation/platform/thread.h"
43
44// Boost headers.
45#include "boost/filesystem/operations.hpp"
46#include "boost/random/mersenne_twister.hpp"
47#include "boost/uuid/random_generator.hpp"
48#include "boost/uuid/uuid.hpp"
49#include "boost/uuid/uuid_io.hpp"
50
51// Standard headers.
52#include <cstddef>
53#include <ctime>
54
55using namespace foundation;
56using namespace renderer;
57using namespace boost;
58using namespace std;
59namespace bf = boost::filesystem;
60
61namespace appleseed {
62namespace cli {
63
64//
65// ContinuousSavingTileCallback.
66//
67
68namespace
69{
70 class ContinuousSavingTileCallback
71 : public ProgressTileCallback
72 {
73 public:
74 ContinuousSavingTileCallback(const string& output_path, Logger& logger)
75 : ProgressTileCallback(logger)
76 , m_output_path(output_path)
77 {
78 boost::mt19937 rng(static_cast<uint32_t>(time(0)));
79 const uuids::uuid u = uuids::basic_random_generator<boost::mt19937>(&rng)();
80 const bf::path ext = m_output_path.extension();
81 const string tmp_filename = uuids::to_string(u) + ext.string();
82
83 m_tmp_output_path = m_output_path.parent_path() / tmp_filename;
84 }
85
86 private:
87 boost::mutex m_mutex;
88 bf::path m_output_path;
89 bf::path m_tmp_output_path;
90
91 virtual void do_post_render_tile(
92 const Frame* frame,
93 const size_t tile_x,
94 const size_t tile_y) APPLESEED_OVERRIDE
95 {
96 boost::mutex::scoped_lock lock(m_mutex);
97 ProgressTileCallback::do_post_render_tile(frame, tile_x, tile_y);
98 frame->write_main_image(m_tmp_output_path.string().c_str());
99 bf::rename(m_tmp_output_path, m_output_path);
100 }
101 };
102}
103
104
105//
106// ContinuousSavingTileCallbackFactory class implementation.
107//
108
109ContinuousSavingTileCallbackFactory::ContinuousSavingTileCallbackFactory(
110 const string& output_path,
111 Logger& logger)
112 : m_callback(new ContinuousSavingTileCallback(output_path, logger))
113{
114}
115
116void ContinuousSavingTileCallbackFactory::release()
117{
118 delete this;
119}
120
121ITileCallback* ContinuousSavingTileCallbackFactory::create()
122{
123 return m_callback.get();
124}
125
126} // namespace cli
127} // namespace appleseed
128