Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Nikon Z7 uncompressed NEF files #4801

Closed
heckflosse opened this issue Sep 12, 2018 · 11 comments
Closed

Support for Nikon Z7 uncompressed NEF files #4801

heckflosse opened this issue Sep 12, 2018 · 11 comments
Assignees
Labels
patch provided scope: file format Camera or image file formats type: enhancement Something could be better than it currently is
Milestone

Comments

@heckflosse
Copy link
Collaborator

Currently only compressed files from Nikon Z7 are decoded correctly.
This first patch adds support for 12-bit uncompressed files. Still working on support for 14-bit uncompressed files.

diff --git a/rtengine/dcraw.cc b/rtengine/dcraw.cc
index bd9fa4b47..030946416 100644
--- a/rtengine/dcraw.cc
+++ b/rtengine/dcraw.cc
@@ -6692,6 +6692,9 @@ void CLASS apply_tiff()
 	  load_raw = &CLASS unpacked_load_raw;
 	  load_flags = 4;
 	  order = 0x4d4d;
+	} else if ((raw_width * raw_height * 2 * tiff_bps) / 16 == tiff_ifd[raw].bytes) {
+	    // 12 bit uncompressed from Nikon Z7
+	    load_raw = &CLASS packed_load_raw;
 	} else
 	  load_raw = &CLASS nikon_load_raw;			break;
       case 65535:
@heckflosse heckflosse added patch provided scope: file format Camera or image file formats labels Sep 12, 2018
@heckflosse heckflosse added this to the v5.5 milestone Sep 12, 2018
@heckflosse heckflosse self-assigned this Sep 12, 2018
@heckflosse
Copy link
Collaborator Author

heckflosse commented Sep 12, 2018

I forgot to mention that you can find Nikon Z7 files on raw.pixls.us. Just search for nikon z

@Desmis
Copy link
Collaborator

Desmis commented Sep 14, 2018

@heckflosse

Ingo
I tested , works fine, but the rawfile is mediocre :)

Jacques

@heckflosse
Copy link
Collaborator Author

@Desmis Jacques, thanks for testing. For the 14-bit uncompressed file I have no solution atm :(

@Desmis
Copy link
Collaborator

Desmis commented Sep 15, 2018

@heckflosse
Copy link
Collaborator Author

Currently I've no solution for Nikon Z7 uncompressed 14-bit files. I managed to decode them geometrically correct (correct width and height). But still the colours are far off (not caused by matrix, but caused by something else). I can add the code for correct width and height, so we don't have to think about that in future again. Then I would like to commit and move this issue from 5.5 to 5.6 milestone.

Any objections?

@Desmis
Copy link
Collaborator

Desmis commented Sep 17, 2018

@heckflosse
Ingo
No objections :)

@agriggio
Copy link
Contributor

14-bit uncompressed should now be supported by libraw:
https://github.com/LibRaw/LibRaw/blob/9e0bf8e225695d68d89cbe605136ea66e0ce17bf/src/libraw_cxx.cpp#L1909

@agriggio
Copy link
Contributor

Here's a patch for porting libraw's code.

diff --git a/rtengine/dcraw.cc b/rtengine/dcraw.cc
--- a/rtengine/dcraw.cc
+++ b/rtengine/dcraw.cc
@@ -6804,13 +6804,15 @@
 	} else if ((raw_width * 2 * tiff_bps / 16 + 8) * raw_height == tiff_ifd[raw].bytes) {
 	    // 14 bit uncompressed from Nikon Z7, still wrong
 	    // each line has 8 padding byte.
-	    row_padding = 8;
-	    load_raw = &CLASS packed_load_raw;
+	    //row_padding = 8;
+	    //load_raw = &CLASS packed_load_raw;
+            load_raw = &CLASS nikon_14bit_load_raw;
 	} else if ((raw_width * 2 * tiff_bps / 16 + 12) * raw_height == tiff_ifd[raw].bytes) {
 	    // 14 bit uncompressed from Nikon Z6, still wrong
 	    // each line has 12 padding byte.
-	    row_padding = 12;
-	    load_raw = &CLASS packed_load_raw;
+	    // row_padding = 12;
+	    // load_raw = &CLASS packed_load_raw;
+            load_raw = &CLASS nikon_14bit_load_raw;
 	} else
 	  load_raw = &CLASS nikon_load_raw;			break;
       case 65535:
@@ -10612,6 +10614,48 @@
 
 #include "fujicompressed.cc"
 
+//-----------------------------------------------------------------------------
+/* Taken from LibRaw
+ 
+LibRaw is free software; you can redistribute it and/or modify
+it under the terms of the one of two licenses as you choose:
+1. GNU LESSER GENERAL PUBLIC LICENSE version 2.1
+   (See file LICENSE.LGPL provided in LibRaw distribution archive for details).
+2. COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0
+   (See file LICENSE.CDDL provided in LibRaw distribution archive for details).
+ */ 
+
+namespace {
+
+inline void unpack7bytesto4x16_nikon(unsigned char *src, unsigned short *dest)
+{
+    dest[3] = (src[6] << 6) | (src[5] >> 2);
+    dest[2] = ((src[5] & 0x3) << 12) | (src[4] << 4) | (src[3] >> 4);
+    dest[1] = (src[3] & 0xf) << 10 | (src[2] << 2) | (src[1] >> 6);
+    dest[0] = ((src[1] & 0x3f) << 8) | src[0];
+}
+
+} // namespace
+
+void CLASS nikon_14bit_load_raw()
+{
+    const unsigned linelen = (unsigned)(ceilf((float)(raw_width * 7 / 4) / 16.0)) * 16; // 14512; // S.raw_width * 7 / 4;
+    const unsigned pitch = raw_width; //S.raw_pitch ? S.raw_pitch / 2 : S.raw_width;
+    unsigned char *buf = (unsigned char *)malloc(linelen);
+    merror(buf, "nikon_14bit_load_raw()");
+    for (int row = 0; row < raw_height; row++)
+    {
+        unsigned bytesread = fread(buf, 1, linelen, ifp);
+        unsigned short *dest = &raw_image[pitch * row];
+        //swab32arr((unsigned *)buf, bytesread / 4);
+        for (int sp = 0, dp = 0; dp < pitch - 3 && sp < linelen - 6 && sp < bytesread - 6; sp += 7, dp += 4)
+            unpack7bytesto4x16_nikon(buf + sp, dest + dp);
+    }
+    free(buf);
+}
+
+//-----------------------------------------------------------------------------
+
 /* RT: Delete from here */
 /*RT*/#undef SQR
 /*RT*/#undef MAX
diff --git a/rtengine/dcraw.h b/rtengine/dcraw.h
--- a/rtengine/dcraw.h
+++ b/rtengine/dcraw.h
@@ -517,6 +517,8 @@
     }
 }
 
+void nikon_14bit_load_raw(); // ported from LibRaw
+
 };
  

@Beep6581 Beep6581 added the type: enhancement Something could be better than it currently is label Feb 10, 2019
@gaaned92
Copy link

patch included in W64 nightly builds https://keybase.pub/gaaned92/RTW64NightlyBuilds/
Not tested by me.

@Beep6581
Copy link
Owner

@agriggio as the 5.6 feature-freeze is in 2-4 weeks, ok to commit the patch to get more testing?

@Beep6581
Copy link
Owner

All Nikon Z 7 raw file types from RPU open fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
patch provided scope: file format Camera or image file formats type: enhancement Something could be better than it currently is
Projects
None yet
Development

No branches or pull requests

5 participants