Beatmup
Beatmup::Python::Bitmap Class Reference

Wrapper of Android.Graphics.Bitmap object. More...

#include <bitmap.h>

Inheritance diagram for Beatmup::Python::Bitmap:
Beatmup::AbstractBitmap Beatmup::GL::TextureHandler Beatmup::Object

Public Member Functions

 Bitmap (Beatmup::Context &context, pybind11::buffer &buffer)
 Creates the bitmap from Android Bitmap java object. More...
 
const PixelFormat getPixelFormat () const
 Pixel format of the bitmap. More...
 
const int getWidth () const
 Width of the texture in pixels. More...
 
const int getHeight () const
 Height of the texture in pixels. More...
 
const pixbytegetData (int x, int y) const
 Returns a pointer to given pixel. More...
 
pixbytegetData (int x, int y)
 
const msize getMemorySize () const
 Bitmap size in bytes. More...
 
pybind11::buffer_info getPythonBuffer ()
 Returns a mutable python buffer containing bitmap data. More...
 
- Public Member Functions inherited from Beatmup::AbstractBitmap
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...
 
bool isUpToDate (ProcessingTarget) const
 
bool isDirty () const
 Returns true if the bitmap does not contain any valid content. More...
 
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 ()
 
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 ()
 

Private Member Functions

void lockPixelData ()
 Locks access to the CPU memory buffer containing pixel data. More...
 
void unlockPixelData ()
 Unlocks access to the CPU memory buffer containing pixel data. More...
 

Private Attributes

pybind11::buffer_info data
 
PixelFormat format
 
int strideBytes
 

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...
 
- Static Public Member Functions inherited from Beatmup::AbstractBitmap
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 inherited from Beatmup::AbstractBitmap
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 inherited from Beatmup::AbstractBitmap
 AbstractBitmap (Context &ctx)
 
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 inherited from Beatmup::AbstractBitmap
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
 

Detailed Description

Wrapper of Android.Graphics.Bitmap object.

Definition at line 35 of file bitmap.h.

Constructor & Destructor Documentation

◆ Bitmap()

Python::Bitmap::Bitmap ( Beatmup::Context context,
pybind11::buffer &  buffer 
)

Creates the bitmap from Android Bitmap java object.

Definition at line 25 of file bitmap.cpp.

25  :
26  AbstractBitmap(context)
27 {
28  auto info = buffer.request();
29 
30  // check if empty
31  if (info.size == 0)
32  throw std::invalid_argument("A bitmap cannot be empty");
33 
34  // check dimensions
35  if (info.ndim < 3)
36  throw std::invalid_argument("A bitmap is expected to have at least 3 dimensions");
37  else
38  for (int i = 0; i + 3 < info.ndim; ++i)
39  if (info.shape[i] != 1)
40  throw std::invalid_argument("A bitmap is expected to have at most 3 inner non-singleton dimensions.");
41 
42  // check pixel format
43  static const char* FORMAT_UINT8 = "B";
44  static const char* FORMAT_FLOAT = "f";
45  const auto numChannels = info.shape[info.ndim - 1];
46 
47  if (info.format == FORMAT_UINT8)
48  switch (numChannels) {
49  case 1: format = PixelFormat::SingleByte; break;
50  case 3: format = PixelFormat::TripleByte; break;
51  case 4: format = PixelFormat::QuadByte; break;
52  default:
53  throw std::invalid_argument("A bitmap may have 1, 3 of 4 channels, but got " + std::to_string(numChannels));
54  }
55  else if (info.format == FORMAT_FLOAT)
56  switch (numChannels) {
57  case 1: format = PixelFormat::SingleFloat; break;
58  case 3: format = PixelFormat::TripleFloat; break;
59  case 4: format = PixelFormat::QuadFloat; break;
60  default:
61  throw std::invalid_argument("A bitmap may have 1, 3 of 4 channels, but got " + std::to_string(numChannels));
62  }
63  else
64  throw std::invalid_argument("Unsupported data format. Expected bitmap values of type float32 or uint8.");
65 
66  // check stride
67  const int bps = getBitsPerPixel() / 8;
68  strideBytes = info.shape[info.ndim - 2] * bps;
69  if (info.strides[info.ndim - 1] * numChannels != bps || info.strides[info.ndim - 2] != bps || info.strides[info.ndim - 3] != strideBytes)
70  throw std::invalid_argument("Strided bitmaps are not supported.");
71 
72  // GOOOD.
73  data = std::move(info);
74 }
const unsigned char getBitsPerPixel() const
Returns number of bits per pixel stored in each bitmap.
AbstractBitmap(const AbstractBitmap &that)=delete
disabling copying constructor
pybind11::buffer_info data
Definition: bitmap.h:37
PixelFormat format
Definition: bitmap.h:38
@ SingleByte
single channel of 8 bits per pixel (like grayscale), unsigned integer values
@ 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
@ 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
std::string to_string(Beatmup::NNets::ActivationFunction function)

Member Function Documentation

◆ lockPixelData()

void Beatmup::Python::Bitmap::lockPixelData ( )
inlineprivatevirtual

Locks access to the CPU memory buffer containing pixel data.

Implements Beatmup::AbstractBitmap.

Definition at line 41 of file bitmap.h.

41 {}

◆ unlockPixelData()

void Beatmup::Python::Bitmap::unlockPixelData ( )
inlineprivatevirtual

Unlocks access to the CPU memory buffer containing pixel data.

Implements Beatmup::AbstractBitmap.

Definition at line 42 of file bitmap.h.

42 {}

◆ getPixelFormat()

const PixelFormat Beatmup::Python::Bitmap::getPixelFormat ( ) const
inlinevirtual

Pixel format of the bitmap.

Implements Beatmup::AbstractBitmap.

Definition at line 50 of file bitmap.h.

50 { return format; };

◆ getWidth()

const int Beatmup::Python::Bitmap::getWidth ( ) const
inlinevirtual

Width of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 52 of file bitmap.h.

52 { return (int)data.shape[data.ndim - 2]; }

◆ getHeight()

const int Beatmup::Python::Bitmap::getHeight ( ) const
inlinevirtual

Height of the texture in pixels.

Implements Beatmup::GL::TextureHandler.

Definition at line 54 of file bitmap.h.

54 { return (int)data.shape[data.ndim - 3]; }

◆ getData() [1/2]

const pixbyte * Python::Bitmap::getData ( int  x,
int  y 
) const
virtual

Returns a pointer to given pixel.

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

Implements Beatmup::AbstractBitmap.

Definition at line 77 of file bitmap.cpp.

77  {
78  return static_cast<const pixbyte*>(data.ptr) + y * strideBytes + x;
79 }
uint8_t pixbyte
Definition: basic_types.h:34
jobject jlong jint jint y
jobject jlong jint x

◆ getData() [2/2]

pixbyte * Python::Bitmap::getData ( int  x,
int  y 
)
virtual

Implements Beatmup::AbstractBitmap.

Definition at line 82 of file bitmap.cpp.

82  {
83  return static_cast<pixbyte*>(data.ptr) + y * strideBytes + x;
84 }

◆ getMemorySize()

const msize Python::Bitmap::getMemorySize ( ) const
virtual

Bitmap size in bytes.

Implements Beatmup::AbstractBitmap.

Definition at line 87 of file bitmap.cpp.

87  {
88  return data.itemsize * data.size;
89 }

◆ getPythonBuffer()

pybind11::buffer_info Beatmup::Python::Bitmap::getPythonBuffer ( )
inline

Returns a mutable python buffer containing bitmap data.

Definition at line 64 of file bitmap.h.

64  {
65 #ifdef BEATMUP_DEBUG
66  DebugAssertion::check(isUpToDate(ProcessingTarget::CPU), "Returning a buffer for outdated Python bitmap");
67 #endif
68  return pybind11::buffer_info(data.ptr, data.itemsize, data.format, data.ndim, data.shape, data.strides, false);
69  }
bool isUpToDate(ProcessingTarget) const

Member Data Documentation

◆ data

pybind11::buffer_info Beatmup::Python::Bitmap::data
private

Definition at line 37 of file bitmap.h.

◆ format

PixelFormat Beatmup::Python::Bitmap::format
private

Definition at line 38 of file bitmap.h.

◆ strideBytes

int Beatmup::Python::Bitmap::strideBytes
private

Definition at line 39 of file bitmap.h.


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