Beep6581 / RawTherapee

A powerful cross-platform raw photo processing program
https://rawtherapee.com
GNU General Public License v3.0
2.74k stars 311 forks source link

Segfault when opening compressed CR3 and cropped raw from Canon R5 #6279

Closed Thanatomanic closed 2 years ago

Thanatomanic commented 3 years ago

See here for the original report: https://discuss.pixls.us/t/canon-r5-cropped-raws/25441

This file does not decode, but instead segfaults R5__0596.ZIP. It is shot as a lossy compressed cRAW in APS-C mode with a crop factor applied. ExifTool tells me:

Compression=JPEG (old-style)
ImageWidth=ExifImageWidth=CroppedImageWidth=SourceImageWidth=5088
ImageHeight=ExifImageHeight=CroppedImageHeight=SourceImageWidth=3392
AspectRatio=3:2 (APS-C crop)
SensorWidth=5248
SensorHeight=3510
SensorLeftBorder=144
SensorTopBorder=108
SensorRightBorder=5231
SensorBottomBorder=3499

There are two issues at play.

In any case, it shouldn't segfault.

heckflosse commented 3 years ago

Here's a quick and very dirty patch to avoid the segfault. It's not meant to be committed, but as a starting point for a better patch.

diff --git a/rtengine/dcraw.cc b/rtengine/dcraw.cc
index ef0b4e8dc..e631d4828 100644
--- a/rtengine/dcraw.cc
+++ b/rtengine/dcraw.cc
@@ -4397,6 +4397,11 @@ void CLASS crop_masked_pixels()
       }
     }
   } else {
+    // dirty hack to show what's wrong and avoid segfault
+    if (height == 3510 && width == 5248 && !strncmp(model, "EOS R5", 6)) {
+        top_margin = 0;
+        left_margin = 0;
+    }
 #ifdef _OPENMP
 #pragma omp parallel for
 #endif

Maybe another dev ( @Thanatomanic ) can make a more correct patch.

Ingo

Thanatomanic commented 3 years ago

The main issue is that our camconst.json format can only handle one particular raw crop per camera, whereas this camera (and others like it) can have multiple depending on a shooting mode. The smaller raw image sizes are read correctly by dcraw, but the crop values in camconst indicate a bigger file, so too much memory is being read leading to the segfault. The actual segfault happens in this function https://github.com/Beep6581/RawTherapee/blob/166538dbcbb45ca86b3258b8615974ba2b8cee87/rtengine/dcraw.cc#L4371 I don't think we should be casually hacking that. A slightly better workaround may be similar to this https://github.com/Beep6581/RawTherapee/issues/6255

To be continued...

Thanatomanic commented 3 years ago

For reference, @agriggio has implemented raw size-dependent crops: https://bitbucket.org/agriggio/art/commits/c8df72cfd2b0c15fda1f5d5c3c5364161daff1bc

Floessie commented 3 years ago

I like how Alberto adopted my style for writing lambdas. :sunglasses:

virxkane commented 3 years ago

I guess same problem with CR3 raw files from Canon EOS R and 1.6 crop mode. log.txt current git 'dev' branch

EGD commented 3 years ago

Same problem with Canon EOS R6 (#6330).

heckflosse commented 3 years ago

I did some hacking. Here's a first result. Still needs some work.

grafik