Beatmup
bmp_file.h
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #pragma once
20 #include <fstream>
21 #include <string>
22 #include <cstdint>
23 
24 namespace Beatmup {
25  /**
26  Toolset to load and store images in BMP format.
27  */
28  class BmpFile {
29  private:
30 #ifdef _MSC_VER
31 #pragma pack(push,1)
32 #endif
33  typedef struct
34 #ifndef _MSC_VER
35  __attribute__((packed))
36 #endif
37  {
38  uint8_t magic[2];
39  uint32_t size;
40  uint16_t reserved[2];
41  uint32_t offset;
42  uint32_t headerSize;
43  int32_t width, height;
44  uint16_t numColorPlanes;
45  uint16_t bpp;
46  uint32_t compression;
47  uint32_t imageSize;
48  int32_t hdpi, vdpi;
49  uint32_t numImportantColors;
50  } Header;
51 #ifdef _MSC_VER
52 #pragma pack(pop)
53 #endif
54 
56 
58  std::ifstream in;
59 
60  public:
61  BmpFile(const char* filename);
62 
63  /**
64  Loads the content of the file into memory.
65  \param[out] pixels The memory buffer to fill with pixel data.
66  \param[in] pixelsSizeInBytes The memory buffer size in bytes
67  */
68  void load(void* pixels, const uint32_t pixelsSizeInBytes);
69 
70  /**
71  Stores an image into a BMP file.
72  \param[in] pixels Memory buffer containing the pixel data to save
73  \param[in] width Image width in pixels
74  \param[in] height Image height in pixels
75  \param[in] bpp Number of bits per pixel
76  \param[in] filename Name/path of the file to write the data to.
77  */
78  static void save(
79  const void* pixels,
80  int32_t width,
81  int32_t height,
82  uint8_t bpp,
83  const char* filename
84  );
85 
86  inline uint8_t getBitsPerPixel() const { return header.bpp; }
87  inline int32_t getWidth() const { return header.width; }
88  inline int32_t getHeight() const { return header.height; }
89  };
90 }
Toolset to load and store images in BMP format.
Definition: bmp_file.h:28
std::ifstream in
Definition: bmp_file.h:58
int32_t getWidth() const
Definition: bmp_file.h:87
int32_t getHeight() const
Definition: bmp_file.h:88
Header header
Definition: bmp_file.h:57
static const Header BMP_HEADER_REFERENCE
Definition: bmp_file.h:55
struct __attribute__((packed))
Definition: bmp_file.h:35
void load(void *pixels, const uint32_t pixelsSizeInBytes)
Loads the content of the file into memory.
Definition: bmp_file.cpp:103
static void save(const void *pixels, int32_t width, int32_t height, uint8_t bpp, const char *filename)
Stores an image into a BMP file.
Definition: bmp_file.cpp:145
BmpFile(const char *filename)
Definition: bmp_file.cpp:82
uint8_t getBitsPerPixel() const
Definition: bmp_file.h:86
JNIEnv jlong jstring filename
jlong jint width
jlong jint jint height
jlong jobject size