1/*
2---------------------------------------------------------------------------
3Open Asset Import Library (assimp)
4---------------------------------------------------------------------------
5
6Copyright (c) 2006-2017, assimp team
7
8
9All rights reserved.
10
11Redistribution and use of this software in source and binary forms,
12with or without modification, are permitted provided that the following
13conditions are met:
14
15* Redistributions of source code must retain the above
16 copyright notice, this list of conditions and the
17 following disclaimer.
18
19* Redistributions in binary form must reproduce the above
20 copyright notice, this list of conditions and the
21 following disclaimer in the documentation and/or other
22 materials provided with the distribution.
23
24* Neither the name of the assimp team, nor the names of its
25 contributors may be used to endorse or promote products
26 derived from this software without specific prior
27 written permission of the assimp team.
28
29THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40---------------------------------------------------------------------------
41*/
42
43/** @file Bitmap.cpp
44 * @brief Defines bitmap format helper for textures
45 *
46 * Used for file formats which embed their textures into the model file.
47 */
48
49
50#include "Bitmap.h"
51#include <assimp/texture.h>
52#include <assimp/IOStream.hpp>
53#include "ByteSwapper.h"
54
55namespace Assimp {
56
57 void Bitmap::Save(aiTexture* texture, IOStream* file) {
58 if(file != NULL) {
59 Header header;
60 DIB dib;
61
62 dib.size = DIB::dib_size;
63 dib.width = texture->mWidth;
64 dib.height = texture->mHeight;
65 dib.planes = 1;
66 dib.bits_per_pixel = 8 * mBytesPerPixel;
67 dib.compression = 0;
68 dib.image_size = (((dib.width * mBytesPerPixel) + 3) & 0x0000FFFC) * dib.height;
69 dib.x_resolution = 0;
70 dib.y_resolution = 0;
71 dib.nb_colors = 0;
72 dib.nb_important_colors = 0;
73
74 header.type = 0x4D42; // 'BM'
75 header.offset = Header::header_size + DIB::dib_size;
76 header.size = header.offset + dib.image_size;
77 header.reserved1 = 0;
78 header.reserved2 = 0;
79
80 WriteHeader(header, file);
81 WriteDIB(dib, file);
82 WriteData(texture, file);
83 }
84 }
85
86 template<typename T>
87 inline std::size_t Copy(uint8_t* data, T& field) {
88#ifdef AI_BUILD_BIG_ENDIAN
89 T field_swapped=AI_BE(field);
90 std::memcpy(data, &field_swapped, sizeof(field)); return sizeof(field);
91#else
92 std::memcpy(data, &AI_BE(field), sizeof(field)); return sizeof(field);
93#endif
94 }
95
96 void Bitmap::WriteHeader(Header& header, IOStream* file) {
97 uint8_t data[Header::header_size];
98
99 std::size_t offset = 0;
100
101 offset += Copy(&data[offset], header.type);
102 offset += Copy(&data[offset], header.size);
103 offset += Copy(&data[offset], header.reserved1);
104 offset += Copy(&data[offset], header.reserved2);
105 Copy(&data[offset], header.offset);
106
107 file->Write(data, Header::header_size, 1);
108 }
109
110 void Bitmap::WriteDIB(DIB& dib, IOStream* file) {
111 uint8_t data[DIB::dib_size];
112
113 std::size_t offset = 0;
114
115 offset += Copy(&data[offset], dib.size);
116 offset += Copy(&data[offset], dib.width);
117 offset += Copy(&data[offset], dib.height);
118 offset += Copy(&data[offset], dib.planes);
119 offset += Copy(&data[offset], dib.bits_per_pixel);
120 offset += Copy(&data[offset], dib.compression);
121 offset += Copy(&data[offset], dib.image_size);
122 offset += Copy(&data[offset], dib.x_resolution);
123 offset += Copy(&data[offset], dib.y_resolution);
124 offset += Copy(&data[offset], dib.nb_colors);
125 Copy(&data[offset], dib.nb_important_colors);
126
127 file->Write(data, DIB::dib_size, 1);
128 }
129
130 void Bitmap::WriteData(aiTexture* texture, IOStream* file) {
131 static const std::size_t padding_offset = 4;
132 static const uint8_t padding_data[padding_offset] = {0x0, 0x0, 0x0, 0x0};
133
134 unsigned int padding = (padding_offset - ((mBytesPerPixel * texture->mWidth) % padding_offset)) % padding_offset;
135 uint8_t pixel[mBytesPerPixel];
136
137 for(std::size_t i = 0; i < texture->mHeight; ++i) {
138 for(std::size_t j = 0; j < texture->mWidth; ++j) {
139 const aiTexel& texel = texture->pcData[(texture->mHeight - i - 1) * texture->mWidth + j]; // Bitmap files are stored in bottom-up format
140
141 pixel[0] = texel.r;
142 pixel[1] = texel.g;
143 pixel[2] = texel.b;
144 pixel[3] = texel.a;
145
146 file->Write(pixel, mBytesPerPixel, 1);
147 }
148
149 file->Write(padding_data, padding, 1);
150 }
151 }
152
153}
154