File: bmp.hpp

package info (click to toggle)
ares 126-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 32,600 kB
  • sloc: cpp: 356,508; ansic: 20,394; makefile: 16; sh: 2
file content (47 lines) | stat: -rw-r--r-- 1,588 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#pragma once

namespace nall::Encode {

struct BMP {
  static auto create(const string& filename, const void* data, u32 pitch, u32 width, u32 height, bool alpha) -> bool {
    auto fp = file::open(filename, file::mode::write);
    if(!fp) return false;

    u32 bitsPerPixel  = alpha ? 32 : 24;
    u32 bytesPerPixel = bitsPerPixel / 8;
    u32 alignedWidth  = width * bytesPerPixel;
    u32 paddingLength = 0;
    u32 imageSize     = alignedWidth * height;
    u32 fileSize      = 0x36 + imageSize;
    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

    pitch >>= 2;
    for(auto y : range(height)) {
      auto p = (const u32*)data + y * pitch;
      for(auto x : range(width)) fp.writel(*p++, bytesPerPixel);
      if(paddingLength) fp.writel(0, paddingLength);
    }

    return true;
  }
};

}