Attempting to publish a tree

Dependencies:   BLE_API mbed-dev-bin nRF51822

Fork of microbit-dal by Lancaster University

Embed: (wiki syntax)

« Back to documentation index

MicroBitImage Class Reference

MicroBitImage Class Reference

Class definition for a MicroBitImage. More...

#include <MicroBitImage.h>

Public Member Functions

ImageData * leakData ()
 Get current ptr, do not decr() it, and set the current instance to empty image.
uint8_t * getBitmap ()
 Return a 2D array representing the bitmap image.
 MicroBitImage (ImageData *ptr)
 Constructor.
 MicroBitImage ()
 Default Constructor.
 MicroBitImage (const MicroBitImage &image)
 Copy Constructor.
 MicroBitImage (const char *s)
 Constructor.
 MicroBitImage (const int16_t x, const int16_t y)
 Constructor.
 MicroBitImage (const int16_t x, const int16_t y, const uint8_t *bitmap)
 Constructor.
 ~MicroBitImage ()
 Destructor.
MicroBitImageoperator= (const MicroBitImage &i)
 Copy assign operation.
bool operator== (const MicroBitImage &i)
 Equality operation.
void clear ()
 Resets all pixels in this image to 0.
int setPixelValue (int16_t x, int16_t y, uint8_t value)
 Sets the pixel at the given co-ordinates to a given value.
int getPixelValue (int16_t x, int16_t y)
 Retreives the value of a given pixel.
int printImage (int16_t x, int16_t y, const uint8_t *bitmap)
 Replaces the content of this image with that of a given 2D array representing the image.
int paste (const MicroBitImage &image, int16_t x=0, int16_t y=0, uint8_t alpha=0)
 Pastes a given bitmap at the given co-ordinates.
int print (char c, int16_t x=0, int16_t y=0)
 Prints a character to the display at the given location.
int shiftLeft (int16_t n)
 Shifts the pixels in this Image a given number of pixels to the left.
int shiftRight (int16_t n)
 Shifts the pixels in this Image a given number of pixels to the right.
int shiftUp (int16_t n)
 Shifts the pixels in this Image a given number of pixels to upward.
int shiftDown (int16_t n)
 Shifts the pixels in this Image a given number of pixels to downward.
int getWidth () const
 Gets the width of this image.
int getHeight () const
 Gets the height of this image.
int getSize () const
 Gets number of bytes in the bitmap, ie., width * height.
ManagedString toString ()
 Converts the bitmap to a csv ManagedString.
MicroBitImage crop (int startx, int starty, int finx, int finy)
 Crops the image to the given dimensions.
bool isReadOnly ()
 Check if image is read-only (i.e., residing in flash).
MicroBitImage clone ()
 Create a copy of the image bitmap.

Detailed Description

Class definition for a MicroBitImage.

An MicroBitImage is a simple bitmap representation of an image. n.b. This is a mutable, managed type.

Definition at line 47 of file MicroBitImage.h.


Constructor & Destructor Documentation

MicroBitImage ( ImageData *  p )

Constructor.

Create an image from a specially prepared constant array, with no copying. Will call ptr->incr().

Parameters:
ptrThe literal - first two bytes should be 0xff, then width, 0, height, 0, and the bitmap. Width and height are 16 bit. The literal has to be 4-byte aligned.
 static const uint8_t heart[] __attribute__ ((aligned (4))) = { 0xff, 0xff, 10, 0, 5, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i((ImageData*)(void*)heart);

Definition at line 208 of file MicroBitImage.cpp.

Default Constructor.

Creates a new reference to the empty MicroBitImage bitmap

 MicroBitImage i(); //an empty image instance

Definition at line 55 of file MicroBitImage.cpp.

MicroBitImage ( const MicroBitImage image )

Copy Constructor.

Add ourselves as a reference to an existing MicroBitImage.

Parameters:
imageThe MicroBitImage to reference.
 MicroBitImage i("0,1,0,1,0\n");
 MicroBitImage i2(i); //points to i

Definition at line 97 of file MicroBitImage.cpp.

MicroBitImage ( const char *  s ) [explicit]

Constructor.

Create a blank bitmap representation of a given size.

Parameters:
sA text based representation of the image given whitespace delimited numeric values.
 MicroBitImage i("0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n"); // 5x5 image

Definition at line 113 of file MicroBitImage.cpp.

MicroBitImage ( const int16_t  x,
const int16_t  y 
)

Constructor.

Create a blank bitmap representation of a given size.

Parameters:
xthe width of the image.
ythe height of the image.

Bitmap buffer is linear, with 8 bits per pixel, row by row, top to bottom with no word alignment. Stride is therefore the image width in pixels. in where w and h are width and height respectively, the layout is therefore:

|[0,0]...[w,o][1,0]...[w,1] ... [[w,h]

A copy of the image is made in RAM, as images are mutable.

Create a blank bitmap representation of a given size.

Parameters:
xthe width of the image.
ythe height of the image.

Bitmap buffer is linear, with 8 bits per pixel, row by row, top to bottom with no word alignment. Stride is therefore the image width in pixels. in where w and h are width and height respectively, the layout is therefore:

|[0,0]...[w,o][1,0]...[w,1] ... [[w,h]

A copy of the image is made in RAM, as images are mutable.

TODO: Consider an immutable flavour, which might save us RAM for animation spritesheets... ...as these could be kept in FLASH.

Definition at line 81 of file MicroBitImage.cpp.

MicroBitImage ( const int16_t  x,
const int16_t  y,
const uint8_t *  bitmap 
)

Constructor.

Create a bitmap representation of a given size, based on a given buffer.

Parameters:
xthe width of the image.
ythe height of the image.
bitmapa 2D array representing the image.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);

Definition at line 242 of file MicroBitImage.cpp.

~MicroBitImage (  )

Destructor.

Removes buffer resources held by the instance.

Definition at line 252 of file MicroBitImage.cpp.


Member Function Documentation

void clear (  )

Resets all pixels in this image to 0.

 MicroBitImage i("0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n"); // 5x5 image
 i.clear();

Definition at line 365 of file MicroBitImage.cpp.

MicroBitImage clone (  )

Create a copy of the image bitmap.

Used particularly, when isReadOnly() is true.

Returns:
an instance of MicroBitImage which can be modified independently of the current instance

Definition at line 884 of file MicroBitImage.cpp.

MicroBitImage crop ( int  startx,
int  starty,
int  cropWidth,
int  cropHeight 
)

Crops the image to the given dimensions.

Parameters:
startxthe location to start the crop in the x-axis
startythe location to start the crop in the y-axis
widththe width of the desired cropped region
heightthe height of the desired cropped region
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);
 i.crop(0,0,2,2).toString() // "0,1\n1,1\n"

Definition at line 839 of file MicroBitImage.cpp.

uint8_t* getBitmap (  )

Return a 2D array representing the bitmap image.

Definition at line 81 of file MicroBitImage.h.

int getHeight (  ) const

Gets the height of this image.

Returns:
The height of this image.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);
 i.getHeight(); // equals 5...

Definition at line 416 of file MicroBitImage.h.

int getPixelValue ( int16_t  x,
int16_t  y 
)

Retreives the value of a given pixel.

Parameters:
xThe x co-ordinate of the pixel to read. Must be within the dimensions of the image.
yThe y co-ordinate of the pixel to read. Must be within the dimensions of the image.
Returns:
The value assigned to the given pixel location (the brightness level 0-255), or MICROBIT_INVALID_PARAMETER.
 MicroBitImage i("0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n"); // 5x5 image
 i.getPixelValue(0,0); //should be 0;

Definition at line 412 of file MicroBitImage.cpp.

int getSize (  ) const

Gets number of bytes in the bitmap, ie., width * height.

Returns:
The size of the bitmap.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);
 i.getSize(); // equals 50...

Definition at line 432 of file MicroBitImage.h.

int getWidth (  ) const

Gets the width of this image.

Returns:
The width of this image.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);
 i.getWidth(); // equals 10...

Definition at line 400 of file MicroBitImage.h.

bool isReadOnly (  )

Check if image is read-only (i.e., residing in flash).

Definition at line 874 of file MicroBitImage.cpp.

ImageData * leakData (  )

Get current ptr, do not decr() it, and set the current instance to empty image.

This is to be used by specialized runtimes which pass ImageData around.

Definition at line 219 of file MicroBitImage.cpp.

MicroBitImage & operator= ( const MicroBitImage i )

Copy assign operation.

Called when one MicroBitImage is assigned the value of another using the '=' operator.

Decrement our reference count and free up the buffer as necessary.

Then, update our buffer to refer to that of the supplied MicroBitImage, and increase its reference count.

Parameters:
sThe MicroBitImage to reference.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);
 MicroBitImage i1();
 i1 = i; // i1 now references i

Definition at line 318 of file MicroBitImage.cpp.

bool operator== ( const MicroBitImage i )

Equality operation.

Called when one MicroBitImage is tested to be equal to another using the '==' operator.

Parameters:
iThe MicroBitImage to test ourselves against.
Returns:
true if this MicroBitImage is identical to the one supplied, false otherwise.
 MicroBitDisplay display;
 MicroBitImage i();
 MicroBitImage i1();

 if(i == i1) //will be true
     display.scroll("true");

Definition at line 348 of file MicroBitImage.cpp.

int paste ( const MicroBitImage image,
int16_t  x = 0,
int16_t  y = 0,
uint8_t  alpha = 0 
)

Pastes a given bitmap at the given co-ordinates.

Any pixels in the relvant area of this image are replaced.

Parameters:
imageThe MicroBitImage to paste.
xThe leftmost X co-ordinate in this image where the given image should be pasted. Defaults to 0.
yThe uppermost Y co-ordinate in this image where the given image should be pasted. Defaults to 0.
alphaset to 1 if transparency clear pixels in given image should be treated as transparent. Set to 0 otherwise. Defaults to 0.
Returns:
The number of pixels written.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart); // a big heart
 i.paste(i, -5, 0); // a small heart

Definition at line 490 of file MicroBitImage.cpp.

int print ( char  c,
int16_t  x = 0,
int16_t  y = 0 
)

Prints a character to the display at the given location.

Parameters:
cThe character to display.
xThe x co-ordinate of on the image to place the top left of the character. Defaults to 0.
yThe y co-ordinate of on the image to place the top left of the character. Defaults to 0.
Returns:
MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
 MicroBitImage i(5,5);
 i.print('a');

Definition at line 566 of file MicroBitImage.cpp.

int printImage ( int16_t  width,
int16_t  height,
const uint8_t *  bitmap 
)

Replaces the content of this image with that of a given 2D array representing the image.

Parameters:
xthe width of the image. Must be within the dimensions of the image.
ythe width of the image. Must be within the dimensions of the image.
bitmapa 2D array representing the image.
Returns:
MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i();
 i.printImage(0,0,heart);
Note:
all coordinates originate from the top left of an image.

Definition at line 441 of file MicroBitImage.cpp.

int setPixelValue ( int16_t  x,
int16_t  y,
uint8_t  value 
)

Sets the pixel at the given co-ordinates to a given value.

Parameters:
xThe co-ordinate of the pixel to change.
yThe co-ordinate of the pixel to change.
valueThe new value of the pixel (the brightness level 0-255)
Returns:
MICROBIT_OK, or MICROBIT_INVALID_PARAMETER.
 MicroBitImage i("0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n1,0,1,0,1\n0,1,0,1,0\n"); // 5x5 image
 i.setPixelValue(0,0,255);
Note:
all coordinates originate from the top left of an image.

Definition at line 388 of file MicroBitImage.cpp.

int shiftDown ( int16_t  n )

Shifts the pixels in this Image a given number of pixels to downward.

Parameters:
nThe number of pixels to shift.
Returns:
MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);
 i.shiftDown(1);

Definition at line 739 of file MicroBitImage.cpp.

int shiftLeft ( int16_t  n )

Shifts the pixels in this Image a given number of pixels to the left.

Parameters:
nThe number of pixels to shift.
Returns:
MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart); // a big heart
 i.shiftLeft(5); // a small heart

Definition at line 616 of file MicroBitImage.cpp.

int shiftRight ( int16_t  n )

Shifts the pixels in this Image a given number of pixels to the right.

Parameters:
nThe number of pixels to shift.
Returns:
MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart); // a big heart
 i.shiftLeft(5); // a small heart
 i.shiftRight(5); // a big heart

Definition at line 655 of file MicroBitImage.cpp.

int shiftUp ( int16_t  n )

Shifts the pixels in this Image a given number of pixels to upward.

Parameters:
nThe number of pixels to shift.
Returns:
MICROBIT_OK on success, or MICROBIT_INVALID_PARAMETER.
 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);
 i.shiftUp(1);

Definition at line 694 of file MicroBitImage.cpp.

ManagedString toString (  )

Converts the bitmap to a csv ManagedString.

 const uint8_t heart[] = { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, }; // a cute heart
 MicroBitImage i(10,5,heart);
 uBit.serial.printString(i.toString()); // "0,1,0,1,0,0,0,0,0,0\n..."

Definition at line 780 of file MicroBitImage.cpp.