Skip to content

Commit

Permalink
Changes for settings per camera model
Browse files Browse the repository at this point in the history
  • Loading branch information
selfemp committed Apr 19, 2024
1 parent dbd0e0e commit e4e4342
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 75 deletions.
2 changes: 1 addition & 1 deletion LibRawFX/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.librawfx</groupId>
<artifactId>LibRawFX</artifactId>
<version>1.8.6</version>
<version>1.8.7</version>

<name>LibRawFX</name>
<description>This installs the native lib libraw as a JavaFX Image format provider similar to imageIO before on Swing</description>
Expand Down
70 changes: 30 additions & 40 deletions LibRawFX/src/main/java/org/librawfx/LibrawImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ public class LibrawImage {
private RAWImageLoader loader;
private String cameraModel;
private LocalDateTime shootingDateTime;
private RawDecoderSettings rawSettings;
private HashMap<String, RawDecoderSettings> rawSettings;
private static SymbolLookup loaderLookup;

public LibrawImage(String imageFile, RawDecoderSettings settings) {
public LibrawImage(String imageFile, HashMap<String, RawDecoderSettings> settings) {
this.imageFileURL = imageFile;
this.rawSettings = settings;
}

public LibrawImage(RAWImageLoader loader, RawDecoderSettings settings) {
public LibrawImage(RAWImageLoader loader, HashMap<String, RawDecoderSettings> settings) {
imageFileURL = null;
this.loader = loader;
this.rawSettings = settings;
Expand Down Expand Up @@ -76,7 +76,6 @@ public static void loadLibs(String tempDir) throws IOException {

Logger.getLogger(LibrawImage.class.getName()).log(Level.FINEST, null, "loadLibraryFromJar: " + Arrays.toString(loadLibraryFromJar) + " , tempdir: " + tempDir);
for (String part : loadLibraryFromJar) {
//System.out.println("libsarray "+part);
new File(part).deleteOnExit();
}
for (String strTemp : loadLibraryFromJar) {
Expand Down Expand Up @@ -110,16 +109,6 @@ public byte[] readPixelDataFromStream(byte[] sourceFileAsByteArray) throws IOExc
//MemorySegment datasegment = org.libraw.win.libraw_data_t.idata(iprc);
MemorySegment params$slice = org.libraw.nativ.libraw_data_t.params(iprc);

if (this.rawSettings == null) {
this.rawSettings = new RawDecoderSettings();
}
new RawDecoderToNativeTranslator(rawSettings, operatingSystem).translate(params$slice);
/*org.libraw.win.libraw_output_params_t.use_camera_wb$set(params$slice, 0);
org.libraw.win.libraw_output_params_t.use_auto_wb$set(params$slice, 0);
org.libraw.win.libraw_output_params_t.output_tiff$set(params$slice, 0);
org.libraw.win.libraw_output_params_t.half_size$set(params$slice, 0);
org.libraw.win.libraw_output_params_t.user_qual$set(params$slice, 0);*/

MemorySegment inputStreamBytes = MemorySegment.ofArray(sourceFileAsByteArray);
MemorySegment allocateNative = scope.allocateFrom(ValueLayout.JAVA_BYTE, sourceFileAsByteArray);//)Array(org.libraw.win.libraw_h.C_CHAR, sourceFileAsByteArray);
int k = org.libraw.nativ.libraw_h.libraw_open_buffer(iprc, allocateNative, inputStreamBytes.byteSize());
Expand All @@ -131,6 +120,20 @@ public byte[] readPixelDataFromStream(byte[] sourceFileAsByteArray) throws IOExc
org.libraw.nativ.libraw_h.libraw_unpack(iprc);

MemorySegment iParams = org.libraw.nativ.libraw_h.libraw_get_iparams(iprc);

MemorySegment maker$slice = org.libraw.nativ.libraw_iparams_t.make(iParams);
MemorySegment model$slice = org.libraw.nativ.libraw_iparams_t.model(iParams);
String model = maker$slice.getString(0) + " " + model$slice.getString(0);
System.out.println("model " + model);

RawDecoderSettings settings;
if (rawSettings.get(model) == null) {
settings = rawSettings.get("Default");
} else {
settings = rawSettings.get(model);
}
new RawDecoderToNativeTranslator(settings, operatingSystem).translate(params$slice);

//MemorySegment iParamsRestricted = org.libraw.win.libraw_iparams_t.allocate(iParams);//.ofAddress(iParams, scope);
MemorySegment modelSlice = org.libraw.nativ.libraw_iparams_t.model(iParams);
cameraModel = new String(modelSlice.toArray(ValueLayout.JAVA_BYTE), StandardCharsets.US_ASCII);
Expand Down Expand Up @@ -205,11 +208,6 @@ public synchronized int[] readPixelData() throws IOException {
//MemorySegment datasegment = org.libraw.win.libraw_data_t.ofAddress(iprc, scope);
MemorySegment params$slice = org.libraw.nativ.libraw_data_t.params(iprc);

if (this.rawSettings == null) {
this.rawSettings = new RawDecoderSettings();
}
new RawDecoderToNativeTranslator(rawSettings, operatingSystem).translate(params$slice);

Logger.getLogger(LibrawImage.class.getName()).log(Level.FINEST, null, "Open file");
//int libraw_open_file = org.libraw.win.libraw_h.libraw_open_file(iprc, SegmentAllocator.nativeAllocator(scope).allocateUtf8String(imageFileURL));
int libraw_open_file = org.libraw.nativ.libraw_h.libraw_open_file(iprc, scope.allocateFrom(imageFileURL));
Expand All @@ -221,7 +219,19 @@ public synchronized int[] readPixelData() throws IOException {
org.libraw.nativ.libraw_h.libraw_unpack(iprc);

MemorySegment iParams = org.libraw.nativ.libraw_h.libraw_get_iparams(iprc);
//MemorySegment iParamsRestricted = org.libraw.win.libraw_iparams_t.ofAddress(iParams, scope);

MemorySegment maker$slice = org.libraw.nativ.libraw_iparams_t.make(iParams);
MemorySegment model$slice = org.libraw.nativ.libraw_iparams_t.model(iParams);
String model = maker$slice.getString(0) + " " + model$slice.getString(0);

RawDecoderSettings settings;
if (rawSettings.get(model) == null) {
settings = rawSettings.get("Default");
} else {
settings = rawSettings.get(model);
}
new RawDecoderToNativeTranslator(settings, operatingSystem).translate(params$slice);

MemorySegment modelSlice = org.libraw.nativ.libraw_iparams_t.model(iParams);
cameraModel = new String(modelSlice.toArray(ValueLayout.JAVA_BYTE), StandardCharsets.UTF_8);
MemorySegment image_other_data = org.libraw.nativ.libraw_h.libraw_get_imgother(iprc);
Expand Down Expand Up @@ -532,26 +542,6 @@ public LocalDateTime getShootingDateTime() {
return shootingDateTime;
}

/**
* getting the actual setting for the raw decoder engine
*
* @return the settings object with all settings applied
*/
public RawDecoderSettings getRawSettings() {
return rawSettings;
}

/**
* Optional settings for the raw decoding engine. If not set than basis
* engine properties are applied. This must be called before any loading of
* images
*
* @param rawSettings the settings object to be set.
*/
public void setRawSettings(RawDecoderSettings rawSettings) {
this.rawSettings = rawSettings;
}

/**
* String representation of the image
*
Expand Down
21 changes: 14 additions & 7 deletions LibRawFX/src/main/java/org/librawfx/RAWImageLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.stage.Screen;
Expand All @@ -31,20 +32,26 @@ public class RAWImageLoader extends ImageLoaderImpl {
private final DimensionProvider dimensionProvider;
private final Lock accessLock = new Lock();
private boolean isDisposed = false;
private LibrawImage libraw;
private static RawDecoderSettings settings;
private LibrawImage libraw;
private static HashMap<String, RawDecoderSettings> settings=new HashMap<>();

protected RAWImageLoader(InputStream input, DimensionProvider dimensionProvider) {
super(RAWDescriptor.getInstance());
if (input == null) {
throw new IllegalArgumentException("input == null!");
}
this.input = input;
this.dimensionProvider = dimensionProvider;
RAWImageLoader.settings = new RawDecoderSettings();
this.dimensionProvider = dimensionProvider;
initSettings();
libraw = new LibrawImage(this, settings);
}

public static void initSettings() {
if (settings.isEmpty()) {
settings.put("Default", new RawDecoderSettings());
}
}

@Override
public synchronized void dispose() {
if (!accessLock.isLocked() && !isDisposed) {
Expand Down Expand Up @@ -210,9 +217,9 @@ public synchronized void unlock() {
}
}

public static RawDecoderSettings getSettings() {
if (settings == null) {
RAWImageLoader.settings = new RawDecoderSettings();
public static final HashMap<String, RawDecoderSettings> getSettings() {
if (settings == null) {
initSettings();
}
return settings;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.sun.javafx.iio.ImageStorage;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -42,7 +43,7 @@ public static final void install(String tempdir) {
*/
public static final void install() {
RAWImageLoaderFactory.tempDir=null;
install(new DefaultDimensionProvider());
install(new DefaultDimensionProvider());
}

public static final void install(DimensionProvider dimensionProvider) {
Expand All @@ -52,15 +53,15 @@ public static final void install(DimensionProvider dimensionProvider) {
Logger.getLogger(RAWImageLoaderFactory.class.getName()).log(Level.SEVERE, null, ex);
}
RAWImageLoaderFactory.dimensionProvider = dimensionProvider;

RAWImageLoader.initSettings();
ImageStorage.getInstance().addImageLoaderFactory(instance);
}

public static final ImageLoaderFactory getInstance() {
return instance;
}

public static final RawDecoderSettings getDecoderSettings(){
public static final HashMap<String, RawDecoderSettings> getDecoderSettings(){
return RAWImageLoader.getSettings();
}

Expand Down
28 changes: 11 additions & 17 deletions LibRawFX/src/main/java/org/librawfx/RawDecoderSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
*/
public class RawDecoderSettings {

private String whiteBalance = "CUSTOM"; //CAMERA
private String whiteBalance = "CAMERA"; //CAMERA
private boolean autoWhitebalance = false;
private boolean output_tiff = false;
private boolean halfSizeOutput = false;
private boolean halfSizeOutput = false;
private int RAWQuality = 0;//0 - linear interpolation, 1 - VNG interpolation, 2 - PPG interpolation, 3 - AHD interpolation, 4 - DCB interpolation, 11 - DHT intepolation, 12 - Modified AHD intepolation (by Anton Petrusevich)
private boolean autoBrightness = false;
private float autoBrightnessThreashold = 0.0f;
Expand All @@ -25,9 +25,9 @@ public class RawDecoderSettings {
private float expoCorrectionHighlight = 0.0f;
private int noiseReduction = 0;

private boolean fixColorsHighlights = false;
private boolean fixColorsHighlights = false;
private boolean sixteenBitsImage = false;
private double brightness = 1.0;
private double brightness = 1.0;
private int saturation = 0;
private int outputColorSpace = 1; //[0-6] Output colorspace (raw, sRGB, Adobe, Wide, ProPhoto, XYZ, ACES)
private boolean RGBInterpolate4Colors = false;
Expand All @@ -36,37 +36,31 @@ public class RawDecoderSettings {
//private int customWhiteBalance = 6500;
//private double customWhiteBalanceGreen = 1.0;
private int medianFilterPasses = 0;


private boolean enableBlackPoint = false;
private int blackPoint = 0;

//private boolean enableWhitePoint = false;
//private int whitePoint = 0;

private String NRType = "NONR";
private int NRThreshold = 0;

//private String inputProfile = "";
//private String outputProfile = "";

//private String deadPixelMap = "";

//private String whiteBalanceArea = "";

//-- Extended demosaicing settings ----------------------------------------------------------
private int dcbIterations = -1;
private boolean dcbEnhanceFl = false;

private boolean dcbEnhanceFl = false;

public RawDecoderSettings() {
}

public boolean isFixColorsHighlights() {
return fixColorsHighlights;
}

public void setFixColorsHighlights(boolean fixColorsHighlights) {
public void setFixColorsHighlights(boolean fixColorsHighlights) {
this.fixColorsHighlights = fixColorsHighlights;
}

Expand Down Expand Up @@ -195,9 +189,11 @@ public boolean isEnableExposureCorrection() {
}

public void setEnableExposureCorrection(boolean enableExposureCorrection) {
if (enableExposureCorrection == true) {
exposureCorrection = 1;
}
this.enableExposureCorrection = enableExposureCorrection;
}


public float getExpoCorrectionShift() {
return exposureShift;
Expand All @@ -214,7 +210,7 @@ public float getExpoCorrectionHighlight() {
public void setExpoCorrectionHighlight(float expoCorrectionHighlight) {
this.expoCorrectionHighlight = expoCorrectionHighlight;
}

public boolean isAutoWhitebalance() {
return autoWhitebalance;
}
Expand Down Expand Up @@ -307,7 +303,5 @@ public void setSaturation(int saturation) {
public String toString() {
return "RawDecoderSettings{" + "whiteBalance=" + whiteBalance + ", autoWhitebalance=" + autoWhitebalance + ", output_tiff=" + output_tiff + ", halfSizeOutput=" + halfSizeOutput + ", RAWQuality=" + RAWQuality + ", autoBrightness=" + autoBrightness + ", autoBrightnessThreashold=" + autoBrightnessThreashold + ", brightNess=" + brightnes + ", coolScanNEFGamma=" + coolScanNEFGamma + ", enableExposureCorrection=" + enableExposureCorrection + ", exposureCorrection=" + exposureCorrection + ", exposureShift=" + exposureShift + ", expoCorrectionHighlight=" + expoCorrectionHighlight + ", noiseReduction=" + noiseReduction + ", fixColorsHighlights=" + fixColorsHighlights + ", sixteenBitsImage=" + sixteenBitsImage + ", brightness=" + brightness + ", saturation=" + saturation + ", outputColorSpace=" + outputColorSpace + ", RGBInterpolate4Colors=" + RGBInterpolate4Colors + ", DontStretchPixels=" + DontStretchPixels + ", unclipColors=" + unclipColors + ", medianFilterPasses=" + medianFilterPasses + ", enableBlackPoint=" + enableBlackPoint + ", blackPoint=" + blackPoint + ", NRType=" + NRType + ", NRThreshold=" + NRThreshold + ", dcbIterations=" + dcbIterations + ", dcbEnhanceFl=" + dcbEnhanceFl + '}';
}



}
32 changes: 25 additions & 7 deletions LibRawFX/src/main/java/org/librawfx/TestApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public class TestApp extends Application {
@Override
public void start(Stage stage) throws FileNotFoundException, IOException {
RAWImageLoaderFactory.install();
HashMap<String, RawDecoderSettings> decoderSettings = RAWImageLoaderFactory.getDecoderSettings(); //.setEnableExposureCorrection(false);
decoderSettings.put("Sigma DP2 Merrill", new RawDecoderSettings());
decoderSettings.get("Sigma DP2 Merrill").setEnableExposureCorrection(true);
decoderSettings.get("Sigma DP2 Merrill").setExposureCorrection(1);
decoderSettings.get("Sigma DP2 Merrill").setExpoCorrectionShift(2.0f);
decoderSettings.get("Sigma DP2 Merrill").setBrightNess(6.0f);

Parameters parameters = getParameters();
String file = parameters.getRaw().get(0);
Expand All @@ -47,15 +53,15 @@ public void start(Stage stage) throws FileNotFoundException, IOException {
stack.setSpacing(5);
//stack.setAlignment(Pos.TOP_CENTER);
loadImagesByStream(stack, file, file2, file3);
//loadImagesByFile(stack, file);
//loadImagesByFile(stack, file2);

var scene = new Scene(stack, 640, 480);
stage.setScene(scene);
stage.show();
}

private void loadImagesByStream(VBox stack, String file, String file2, String file3) throws MalformedURLException {
File initialFile = new File(file);
File initialFile = new File(file);
File initialFile2 = new File(file2);
File initialFile3 = new File(file3);
//ind.progressProperty().bind(img.progressProperty());
Expand Down Expand Up @@ -100,7 +106,9 @@ private void loadImages(VBox stack, File initialFile, File initialFile2, File in
hb.getChildren().add(view);
Platform.runLater(() -> {
try {
HashMap<String, String> metaData = new LibrawImage(initialFile.getAbsolutePath(), new RawDecoderSettings()).getMetaData();
HashMap<String, RawDecoderSettings> settings = new HashMap<>();
settings.put("Default", new RawDecoderSettings());
HashMap<String, String> metaData = new LibrawImage(initialFile.getAbsolutePath(), settings).getMetaData();
ScrollPane sc = new ScrollPane();
TextArea vb = new TextArea();
metaData.entrySet().forEach((entry) -> {
Expand Down Expand Up @@ -130,7 +138,9 @@ private void loadImages(VBox stack, File initialFile, File initialFile2, File in
hb.getChildren().add(view2);
Platform.runLater(() -> {
try {
HashMap<String, String> metaData = new LibrawImage(initialFile2.getAbsolutePath(), new RawDecoderSettings()).getMetaData();
HashMap<String, RawDecoderSettings> settings = new HashMap<>();
settings.put("Default", new RawDecoderSettings());
HashMap<String, String> metaData = new LibrawImage(initialFile2.getAbsolutePath(), settings).getMetaData();
ScrollPane sc = new ScrollPane();
TextArea vb = new TextArea();
metaData.entrySet().forEach((entry) -> {
Expand Down Expand Up @@ -160,7 +170,9 @@ private void loadImages(VBox stack, File initialFile, File initialFile2, File in
hb.getChildren().add(view3);
Platform.runLater(() -> {
try {
HashMap<String, String> metaData = new LibrawImage(initialFile3.getAbsolutePath(), new RawDecoderSettings()).getMetaData();
HashMap<String, RawDecoderSettings> settings = new HashMap<>();
settings.put("Default", new RawDecoderSettings());
HashMap<String, String> metaData = new LibrawImage(initialFile3.getAbsolutePath(), settings).getMetaData();
ScrollPane sc = new ScrollPane();
TextArea vb = new TextArea();
metaData.entrySet().forEach((entry) -> {
Expand Down Expand Up @@ -200,8 +212,14 @@ private void loadImagesByFile(VBox stack, String file) throws IOException {
//ind.progressProperty().bind(img.progressProperty());
for (int i = 0; i < 1; i++) {
File initialFile = new File(file);
LibrawImage libraw = new LibrawImage(initialFile.getAbsolutePath(), new RawDecoderSettings());
System.out.println("CameraMaker "+libraw.getCameraMaker());
HashMap<String, RawDecoderSettings> settings = new HashMap<>();
settings.put("Default", new RawDecoderSettings());
settings.put("Sigma DP2 Merrill", new RawDecoderSettings());
settings.get("Sigma DP2 Merrill").setEnableExposureCorrection(true);
settings.get("Sigma DP2 Merrill").setExposureCorrection(1);
settings.get("Sigma DP2 Merrill").setExpoCorrectionShift(2.0f);
settings.get("Sigma DP2 Merrill").setBrightNess(6.0f);
LibrawImage libraw = new LibrawImage(initialFile.getAbsolutePath(), settings);
int[] raw = libraw.readPixelData();
WritableImage img = new WritableImage(libraw.getImageWidth(), libraw.getImageHeight());
PixelWriter pw = img.getPixelWriter();
Expand Down

0 comments on commit e4e4342

Please sign in to comment.