MCPcopy Create free account
hub / github.com/assimp/assimp / SaveAsBMP

Function SaveAsBMP

tools/assimp_cmd/ImageExtractor.cpp:117–172  ·  view source on GitHub ↗

----------------------------------------------------------------------------------- Save a texture as bitmap

Source from the content-addressed store, hash-verified

115// -----------------------------------------------------------------------------------
116// Save a texture as bitmap
117int SaveAsBMP(FILE *file, const aiTexel *data, unsigned int width, unsigned int height, bool SaveAlpha = false) {
118 if (!file || !data) {
119 return 1;
120 }
121
122 const unsigned int numc = (SaveAlpha ? 4 : 3);
123 unsigned char *buffer = new unsigned char[width * height * numc];
124
125 for (unsigned int y = 0; y < height; ++y) {
126 for (unsigned int x = 0; x < width; ++x) {
127
128 unsigned char *s = &buffer[(y * width + x) * numc];
129 const aiTexel *t = &data[y * width + x];
130 s[0] = t->b;
131 s[1] = t->g;
132 s[2] = t->r;
133 if (4 == numc) {
134 s[3] = t->a;
135 }
136 }
137 }
138
139 BITMAPFILEHEADER header;
140 header.bfType = 'B' | (int('M') << 8u);
141 header.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
142 header.bfSize = header.bfOffBits + width * height * numc;
143 header.bfReserved1 = header.bfReserved2 = 0;
144
145 fwrite(&header, sizeof(BITMAPFILEHEADER), 1, file);
146
147 BITMAPINFOHEADER info;
148 info.biSize = 40;
149 info.biWidth = width;
150 info.biHeight = height;
151 info.biPlanes = 1;
152 info.biBitCount = (int16_t)numc << 3;
153 info.biCompression = 0;
154 info.biSizeImage = width * height * numc;
155 info.biXPelsPerMeter = 1; // dummy
156 info.biYPelsPerMeter = 1; // dummy
157 info.biClrUsed = 0;
158 info.biClrImportant = 0;
159
160 fwrite(&info, sizeof(BITMAPINFOHEADER), 1, file);
161
162 unsigned char *temp = buffer + info.biSizeImage;
163 const unsigned int row = width * numc;
164
165 for (int y = 0; temp -= row, y < info.biHeight; ++y) {
166 fwrite(temp, row, 1, file);
167 }
168
169 // delete the buffer
170 delete[] buffer;
171 return 0;
172}
173
174// -----------------------------------------------------------------------------------

Callers 1

DoExportFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected