Moscrif API Docs for: 2012q3
Show:

Bitmap Class

Library: core

Bitmap class manages all operations with pictures. Using this class developer can load images from files and get theirs size, configuration etc.Bitmap class supports following formats (*.jpg, *.png)

Example:

function loadImage()
{
    // load bitmap file into memory
    this.bitmap = Bitmap.fromFile("dat://moscrif.jpg");
}

...

// draw bitmap inside onDrawFunction
app.onDraw = function(sender, canvas)
{
    canvas.drawBitmap(this.bitmap, 0, 0);
    // there are also other functions for bitmap drawing see canvas class
}

Item Index

Methods

Properties

Methods

fromBytes

(
  • data
)
Bitmap static

Loads image from byte array.

Parameters:

Returns:

Bitmap: Returns Bitmap object, with loaded picture.

Example:

function loadImage ()
{
    const HOST = "moscrif.com";
    const FILE = "/userfiles/moscrif2.png";

    // variable for downloaded image
    app.img = null;

    var request = new WebClient();

    request.open(HOST, 80, false, "");

    request.onReceived = function(sender) {
        // create bitmap from downloaded data
        app.img = Bitmap.fromBytes(this.data);
        app.invalidate();
    }

    request.getData(FILE);
}
….
app.onDraw = function(sender, canvas)
{
    // draw image, if it has been downloaded
    if(app.img)
        canvas.drawBitmap(app.img, 0, 0);
}

fromFile

(
  • file
)
Bitmap static

Function loads image from file. Supported types are:

  • .jpg - format uses lossy compression but does not support alpha channel. Frequently used for digital photography.
  • .png - bitmap format of image that employs lossless data compression. This format also support alpha channel.

Parameters:

  • file String

    File name of a picture to be opened.

Returns:

Bitmap: Returns Bitmap object, with loaded picture.

Example:

this._img = Bitmap.fromFile("app://img.png");
...
canvas.drawBitmap(this._img, 0, 0);

fromRect

(
  • width
  • height
  • [opaque=false]
)
Bitmap static

Function creates bitmap with specified dimension. Bitmap can be initially opaque.

Parameters:

  • width Integer

    Non-zero positive width for new bitmap object.

  • height Integer

    Non-zero positive height for new bitmap object.

  • [opaque=false] Boolean optional

    Specifies transparency for new bitmap object. True means totally opaque, false means totally transparent.

Returns:

Bitmap: Returns the new Bitmap object.

Example:

game.onStart = function()
{
    // creates offscreen bitmap
    this._bmp = Bitmap.fromRect(200, 100);
    // creates canvas for the ofscreen bitmap
    this._canvas = Canvas.fromBitmap(this._bmp);
    // draws to bitmap - can be something more complex
    this._canvas.drawLine(0, 0, 100, 100, new Paint());
}
game.onDraw = function(canvas)
{
    canvas.clear(0xffffffff);
    // draws the offscreen drawn bitmap
    canvas.drawBitmap(this._bmp, 100, 100);
}

fromScreen

() Bitmap static

Function creates bitmap object from screen. THe bitmap is same as a current image on he screen.

Returns:

Bitmap: Returns Bitmap object, with loaded picture.

Example:

game.onPointerPressed = function()
{
    // make screenshot
    this._bitmap = Bitmap.fromScreen();
    // save screen shot
    this._bitmap.toFile("data://screenshot.jpg");
}

getSize

(
  • file
)
Multivalue static

Returns size of image file in pixels without loading an image.

Parameters:

Returns:

Multivalue: A pair of values. The first value is width and second is height in pixels.

Example:

// get image dimensions before loading
var (w, h) = Bitmap.getSize("app://img.png");
// w: 640 h: 960
console<<"w: "<<w<<" h: "<<h<<"\n";

reset

() chainable

Reset all values in object, included loaded image, height and width.

resize

(
  • param1
  • param2
)
Bitmap chainable

Resizes bitmap according to required width and height.

Parameters:

  • param1 Integer

    Required width of bitmap in pixels.

  • param2 Integer

    Required height of bitmap in pixels.

Returns:

Bitmap: Returns resized bitmap.

Example:

// resize bitmap
this._img = Bitmap.fromFile("app://img.png");
this._img = this._img.resize(System.width, System.height);
.......
canvas.drawBitmap(this._img, 0, 0);

toFile

(
  • file
)
Bitmap chainable

Save bitmap to the image file. Supported types are:

  • .jpg - format uses lossy compression but does not support alpha channel. Frequently used for digital photography.
  • .png - bitmap format of image that employs lossless data compression. This format also support alpha channel.

Parameters:

  • file String

    Name of a new image file.

Returns:

Bitmap: Returns Bitmap object, with loaded picture.

Example:

 game.onPointerPressed = function()
 {
     // make screenshot
     this._bitmap = Bitmap.fromScreen();
     // save screen shot
     this._bitmap.toFile("data://screenshot.jpg");
 }

Properties

config

Integer

Returns a number representating bitmap's configuration. With this function, a developer can get a number of bits per pixel and also a color model.

  • 0 - bitmap has not been configured
  • 1 - 1-bit per pixel, (0 is transparent, 1 is opaque)
  • 2 - 8-bits per pixel, with only alpha specified (0 is transparent, 0xFF is opaque)
  • 3 - 8-bits per pixel, using SkColorTable to specify the colors
  • 4 - 16-bits per pixel (RGB)
  • 5 - 16-bits per pixel (ARGB)
  • 6 - 32-bits per pixel (ARGB)

height

Integer

Height of loaded image.

opaque

Integer

Opaque

width

Integer

Width of loaded image.