1 
2 //
3 // This source file is part of appleseed.
4 // Visit https://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-2018 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 #pragma once
31 
32 // appleseed.foundation headers.
33 #include "foundation/core/concepts/iunknown.h"
34 #include "foundation/image/canvasproperties.h"
35 #include "foundation/image/icanvas.h"
36 #include "foundation/image/pixel.h"
37 
38 // appleseed.main headers.
39 #include "main/dllsymbol.h"
40 
41 // Standard headers.
42 #include <cstddef>
43 
44 // Forward declarations.
45 namespace foundation    { class Tile; }
46 
47 namespace foundation
48 {
49 
50 //
51 // An image whose tiles are lazily constructed.
52 //
53 // Tiles are initially blank.
54 //
55 
56 class APPLESEED_DLLSYMBOL Image
57   : public ICanvas
58   , public IUnknown
59 {
60   public:
61     // Construct an empty image.
62     Image(
63         const size_t        image_width,        // image width, in pixels
64         const size_t        image_height,       // image height, in pixels
65         const size_t        tile_width,         // tile width, in pixels
66         const size_t        tile_height,        // tile height, in pixels
67         const size_t        channel_count,
68         const PixelFormat   pixel_format);
69 
70     // An alternative way to construct an empty image.
71     explicit Image(const CanvasProperties& props);
72 
73     // Copy constructor, duplicates both the layout and the data of the source image.
74     Image(const Image& rhs);
75 
76     // Copy image data but allow to tweak the layout.
77     Image(
78         const Image&        source,
79         const size_t        tile_width,         // tile width, in pixels
80         const size_t        tile_height,        // tile height, in pixels
81         const PixelFormat   pixel_format);
82 
83     // Construct an image by converting an existing image to a given pixel format,
84     // and allowing reordering, deletion of channels.
85     Image(
86         const Image&        source,             // source image
87         const PixelFormat   pixel_format,       // new pixel format
88         const size_t*       shuffle_table);     // channel shuffling table
89 
90     // Destructor.
91     ~Image() override;
92 
93     // Delete this instance.
94     void release() override;
95 
96     // Access canvas properties.
97     const CanvasProperties& properties() const override;
98 
99     // Direct access to a given tile.
100     // It is safe to access distinct tiles from multiple threads concurrently
101     // (however it is not safe to access the same tile from multiple threads).
102     Tile& tile(
103         const size_t        tile_x,
104         const size_t        tile_y) override;
105     const Tile& tile(
106         const size_t        tile_x,
107         const size_t        tile_y) const override;
108 
109     // Set a given tile. Ownership of the tile is transfered to the Image class.
110     // If a tile already exists at the given coordinates, it gets replaced.
111     void set_tile(
112         const size_t        tile_x,
113         const size_t        tile_y,
114         Tile*               tile);
115 
116     // Copy the contents of another image of identical geometry (but possibly with a different pixel format).
117     void copy_from(const Image& source);
118 
119   protected:
120     CanvasProperties        m_props;
121     Tile**                  m_tiles;
122 };
123 
124 
125 //
126 // Image class implementation.
127 //
128 
properties()129 inline const CanvasProperties& Image::properties() const
130 {
131     return m_props;
132 }
133 
134 }   // namespace foundation
135