2016-01-07 08:14:33 +00:00
|
|
|
#pragma once
|
2015-08-02 06:23:13 +00:00
|
|
|
|
2019-01-16 00:46:42 +00:00
|
|
|
namespace nall::Encode {
|
2015-08-02 06:23:13 +00:00
|
|
|
|
|
|
|
struct BMP {
|
2018-08-04 11:44:00 +00:00
|
|
|
static auto create(const string& filename, const void* data, uint pitch, uint width, uint height, bool alpha) -> bool {
|
2018-10-04 10:11:23 +00:00
|
|
|
auto fp = file::open(filename, file::mode::write);
|
2015-08-02 06:23:13 +00:00
|
|
|
if(!fp) return false;
|
|
|
|
|
2018-07-02 01:55:42 +00:00
|
|
|
uint bitsPerPixel = alpha ? 32 : 24;
|
|
|
|
uint bytesPerPixel = bitsPerPixel / 8;
|
|
|
|
uint alignedWidth = width * bytesPerPixel;
|
|
|
|
uint paddingLength = 0;
|
|
|
|
uint imageSize = alignedWidth * height;
|
|
|
|
uint fileSize = 0x36 + imageSize;
|
2015-08-02 06:23:13 +00:00
|
|
|
while(alignedWidth % 4) alignedWidth++, paddingLength++;
|
|
|
|
|
|
|
|
fp.writel(0x4d42, 2); //signature
|
|
|
|
fp.writel(fileSize, 4); //file size
|
|
|
|
fp.writel(0, 2); //reserved
|
|
|
|
fp.writel(0, 2); //reserved
|
|
|
|
fp.writel(0x36, 4); //offset
|
|
|
|
|
|
|
|
fp.writel(40, 4); //DIB size
|
|
|
|
fp.writel(width, 4); //width
|
|
|
|
fp.writel(-height, 4); //height
|
|
|
|
fp.writel(1, 2); //color planes
|
|
|
|
fp.writel(bitsPerPixel, 2); //bits per pixel
|
|
|
|
fp.writel(0, 4); //compression method (BI_RGB)
|
|
|
|
fp.writel(imageSize, 4); //image data size
|
|
|
|
fp.writel(3780, 4); //horizontal resolution
|
|
|
|
fp.writel(3780, 4); //vertical resolution
|
|
|
|
fp.writel(0, 4); //palette size
|
|
|
|
fp.writel(0, 4); //important color count
|
|
|
|
|
2018-07-02 01:55:42 +00:00
|
|
|
pitch >>= 2;
|
2015-08-02 06:23:13 +00:00
|
|
|
for(auto y : range(height)) {
|
2018-08-04 11:44:00 +00:00
|
|
|
auto p = (const uint32_t*)data + y * pitch;
|
2015-08-02 06:23:13 +00:00
|
|
|
for(auto x : range(width)) fp.writel(*p++, bytesPerPixel);
|
|
|
|
if(paddingLength) fp.writel(0, paddingLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-16 00:46:42 +00:00
|
|
|
}
|