Skip to content

Commit

Permalink
Updated alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
eszdman committed Jun 3, 2022
1 parent 9d510c8 commit 8941f36
Show file tree
Hide file tree
Showing 12 changed files with 90 additions and 143 deletions.
48 changes: 48 additions & 0 deletions app/src/main/java/com/particlesdevs/photoncamera/WrapperGPU.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.particlesdevs.photoncamera;

import android.annotation.SuppressLint;
import android.util.Log;

import com.particlesdevs.photoncamera.app.PhotonCamera;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;

//PhotonCamera
//Copyright (C) 2020-2021 Eszdman
//https://github.com/eszdman/PhotonCamera
//Using this file when changing the main application package is prohibited
public class WrapperGPU {
static {
System.loadLibrary("hdrxgpu");
}
/**
* Function to create pointers for image buffers.
*
* @param rows Image rows.
* @param cols Image cols.
* @param frames Image count.
*/
public static native void init(int rows, int cols, int frames);
public static native void initAlignments(int rows, int cols, int frames);

/**
* Function to load images.
*
* @param bufferptr Image buffer.
*/
public static native void loadFrame(ByteBuffer bufferptr, float Exposure);
public static native void loadFrameAlignments(ByteBuffer bufferptr, float Exposure);

public static native void loadInterpolatedGainMap(ByteBuffer GainMap);

public static native void outputBuffer(ByteBuffer outputBuffer);
public static native void processFrame(float NoiseS, float NoiseO,float Smooth, float ElFactor, float BLr,float BLg,float BLb, float WLFactor,
float wpR,float wpG, float wpB,int CfaPattern);
public static native ByteBuffer processFrameAlignments();
}
107 changes: 3 additions & 104 deletions app/src/main/java/com/particlesdevs/photoncamera/app/PhotonCamera.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import com.particlesdevs.photoncamera.control.Gyro;
import com.particlesdevs.photoncamera.control.Vibration;
import com.particlesdevs.photoncamera.debugclient.Debugger;
import com.particlesdevs.photoncamera.ml.Model;
import com.particlesdevs.photoncamera.pro.SensorSpecifics;
import com.particlesdevs.photoncamera.pro.Specific;
import com.particlesdevs.photoncamera.pro.SpecificSetting;
Expand Down Expand Up @@ -201,6 +200,9 @@ public static String getVersion() {
}
return version;
}
public static String getLibsDirectory(){
return sPhotonCamera.getApplicationInfo().nativeLibraryDir;
}

public ExecutorService getExecutorService() {
return executorService;
Expand All @@ -222,109 +224,6 @@ public void onCreate() {
initModules();
super.onCreate();
}
void test(){
try {
org.tensorflow.lite.support.model.Model.Options.Builder builder = new org.tensorflow.lite.support.model.Model.Options.Builder();
builder.setDevice(org.tensorflow.lite.support.model.Model.Device.GPU);
Model model = Model.newInstance(this);
AssetLoader loader = PhotonCamera.getAssetLoader();
Bitmap bitmap = BitmapFactory.decodeStream(loader.getInputStream("lr-4.jpg"));

ByteBuffer img = ByteBuffer.allocateDirect(bitmap.getByteCount());
int width = 256;
int height = 256;
int channels = 1;
int wcount = (bitmap.getByteCount()/height);
Log.d("PhotonCamera","Wcount:"+wcount);
bitmap.copyPixelsToBuffer(img);
byte[] bytesIn = new byte[width*height*4];
img.limit(width*height*4);
img.position(0);
img.get(bytesIn);

Log.d("PhotonCamera","bb input:"+bytesIn[0]);
float[] floatsIn = new float[width*height*channels];
int cnt = 0;
for(int i = 0; i<bytesIn.length;i+=4){
for(int ch = 0; ch<channels;ch++) {
floatsIn[cnt] = (((int) bytesIn[i + ch] & 0xff) / 256.f);
cnt += 1;
}
}
Log.d("PhotonCamera","float input:"+floatsIn[50]+","+floatsIn[51]);
// Creates inputs for reference.
TensorBuffer inputFeature0 = TensorBuffer.createFixedSize(new int[]{1, width, height, channels}, DataType.FLOAT32);
ByteBuffer buffer = ByteBuffer.allocateDirect(width * height * 4 * channels);
FloatBuffer fb = buffer.asFloatBuffer();

fb.position(0);
fb.put(floatsIn);
buffer.position(0);
buffer.limit(width * height * 4 * channels);
inputFeature0.loadBuffer(buffer);

// Runs model inference and gets result.
Model.Outputs outputs = model.process(inputFeature0);

TensorBuffer outputFeature0 = outputs.getOutputFeature0AsTensorBuffer();
fb.position(0);
/*float[] floats = outputFeature0.getFloatArray();
ByteBuffer bb2 = ByteBuffer.allocate(width*height*4*channels);
FloatBuffer fb2 = bb2.asFloatBuffer();
fb2.position(0);
fb2.put(floats);
fb2.position(0);
bb2.position(0);
bb2.put(outputFeature0.getBuffer());
fb2.position(0);
//fb2.limit(width*height);
//fb2.get(floats);
Log.d("PhotonCamera","bb outputsize:"+outputFeature0.getBuffer().capacity());*/
byte[] bytes = new byte[width*height*4];
cnt = 0;
//Log.d("PhotonCamera","bb output2:"+floats[50]+","+floats[51]);
for(int i = 0; i<width*height*channels;i++){
if(cnt + 4 >= bytes.length) break;

bytes[cnt] = (byte)(255);
bytes[cnt+1] = (byte)(255);
bytes[cnt+2] = (byte)(255);
bytes[cnt+3] = (byte)(255);
Log.d("PhotonCamera","TF runtime:"+TensorFlowLite.runtimeVersion()+",TF native:"+TensorFlowLite.nativeRuntimeVersion());
for(int ch = 0;ch<channels;ch++){
float inp = outputFeature0.getFloatValue(i)*255.f;
bytes[cnt+ch] = (byte)(inp);
Log.d("PhotonCamera","float input:"+inp);
}
/*
bytes[cnt] = (byte)(inp);
bytes[cnt+1] = (byte)(inp);
bytes[cnt+2] = (byte)(inp);
bytes[cnt+3] = (byte)(255);
*/

cnt+=4;
}
ByteBuffer outBuffer = ByteBuffer.wrap(bytes);
bitmap.copyPixelsFromBuffer(outBuffer);
File debug = new File(ImagePath.newJPGFilePath()+"test.jpg");
FileOutputStream fOut = null;
try {
debug.createNewFile();
fOut = new FileOutputStream(debug);
} catch (IOException e) {
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);


// Releases model resources if no longer used.
model.close();
} catch (IOException e) {
Log.d("PhotonCamera",e.toString());
}
Log.d("PhotonCamera","Test Complete!");
}
private void initModules() {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
public class CaptureController implements MediaRecorder.OnInfoListener {
public static final int RAW_FORMAT = ImageFormat.RAW_SENSOR;
public static final int YUV_FORMAT = ImageFormat.YUV_420_888;
public static final int PREVIEW_FORMAT = ImageFormat.JPEG;
public static final int PREVIEW_FORMAT = ImageFormat.YUV_420_888;
private static final String TAG = CaptureController.class.getSimpleName();
public List<Future<?>> taskResults = new ArrayList<>();
private final ExecutorService processExecutor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.util.Log;

import com.particlesdevs.photoncamera.Wrapper;
import com.particlesdevs.photoncamera.WrapperGPU;
import com.particlesdevs.photoncamera.api.Camera2ApiAutoFix;
import com.particlesdevs.photoncamera.api.CameraMode;
import com.particlesdevs.photoncamera.api.ParseExif;
Expand All @@ -19,12 +20,12 @@
import com.particlesdevs.photoncamera.processing.ImageSaver;
import com.particlesdevs.photoncamera.processing.ProcessingEventsListener;
import com.particlesdevs.photoncamera.processing.opengl.postpipeline.PostPipeline;
import com.particlesdevs.photoncamera.processing.opengl.rawpipeline.RawPipeline;
import com.particlesdevs.photoncamera.processing.opengl.scripts.InterpolateGainMap;
import com.particlesdevs.photoncamera.processing.parameters.FrameNumberSelector;
import com.particlesdevs.photoncamera.processing.parameters.IsoExpoSelector;
import com.particlesdevs.photoncamera.processing.render.Parameters;

import java.io.File;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -112,7 +113,6 @@ private void ApplyHdrX() {
exifData.IMAGE_DESCRIPTION = processingParameters.toString();

Log.d(TAG, "Wrapper.init");
RawPipeline rawPipeline = new RawPipeline();
ArrayList<ImageFrame> images = new ArrayList<>();
ByteBuffer lowexp = null;
ByteBuffer highexp = null;
Expand Down Expand Up @@ -197,20 +197,24 @@ private void ApplyHdrX() {
NoiseS = (float) Math.max(NoiseS*noisempy, Float.MIN_NORMAL);
NoiseO = (float) Math.max(NoiseO*noisempy, Float.MIN_NORMAL);
FrameNumberSelector.frameCount = cnt;
if (!debugAlignment) {
Wrapper.init(width, height, cnt);

for (int i = 0; i < cnt; i++) {
float mpy = minMpy / images.get(i).pair.layerMpy;
//if (images.get(i).pair.curlayer == IsoExpoSelector.ExpoPair.exposureLayer.Normal)
// mpy = 1.f;
//if(images.get(i).pair.curlayer == IsoExpoSelector.ExpoPair.exposureLayer.Low) mpy = 1.f;
Log.d(TAG, "Load: i: " + i + " expo layer:" + images.get(i).pair.curlayer + " mpy:" + mpy);
Wrapper.init(width, height, cnt);
WrapperGPU.init(width,height,cnt);
//WrapperGPU.InitCL();
//WrapperGPU.nativeLib(new File(PhotonCamera.getLibsDirectory(),"libOpenCL.so").getAbsolutePath());
for (int i = 0; i < cnt; i++) {
float mpy = minMpy / images.get(i).pair.layerMpy;
//if (images.get(i).pair.curlayer == IsoExpoSelector.ExpoPair.exposureLayer.Normal)
// mpy = 1.f;
//if(images.get(i).pair.curlayer == IsoExpoSelector.ExpoPair.exposureLayer.Low) mpy = 1.f;
Log.d(TAG, "Load: i: " + i + " expo layer:" + images.get(i).pair.curlayer +
" mpy:" + mpy+ " wl:"+((FAKE_WL) / processingParameters.whiteLevel) * mpy);
if (!debugAlignment) {
Wrapper.loadFrame(images.get(i).buffer, ((FAKE_WL) / processingParameters.whiteLevel) * mpy);
} else {
WrapperGPU.loadFrame(images.get(i).buffer, ((FAKE_WL) / processingParameters.whiteLevel) * mpy);
}
}
//rawPipeline.imageObj = mImageFramesToProcess;
rawPipeline.images = images;

Log.d(TAG, "White Level:" + processingParameters.whiteLevel);
Log.d(TAG, "Wrapper.loadFrame");
//float noiseLevel = (float) Math.sqrt((CaptureController.mCaptureResult.get(CaptureResult.SENSOR_SENSITIVITY)) *
Expand All @@ -220,30 +224,23 @@ private void ApplyHdrX() {

ByteBuffer output = null;
InterpolateGainMap interpolateGainMap;
interpolateGainMap = new InterpolateGainMap(new Point(width, height));
interpolateGainMap.parameters = processingParameters;
interpolateGainMap.Run();
interpolateGainMap.close();


output = ByteBuffer.allocateDirect(images.get(0).buffer.capacity());
if (!debugAlignment) {
interpolateGainMap = new InterpolateGainMap(new Point(width, height));
interpolateGainMap.parameters = processingParameters;
interpolateGainMap.Run();
interpolateGainMap.close();
Wrapper.loadInterpolatedGainMap(interpolateGainMap.Output);

/*float noiseLevel = (processingParameters.noiseModeler.computeModel[0].first.floatValue()+
processingParameters.noiseModeler.computeModel[1].first.floatValue()+
processingParameters.noiseModeler.computeModel[2].first.floatValue())*0.7f;
noiseLevel += processingParameters.noiseModeler.computeModel[0].second.floatValue()+
processingParameters.noiseModeler.computeModel[1].second.floatValue()+
processingParameters.noiseModeler.computeModel[2].second.floatValue();
noiseLevel*=Math.pow(2.0,19.0+PhotonCamera.getSettings().noiseRstr);
noiseLevel = Math.max(1.f,noiseLevel);
Log.d(TAG, "Denoising level:" + noiseLevel);*/
output = ByteBuffer.allocateDirect(images.get(0).buffer.capacity());
Wrapper.outputBuffer(output);

Wrapper.processFrame(NoiseS, NoiseO, 1.5f,1,0.f, 0.f, 0.f, processingParameters.whiteLevel
, processingParameters.whitePoint[0], processingParameters.whitePoint[1], processingParameters.whitePoint[2], processingParameters.cfaPattern);
} else {
rawPipeline.alignAlgorithm = alignAlgorithm;
output = rawPipeline.Run();
WrapperGPU.loadInterpolatedGainMap(interpolateGainMap.Output);
WrapperGPU.outputBuffer(output);
WrapperGPU.processFrame(NoiseS, NoiseO, 0.007f,1,0.f, 0.f, 0.f, processingParameters.whiteLevel
, processingParameters.whitePoint[0], processingParameters.whitePoint[1], processingParameters.whitePoint[2], processingParameters.cfaPattern);
}
float[] oldBL = processingParameters.blackLevel.clone();

Expand All @@ -256,9 +253,6 @@ private void ApplyHdrX() {
// continue;
images.get(i).image.close();
}
if (debugAlignment) {
rawPipeline.close();
}
Log.d(TAG, "HDRX Alignment elapsed:" + (System.currentTimeMillis() - startTime) + " ms");

if (saveRAW) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected void onCreate(Bundle savedInstanceState) {
if (!path.exists()) {
path.mkdirs();
}
Runtime.getRuntime().exec("adb logcat --clear");
Runtime.getRuntime().exec(
"logcat -d -f " + path + File.separator
+ "dbo_logcat"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import androidx.annotation.NonNull;

import com.particlesdevs.photoncamera.app.PhotonCamera;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
Expand All @@ -19,6 +21,7 @@ public class FileManager {
return ACCEPTED_FILES_EXTENSIONS.contains(-1 == index ? "" : name.substring(index + 1).toUpperCase()) && new File(dir, name).length() > 0;
};
public static File sEXTERNAL_DIR = Environment.getExternalStorageDirectory();
public static File sCACHE_DIR;
public static File sPHOTON_DIR = new File(sEXTERNAL_DIR + "//DCIM//PhotonCamera//");
public static File sPHOTON_RAW_DIR = new File(sEXTERNAL_DIR + "//DCIM//PhotonCamera//Raw//");
public static File sPHOTON_TUNING_DIR = new File(sEXTERNAL_DIR + "//DCIM//PhotonCamera//Tuning//");
Expand Down
Binary file modified app/src/main/jniLibs/arm64-v8a/libHdrX.so
Binary file not shown.
Binary file added app/src/main/jniLibs/arm64-v8a/libc++_shared.so
Binary file not shown.
Binary file removed app/src/main/ml/model.tflite
Binary file not shown.
2 changes: 0 additions & 2 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
</string-array>
<string-array name="alignset_entries">
<item>PCam CPU</item>
<item>PCam GPU</item>
<item>PCam Hybrid</item>
</string-array>
<string-array name="location_geotag_entryValues">
Expand All @@ -31,7 +30,6 @@
<string-array name="alignset_entryValues">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="theme_entries">
<item>@string/theme_follow_system</item>
Expand Down
4 changes: 2 additions & 2 deletions app/version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
#Sun May 01 16:39:30 MSK 2022
VERSION_BUILD=14949
#Fri Jun 03 08:04:23 MSK 2022
VERSION_BUILD=15200
4 changes: 4 additions & 0 deletions photonbypass/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ android {
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
prefab true
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
Expand All @@ -45,6 +48,7 @@ dependencies {
implementation 'com.google.android.material:material:1.5.0'
implementation "androidx.core:core-ktx:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//implementation 'io.hexhacking:xdl:1.1.3'
/* testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'*/
Expand Down

0 comments on commit 8941f36

Please sign in to comment.