1 /*
2 ---------------------------------------------------------------------------
3 Open Asset Import Library (assimp)
4 ---------------------------------------------------------------------------
5 
6 Copyright (c) 2006-2021, assimp team
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the following
12 conditions are met:
13 
14 * Redistributions of source code must retain the above
15   copyright notice, this list of conditions and the
16   following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19   copyright notice, this list of conditions and the
20   following disclaimer in the documentation and/or other
21   materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24   contributors may be used to endorse or promote products
25   derived from this software without specific prior
26   written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 ---------------------------------------------------------------------------
40 */
41 
42 /** @file Bitmap.h
43  *  @brief Defines bitmap format helper for textures
44  *
45  * Used for file formats which embed their textures into the model file.
46  */
47 #pragma once
48 #ifndef AI_BITMAP_H_INC
49 #define AI_BITMAP_H_INC
50 
51 #ifdef __GNUC__
52 #   pragma GCC system_header
53 #endif
54 
55 #include "defs.h"
56 #include <cstdint>
57 #include <cstddef>
58 
59 struct aiTexture;
60 
61 namespace Assimp {
62 
63 class IOStream;
64 
65 // ---------------------------------------------------------------------------
66 /**
67  *  This class is used to store and write bitmap information.
68  */
69 class ASSIMP_API Bitmap {
70 protected:
71 
72     struct Header {
73         uint16_t type;
74         uint32_t size;
75         uint16_t reserved1;
76         uint16_t reserved2;
77         uint32_t offset;
78 
79         // We define the struct size because sizeof(Header) might return a wrong result because of structure padding.
80         static constexpr std::size_t header_size =
81             sizeof(type) +
82             sizeof(size) +
83             sizeof(reserved1) +
84             sizeof(reserved2) +
85             sizeof(offset);
86     };
87 
88     struct DIB {
89         uint32_t size;
90         int32_t width;
91         int32_t height;
92         uint16_t planes;
93         uint16_t bits_per_pixel;
94         uint32_t compression;
95         uint32_t image_size;
96         int32_t x_resolution;
97         int32_t y_resolution;
98         uint32_t nb_colors;
99         uint32_t nb_important_colors;
100 
101         // We define the struct size because sizeof(DIB) might return a wrong result because of structure padding.
102         static constexpr std::size_t dib_size =
103             sizeof(size) +
104             sizeof(width) +
105             sizeof(height) +
106             sizeof(planes) +
107             sizeof(bits_per_pixel) +
108             sizeof(compression) +
109             sizeof(image_size) +
110             sizeof(x_resolution) +
111             sizeof(y_resolution) +
112             sizeof(nb_colors) +
113             sizeof(nb_important_colors);
114     };
115 
116     static constexpr std::size_t mBytesPerPixel = 4;
117 
118 public:
119     /// @brief  Will save an aiTexture instance as a bitmap.
120     /// @param texture  The pointer to the texture instance
121     /// @param file     The filename to save into.
122     /// @return true if successfully saved, false if not.
123     static bool Save(aiTexture* texture, IOStream* file);
124 
125 protected:
126     static void WriteHeader(Header& header, IOStream* file);
127     static void WriteDIB(DIB& dib, IOStream* file);
128     static void WriteData(aiTexture* texture, IOStream* file);
129 };
130 
131 }
132 
133 #endif // AI_BITMAP_H_INC
134