| 1 |
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- |
| 2 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 |
|
| 6 |
package org.mozilla.gecko.gfx; |
| 7 |
|
| 8 |
import org.json.JSONException; |
| 9 |
import org.json.JSONObject; |
| 10 |
|
| 11 |
public class IntSize { |
| 12 |
public final int width, height; |
| 13 |
|
| 14 |
public IntSize(IntSize size) { width = size.width; height = size.height; } |
| 15 |
public IntSize(int inWidth, int inHeight) { width = inWidth; height = inHeight; } |
| 16 |
|
| 17 |
public IntSize(FloatSize size) { |
| 18 |
width = Math.round(size.width); |
| 19 |
height = Math.round(size.height); |
| 20 |
} |
| 21 |
|
| 22 |
public IntSize(JSONObject json) { |
| 23 |
try { |
| 24 |
width = json.getInt("width"); |
| 25 |
height = json.getInt("height"); |
| 26 |
} catch (JSONException e) { |
| 27 |
throw new RuntimeException(e); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
public int getArea() { |
| 32 |
return width * height; |
| 33 |
} |
| 34 |
|
| 35 |
public boolean equals(IntSize size) { |
| 36 |
return ((size.width == width) && (size.height == height)); |
| 37 |
} |
| 38 |
|
| 39 |
public boolean isPositive() { |
| 40 |
return (width > 0 && height > 0); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public String toString() { return "(" + width + "," + height + ")"; } |
| 45 |
|
| 46 |
public IntSize scale(float factor) { |
| 47 |
return new IntSize(Math.round(width * factor), |
| 48 |
Math.round(height * factor)); |
| 49 |
} |
| 50 |
|
| 51 |
/* Returns the power of two that is greater than or equal to value */ |
| 52 |
public static int nextPowerOfTwo(int value) { |
| 53 |
// code taken from http://acius2.blogspot.com/2007/11/calculating-next-power-of-2.html |
| 54 |
if (0 == value--) { |
| 55 |
return 1; |
| 56 |
} |
| 57 |
value = (value >> 1) | value; |
| 58 |
value = (value >> 2) | value; |
| 59 |
value = (value >> 4) | value; |
| 60 |
value = (value >> 8) | value; |
| 61 |
value = (value >> 16) | value; |
| 62 |
return value + 1; |
| 63 |
} |
| 64 |
|
| 65 |
public IntSize nextPowerOfTwo() { |
| 66 |
return new IntSize(nextPowerOfTwo(width), nextPowerOfTwo(height)); |
| 67 |
} |
| 68 |
} |
| 69 |
|