Sketcher2 source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
2.9 KiB

package com.jotuntech.sketcher.common;
public class Pixels {
private final static int RANDOM_SIZE = 3079;
private static int[] randomNumbers;
private static int randomCounter;
public static int random() {
if(randomNumbers == null) {
randomNumbers = new int[RANDOM_SIZE];
for(int i = 0; i < RANDOM_SIZE; i++) {
randomNumbers[i] = (int) Math.round(Math.random() * 255f);
}
}
randomCounter %= RANDOM_SIZE;
return randomNumbers[randomCounter++];
}
/*
public final static int squareRoot(int a_nInput)
{
int op = a_nInput;
int res = 0;
int one = 1 << 30; // The second-to-top bit is set: use 1u << 14 for uint16_t type; use 1uL<<30 for int type
// "one" starts at the highest power of four <= than the argument.
while (one > op) {
one >>= 2;
}
while (one != 0) {
if (op >= res + one) {
op = op - (res + one);
res = res + 2 * one;
}
res >>= 1;
one >>= 2;
}
// Do arithmetic rounding to nearest integer
if (op > res) {
res++;
}
return res;
}
*/
public static int lerp(int destination, int source, int alpha) {
/* mask destination */
int destinationRB = destination & 0x00FF00FF;
int destinationAG = destination >>> 8 & 0x00FF00FF;
/* mask source */
int sourceRB = source & 0x00FF00FF;
int sourceAG = source >>> 8 & 0x00FF00FF;
/* calc difference */
int deltaRB = sourceRB - destinationRB;
int deltaAG = sourceAG - destinationAG;
/* scale difference by alpha */
deltaRB *= alpha;
deltaAG *= alpha;
deltaRB >>>= 8;
deltaAG >>>= 8;
/* add delta to destination and mask */
int RB = (deltaRB + destinationRB) & 0x00FF00FF;
int AG = (deltaAG + destinationAG) << 8 & 0xFF00FF00;
return RB | AG;
}
public static int getChannel0(int pixel) {
return pixel >>> 24;
}
public static int getChannel1(int pixel) {
return (pixel >>> 16) & 0xFF;
}
public static int getChannel2(int pixel) {
return (pixel >>> 8) & 0xFF;
}
public static int getChannel3(int pixel) {
return pixel & 0xFF;
}
public static int stripChannel0(int pixel) {
return pixel & 0xFFFFFF;
}
public static int pack(int c1, int c2, int c3) {
return (c1 << 16) | (c2 << 8) | c3;
}
public static int pack(int c0, int color) {
return (c0 << 24) | color;
}
public static int pack(int c0, int c1, int c2, int c3) {
return (c0 << 24) | (c1 << 16) | (c2 << 8) | c3;
}
private final static float a = 0.055f;
public static float gammaDecode(float v) {
v /= 255f;
if(v <= 0.04045d) {
v /= 12.92d;
} else {
v = (float) Math.pow((v + a) / (1d + a), 2.4f);
}
return v;
}
public static int gammaEncode(float v) {
if(v <= 0.0031308) {
v *= 12.92d;
} else {
v = (1f + a) * (float) Math.pow(v, 1 / 2.4f) - a;
}
return Math.round(v * 255f);
}
}