Skip to content

Commit

Permalink
feat(raw): make the crop match in-camera JPEG (AcademySoftwareFoundat…
Browse files Browse the repository at this point in the history
…ion#4397)

Make the 'display window' match the default crop, which is applied to
in-camera JPEG.

---------

Signed-off-by: Anton Dukhovnikov <[email protected]>
Signed-off-by: Zach Lewis <[email protected]>
  • Loading branch information
antond-weta authored and zachlewis committed Sep 16, 2024
1 parent acb513a commit f784415
Show file tree
Hide file tree
Showing 16 changed files with 148 additions and 4 deletions.
9 changes: 9 additions & 0 deletions src/doc/builtinplugins.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2086,6 +2086,15 @@ options are supported:
equal to 0, ``raw:use_auto_wb`` is not equal to 0. Takes
precedence over ``raw:user_mul``.
(Default: 0, 0, 0, 0; meaning no correction.)
* - ``raw:cropbox``
- int[4]
- If present, sets the box to crop the image to. The four values are the
X and Y coordinate of the top-left corner, the width and the height.
If not present, the image is cropped to match the in-camera JPEG,
assuming the necessary information is present in the metadata. The
cropping is done by setting the display window, so the whole image
pixels are still available. The default cropping can be disabled by
setting the cropbox to zero size.
* - ``raw:use_camera_matrix``
- int
- Whether to use the embedded color profile, if it's present: 0 =
Expand Down
71 changes: 71 additions & 0 deletions src/raw.imageio/rawinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,77 @@ RawInput::open_raw(bool unpack, const std::string& name,
m_spec.attribute("raw:Demosaic", "AHD");
}

// Apply crop. If the user has provided a custom crop with `raw:cropbox`,
// use that. Otherwise crop to the imgdata.sizes.raw_inset_crops[0] if
// available. That should match the crop used in the in-camera jpeg in
// most cases. The crop is set as a 'display window', so the whole image
// pixels are still available.
{
ushort crop_left = 0;
ushort crop_top = 0;
ushort crop_width = 0;
ushort crop_height = 0;
ushort left_margin = 0;
ushort top_margin = 0;

auto p = config.find_attribute("raw:cropbox");
if (p && p->type() == TypeDesc(TypeDesc::INT, 4)) {
crop_left = p->get_int_indexed(0);
crop_top = p->get_int_indexed(1);
crop_width = p->get_int_indexed(2);
crop_height = p->get_int_indexed(3);
} else if (m_processor->imgdata.sizes.raw_inset_crops[0].cwidth != 0) {
crop_left = m_processor->imgdata.sizes.raw_inset_crops[0].cleft;
crop_top = m_processor->imgdata.sizes.raw_inset_crops[0].ctop;
crop_width = m_processor->imgdata.sizes.raw_inset_crops[0].cwidth;
crop_height = m_processor->imgdata.sizes.raw_inset_crops[0].cheight;
left_margin = m_processor->imgdata.sizes.left_margin;
top_margin = m_processor->imgdata.sizes.top_margin;
}

if (crop_width > 0 && crop_height > 0) {
ushort image_width = m_processor->imgdata.sizes.width;
ushort image_height = m_processor->imgdata.sizes.height;

// If crop_left is undefined, assume central crop.
if (crop_left == 65535) {
crop_left = (image_width - crop_width) / 2;
}

// If crop_top is undefined, assume central crop.
if (crop_top == 65535) {
crop_top = (image_height - crop_height) / 2;
}

if (m_processor->imgdata.sizes.flip & 1) {
crop_left = image_width - crop_width - crop_left;
}

if (m_processor->imgdata.sizes.flip & 2) {
crop_top = image_height - crop_height - crop_top;
}

if (crop_top >= top_margin && crop_left >= left_margin) {
crop_top -= top_margin;
crop_left -= left_margin;

if (m_processor->imgdata.sizes.flip & 4) {
std::swap(crop_left, crop_top);
std::swap(crop_width, crop_height);
std::swap(image_width, image_height);
}

if ((crop_left + crop_width <= image_width)
&& (crop_top + crop_height <= image_height)) {
m_spec.full_x = crop_left;
m_spec.full_y = crop_top;
m_spec.full_width = crop_width;
m_spec.full_height = crop_height;
}
}
}
}

// Wavelets denoise before demosaic
// Use wavelets to erase noise while preserving real detail.
// The best threshold should be somewhere between 100 and 1000.
Expand Down
Binary file modified testsuite/raw/ref/RAW_CANON_EOS_7D.CR2.tif
Binary file not shown.
Binary file modified testsuite/raw/ref/RAW_FUJI_F700.RAF.tif
Binary file not shown.
Binary file modified testsuite/raw/ref/RAW_NIKON_D3X.NEF.tif
Binary file not shown.
Binary file modified testsuite/raw/ref/RAW_OLYMPUS_E3.ORF.tif
Binary file not shown.
Binary file modified testsuite/raw/ref/RAW_PANASONIC_G1.RW2.tif
Binary file not shown.
Binary file modified testsuite/raw/ref/RAW_PENTAX_K200D.PEF.tif
Binary file not shown.
Binary file modified testsuite/raw/ref/RAW_SONY_A300.ARW.tif
Binary file not shown.
8 changes: 8 additions & 0 deletions testsuite/raw/ref/out-libraw-0.20.2-gh.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
../oiio-images/raw/RAW_CANON_EOS_7D.CR2 : 5202 x 3464, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 5184 x 3456
full/display origin: 10, 4
DateTime: "2009-10-09 14:18:45"
ExposureTime: 0.003125
FNumber: 2.8
Expand Down Expand Up @@ -340,6 +342,8 @@
raw:rgb_cam: 1.63112, -0.49571, -0.135412, 0, -0.0198918, 1.37117, -0.351275, 0, 0.0569094, -0.396354, 1.33944, 0
../oiio-images/raw/RAW_OLYMPUS_E3.ORF : 3720 x 2800, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3648 x 2736
full/display origin: 46, 56
DateTime: "2008-12-19 12:29:40"
ExposureTime: 0.003125
FNumber: 2
Expand Down Expand Up @@ -412,6 +416,8 @@
raw:rgb_cam: 1.51654, -0.430227, -0.0863105, 0, -0.0494907, 1.30045, -0.250959, 0, 0.0301381, -0.345784, 1.31565, 0
../oiio-images/raw/RAW_PENTAX_K200D.PEF : 2616 x 3896, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 2592 x 3872
full/display origin: 8, 16
DateTime: "2008-08-02 16:46:42"
ExposureTime: 0.004
FNumber: 8
Expand Down Expand Up @@ -477,6 +483,8 @@
raw:rgb_cam: 1.57863, -0.445627, -0.133005, 0, 0.00253742, 1.23613, -0.23867, 0, 0.0130546, -0.219968, 1.20691, 0
../oiio-images/raw/RAW_SONY_A300.ARW : 3880 x 2608, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3872 x 2592
full/display origin: 4, 8
DateTime: "2008-08-10 23:16:11"
ExposureTime: 0.0166667
FNumber: 5.6
Expand Down
10 changes: 10 additions & 0 deletions testsuite/raw/ref/out-libraw-0.20.2.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
../oiio-images/raw/RAW_CANON_EOS_7D.CR2 : 5202 x 3464, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 5184 x 3456
full/display origin: 10, 4
DateTime: "2009-10-09 14:18:45"
ExposureTime: 0.003125
FNumber: 2.8
Expand Down Expand Up @@ -340,6 +342,8 @@
raw:rgb_cam: 1.63112, -0.49571, -0.135412, 0, -0.0198918, 1.37117, -0.351275, 0, 0.0569094, -0.396354, 1.33944, 0
../oiio-images/raw/RAW_OLYMPUS_E3.ORF : 3720 x 2800, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3648 x 2736
full/display origin: 46, 56
DateTime: "2008-12-19 12:29:40"
ExposureTime: 0.003125
FNumber: 2
Expand Down Expand Up @@ -412,6 +416,8 @@
raw:rgb_cam: 1.51654, -0.430227, -0.0863105, 0, -0.0494907, 1.30045, -0.250959, 0, 0.0301381, -0.345784, 1.31565, 0
../oiio-images/raw/RAW_PANASONIC_G1.RW2 : 4016 x 3016, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 4000 x 3000
full/display origin: 8, 4
DateTime: "2008-12-10 15:06:33"
ExposureTime: 0.0025
FNumber: 6.3
Expand Down Expand Up @@ -450,6 +456,8 @@
raw:rgb_cam: 1.57485, -0.535818, -0.0390291, 0, -0.0341545, 1.28465, -0.250491, 0, 0.0659772, -0.433534, 1.36756, 0
../oiio-images/raw/RAW_PENTAX_K200D.PEF : 2616 x 3896, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 2592 x 3872
full/display origin: 8, 16
DateTime: "2008-08-02 16:46:42"
ExposureTime: 0.004
FNumber: 8
Expand Down Expand Up @@ -515,6 +523,8 @@
raw:rgb_cam: 1.57863, -0.445627, -0.133005, 0, 0.00253742, 1.23613, -0.23867, 0, 0.0130546, -0.219968, 1.20691, 0
../oiio-images/raw/RAW_SONY_A300.ARW : 3880 x 2608, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3872 x 2592
full/display origin: 4, 8
DateTime: "2008-08-10 23:16:11"
ExposureTime: 0.0166667
FNumber: 5.6
Expand Down
8 changes: 8 additions & 0 deletions testsuite/raw/ref/out-libraw0.20.0-gh.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
../oiio-images/raw/RAW_CANON_EOS_7D.CR2 : 5202 x 3464, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 5184 x 3456
full/display origin: 10, 4
DateTime: "2009-10-09 14:18:45"
ExposureTime: 0.003125
FNumber: 2.8
Expand Down Expand Up @@ -340,6 +342,8 @@
raw:rgb_cam: 1.63112, -0.49571, -0.135412, 0, -0.0198918, 1.37117, -0.351275, 0, 0.0569094, -0.396354, 1.33944, 0
../oiio-images/raw/RAW_OLYMPUS_E3.ORF : 3720 x 2800, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3648 x 2736
full/display origin: 46, 56
DateTime: "2008-12-19 12:29:40"
ExposureTime: 0.003125
FNumber: 2
Expand Down Expand Up @@ -412,6 +416,8 @@
raw:rgb_cam: 1.51654, -0.430227, -0.0863105, 0, -0.0494907, 1.30045, -0.250959, 0, 0.0301381, -0.345784, 1.31565, 0
../oiio-images/raw/RAW_PENTAX_K200D.PEF : 2616 x 3896, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 2592 x 3872
full/display origin: 8, 16
DateTime: "2008-08-02 16:46:42"
ExposureTime: 0.004
FNumber: 8
Expand Down Expand Up @@ -477,6 +483,8 @@
raw:rgb_cam: 1.57863, -0.445627, -0.133005, 0, 0.00253742, 1.23613, -0.23867, 0, 0.0130546, -0.219968, 1.20691, 0
../oiio-images/raw/RAW_SONY_A300.ARW : 3880 x 2608, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3872 x 2592
full/display origin: 4, 8
DateTime: "2008-08-10 23:16:11"
ExposureTime: 0.0166667
FNumber: 5.6
Expand Down
10 changes: 10 additions & 0 deletions testsuite/raw/ref/out-libraw0.20.0.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
../oiio-images/raw/RAW_CANON_EOS_7D.CR2 : 5202 x 3464, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 5184 x 3456
full/display origin: 10, 4
DateTime: "2009-10-09 14:18:45"
ExposureTime: 0.003125
FNumber: 2.8
Expand Down Expand Up @@ -340,6 +342,8 @@
raw:rgb_cam: 1.63112, -0.49571, -0.135412, 0, -0.0198918, 1.37117, -0.351275, 0, 0.0569094, -0.396354, 1.33944, 0
../oiio-images/raw/RAW_OLYMPUS_E3.ORF : 3720 x 2800, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3648 x 2736
full/display origin: 46, 56
DateTime: "2008-12-19 12:29:40"
ExposureTime: 0.003125
FNumber: 2
Expand Down Expand Up @@ -412,6 +416,8 @@
raw:rgb_cam: 1.51654, -0.430227, -0.0863105, 0, -0.0494907, 1.30045, -0.250959, 0, 0.0301381, -0.345784, 1.31565, 0
../oiio-images/raw/RAW_PANASONIC_G1.RW2 : 4016 x 3016, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 4000 x 3000
full/display origin: 8, 4
DateTime: "2008-12-10 15:06:33"
ExposureTime: 0.0025
FNumber: 6.3
Expand Down Expand Up @@ -450,6 +456,8 @@
raw:rgb_cam: 1.57485, -0.535818, -0.0390291, 0, -0.0341545, 1.28465, -0.250491, 0, 0.0659772, -0.433534, 1.36756, 0
../oiio-images/raw/RAW_PENTAX_K200D.PEF : 2616 x 3896, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 2592 x 3872
full/display origin: 8, 16
DateTime: "2008-08-02 16:46:42"
ExposureTime: 0.004
FNumber: 8
Expand Down Expand Up @@ -515,6 +523,8 @@
raw:rgb_cam: 1.57863, -0.445627, -0.133005, 0, 0.00253742, 1.23613, -0.23867, 0, 0.0130546, -0.219968, 1.20691, 0
../oiio-images/raw/RAW_SONY_A300.ARW : 3880 x 2608, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3872 x 2592
full/display origin: 4, 8
DateTime: "2008-08-10 23:16:11"
ExposureTime: 0.0166667
FNumber: 5.6
Expand Down
12 changes: 10 additions & 2 deletions testsuite/raw/ref/out-libraw0.21.0-gh.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
../oiio-images/raw/RAW_CANON_EOS_7D.CR2 : 5202 x 3464, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 5184 x 3456
full/display origin: 10, 4
DateTime: "2009-10-09 14:18:45"
ExposureTime: 0.003125
FNumber: 2.8
Expand Down Expand Up @@ -329,6 +331,8 @@
raw:rgb_cam: 1.63112, -0.49571, -0.135412, 0, -0.0198918, 1.37117, -0.351275, 0, 0.0569094, -0.396354, 1.33944, 0
../oiio-images/raw/RAW_OLYMPUS_E3.ORF : 3720 x 2800, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3648 x 2736
full/display origin: 46, 56
DateTime: "2008-12-19 12:29:40"
ExposureTime: 0.003125
FNumber: 2
Expand Down Expand Up @@ -402,6 +406,8 @@
raw:rgb_cam: 1.51654, -0.430227, -0.0863105, 0, -0.0494907, 1.30045, -0.250959, 0, 0.0301381, -0.345784, 1.31565, 0
../oiio-images/raw/RAW_PENTAX_K200D.PEF : 2616 x 3896, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 2592 x 3872
full/display origin: 8, 16
DateTime: "2008-08-02 16:46:42"
ExposureTime: 0.004
FNumber: 8
Expand Down Expand Up @@ -467,6 +473,8 @@
raw:rgb_cam: 1.57863, -0.445627, -0.133005, 0, 0.00253742, 1.23613, -0.23867, 0, 0.0130546, -0.219968, 1.20691, 0
../oiio-images/raw/RAW_SONY_A300.ARW : 3880 x 2608, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3872 x 2592
full/display origin: 4, 8
DateTime: "2008-08-10 23:16:11"
ExposureTime: 0.0166667
FNumber: 5.6
Expand Down Expand Up @@ -533,11 +541,11 @@
Sony:ImageStabilization: 1
Sony:LensID: 40
Sony:LensMount: 25
Comparing "RAW_CANON_EOS_7D.CR2.tif" and "ref/RAW_CANON_EOS_7D.CR2-libraw0.20.0.tif"
Comparing "RAW_CANON_EOS_7D.CR2.tif" and "ref/RAW_CANON_EOS_7D.CR2.tif"
PASS
Comparing "RAW_NIKON_D3X.NEF.tif" and "ref/RAW_NIKON_D3X.NEF.tif"
PASS
Comparing "RAW_FUJI_F700.RAF.tif" and "ref/RAW_FUJI_F700.RAF-libraw0.21.tif"
Comparing "RAW_FUJI_F700.RAF.tif" and "ref/RAW_FUJI_F700.RAF.tif"
PASS
Comparing "RAW_NIKON_D3X.NEF.tif" and "ref/RAW_NIKON_D3X.NEF.tif"
PASS
Expand Down
14 changes: 12 additions & 2 deletions testsuite/raw/ref/out-libraw0.21.0-mac.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
../oiio-images/raw/RAW_CANON_EOS_7D.CR2 : 5202 x 3464, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 5184 x 3456
full/display origin: 10, 4
DateTime: "2009-10-09 14:18:45"
ExposureTime: 0.003125
FNumber: 2.8
Expand Down Expand Up @@ -329,6 +331,8 @@
raw:rgb_cam: 1.63112, -0.49571, -0.135412, 0, -0.0198918, 1.37117, -0.351275, 0, 0.0569094, -0.396354, 1.33944, 0
../oiio-images/raw/RAW_OLYMPUS_E3.ORF : 3720 x 2800, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3648 x 2736
full/display origin: 46, 56
DateTime: "2008-12-19 12:29:40"
ExposureTime: 0.003125
FNumber: 2
Expand Down Expand Up @@ -402,6 +406,8 @@
raw:rgb_cam: 1.51654, -0.430227, -0.0863105, 0, -0.0494907, 1.30045, -0.250959, 0, 0.0301381, -0.345784, 1.31565, 0
../oiio-images/raw/RAW_PANASONIC_G1.RW2 : 4016 x 3016, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 4000 x 3000
full/display origin: 8, 4
DateTime: "2008-12-10 15:06:33"
ExposureTime: 0.0025
FNumber: 6.3
Expand Down Expand Up @@ -440,6 +446,8 @@
raw:rgb_cam: 1.57485, -0.535818, -0.0390291, 0, -0.0341545, 1.28465, -0.250491, 0, 0.0659772, -0.433534, 1.36756, 0
../oiio-images/raw/RAW_PENTAX_K200D.PEF : 2616 x 3896, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 2592 x 3872
full/display origin: 8, 16
DateTime: "2008-08-02 16:46:42"
ExposureTime: 0.004
FNumber: 8
Expand Down Expand Up @@ -505,6 +513,8 @@
raw:rgb_cam: 1.57863, -0.445627, -0.133005, 0, 0.00253742, 1.23613, -0.23867, 0, 0.0130546, -0.219968, 1.20691, 0
../oiio-images/raw/RAW_SONY_A300.ARW : 3880 x 2608, 3 channel, uint16 raw
channel list: R, G, B
full/display size: 3872 x 2592
full/display origin: 4, 8
DateTime: "2008-08-10 23:16:11"
ExposureTime: 0.0166667
FNumber: 5.6
Expand Down Expand Up @@ -571,11 +581,11 @@
Sony:ImageStabilization: 1
Sony:LensID: 40
Sony:LensMount: 25
Comparing "RAW_CANON_EOS_7D.CR2.tif" and "ref/RAW_CANON_EOS_7D.CR2-libraw0.20.0.tif"
Comparing "RAW_CANON_EOS_7D.CR2.tif" and "ref/RAW_CANON_EOS_7D.CR2.tif"
PASS
Comparing "RAW_NIKON_D3X.NEF.tif" and "ref/RAW_NIKON_D3X.NEF.tif"
PASS
Comparing "RAW_FUJI_F700.RAF.tif" and "ref/RAW_FUJI_F700.RAF-libraw0.21.tif"
Comparing "RAW_FUJI_F700.RAF.tif" and "ref/RAW_FUJI_F700.RAF.tif"
PASS
Comparing "RAW_NIKON_D3X.NEF.tif" and "ref/RAW_NIKON_D3X.NEF.tif"
PASS
Expand Down
Loading

0 comments on commit f784415

Please sign in to comment.