B2G/gecko/browser/devtools/tilt/TiltGL.jsm
/**
   * Scales an image's width and height to next power of two.
   * If the image already has power of two sizes, it is immediately returned,
   * otherwise, a new image is created.
   *
   * @param {Image} aImage
   *                the image to be scaled
   * @param {Object} aProperties
   *                 an object containing the following properties:
   *         {Image} source: the source image to resize
   *       {Boolean} resize: true to resize the image if it has npot dimensions
   *        {String} fill: optional, color to fill the transparent bits
   *        {String} stroke: optional, color to draw an image outline
   *        {Number} strokeWeight: optional, the width of the outline
   *
   * @return {Image} the resized image
   */
Scales an image's width and height to next power of two.
If the image already has power of two sizes, it is immediately returned,
otherwise, a new image is created.



@return {Image} the resized image
Scales an image ' s width and height to next power of two . If the image already has power of two sizes it is immediately returned otherwise a new image is created . @return { Image } the resized image
function TGLTU_resizeImageToPowerOfTwo(aProperties)
  {
    aProperties = aProperties || {};
    if (!aProperties.source) {
      return null;
    }
    let isPowerOfTwoWidth = TiltMath.isPowerOfTwo(aProperties.source.width);
    let isPowerOfTwoHeight = TiltMath.isPowerOfTwo(aProperties.source.height);
    if (!aProperties.resize || (isPowerOfTwoWidth && isPowerOfTwoHeight)) {
      return aProperties.source;
    }
    let width = TiltMath.nextPowerOfTwo(aProperties.source.width);
    let height = TiltMath.nextPowerOfTwo(aProperties.source.height);
    let canvas = TiltUtils.DOM.initCanvas(null, {
      width: width,
      height: height
    });
    let ctx = canvas.getContext("2d");
    if (aProperties.fill) {
      ctx.fillStyle = aProperties.fill;
      ctx.fillRect(0, 0, width, height);
    }
    ctx.drawImage(aProperties.source, 0, 0, width, height);
    if (aProperties.stroke) {
      ctx.strokeStyle = aProperties.stroke;
      ctx.lineWidth = aProperties.strokeWeight;
      ctx.strokeRect(0, 0, width, height);
    }
    return canvas;
  }
function TGLTU_resizeImageToPowerOfTwo ( aProperties ) { aProperties = aProperties || { } ; if ( ! aProperties . source ) { return null ; } let isPowerOfTwoWidth = TiltMath . isPowerOfTwo ( aProperties . source . width ) ; let isPowerOfTwoHeight = TiltMath . isPowerOfTwo ( aProperties . source . height ) ; if ( ! aProperties . resize || ( isPowerOfTwoWidth && isPowerOfTwoHeight ) ) { return aProperties . source ; } let width = TiltMath . nextPowerOfTwo ( aProperties . source . width ) ; let height = TiltMath . nextPowerOfTwo ( aProperties . source . height ) ; let canvas = TiltUtils . DOM . initCanvas ( null , { width : width , height : height } ) ; let ctx = canvas . getContext ( "2d" ) ; if ( aProperties . fill ) { ctx . fillStyle = aProperties . fill ; ctx . fillRect ( 0 , 0 , width , height ) ; } ctx . drawImage ( aProperties . source , 0 , 0 , width , height ) ; if ( aProperties . stroke ) { ctx . strokeStyle = aProperties . stroke ; ctx . lineWidth = aProperties . strokeWeight ; ctx . strokeRect ( 0 , 0 , width , height ) ; } return canvas ; }
Scales an image's width and height to next power of two.
Scales an image ' s width and height to next power of two .
// make sure the properties argument is an object // first check if the image is not already power of two // calculate the power of two dimensions for the npot image // create a canvas, then we will use a 2d context to scale the image // optional fill (useful when handling transparent images) // draw the image with power of two dimensions // optional stroke (useful when creating textures for edges)