Beatmup
internal_bitmap.cpp
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 #include "internal_bitmap.h"
20 #include "../exception.h"
21 #include "../utils/bmp_file.h"
22 #include "../utils/utils.hpp"
23 
24 using namespace Beatmup;
25 
26 
30 {
31  if (getBitsPerPixel() < 8) {
32  int n = 8 / getBitsPerPixel();
33  this->width = ceili(width, n) * n;
34  }
35  if (allocate)
37  upToDate[ProcessingTarget::CPU] = allocate;
38 }
39 
40 
41 InternalBitmap::InternalBitmap(Context& ctx, const char* bmpFilename) :
43 {
44  // read header
45  BmpFile bmp(bmpFilename);
46  switch (bmp.getBitsPerPixel()) {
47  case 1:
49  break;
50  case 4:
52  break;
53  case 8:
55  break;
56  case 24:
58  break;
59  case 32:
61  break;
62  default:
63  throw IOError(bmpFilename, "Unsupported pixel format");
64  }
65  this->width = bmp.getWidth();
66  this->height = bmp.getHeight();
67 
68  // allocate & read
70  bmp.load(memory(), getMemorySize());
71 }
72 
73 
75  if (this->width * this->height != width * height && memory) {
76  this->width = width;
77  this->height = height;
78  memory = AlignedMemory(getMemorySize());
79  }
80  else {
81  this->width = width;
82  this->height = height;
83  }
84 
85  if (upToDate[ProcessingTarget::GPU])
86  TextureHandler::invalidate(*ctx.getGpuRecycleBin());
87 
88  upToDate[ProcessingTarget::CPU] = false;
89  upToDate[ProcessingTarget::GPU] = false;
90 }
91 
92 
94  return pixelFormat;
95 }
96 
97 
98 const int InternalBitmap::getWidth() const {
99  return width;
100 }
101 
102 
103 const int InternalBitmap::getHeight() const {
104  return height;
105 }
106 
107 
109  // a proper way to compute required memory size (works for bpp < 8)
111 }
112 
113 
114 const pixbyte* InternalBitmap::getData(int x, int y) const {
116 }
117 
120 }
121 
122 
124  if (!memory)
126 }
A very basic class for any image.
static const unsigned char BITS_PER_PIXEL[NUM_PIXEL_FORMATS]
number of bits per pixel for each pixel format
const unsigned char getBitsPerPixel() const
Returns number of bits per pixel stored in each bitmap.
bool upToDate[2]
bitmap up-to-date state on CPU and GPU
Aligned memory buffer.
Definition: memory.h:27
datatype * ptr(int offset=0)
Definition: memory.h:46
Toolset to load and store images in BMP format.
Definition: bmp_file.h:28
int32_t getWidth() const
Definition: bmp_file.h:87
int32_t getHeight() const
Definition: bmp_file.h:88
void load(void *pixels, const uint32_t pixelsSizeInBytes)
Loads the content of the file into memory.
Definition: bmp_file.cpp:103
uint8_t getBitsPerPixel() const
Definition: bmp_file.h:86
Basic class: task and memory management, any kind of static data.
Definition: context.h:59
GL::RecycleBin * getGpuRecycleBin() const
Definition: context.cpp:340
const pixbyte * getData(int x, int y) const
Returns a pointer to given pixel.
const int getHeight() const
Height of the texture in pixels.
const msize getMemorySize() const
Bitmap size in bytes.
const int getWidth() const
Width of the texture in pixels.
const PixelFormat getPixelFormat() const
Pixel format of the bitmap.
InternalBitmap(Context &ctx, PixelFormat pixelFormat, int width, int height, bool allocate=true)
Creates a bitmap.
void lockPixelData()
Locks access to the CPU memory buffer containing pixel data.
void reshape(int width, int height)
Changes bitmap size.
uint32_t msize
memory size
Definition: basic_types.h:30
uint8_t pixbyte
Definition: basic_types.h:34
@ SingleByte
single channel of 8 bits per pixel (like grayscale), unsigned integer values
@ QuadByte
4 channels of 8 bits per pixel (like RGBA), unsigned integer values
@ TripleByte
3 channels of 8 bits per pixel (like RGB), unsigned integer values
@ BinaryMask
1 bit per pixel
@ HexMask
4 bits per pixel
#define ceili(x, y)
integer division x/y with ceiling
Definition: utils.hpp:21
jlong jint jint jint jint pixelFormat
jobject jlong jint jint y
Beatmup::Context * ctx
jlong jint width
jlong jint jint height
jobject jlong jint x
int n