Beatmup
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 "bitmap.h"
20 #include "../../core/bitmap/abstract_bitmap.h"
21 #include <core/exception.h>
22 #include <core/gpu/pipeline.h>
23 
24 using namespace Beatmup;
25 using namespace Android;
26 
27 
28 JNIEnv* Bitmap::getEnv() const {
29  JNIEnv *env;
30  jvm->AttachCurrentThread(&env, NULL);
31  return env;
32 }
33 
34 
36  AbstractBitmap(ctx), lockedPixels(NULL)
37 {
38  jenv->GetJavaVM(&this->jvm);
39  this->bitmap = jenv->NewGlobalRef(bitmap);
40 
41  // checking bitmap format
42  AndroidBitmapFormat format = (AndroidBitmapFormat) getInfo().format;
43  if (
44  format != ANDROID_BITMAP_FORMAT_RGBA_8888 &&
45  format != ANDROID_BITMAP_FORMAT_A_8
46  )
47  throw RuntimeError("Pixel format is not supported");
48 }
49 
50 
52  getEnv()->DeleteGlobalRef(bitmap);
53 }
54 
55 
56 AndroidBitmapInfo Bitmap::getInfo() const {
57  AndroidBitmapInfo info;
58  int result = AndroidBitmap_getInfo(getEnv(), bitmap, &info);
59  if (result < 0)
60  throw RuntimeError("AndroidBitmap_getInfo() failed");
61  return info;
62 }
63 
64 
66  if (lockedPixels)
67  return lockedPixelFormat;
68 
69  AndroidBitmapFormat format = (AndroidBitmapFormat) getInfo().format;
70  switch (format) {
71  case ANDROID_BITMAP_FORMAT_RGBA_8888:
72  return QuadByte;
73  case ANDROID_BITMAP_FORMAT_A_8:
74  return SingleByte;
75  default:
76  Insanity::insanity("AndroidBitmap_getInfo() returned unexpected format");
77  return SingleByte;
78  }
79 }
80 
81 
82 const int Bitmap::getWidth() const {
83  if (lockedPixels)
84  return lockedWidth;
85  return getInfo().width;
86 }
87 
88 
89 const int Bitmap::getHeight() const {
90  if (lockedPixels)
91  return lockedHeight;
92  return getInfo().height;
93 }
94 
95 
101  int result = AndroidBitmap_lockPixels(getEnv(), bitmap, &lockedPixels);
102  if (result < 0)
103  throw RuntimeError("AndroidBitmap_lockPixels() failed with error");
104 }
105 
106 
109  AndroidBitmap_unlockPixels(getEnv(), bitmap);
110  lockedPixels = nullptr;
111 }
112 
113 
114 const pixbyte* Bitmap::getData(int x, int y) const {
115  RuntimeError::check(lockedPixels, "No pixel data available. Forget to lock the bitmap?");
116  if (x < 0 || y < 0 || x >= lockedWidth || y >= lockedHeight)
117  return nullptr;
118  msize n = y * lockedWidth + x;
119  return (pixbyte*)( (unsigned char*)lockedPixels + n * BITS_PER_PIXEL[lockedPixelFormat] / 8 );
120 }
121 
122 
124  RuntimeError::check(lockedPixels, "No pixel data available. Forget to lock the bitmap?");
125  if (x < 0 || y < 0 || x >= lockedWidth || y >= lockedHeight)
126  return nullptr;
127  msize n = y * lockedWidth + x;
128  return (pixbyte*)( (unsigned char*)lockedPixels + n * BITS_PER_PIXEL[lockedPixelFormat] / 8 );
129 }
130 
131 
133  return getWidth() * getHeight() * BITS_PER_PIXEL[ getPixelFormat() ] / 8;
134 }
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
JNIEnv * getEnv() const
Definition: bitmap.cpp:28
Bitmap(Beatmup::Context &, JNIEnv *, jobject)
Creates the bitmap from Android Bitmap java object.
const int getWidth() const
Width of the texture in pixels.
Definition: bitmap.cpp:82
const PixelFormat getPixelFormat() const
Pixel format of the bitmap.
Definition: bitmap.cpp:65
const msize getMemorySize() const
Bitmap size in bytes.
Definition: bitmap.cpp:132
void * lockedPixels
pixel buffer; available only after calling lockPixelData()
Definition: bitmap.h:39
JavaVM * jvm
java environment
Definition: bitmap.h:37
PixelFormat lockedPixelFormat
Definition: bitmap.h:41
AndroidBitmapInfo getInfo() const
Definition: bitmap.cpp:56
jobject bitmap
java object representing the bitmap
Definition: bitmap.h:36
const pixbyte * getData(int x, int y) const
Returns a pointer to given pixel.
Definition: bitmap.cpp:114
void unlockPixelData()
Unlocks access to the CPU memory buffer containing pixel data.
Definition: bitmap.cpp:107
void lockPixelData()
Locks access to the CPU memory buffer containing pixel data.
Definition: bitmap.cpp:96
const int getHeight() const
Height of the texture in pixels.
Definition: bitmap.cpp:89
Android context.
Definition: context.h:32
static void insanity(const char *message)
Definition: exception.h:136
static void check(const bool condition, const std::string &message)
Definition: exception.h:64
#define BEATMUP_ASSERT_DEBUG(C)
Definition: exception.h:163
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
JNIEnv * jenv
JNIEnv jobject
JNIEnv jobject jint format
jobject jlong jint jint y
Beatmup::Context * ctx
Beatmup::IntPoint result
jobject jlong jint x
Beatmup::InternalBitmap * bitmap
int n