Beatmup
Beatmup::AbstractBitmap Class Referenceabstract

A very basic class for any image. More...

#include <abstract_bitmap.h>

Inheritance diagram for Beatmup::AbstractBitmap:
Beatmup::GL::TextureHandler Beatmup::Object Beatmup::Android::Bitmap Beatmup::Android::ExternalBitmap Beatmup::GDIBitmap Beatmup::InternalBitmap Beatmup::Python::Bitmap

Classes

class  ReadLock
 Locks a bitmap for reading on CPU. More...
 
class  WriteLock
 Makes a bitmap writable for a specific target device. More...
 

Public Member Functions

virtual const int getDepth () const
 Depth of the texture in pixels. More...
 
virtual const TextureFormat getTextureFormat () const
 Returns the texture format specifying how the shader must interpret the data. More...
 
virtual const PixelFormat getPixelFormat () const =0
 Pixel format of the bitmap. More...
 
virtual const msize getMemorySize () const =0
 Bitmap size in bytes. More...
 
bool isUpToDate (ProcessingTarget) const
 
bool isDirty () const
 Returns true if the bitmap does not contain any valid content. More...
 
virtual const pixbytegetData (int x, int y) const =0
 Returns a pointer to given pixel. More...
 
virtual pixbytegetData (int x, int y)=0
 
int getPixelInt (int x, int y, int cha=0) const
 Retrieves integer value of given channel at given pixel. More...
 
const unsigned char getBitsPerPixel () const
 Returns number of bits per pixel stored in each bitmap. More...
 
const unsigned char getNumberOfChannels () const
 Returns number of bytes per pixel stored in each bitmap. More...
 
const ImageResolution getSize () const
 Returns the bitmap resolution within ImageResolution object. More...
 
ContextgetContext () const
 
void zero ()
 Sets all the pixels to zero. More...
 
bool isInteger () const
 Returns true if the bitmap contains integer values, false otherwise. More...
 
bool isFloat () const
 Returns true if the bitmap contains floating point values, false otherwise. More...
 
bool isMask () const
 Returns true if the bitmap is a mask, false otherwise. More...
 
std::string toString () const
 Retruns a string describing the bitmap. More...
 
void saveBmp (const char *filename)
 Saves the bitmap to a BMP file. More...
 
 ~AbstractBitmap ()
 
- Public Member Functions inherited from Beatmup::GL::TextureHandler
 ~TextureHandler ()
 
virtual const int getWidth () const =0
 Width of the texture in pixels. More...
 
virtual const int getHeight () const =0
 Height of the texture in pixels. More...
 
float getAspectRatio () const
 Aspect ratio of the texture. More...
 
float getInvAspectRatio () const
 Inverse of the aspect ratio of the texture. More...
 
const bool isFloatingPoint () const
 
const int getNumberOfChannels () const
 Returns number of channels containing in the texture. More...
 
bool hasValidHandle () const
 Returns true if the texture handle points to a valid texture. More...
 
- Public Member Functions inherited from Beatmup::Object
virtual ~Object ()
 

Static Public Member Functions

static bool isInteger (PixelFormat pixelFormat)
 Returns true if a given pixel format corresponds to integer values, false otherwise. More...
 
static bool isFloat (PixelFormat pixelFormat)
 Returns true if a given pixel format corresponds to floating point values, false otherwise. More...
 
static bool isMask (PixelFormat pixelFormat)
 Returns true if a given pixel format corresponds to a mask, false otherwise. More...
 
- Static Public Member Functions inherited from Beatmup::GL::TextureHandler
static const char * textureFormatToString (const TextureFormat &)
 

Static Public Attributes

static const int NUM_PIXEL_FORMATS = 9
 
static const char * PIXEL_FORMAT_NAMES [NUM_PIXEL_FORMATS]
 pixel format names More...
 
static const unsigned char CHANNELS_PER_PIXEL [NUM_PIXEL_FORMATS]
 number of channels for each pixel format More...
 
static const unsigned char BITS_PER_PIXEL [NUM_PIXEL_FORMATS]
 number of bits per pixel for each pixel format More...
 
- Static Public Attributes inherited from Beatmup::GL::TextureHandler
static const int TEXTURE_FORMAT_BYTES_PER_PIXEL []
 size of a texel in bytes for different texture formats More...
 

Protected Member Functions

 AbstractBitmap (Context &ctx)
 
virtual void lockPixelData ()=0
 Locks access to the CPU memory buffer containing pixel data. More...
 
virtual void unlockPixelData ()=0
 Unlocks access to the CPU memory buffer containing pixel data. More...
 
virtual void prepare (GraphicPipeline &gpu)
 Prepares (eventually uploads) texture data on GPU. More...
 
- Protected Member Functions inherited from Beatmup::GL::TextureHandler
 TextureHandler ()
 
void invalidate (RecycleBin &)
 Forces disposing the texture data, e.g. More...
 

Protected Attributes

Contextctx
 context managing this bitmap More...
 
bool upToDate [2]
 bitmap up-to-date state on CPU and GPU More...
 
- Protected Attributes inherited from Beatmup::GL::TextureHandler
handle_t textureHandle
 

Private Member Functions

 AbstractBitmap (const AbstractBitmap &that)=delete
 disabling copying constructor More...
 

Friends

class GraphicPipeline
 
class BitmapContentLock
 
class AbstractBitmap::ReadLock
 
class AbstractBitmap::WriteLock< ProcessingTarget::CPU >
 
class AbstractBitmap::WriteLock< ProcessingTarget::GPU >
 

Additional Inherited Members

- Public Types inherited from Beatmup::GL::TextureHandler
enum  TextureFormat {
  Rx8 , RGBx8 , RGBAx8 , Rx32f ,
  RGBx32f , RGBAx32f , OES_Ext
}
 Texture format, specifies how the texture should be interpreted on the shader side. More...
 

Detailed Description

A very basic class for any image.

Contains interfaces to access the bitmap information and its content.

Definition at line 87 of file abstract_bitmap.h.

Constructor & Destructor Documentation

◆ AbstractBitmap() [1/2]

Beatmup::AbstractBitmap::AbstractBitmap ( const AbstractBitmap that)
privatedelete

disabling copying constructor

◆ AbstractBitmap() [2/2]

AbstractBitmap::AbstractBitmap ( Context ctx)
protected

Definition at line 208 of file abstract_bitmap.cpp.

208  : ctx(ctx) {
211 }
Context & ctx
context managing this bitmap
bool upToDate[2]
bitmap up-to-date state on CPU and GPU

◆ ~AbstractBitmap()

AbstractBitmap::~AbstractBitmap ( )

Definition at line 214 of file abstract_bitmap.cpp.

214  {
215  if (hasValidHandle()) {
216  TextureHandler::invalidate(*ctx.getGpuRecycleBin());
217  }
218 }
GL::RecycleBin * getGpuRecycleBin() const
Definition: context.cpp:340
bool hasValidHandle() const
Returns true if the texture handle points to a valid texture.

Member Function Documentation

◆ lockPixelData()

virtual void Beatmup::AbstractBitmap::lockPixelData ( )
protectedpure virtual

Locks access to the CPU memory buffer containing pixel data.

Implemented in Beatmup::Python::Bitmap, Beatmup::Android::ExternalBitmap, Beatmup::Android::Bitmap, Beatmup::GDIBitmap, and Beatmup::InternalBitmap.

◆ unlockPixelData()

virtual void Beatmup::AbstractBitmap::unlockPixelData ( )
protectedpure virtual

Unlocks access to the CPU memory buffer containing pixel data.

Implemented in Beatmup::Python::Bitmap, Beatmup::Android::ExternalBitmap, Beatmup::Android::Bitmap, Beatmup::GDIBitmap, and Beatmup::InternalBitmap.

◆ prepare()

void AbstractBitmap::prepare ( GraphicPipeline gpu)
protectedvirtual

Prepares (eventually uploads) texture data on GPU.

Called only by the context managing thread.

Parameters
[in]gpuGraphic pipeline instance

Reimplemented from Beatmup::GL::TextureHandler.

Reimplemented in Beatmup::Android::ExternalBitmap.

Definition at line 54 of file abstract_bitmap.cpp.

54  {
55  const bool handleValid = hasValidHandle();
56 
57  if (!handleValid)
58  TextureHandler::prepare(gpu);
59  glBindTexture(GL_TEXTURE_2D, textureHandle);
60 
61  // setup alignment
62  if (isMask()) {
63  // masks are stored as horizontally-stretched bitmaps
64  const int textureWidth = getWidth() / (8 / getBitsPerPixel());
65 
66 #ifdef BEATMUP_OPENGLVERSION_GLES20
67  if (!handleValid)
68  glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, textureWidth, getHeight(), 0, GL_ALPHA, GL_UNSIGNED_BYTE, nullptr);
69 #else
70  if (!handleValid)
71  glTexStorage2D(GL_TEXTURE_2D, 1, GL_R8, textureWidth, getHeight());
72 #endif
73  GL::GLException::check("allocating texture image (mask)");
74  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
75  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
76  }
77 
78  else {
79 #ifdef BEATMUP_OPENGLVERSION_GLES20
80  if (!handleValid)
81  glTexImage2D(GL_TEXTURE_2D,
82  0,
84  getWidth(), getHeight(),
85  0,
88  nullptr);
89 
90 #else
91  if (!handleValid)
92  glTexStorage2D(GL_TEXTURE_2D, 1, GL::BITMAP_INTERNALFORMATS[getPixelFormat()], getWidth(), getHeight());
93 #endif
94  GL::GLException::check("allocating texture image");
95  }
96 }
virtual const PixelFormat getPixelFormat() const =0
Pixel format of the bitmap.
const unsigned char getBitsPerPixel() const
Returns number of bits per pixel stored in each bitmap.
bool isMask() const
Returns true if the bitmap is a mask, false otherwise.
static void check(const std::string &info)
Definition: bgl.h:62
virtual const int getHeight() const =0
Height of the texture in pixels.
virtual const int getWidth() const =0
Width of the texture in pixels.
const GLuint BITMAP_INTERNALFORMATS[]
Definition: bgl.h:147
const GLuint BITMAP_PIXELTYPES[]
Mapping of bitmap pixel formats to GL pixel types.
Definition: bgl.h:160
const GLuint BITMAP_PIXELFORMATS[]
Mapping of bitmap pixel formats to GL pixel formats.
Definition: bgl.h:80

◆ getDepth()

virtual const int Beatmup::AbstractBitmap::getDepth ( ) const
inlinevirtual

Depth of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Reimplemented in Beatmup::Android::ExternalBitmap.

Definition at line 127 of file abstract_bitmap.h.

127 { return 1; }

◆ getTextureFormat()

const GL::TextureHandler::TextureFormat AbstractBitmap::getTextureFormat ( ) const
virtual

Returns the texture format specifying how the shader must interpret the data.

Implements Beatmup::GL::TextureHandler.

Reimplemented in Beatmup::Android::ExternalBitmap.

Definition at line 99 of file abstract_bitmap.cpp.

99  {
100  if (isMask())
101  return TextureFormat::Rx8;
102  return (TextureFormat)getPixelFormat();
103 }
TextureFormat
Texture format, specifies how the texture should be interpreted on the shader side.

◆ getPixelFormat()

virtual const PixelFormat Beatmup::AbstractBitmap::getPixelFormat ( ) const
pure virtual

◆ getMemorySize()

virtual const msize Beatmup::AbstractBitmap::getMemorySize ( ) const
pure virtual

◆ isUpToDate()

bool AbstractBitmap::isUpToDate ( ProcessingTarget  target) const

Definition at line 106 of file abstract_bitmap.cpp.

106  {
107  return upToDate[target];
108 }

◆ isDirty()

bool AbstractBitmap::isDirty ( ) const

Returns true if the bitmap does not contain any valid content.

Definition at line 111 of file abstract_bitmap.cpp.

111  {
113 }

◆ getData() [1/2]

virtual const pixbyte* Beatmup::AbstractBitmap::getData ( int  x,
int  y 
) const
pure virtual

Returns a pointer to given pixel.

Parameters
xtarget pixel horizontal coordinate
ytarget pixel vertical coordinate
Returns
a pointer, may be NULL.

Implemented in Beatmup::Python::Bitmap, Beatmup::Android::ExternalBitmap, Beatmup::Android::Bitmap, Beatmup::GDIBitmap, and Beatmup::InternalBitmap.

◆ getData() [2/2]

virtual pixbyte* Beatmup::AbstractBitmap::getData ( int  x,
int  y 
)
pure virtual

◆ getPixelInt()

int AbstractBitmap::getPixelInt ( int  x,
int  y,
int  cha = 0 
) const

Retrieves integer value of given channel at given pixel.

Parameters
xtarget pixel horizontal coordinate
ytarget pixel vertical coordinate
chatarget channel
Returns
requested pixel value

Definition at line 116 of file abstract_bitmap.cpp.

116  {
118  if (isMask(pf)) {
119  unsigned char
120  pixPerByte = 8 / BITS_PER_PIXEL[pf],
121  offset = (x + y*getWidth()) % pixPerByte; // offset from byte-aligned bound in pixels
122  const pixbyte* p = getData(x, y);
123  return ((*p) >> (offset*BITS_PER_PIXEL[pf])) & ((1 << BITS_PER_PIXEL[pf]) - 1);
124  }
125  const pixbyte* p = getData(x, y) + cha * BITS_PER_PIXEL[pf] / 8 / CHANNELS_PER_PIXEL[pf];
126  if (isInteger(pf))
127  return *p;
128  //if (isFloat(pf))
129  return (int)(*((float*)p) * 255);
130 }
static const unsigned char BITS_PER_PIXEL[NUM_PIXEL_FORMATS]
number of bits per pixel for each pixel format
bool isInteger() const
Returns true if the bitmap contains integer values, false otherwise.
virtual const pixbyte * getData(int x, int y) const =0
Returns a pointer to given pixel.
static const unsigned char CHANNELS_PER_PIXEL[NUM_PIXEL_FORMATS]
number of channels for each pixel format
uint8_t pixbyte
Definition: basic_types.h:34
jobject jlong jint jint y
jobject jlong jint x
Beatmup::IntPoint p((int) x,(int) y)

◆ getBitsPerPixel()

const unsigned char AbstractBitmap::getBitsPerPixel ( ) const

Returns number of bits per pixel stored in each bitmap.

Definition at line 133 of file abstract_bitmap.cpp.

133  {
134  return BITS_PER_PIXEL[getPixelFormat()];
135 }

◆ getNumberOfChannels()

const unsigned char AbstractBitmap::getNumberOfChannels ( ) const

Returns number of bytes per pixel stored in each bitmap.

Definition at line 138 of file abstract_bitmap.cpp.

138  {
140 }

◆ getSize()

const ImageResolution Beatmup::AbstractBitmap::getSize ( ) const
inline

Returns the bitmap resolution within ImageResolution object.

Definition at line 178 of file abstract_bitmap.h.

178  {
179  return ImageResolution(getWidth(), getHeight());
180  }

◆ getContext()

Context & AbstractBitmap::getContext ( ) const

Definition at line 221 of file abstract_bitmap.cpp.

221  {
222  return ctx;
223 }

◆ zero()

void AbstractBitmap::zero ( )

Sets all the pixels to zero.

Definition at line 226 of file abstract_bitmap.cpp.

226  {
227  lockPixelData();
228  memset(getData(0, 0), 0, getMemorySize());
229  unlockPixelData();
232 }
virtual void lockPixelData()=0
Locks access to the CPU memory buffer containing pixel data.
virtual const msize getMemorySize() const =0
Bitmap size in bytes.
virtual void unlockPixelData()=0
Unlocks access to the CPU memory buffer containing pixel data.

◆ isInteger() [1/2]

bool AbstractBitmap::isInteger ( ) const

Returns true if the bitmap contains integer values, false otherwise.

Definition at line 143 of file abstract_bitmap.cpp.

143  {
144  return isInteger(getPixelFormat());
145 }

◆ isFloat() [1/2]

bool AbstractBitmap::isFloat ( ) const

Returns true if the bitmap contains floating point values, false otherwise.

Definition at line 148 of file abstract_bitmap.cpp.

148  {
149  return isFloat(getPixelFormat());
150 }
bool isFloat() const
Returns true if the bitmap contains floating point values, false otherwise.

◆ isMask() [1/2]

bool AbstractBitmap::isMask ( ) const

Returns true if the bitmap is a mask, false otherwise.

Definition at line 153 of file abstract_bitmap.cpp.

153  {
154  return isMask(getPixelFormat());
155 }

◆ isInteger() [2/2]

bool AbstractBitmap::isInteger ( PixelFormat  pixelFormat)
static

Returns true if a given pixel format corresponds to integer values, false otherwise.

Definition at line 158 of file abstract_bitmap.cpp.

158  {
160 }
@ 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
jlong jint jint jint jint pixelFormat

◆ isFloat() [2/2]

bool AbstractBitmap::isFloat ( PixelFormat  pixelFormat)
static

Returns true if a given pixel format corresponds to floating point values, false otherwise.

Definition at line 163 of file abstract_bitmap.cpp.

163  {
165 }
@ SingleFloat
single channel of 32 bits per pixel (like grayscale), single precision floating point values
@ QuadFloat
4 channels of 32 bits per pixel, single precision floating point values,
@ TripleFloat
3 channels of 32 bits per pixel, single precision floating point values

◆ isMask() [2/2]

bool AbstractBitmap::isMask ( PixelFormat  pixelFormat)
static

Returns true if a given pixel format corresponds to a mask, false otherwise.

Definition at line 168 of file abstract_bitmap.cpp.

168  {
170 }
@ QuaternaryMask
2 bits per pixel
@ BinaryMask
1 bit per pixel
@ HexMask
4 bits per pixel

◆ toString()

std::string AbstractBitmap::toString ( ) const

Retruns a string describing the bitmap.

Definition at line 173 of file abstract_bitmap.cpp.

173  {
174  std::string desc = std::to_string(getWidth()) + "x" + std::to_string(getHeight()) + " " + PIXEL_FORMAT_NAMES[getPixelFormat()];
176  desc += " stored on CPU+GPU";
177  else
179  desc += " stored on CPU";
180  else
182  desc += " stored on GPU";
183  else
184  desc += " dirty";
185  return desc;
186 }
static const char * PIXEL_FORMAT_NAMES[NUM_PIXEL_FORMATS]
pixel format names
bool isUpToDate(ProcessingTarget) const
std::string to_string(Beatmup::NNets::ActivationFunction function)

◆ saveBmp()

void AbstractBitmap::saveBmp ( const char *  filename)

Saves the bitmap to a BMP file.

Definition at line 189 of file abstract_bitmap.cpp.

189  {
191  // Grab output bitmap from GPU memory to RAM
192  Swapper::pullPixels(*this);
193  }
194 
195  lockPixelData();
196 
198  getData(0, 0),
199  getWidth(), getHeight(),
200  getBitsPerPixel(),
201  filename
202  );
203 
204  unlockPixelData();
205 }
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
static void pullPixels(AbstractBitmap &bitmap)
Copies bitmap from GPU memory to RAM.
Definition: swapper.cpp:48
JNIEnv jlong jstring filename

Friends And Related Function Documentation

◆ GraphicPipeline

friend class GraphicPipeline
friend

Definition at line 93 of file abstract_bitmap.h.

◆ BitmapContentLock

friend class BitmapContentLock
friend

Definition at line 94 of file abstract_bitmap.h.

◆ AbstractBitmap::ReadLock

friend class AbstractBitmap::ReadLock
friend

Definition at line 95 of file abstract_bitmap.h.

◆ AbstractBitmap::WriteLock< ProcessingTarget::CPU >

friend class AbstractBitmap::WriteLock< ProcessingTarget::CPU >
friend

Definition at line 95 of file abstract_bitmap.h.

◆ AbstractBitmap::WriteLock< ProcessingTarget::GPU >

friend class AbstractBitmap::WriteLock< ProcessingTarget::GPU >
friend

Definition at line 95 of file abstract_bitmap.h.

Member Data Documentation

◆ ctx

Context& Beatmup::AbstractBitmap::ctx
protected

context managing this bitmap

Definition at line 102 of file abstract_bitmap.h.

◆ upToDate

bool Beatmup::AbstractBitmap::upToDate[2]
protected

bitmap up-to-date state on CPU and GPU

Definition at line 103 of file abstract_bitmap.h.

◆ NUM_PIXEL_FORMATS

const int Beatmup::AbstractBitmap::NUM_PIXEL_FORMATS = 9
static

Definition at line 121 of file abstract_bitmap.h.

◆ PIXEL_FORMAT_NAMES

const char * AbstractBitmap::PIXEL_FORMAT_NAMES
static
Initial value:
= {
"single byte", "triple byte", "quad byte",
"single floating point", "triple floating point", "quad floating point",
"binary mask", "quaternary mask", "hexadecimal mask"
}

pixel format names

Definition at line 122 of file abstract_bitmap.h.

◆ CHANNELS_PER_PIXEL

const unsigned char AbstractBitmap::CHANNELS_PER_PIXEL
static
Initial value:
= {
1, 3, 4,
1, 3, 4,
1, 1, 1
}

number of channels for each pixel format

Definition at line 123 of file abstract_bitmap.h.

◆ BITS_PER_PIXEL

const unsigned char AbstractBitmap::BITS_PER_PIXEL
static
Initial value:
= {
8, 8*3, 8*4,
32, 32*3, 32*4,
1, 2, 4
}

number of bits per pixel for each pixel format

Definition at line 124 of file abstract_bitmap.h.


The documentation for this class was generated from the following files: