Skip to content

Commit

Permalink
Code refactor, improved debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
eszdman committed Jul 26, 2021
1 parent eb76727 commit 7775576
Show file tree
Hide file tree
Showing 19 changed files with 245 additions and 59 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions app/src/main/assets/import_labtoxyz.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
vec3 labtoxyz(vec3 c ) {
float fy = ( c.x + 16.0 ) / 116.0;
float fx = c.y / 500.0 + fy;
float fz = fy - c.z / 200.0;
return vec3(
95.047 * (( fx > 0.206897 ) ? fx * fx * fx : ( fx - 16.0 / 116.0 ) / 7.787),
100.000 * (( fy > 0.206897 ) ? fy * fy * fy : ( fy - 16.0 / 116.0 ) / 7.787),
108.883 * (( fz > 0.206897 ) ? fz * fz * fz : ( fz - 16.0 / 116.0 ) / 7.787)
);
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 8 additions & 0 deletions app/src/main/assets/import_xyztolab.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
vec3 xyztolab(vec3 c) {
vec3 n = c / vec3(95.047, 100, 108.883);
vec3 v;
v.x = ( n.x > 0.008856 ) ? pow( n.x, 1.0 / 3.0 ) : ( 7.787 * n.x ) + ( 16.0 / 116.0 );
v.y = ( n.y > 0.008856 ) ? pow( n.y, 1.0 / 3.0 ) : ( 7.787 * n.y ) + ( 16.0 / 116.0 );
v.z = ( n.z > 0.008856 ) ? pow( n.z, 1.0 / 3.0 ) : ( 7.787 * n.z ) + ( 16.0 / 116.0 );
return vec3(( 116.0 * v.y ) - 16.0, 500.0 * ( v.x - v.y ), 200.0 * ( v.y - v.z ));
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -1326,9 +1326,11 @@ private void captureStillPicture() {
//IsoExpoSelector.HDR = (PhotonCamera.getSettings().alignAlgorithm == 1);
IsoExpoSelector.HDR = false;
Log.d(TAG, "HDR:" + IsoExpoSelector.HDR);
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CONTROL_AF_MODE_OFF);
if (!(focus == 0.0 && Build.DEVICE.toLowerCase().equals("samsung")))
if (!(focus == 0.0 && Build.DEVICE.toLowerCase().equals("samsung"))){
captureBuilder.set(CaptureRequest.CONTROL_AF_MODE, CONTROL_AF_MODE_OFF);
captureBuilder.set(CaptureRequest.LENS_FOCUS_DISTANCE, focus);
}



/*mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_MODE, CaptureRequest.CONTROL_AF_MODE_OFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static public String readprog(BufferedReader reader, ArrayList<String[]> defines
linecnt++;
String val = String.valueOf(line);
if(val.contains("#import")){
val = val.replace("\n","").replace(" ","").toLowerCase();
/*val = val.replace("\n","").replace(" ","").toLowerCase();
@RawRes
int id = 0;
switch (val){
Expand Down Expand Up @@ -83,8 +83,18 @@ static public String readprog(BufferedReader reader, ArrayList<String[]> defines
case "#importmedian":
id = R.raw.import_median;
break;
}*/
String imported = "";
if(!val.contains("//")) {
imported = PhotonCamera.getAssetLoader().getString(
val
.replace("#", "")
.replace(" ", "_")
.replace("\n", "")
+ ".glsl");
}
if(id!= 0) {
//val.replace("#")
/*if(id!= 0) {
BufferedReader reader2 = new BufferedReader(new InputStreamReader(PhotonCamera.getResourcesStatic().openRawResource(id)));
for (Object line2 : reader2.lines().toArray()) {
source.append("#line 1\n");
Expand All @@ -93,7 +103,14 @@ static public String readprog(BufferedReader reader, ArrayList<String[]> defines
}
//linecnt++;
source.append("#line ").append(linecnt+1).append("\n");
}*/
if(!imported.equals("")){
source.append("#line 1\n");
source.append(imported);
source.append("\n");
source.append("#line ").append(linecnt+1).append("\n");
}

continue;
}
if(val.contains("#define") && defines != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,32 @@ public Equalization() {
}
private static final float MIN_GAMMA = 0.55f;
private final PorterDuffXfermode porterDuffXfermode = new PorterDuffXfermode(PorterDuff.Mode.ADD);
private void GenerateCurveBitm(float[] curve){
Bitmap CurveEQ = Bitmap.createBitmap(512,512, Bitmap.Config.ARGB_8888);
private void GenerateCurveBitm(float[] curve,float[] BL){
Bitmap CurveEQ = Bitmap.createBitmap(256,256, Bitmap.Config.ARGB_8888);
((PostPipeline)basePipeline).debugData.add(CurveEQ);
Utilities.drawArray(curve,CurveEQ);
Utilities.drawBL(BL,CurveEQ);
Utilities.drawWL(new float[]{0.9f,0.9f,0.9f},CurveEQ);//Test WL draw
}
private void GenerateCurveBitm(int[] curve){
Bitmap CurveEQ = Bitmap.createBitmap(256,256, Bitmap.Config.ARGB_8888);
((PostPipeline)basePipeline).debugData.add(CurveEQ);
Utilities.drawArray(curve,CurveEQ);
}
private void GenerateCurveBitm(float[] r,float[] g,float[] b){
Bitmap CurveEQ = Bitmap.createBitmap(512,512, Bitmap.Config.ARGB_8888);
Bitmap CurveEQ = Bitmap.createBitmap(256,256, Bitmap.Config.ARGB_8888);
((PostPipeline)basePipeline).debugData.add(CurveEQ);
Utilities.drawArray(r,g,b,CurveEQ);
}
private void GenerateCurveBitm(int[] r,int[] g,int[] b){
Bitmap CurveEQ = Bitmap.createBitmap(256,256, Bitmap.Config.ARGB_8888);
((PostPipeline)basePipeline).debugData.add(CurveEQ);
Utilities.drawArray(r,g,b,CurveEQ);
}
@Override
public void Compile() {}
private Histogram Analyze(){
int resize = 8;
int resize = 6;
GLTexture r1 = new GLTexture(previousNode.WorkingTexture.mSize.x/resize,
previousNode.WorkingTexture.mSize.y/resize,previousNode.WorkingTexture.mFormat);
//glProg.setDefine("BR",(float)shadowW*0.4f);
Expand Down Expand Up @@ -210,49 +222,49 @@ private float[] bSpline(float[] input,float WL){
}
return output;
}
private float[] bilateralSmoothCurve(float[]input, float BL,float WL){
private float[] bilateralSmoothCurve(float[]input, float[] blwl){
boolean nightMode = PhotonCamera.getSettings().selectedMode == CameraMode.NIGHT;
float bilateralk = edgesBilateralSmooth;
if(nightMode) bilateralk = edgesBilateralSmoothNight;
ArrayList<Float> my,mx;
my = new ArrayList<>();
mx = new ArrayList<>();
Log.d(Name,"BL0:"+BL);
Log.d(Name,"WL0:"+WL);
BL = pdf(BL/input.length,bilateralk)*BL*edgesStretchShadows;
WL = input.length - pdf(1.f - WL/input.length,bilateralk*highLightSmoothAmplify)*(input.length - WL)*edgesStretchHighLight;
WL = Math.min(WL,input.length-1);
Log.d(Name,"BL0:"+blwl[0]);
Log.d(Name,"WL0:"+blwl[1]);
blwl[0] = pdf(blwl[0]/input.length,bilateralk)*blwl[0]*edgesStretchShadows;
blwl[1] = input.length - pdf(1.f - blwl[1]/input.length,bilateralk*highLightSmoothAmplify)*(input.length - blwl[1])*edgesStretchHighLight;
blwl[1] = Math.min(blwl[1],input.length-1);
float centerY = 0.f;
float msum = 0.f;
float centerX = mix(BL,WL,curveCenter);
for(int i =(int)BL; i<(int)WL;i++){
float k = pdf((i-mix(BL,WL,analyzeCenter))/WL,1.f);
float centerX = mix(blwl[0],blwl[1],curveCenter);
for(int i =(int)blwl[0]; i<(int)blwl[1];i++){
float k = pdf((i-mix(blwl[0],blwl[1],analyzeCenter))/blwl[1],1.f);
centerY+=k*input[i];
msum+=k;
}
centerY = (centerY+0.0001f)/(msum+0.0001f);

mx.add(BL);
//mx.add(BL+0.01f);
mx.add(blwl[0]);
//mx.add(blwl[0]+0.01f);
mx.add(centerX);
mx.add(WL);
mx.add(WL+0.01f);
mx.add(blwl[1]);
mx.add(blwl[1]+0.01f);


my.add(0.f);
// my.add(0.f);
my.add(centerY);
my.add(1.f);
my.add(1.f);
Log.d(Name,"BL:"+BL);
Log.d(Name,"WL:"+WL);
Log.d(Name,"blwl[0]:"+blwl[0]);
Log.d(Name,"blwl[1]:"+blwl[1]);
Log.d(Name,"Mx:"+mx.toString());
Log.d(Name,"My:"+my.toString());
float[]output = new float[input.length];
SplineInterpolator splineInterpolator = SplineInterpolator.createMonotoneCubicSpline(mx,my);
for(int i =0; i<output.length;i++){
output[i] = splineInterpolator.interpolate(i);
if(i < BL) output[i] = 0.f;
if(i < blwl[0]) output[i] = 0.f;
}
return output;
}
Expand Down Expand Up @@ -376,7 +388,7 @@ private float[] bezier2(float[] input){
float edgesBilateralSmoothNight = 0.7f;
float highLightSmoothAmplify = 2.f;
float shadowsSensitivity = 0.6f;
float blackLevelSensitivity = 1.0f;
float blackLevelSensitivity = 1.1f;
@Override
public void Run() {
analyzeIntensity = getTuning("AnalyzeIntensity", analyzeIntensity);
Expand Down Expand Up @@ -459,11 +471,30 @@ public void Run() {

//Log.d(Name,"Hist:"+Arrays.toString(histParser.hist));




float[] averageCurve = new float[histParser.hist.length];
for(int i =0; i<averageCurve.length;i++){
averageCurve[i] = (histParser.histr[i]+histParser.histg[i]+histParser.histb[i])/3.f;
}
if(basePipeline.mSettings.DebugData) {
GenerateCurveBitm(histParser.histr,histParser.histg,histParser.histb);
}
float max = 0.f;
float WL = findWL(histParser.hist);
float BL = findBL(histParser.hist);
float[] blwl = new float[]{BL,WL};
double compensation = averageCurve.length/WL;
histParser.hist = bilateralSmoothCurve(histParser.hist,blwl);

//Use kx+b prediction for curve start
//Depurple Degreen
float[] BLPredict = new float[3];
float[] BLPredictShift = new float[3];
int maxshift = blackLevelSearch;
int maxshift = (int) (blwl[0] + blwl[1]*blackLevelSearch/4096.f);
maxshift = Math.max(maxshift,10);
Log.d(Name,"BlSearch:"+maxshift);
int cnt = 0;
for(int i =5; i<maxshift;i++){
float x = (float)(i)/histSize;
Expand All @@ -490,9 +521,9 @@ public void Run() {
//Saturate shift
float avr = (BLPredictShift[0]+BLPredictShift[1]+BLPredictShift[2])/3.f;
float saturation = 0.0f;
BLPredictShift[0] = (BLPredictShift[0]-avr*saturation) / (1.f-avr*saturation);
BLPredictShift[1] = (BLPredictShift[1]-avr*saturation) / (1.f-avr*saturation);
BLPredictShift[2] = (BLPredictShift[2]-avr*saturation) / (1.f-avr*saturation);
BLPredictShift[0] = -(BLPredictShift[0]-avr*saturation) / (1.f-avr*saturation);
BLPredictShift[1] = -(BLPredictShift[1]-avr*saturation) / (1.f-avr*saturation);
BLPredictShift[2] = -(BLPredictShift[2]-avr*saturation) / (1.f-avr*saturation);

float mins = Math.min(BLPredictShift[0],Math.min(BLPredictShift[1],BLPredictShift[2]));
if(mins < 0.0) {
Expand All @@ -512,19 +543,6 @@ public void Run() {
BLPredictShift[1]*=blackLevelSensitivity;
BLPredictShift[2]*=blackLevelSensitivity;


float[] averageCurve = new float[histParser.hist.length];
for(int i =0; i<averageCurve.length;i++){
averageCurve[i] = (histParser.histr[i]+histParser.histg[i]+histParser.histb[i])/3.f;
}
if(basePipeline.mSettings.DebugData) {
GenerateCurveBitm(histParser.histr,histParser.histg,histParser.histb);
GenerateCurveBitm(averageCurve);
}
float max = 0.f;
float WL = findWL(averageCurve);
float BL = findBL(averageCurve);
double compensation = averageCurve.length/WL;
/*float[] smoothArr = bilateralSmoothCurve(averageCurve,BL,WL);
for(int i =0; i<smoothArr.length;i++){
float t = ((float)i)/smoothArr.length;
Expand All @@ -546,8 +564,8 @@ public void Run() {
} else
histParser.hist[i] = 1.f;
}*/
histParser.hist = bilateralSmoothCurve(averageCurve,BL,WL);
Log.d(Name,"PredictedShift:"+Arrays.toString(BLPredictShift));
if(basePipeline.mSettings.DebugData)GenerateCurveBitm(histParser.histInr,histParser.histIng,histParser.histInb);


/*float[] equalizingCurve = new float[histParser.hist.length];
Expand All @@ -574,7 +592,7 @@ public void Run() {
}
//histParser.hist[i] = mix(histParser.hist[i],line,line*0.35f);
}
if(basePipeline.mSettings.DebugData) GenerateCurveBitm(histParser.hist);
if(basePipeline.mSettings.DebugData) GenerateCurveBitm(histParser.hist,BLPredictShift);
GLTexture histogram = new GLTexture(histParser.hist.length,1,new GLFormat(GLFormat.DataType.FLOAT_16),
FloatBuffer.wrap(histParser.hist), GL_LINEAR, GL_CLAMP_TO_EDGE);
//GLTexture shadows = new GLTexture(histParser.hist.length,1,new GLFormat(GLFormat.DataType.FLOAT_16,3),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class Histogram {
public final float[] histr;
public final float[] histg;
public final float[] histb;
public final int[] histIn;
public final int[] histInr;
public final int[] histIng;
public final int[] histInb;
public float gamma;
public final float logAvgLuminance;
public final int histSize;
Expand All @@ -30,24 +34,24 @@ private static float getInterpolated(float[] in, float ind){
}
public Histogram(Bitmap bmp, int whPixels,int histSize) {
this.histSize = histSize;
int[] histv;
/*int[] histv;
int[] histx;
int[] histy;
int[] histz;
int[] histz;*/
int[][] histin = HistogramRs.getHistogram(bmp);
histx = histin[3];
histy = histin[2];
histz = histin[1];
histv = histin[0];
histIn = histin[3];
histInr = histin[2];
histIng = histin[1];
histInb = histin[0];
final double[] logTotalLuminance = {0d};
logAvgLuminance = (float) Math.exp(logTotalLuminance[0] * 4 / (whPixels*4));
for (int j = 0; j < 3; j++) {
sigma[j] /= whPixels;
}
hist = buildCumulativeHist(histx);
histr = buildCumulativeHist(histy);
histg = buildCumulativeHist(histz);
histb = buildCumulativeHist(histv);
hist = buildCumulativeHist(histIn);
histr = buildCumulativeHist(histInr);
histg = buildCumulativeHist(histIng);
histb = buildCumulativeHist(histInb);

// Find gamma: Inverse of the average exponent.
gamma = findGamma(hist);
Expand Down
Loading

0 comments on commit 7775576

Please sign in to comment.