123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- #pragma once
- namespace nall::Encode {
- struct BMP {
- static auto create(const string& filename, const void* data, uint pitch, uint width, uint height, bool alpha) -> bool {
- auto fp = file::open(filename, file::mode::write);
- if(!fp) return false;
- uint bitsPerPixel = alpha ? 32 : 24;
- uint bytesPerPixel = bitsPerPixel / 8;
- uint alignedWidth = width * bytesPerPixel;
- uint paddingLength = 0;
- uint imageSize = alignedWidth * height;
- uint fileSize = 0x36 + imageSize;
- while(alignedWidth % 4) alignedWidth++, paddingLength++;
- fp.writel(0x4d42, 2);
- fp.writel(fileSize, 4);
- fp.writel(0, 2);
- fp.writel(0, 2);
- fp.writel(0x36, 4);
- fp.writel(40, 4);
- fp.writel(width, 4);
- fp.writel(-height, 4);
- fp.writel(1, 2);
- fp.writel(bitsPerPixel, 2);
- fp.writel(0, 4);
- fp.writel(imageSize, 4);
- fp.writel(3780, 4);
- fp.writel(3780, 4);
- fp.writel(0, 4);
- fp.writel(0, 4);
- pitch >>= 2;
- for(auto y : range(height)) {
- auto p = (const uint32_t*)data + y * pitch;
- for(auto x : range(width)) fp.writel(*p++, bytesPerPixel);
- if(paddingLength) fp.writel(0, paddingLength);
- }
- return true;
- }
- };
- }
|