Beatmup
Beatmup::BitmapTools Namespace Reference

Set of handy operations with images. More...

Functions

InternalBitmapmakeCopy (AbstractBitmap &bitmap)
 Makes a copy of a bitmap. More...
 
InternalBitmapmakeCopy (AbstractBitmap &bitmap, PixelFormat pixelFormat)
 Makes a copy of a bitmap converting the data to a given pixel format. More...
 
InternalBitmapmakeCopy (AbstractBitmap &bitmap, Context &context, PixelFormat pixelFormat)
 Makes a copy of a bitmap for a given Context converting the data to a given pixel format. More...
 
InternalBitmapchessboard (Context &context, int width, int height, int cellSize, PixelFormat pixelFormat=BinaryMask)
 Renders a chessboard image. More...
 
void noise (AbstractBitmap &bitmap, IntRectangle area)
 Replaces a rectangular area in a bitmap by random noise. More...
 
void noise (AbstractBitmap &bitmap)
 Fills a given bitmap with random noise. More...
 
void makeOpaque (AbstractBitmap &bitmap, IntRectangle area)
 Makes a bitmap area opaque. More...
 
void invert (AbstractBitmap &input, AbstractBitmap &output)
 Inverses colors of an image in a pixelwise fashion. More...
 
IntPoint scanlineSearch (AbstractBitmap &source, pixint4 val, const IntPoint &startFrom)
 Goes through a bitmap in scanline order (left to right, top to bottom) until a pixel of a given color is met. More...
 
IntPoint scanlineSearch (AbstractBitmap &source, pixfloat4 val, const IntPoint &startFrom)
 

Detailed Description

Set of handy operations with images.

Function Documentation

◆ makeCopy() [1/3]

InternalBitmap * Beatmup::BitmapTools::makeCopy ( AbstractBitmap bitmap)

Makes a copy of a bitmap.

The copy is done in an AbstractTask run in the default thread pool of the context the bitmap is attached to.

Parameters
[in]bitmapThe bitmap to copy

Definition at line 72 of file tools.cpp.

72  {
73  return makeCopy(source, source.getPixelFormat());
74 }
InternalBitmap * makeCopy(AbstractBitmap &bitmap)
Makes a copy of a bitmap.
Definition: tools.cpp:72

◆ makeCopy() [2/3]

InternalBitmap * Beatmup::BitmapTools::makeCopy ( AbstractBitmap bitmap,
PixelFormat  pixelFormat 
)

Makes a copy of a bitmap converting the data to a given pixel format.

The copy is done in an AbstractTask run in the default thread pool of the context the bitmap is attached to.

Parameters
[in]bitmapThe bitmap to copy
[in]pixelFormatPixel format of the copy

Definition at line 77 of file tools.cpp.

77  {
78  return makeCopy(source, source.getContext(), newPixelFormat);
79 }

◆ makeCopy() [3/3]

InternalBitmap * Beatmup::BitmapTools::makeCopy ( AbstractBitmap bitmap,
Context context,
PixelFormat  pixelFormat 
)

Makes a copy of a bitmap for a given Context converting the data to a given pixel format.

Can be used to exchange image content between different instances of Context. The copy is done in an AbstractTask run in the default thread pool of the source bitmap context.

Parameters
[in]bitmapThe bitmap to copy
[in]contextThe Context instance the copy is associated with
[in]pixelFormatPixel format of the copy

Definition at line 82 of file tools.cpp.

82  {
83  InternalBitmap* copy = new Beatmup::InternalBitmap(context, newPixelFormat, source.getWidth(), source.getHeight());
84  FormatConverter converter;
85  converter.setBitmaps(&source, copy);
86  source.getContext().performTask(converter);
87  return copy;
88 }
Converts bitmap content from one pixel format to another one.
Definition: converter.h:28
void setBitmaps(AbstractBitmap *input, AbstractBitmap *output)
Definition: converter.cpp:43
Bitmap whose memory is managed by the Beatmup engine.
Beatmup::AbstractBitmap * copy

◆ chessboard()

InternalBitmap * Beatmup::BitmapTools::chessboard ( Context context,
int  width,
int  height,
int  cellSize,
PixelFormat  pixelFormat = BinaryMask 
)

Renders a chessboard image.

Parameters
[in]contextA Context instance
[in]widthWidth in pixels of the resulting bitmap
[in]heightHeight in pixels of the resulting bitmap
[in]cellSizeSize of a single chessboard cell in pixels
[in]pixelFormatPixel format of the resulting bitmap

Definition at line 91 of file tools.cpp.

91  {
92  RuntimeError::check(cellSize > 0, "Chessboard cell size must be positive");
95  BitmapProcessing::write<Kernels::ChessboardRendering>(*chess, width, height, cellSize);
96  return chess;
97 }
Makes a bitmap writable for a specific target device.
jlong jint jint jint jint pixelFormat
Beatmup::Context * ctx
jlong jint width
jlong jint jint height

◆ noise() [1/2]

void Beatmup::BitmapTools::noise ( AbstractBitmap bitmap,
IntRectangle  area 
)

Replaces a rectangular area in a bitmap by random noise.

Parameters
[in]bitmapThe bitmap
[in]areaThe area in pixels to process

Definition at line 100 of file tools.cpp.

100  {
102  const int n = bitmap.getNumberOfChannels();
103 
104  if (bitmap.isMask())
105  throw ImplementationUnsupported("Noise is not implemented for mask bitmaps");
106  // floating-point bitmap
107  else if (bitmap.isFloat())
108  for (int y = area.a.y; y <= area.b.y; ++y) {
109  pixfloat* p = (pixfloat*)bitmap.getData(area.a.x, y);
110  for (int x = area.a.x; x <= area.b.x; ++x)
111  for (int i = 0; i < n; ++i, ++p)
112  *p = (float)std::rand() / RAND_MAX;
113  }
114  // integer bitmap
115  else if (bitmap.isInteger()) {
116  for (int y = area.a.y; y <= area.b.y; ++y) {
117  pixbyte* p = bitmap.getData(area.a.x, y);
118  for (int x = area.a.x; x <= area.b.x; ++x)
119  for (int i = 0; i < n; ++i, ++p)
120  *p = std::rand() % 256;
121  }
122  }
123 }
const unsigned char getNumberOfChannels() const
Returns number of bytes per pixel stored in each bitmap.
bool isInteger() const
Returns true if the bitmap contains integer values, false otherwise.
bool isMask() const
Returns true if the bitmap is a mask, false otherwise.
bool isFloat() const
Returns true if the bitmap contains floating point values, false otherwise.
CustomPoint< numeric > b
Definition: geometry.h:131
CustomPoint< numeric > a
Definition: geometry.h:131
Exception thrown when an implementation restriction is encountered.
Definition: exception.h:124
const pixbyte * getData(int x, int y) const
Returns a pointer to given pixel.
uint8_t pixbyte
Definition: basic_types.h:34
float pixfloat
Definition: basic_types.h:35
jobject jlong jint jint y
jobject jlong jint x
Beatmup::InternalBitmap * bitmap
Beatmup::IntPoint p((int) x,(int) y)
int n

◆ noise() [2/2]

void Beatmup::BitmapTools::noise ( AbstractBitmap bitmap)

Fills a given bitmap with random noise.

Parameters
[in]bitmapThe bitmap to fill

Definition at line 126 of file tools.cpp.

126  {
127  noise(bitmap, IntRectangle(0, 0, bitmap.getWidth() - 1, bitmap.getHeight() - 1));
128 }
const int getHeight() const
Height of the texture in pixels.
const int getWidth() const
Width of the texture in pixels.
void noise(AbstractBitmap &bitmap, IntRectangle area)
Replaces a rectangular area in a bitmap by random noise.
Definition: tools.cpp:100
CustomRectangle< int > IntRectangle
Definition: geometry.h:630

◆ makeOpaque()

void Beatmup::BitmapTools::makeOpaque ( AbstractBitmap bitmap,
IntRectangle  area 
)

Makes a bitmap area opaque.

Applies for bitmaps having the alpha channel (of QuadByte and QuadFloat pixel formats). Bitmaps of other pixel formats remain unchanged.

Parameters
[in]bitmapThe bitmap
[in]areaThe area in pixels to process

Definition at line 131 of file tools.cpp.

131  {
133 
134  // floating-point bitmap
136  for (int y = area.a.y; y <= area.b.y; ++y) {
137  pixfloat* p = (pixfloat*)bitmap.getData(area.a.x, y);
138  p += CHANNELS_4.A;
139  *p = 1.0f;
140  for (int x = area.a.x + 1; x <= area.b.x; ++x)
141  *(p += 4) = 1.0f;
142  }
143  // integer bitmap
144  else if (bitmap.getPixelFormat() == QuadByte)
145  for (int y = area.a.y; y <= area.b.y; ++y) {
146  pixbyte* p = bitmap.getData(area.a.x, y);
147  p += CHANNELS_4.A;
148  *p = 255;
149  for (int x = area.a.x + 1; x <= area.b.x; ++x)
150  *(p += 4) = 255;
151  }
152 }
const PixelFormat getPixelFormat() const
Pixel format of the bitmap.
@ QuadFloat
4 channels of 32 bits per pixel, single precision floating point values,
@ QuadByte
4 channels of 8 bits per pixel (like RGBA), unsigned integer values
static const struct Beatmup::@1 CHANNELS_4

◆ invert()

void Beatmup::BitmapTools::invert ( AbstractBitmap input,
AbstractBitmap output 
)

Inverses colors of an image in a pixelwise fashion.

Parameters
[in]inputThe input image. Its content unchanged.
[in]outputThe output image.

Definition at line 155 of file tools.cpp.

155  {
156  RuntimeError::check(input.getWidth() == output.getWidth() && input.getHeight() <= output.getHeight(),
157  "Input size does not fit output size");
158  RuntimeError::check(input.getPixelFormat() == output.getPixelFormat(),
159  "Input/output pixel formats mismatch");
160 
162  AbstractBitmap::ReadLock* readLock = (&input == &output) ? nullptr : new AbstractBitmap::ReadLock(input);
163 
164 
165  const size_t NPIX = input.getSize().numPixels();
166  if (input.isFloat()) {
167  pixfloat
168  *pi = (pixfloat*)input.getData(0, 0),
169  *po = (pixfloat*)output.getData(0, 0);
170  const pixfloat* STOP = pi + NPIX * AbstractBitmap::CHANNELS_PER_PIXEL[input.getPixelFormat()];
171  while (pi < STOP)
172  *(po++) = 1 - *(pi++);
173  }
174  else {
175  const size_t N = NPIX * AbstractBitmap::BITS_PER_PIXEL[input.getPixelFormat()] / 8;
176  // fast integer inverse
178  *pi = (pixint_platform*)input.getData(0, 0),
179  *po = (pixint_platform*)output.getData(0, 0);
180  const pixint_platform* STOP = pi + N / sizeof(pixint_platform);
181  while (pi < STOP)
182  *(po++) = ~*(pi++);
183  // process remaining bytes
184  pixbyte
185  *ri = (pixbyte*)pi,
186  *ro = (pixbyte*)po;
187  for (int r = 0; r < N % sizeof(pixint_platform); ++r)
188  *(ro++) = ~*(ri++);
189  }
190 
191  delete readLock;
192 }
Locks a bitmap for reading on CPU.
virtual const PixelFormat getPixelFormat() const =0
Pixel format of the bitmap.
const ImageResolution getSize() const
Returns the bitmap resolution within ImageResolution object.
virtual const pixbyte * getData(int x, int y) const =0
Returns a pointer to given pixel.
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 float pi
Definition: basic_types.h:29
uint32_t pixint_platform
Definition: basic_types.h:31
jobject jlong jint jint jint r

◆ scanlineSearch() [1/2]

IntPoint Beatmup::BitmapTools::scanlineSearch ( AbstractBitmap source,
pixint4  val,
const IntPoint startFrom 
)

Goes through a bitmap in scanline order (left to right, top to bottom) until a pixel of a given color is met.

Parameters
[in]sourceThe bitmap to scan
[in]valThe color value to look for
[in]startFromStarting pixel position
Returns
the next closest position of the searched value (in scanline order) or (-1,-1) if not found.

Definition at line 195 of file tools.cpp.

195  {
197  AbstractBitmap::ReadLock lock(source);
198  BitmapProcessing::read<Kernels::ScanlineSearch>(source, val, startFrom, result);
199  return result;
200 }
Beatmup::IntPoint result
return(jlong) new Beatmup jlong jstring jint val

◆ scanlineSearch() [2/2]

IntPoint Beatmup::BitmapTools::scanlineSearch ( AbstractBitmap source,
pixfloat4  val,
const IntPoint startFrom 
)

Definition at line 203 of file tools.cpp.

203  {
205  AbstractBitmap::ReadLock lock(source);
206  BitmapProcessing::read<Kernels::ScanlineSearch>(source, val, startFrom, result);
207  return result;
208 }