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#ifndef APPLESEED_FOUNDATION_IMAGE_CANVASPROPERTIES_H
31#define APPLESEED_FOUNDATION_IMAGE_CANVASPROPERTIES_H
32
33// appleseed.foundation headers.
34#include "foundation/image/pixel.h"
35#include "foundation/math/scalar.h"
36
37// appleseed.main headers.
38#include "main/dllsymbol.h"
39
40// Standard headers.
41#include <algorithm>
42#include <cmath>
43#include <cstddef>
44
45namespace foundation
46{
47
48//
49// Canvas properties.
50//
51
52class APPLESEED_DLLSYMBOL CanvasProperties
53{
54 public:
55 // Properties.
56 size_t m_canvas_width; // canvas width, in pixels
57 size_t m_canvas_height; // canvas height, in pixels
58 size_t m_tile_width; // tile width, in pixels
59 size_t m_tile_height; // tile height, in pixels
60 size_t m_channel_count; // number of channels per pixel
61 PixelFormat m_pixel_format; // pixel format
62
63 // Precomputed values.
64 double m_rcp_canvas_width; // canvas width reciprocal
65 double m_rcp_canvas_height; // canvas height reciprocal
66 double m_rcp_tile_width; // tile width reciprocal
67 double m_rcp_tile_height; // tile height reciprocal
68 size_t m_tile_count_x; // number of tiles of a row in the canvas
69 size_t m_tile_count_y; // number of tiles of a column in the canvas
70 size_t m_tile_count; // total number of tiles in the canvas
71 size_t m_pixel_count; // total number of pixels in the canvas
72 size_t m_pixel_size; // size in bytes of one pixel
73
74 // Constructors.
75 CanvasProperties();
76 CanvasProperties(
77 const size_t canvas_width,
78 const size_t canvas_height,
79 const size_t tile_width,
80 const size_t tile_height,
81 const size_t channel_count,
82 const PixelFormat pixel_format);
83
84 // Compute the width and height in pixels of a given tile.
85 size_t get_tile_width(const size_t tile_x) const;
86 size_t get_tile_height(const size_t tile_y) const;
87};
88
89
90//
91// CanvasProperties class implementation.
92//
93
94
95inline CanvasProperties::CanvasProperties()
96{
97}
98
99inline CanvasProperties::CanvasProperties(
100 const size_t canvas_width,
101 const size_t canvas_height,
102 const size_t tile_width,
103 const size_t tile_height,
104 const size_t channel_count,
105 const PixelFormat pixel_format)
106 : m_canvas_width(canvas_width)
107 , m_canvas_height(canvas_height)
108 , m_tile_width(tile_width)
109 , m_tile_height(tile_height)
110 , m_channel_count(channel_count)
111 , m_pixel_format(pixel_format)
112{
113 m_rcp_canvas_width = 1.0 / m_canvas_width;
114 m_rcp_canvas_height = 1.0 / m_canvas_height;
115
116 m_rcp_tile_width = 1.0 / m_tile_width;
117 m_rcp_tile_height = 1.0 / m_tile_height;
118
119 const double nx = std::ceil(static_cast<double>(m_canvas_width) / m_tile_width);
120 const double ny = std::ceil(static_cast<double>(m_canvas_height) / m_tile_height);
121
122 m_tile_count_x = truncate<size_t>(nx);
123 m_tile_count_y = truncate<size_t>(ny);
124 m_tile_count = m_tile_count_x * m_tile_count_y;
125
126 m_pixel_count = m_canvas_width * m_canvas_height;
127 m_pixel_size = m_channel_count * Pixel::size(m_pixel_format);
128}
129
130inline size_t CanvasProperties::get_tile_width(const size_t tile_x) const
131{
132 return
133 std::min(
134 m_canvas_width - tile_x * m_tile_width,
135 m_tile_width);
136}
137
138inline size_t CanvasProperties::get_tile_height(const size_t tile_y) const
139{
140 return
141 std::min(
142 m_canvas_height - tile_y * m_tile_height,
143 m_tile_height);
144}
145
146} // namespace foundation
147
148#endif // !APPLESEED_FOUNDATION_IMAGE_CANVASPROPERTIES_H
149