Beep6581 / RawTherapee

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

LCP (Lens Correction Profile) support #1327

Closed Beep6581 closed 9 years ago

Beep6581 commented 9 years ago

Originally reported on Google Code with ID 1343

Sorry, no patch yet, but following our new process of announcing when you begin a larger
endavour, I just want to mention that I'll try to develop a RT LCP profile engine.

There are not only Adobe official LCP profiles available, there also seem to be a community
of user generated profiles. There also is good tool support to generate these profiles
yourself.
http://labs.adobe.com/technologies/lensprofile_creator/

First version is planned to just cover automatic distortion correction. The UI will
be super simple, a selection of the LCP file in the style of the existing DCP profile
selection. The other corrections, auto-searching etc. will be kept up for later versions.

Will take a while though, no hurry.

Reported by oduis@hotmail.com on 2012-04-26 19:32:29

Beep6581 commented 9 years ago
Little update: the feature will use Expat v2.x as XML parser:
http://www.libexpat.org/
Expat is very easy to build, so it should be no hassle.
It is also used by the "big" XMP Reference Library, so if XMP branch comes alive it
will be compatible.

Reported by oduis@hotmail.com on 2012-04-30 09:52:14

Beep6581 commented 9 years ago
Another update: the LCP profile file parser and file cache is working now. Next is some
little V1 UI and the usage in RT.

Reported by oduis@hotmail.com on 2012-05-06 16:57:51

Beep6581 commented 9 years ago
Super! Olli, would you like me to help at this stage?

Reported by michaelezra000 on 2012-05-06 17:14:14

Beep6581 commented 9 years ago
Thanks Michael. I think it would be helpful if Emil could chime in for the math (the
UI will be very simple), however I remember he said he's still busy.

Reported by oduis@hotmail.com on 2012-05-06 19:48:11

Beep6581 commented 9 years ago
Update, 550 lines later: GUI is also done (similar like darkframe file selection, with
a fix), so it should all be ready to be embedded in the pipeline...

Reported by oduis@hotmail.com on 2012-05-08 21:37:12

Beep6581 commented 9 years ago
I use FlatField for all my shots and all camera-lens permutations, and it works perfect
for vignetting and dustspecks removal.

The LCP profiles will take care of vignetting but not dust, so there will be a need
to avoid interference between LCP and FF for vignetting.

I have a few cameras and lenses; if weather holds I will post here a Colorchecker shot
from a Nikon 5100, and later on I will try to play with the Adobe' LCP profile creator.
If I am successful you'll have my contribution.

Reported by RitaLaCorte on 2012-05-14 08:23:25

Beep6581 commented 9 years ago
V1 will be distortion-only, as outlined above. But thanks for the hint, I'll keep it
in mind for LCP vignette.
A color checker reference RAW for Nikon D5100 would be helpful :-)
I think RT does not need to provide it's own LCPs though, since the Adobe community
is vibrant it seems.

Reported by oduis@hotmail.com on 2012-05-14 16:18:42

Beep6581 commented 9 years ago
As a regular flatfield@dust-removal user I'd also ask that future LCP support not overdo
the FF light falloff correction ;]

Reported by entertheyoni on 2012-05-15 08:32:39

Beep6581 commented 9 years ago
Hey, I never planned to do that ;-)

Does anyone have a Lightroom installation with LCP support? I got one image that's
not corrected as I expected, however I heared that LR and some profiles are also not
perfect.

Reported by oduis@hotmail.com on 2012-05-15 17:19:08

Beep6581 commented 9 years ago
After much work and curses (found two "bugs" in the Adobe documentation), here is the
new LCP engine. It correct geometric distortions and vignette.  :-)

The lcp.cc/h must go to rtengine dir. For compiling you need expat 2.x (http://www.libexpat.org/),
Win64 users can download the updated precompiled package from my download page (http://www.visualbakery.com/RawTherapee/Downloads.aspx)

Here is a demo, correcting pincushion as well as vignetting:
http://www.visualbakery.com/RawTherapee/Download/LCPDemo2.jpg
I also made some little speedups in distortion code, so it should be fast.

Please only test using official Adobe LCPs, not self generated or community ones. You
can do a lot wrong creating them, so just safe ones for testing.

Reported by oduis@hotmail.com on 2012-05-16 16:16:00

Beep6581 commented 9 years ago
Where canI download profiles? Is there a central place? I couldn't find anything apart
from few forum threads with few lense profiles. 

Or does it all come with Photoshop?

Reported by skraft61 on 2012-05-16 18:39:26

Beep6581 commented 9 years ago
See link above, if you click through there is a downloader program for the community
ones.
I think the offical ones are installed by Adobe programs, I have them on my system,
and I only have the free Adobe DNG Converter. Could be a remainder of an old trial
installation of Lightroom I uninstalled though.

PS: Only RAW profiles work correctly, not JPG profiles.

Reported by oduis@hotmail.com on 2012-05-16 19:15:27

Beep6581 commented 9 years ago
Ok, there is a trick to get an offical profile (if Adobe has one):
Download the latest Adobes Camera Raw update (6.7 as of writing), unpack it to disk.
Don't start it, won't work.
No go to the payloads folder and the camera profiles folder. Unpack the Asset*.zip
files in there.
Go to command line in that folder and make "ren *.* *.xml" to add an xml extension
to them.
Now use Windows search (or grep or whatever) to find a keyword WITHIN the file e.g.
"Nikkor 24-120". You'll get many hits, just open them e.g. with notepad to have a look
inside. You'll see the lens name in there.
If you've found your lens, just rename the file with a nicer name e.g. "my24_120.dcp"
and you're good to go.

Reported by oduis@hotmail.com on 2012-05-16 20:43:47

Beep6581 commented 9 years ago
Olli, this is great! I will try to test tonight or tomorrow to give you some feedback
(got swamped with work@work and distracted from the fun stuff:)

Reported by michaelezra000 on 2012-05-17 17:03:02

Beep6581 commented 9 years ago
Great Michael. So far no bug reports, so it seems to be safe. Will commit after the
usual 24h period.

Reported by oduis@hotmail.com on 2012-05-17 18:52:57

Beep6581 commented 9 years ago
Commited to DEFAULT. Thanks for testing skraft61.

Reported by oduis@hotmail.com on 2012-05-17 19:43:00

Beep6581 commented 9 years ago
After applying that commit i can't compile rawtherapee any more.

It complains:
-- checking for module 'expat>=2.0'
--   package 'expat>=2.0' not found
CMake Error at /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake:266 (message):
  A required package was not found

But acually I do have libexpat1-dev installed, version 2.0.1-7.2ubuntu1

This seems like a bug either in CMakeLists.txt or in cmake itself.

Ubuntu 12.04 64-bit

Reported by iliaworld@yandex.ru on 2012-05-17 22:19:13

Beep6581 commented 9 years ago
I get this in a clean clone:
(...)

-- Configuring done
CMake Error at rtgui/CMakeLists.txt:50 (add_executable):
  Cannot find source file:

    lensprofile.cc

  Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
  .hxx .in .txx

Indeed that file is nowhere in the repo.

"Will commit after the usual 24h period. (3 hours ago)"
"Commited to DEFAULT. (2 hours ago)"

What happened there? :]

Reported by entertheyoni on 2012-05-17 22:38:58

Beep6581 commented 9 years ago
Sorry, I forgot two files lensprof.h/cc in checkin.

> Will commit after the usual 24h period.
The period starts after the patch was submitted for testing. Which was 27h later ;-)

As for the expat: the version check is exactly like for the other versions:
kg_check_modules (GIOMM   REQUIRED giomm-2.4>=2.12)
pkg_check_modules (GTHREAD REQUIRED gthread-2.0>=2.16)
pkg_check_modules (GOBJECT REQUIRED gobject-2.0>=2.16)
...
pkg_check_modules (EXPAT REQUIRED expat>=2.0)

And it works here and probably also with DrSlony, how is also using Linux.

Reported by oduis@hotmail.com on 2012-05-18 05:46:32

Beep6581 commented 9 years ago
same message than DrSlony...i can't compile rawtherapee any more.

Reported by jdesmis on 2012-05-18 05:56:42

Beep6581 commented 9 years ago
Sorry, I just committed the files missing from the commit.

Reported by oduis@hotmail.com on 2012-05-18 06:01:36

Beep6581 commented 9 years ago
Sorry I wanted to try yesterday but did not have time to start this. So here's my feedback
now:

At least on Ubuntu libexpat1-dev does not come with a pkg-config file. No idea why
that's missing there...

So replacing 

> pkg_check_modules (EXPAT REQUIRED expat>=2.0)

by

> find_package (EXPAT REQUIRED expat>=2.0)

in CMakeLists.txt will find the required package.

Afterwards compilation fails:
[ 31%] Building CXX object rtengine/CMakeFiles/rtengine.dir/dcp.cc.o
/home/daseeb/devel/rawtherapee-clean/rtengine/lcp.cc: In statischer Elementfunktion
»static void rtengine::LCPProfile::XmlStartHandler(void*, const char*, const char**)«:
/home/daseeb/devel/rawtherapee-clean/rtengine/lcp.cc:209:29: Fehler: ungültige Umwandlung
von »const char*« in »char*« [-fpermissive]
make[2]: *** [rtengine/CMakeFiles/rtengine.dir/lcp.cc.o] Fehler 1

I am on Ubuntu 12.04 with gcc 4.6.3

Reported by skraft61 on 2012-05-18 07:02:17

Beep6581 commented 9 years ago
Oh that was easy ;) Just replace lpc.cc:209 with const char* src=strrchr(el,':');

Reported by skraft61 on 2012-05-18 07:05:24

Beep6581 commented 9 years ago
No luck today... now it crashes. Probably "LCPModelCommon corr" ist not allocated?

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffe50cf700 (LWP 7715)]
0x00000000009c8109 in rtengine::LCPProfile::calcBasePerspectiveParams (this=0x7fffa0093b20,
focalLength=18, vignette=true, corr=...)
    at /home/daseeb/devel/rawtherapee-clean/rtengine/lcp.cc:189
189     corr.focLenX    = facLow * mLow.focLenX    + facHigh * mHigh.focLenX;
(gdb) bt
#0  0x00000000009c8109 in rtengine::LCPProfile::calcBasePerspectiveParams (this=0x7fffa0093b20,
focalLength=18, vignette=true, corr=...)
    at /home/daseeb/devel/rawtherapee-clean/rtengine/lcp.cc:189
#1  0x00000000009c750a in rtengine::LCPMapper::LCPMapper (this=0x7fffa0004070, pProf=0x7fffa0093b20,
focalLength=18, vignette=true, 
    fullWidth=3896, fullHeight=2616, coarse=..., rawRotationDeg=270) at /home/daseeb/devel/rawtherapee-clean/rtengine/lcp.cc:78
#2  0x00000000008bb11c in rtengine::RawImageSource::preprocess (this=0x7fff9800a000,
raw=..., lensProf=..., coarse=...)
    at /home/daseeb/devel/rawtherapee-clean/rtengine/rawimagesource.cc:1022
#3  0x000000000090c680 in rtengine::ImProcCoordinator::updatePreviewImage (this=0x7069400,
todo=1023, cropCall=0x0)
    at /home/daseeb/devel/rawtherapee-clean/rtengine/improccoordinator.cc:125
#4  0x0000000000910160 in rtengine::ImProcCoordinator::process (this=0x7069400)
    at /home/daseeb/devel/rawtherapee-clean/rtengine/improccoordinator.cc:656
#5  0x0000000000913333 in sigc::bound_mem_functor0<void, rtengine::ImProcCoordinator>::operator()
(this=0x676bf98)
    at /usr/include/sigc++-2.0/sigc++/functors/mem_fun.h:1787
#6  0x00000000009130a4 in sigc::adaptor_functor<sigc::bound_mem_functor0<void, rtengine::ImProcCoordinator>
>::operator() (this=0x676bf90)
    at /usr/include/sigc++-2.0/sigc++/adaptors/adaptor_trait.h:251
#7  0x0000000000912c73 in sigc::internal::slot_call0<sigc::bound_mem_functor0<void,
rtengine::ImProcCoordinator>, void>::call_it (rep=0x676bf60)
    at /usr/include/sigc++-2.0/sigc++/functors/slot.h:103
#8  0x00007ffff6d8b87d in ?? () from /usr/lib/x86_64-linux-gnu/libglibmm-2.4.so.1
#9  0x00007ffff72259a5 in ?? () from /lib/x86_64-linux-gnu/libglib-2.0.so.0
#10 0x00007ffff3b20e9a in start_thread (arg=0x7fffe50cf700) at pthread_create.c:308
#11 0x00007ffff384e4bd in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:112

Reported by skraft61 on 2012-05-18 07:34:16

Beep6581 commented 9 years ago
Is expat 2.* called libexpat1-dev (I stress the 1) in Ubuntu?

Reported by entertheyoni on 2012-05-18 10:37:54

Beep6581 commented 9 years ago
Looks strange, but yes it is
http://packages.ubuntu.com/precise/libexpat1-dev

Reported by skraft61 on 2012-05-18 11:23:15

Beep6581 commented 9 years ago
Hi Olli, thanks again for this great addition.
It was interesting to read the patch.

There is a small problem with lcp filename sticking - when new file is opened without
lcp, lcp filename from previous file is retained.
(Hopefully this weekend) I will do some comparisons in applying the same profile in
ACR and RT.

Reported by michaelezra000 on 2012-05-18 11:35:26

Beep6581 commented 9 years ago
Hi Michael,
Was difficult to repro it, till I mentioned that it just happened in single tab mode.
Here is the patch to correct that.

Hi skraft61,
I could add the const* in that patch which obviously disturbed your version of GCC
(my older version did not complain).
The expat problem is strange though. I think the problems are linked, it would just
throw that exception when the xml parsing with expat seriously hosed up, and it’s suspicious
that cmake does not find a correct installation using the standard routines on your
config.
Could you install the current 2.1 from the official webpage to be sure, not some expat1
something like now?

I think DrSlony also uses Linux and he had not expat compile problems (I hope).

Reported by oduis@hotmail.com on 2012-05-18 12:48:42

Beep6581 commented 9 years ago
Although it is named expat1 it is version 2.0. See http://packages.ubuntu.com/precise/libexpat1-dev

But I will give version 2.1 a try this evening. At least the changelog says there is
pkg-config support added in this release. http://sourceforge.net/projects/expat/files/expat/2.1.0/
This should solve the pkg_check_modules() problem with cmake.

Reported by skraft61 on 2012-05-18 13:39:14

Beep6581 commented 9 years ago
This also adds replacement for find_package. I just tried it with some older version
on a Win32 and Cmake had the same problem. So this is probably better backward compatible.

Reported by oduis@hotmail.com on 2012-05-18 15:36:25


Beep6581 commented 9 years ago
With this last patch compilation runs fine as expected. 

Sadly I could not succeed in installing expat 2.1 as this would break package dependencies
and I don't want to risk this on my production system. So my current version is sticked
to 2.0.1.

Is there another way I can help you with the crash? Any other linux users around that
use original expat from Ubuntu 12.04?

Reported by skraft61 on 2012-05-18 16:07:42

Beep6581 commented 9 years ago
What profile are you using? I'll try it here then to verify it's not a problem with
the LCP.

Reported by oduis@hotmail.com on 2012-05-18 16:08:43

Beep6581 commented 9 years ago
I sent it to you by mail.

Reported by skraft61 on 2012-05-18 16:22:28

Beep6581 commented 9 years ago
Ok, the profile has a different XML structure. I'll try to come up with an addition
to handle this type of profile.
Meanwhile I'll commit this patch since you already tested it and Linux people begin
to complain they can't compile any more.

Reported by oduis@hotmail.com on 2012-05-18 16:29:35

Beep6581 commented 9 years ago
@ comment 19
"And it works here and probably also with DrSlony, how is also using Linux."
Correct, I just updated and RT compiled, no problems. Runs fine too, though I can't
test the LCP as I don't have any profiles.

Gentoo x86_64, dev-libs/expat-2.1.0

2.0.1 is very old - 2007. Version 2.1.0 is from March 2012. If your distro doesn't
have 2.1.0, open a feature request for it in your distro's bugtracker.

Reported by entertheyoni on 2012-05-18 18:35:03

Beep6581 commented 9 years ago
This gets printed in the terminal:

Sorry, default LCP directory are currently only configured on Windows
Sorry, default LCP directory are currently only configured on Windows

Yes, twice.
As with issue 1365, we don't need to see it printed at all ;]

rawtherapee/rtengine/lcp.cc:384-387

Reported by entertheyoni on 2012-05-18 18:53:27

Beep6581 commented 9 years ago
Will be adressed with the next patch. I wonder why it's visible at all, since the terminal
window is just in development mode when not linking with -swindows I understood.

Reported by oduis@hotmail.com on 2012-05-18 19:04:36

Beep6581 commented 9 years ago
Tried version 4.0.8.39 on Win7 64bit, and found some open points:
- files have to be called *.lcp (not *.dcp) to be found
- "Lens correction files" filter on lower right corner of the dialog does not work
and you cannot change the file pattern

I thought that lens correction would be something like anti-distortion, but some D300s
images look like having faded frames.

http://vwlnt7vw4897.minus.com/mberJZQ2Cd/2

Problem comes when using
Lens_Nikon_16-85mm_f3.5-5.6G_ED_VR_DX_AF-S_Nikkor.lcp
but works fine when using
Lens_Nikon_10.5mm_f2.8G_ED_AF_DX_Fisheye_Nikkor_RAW.lcp
(of course on images made with these lenses).

RAW is here
http://min.us/mbk9ND8i2l

Reported by globetrotter.75d on 2012-05-18 19:35:51

Beep6581 commented 9 years ago
@Globetrotter:
That's not the official Adobe profile, is it? If I use the offical one it correct without
those wildly wrong vignetting.
Where do you see DCP? DCPs have nothing to do with this, it should be all to fixed
to only allow LCP files.

Reported by oduis@hotmail.com on 2012-05-18 19:52:41

Beep6581 commented 9 years ago
It is the official profile from Adobe, extracted from download as described in #13.
In #13 it's described also to name the profiles *.dcp.
Will try some more images, just tried two, and both made with this lens had been vignetted.
OS dependent, maybe?

Reported by globetrotter.75d on 2012-05-18 20:05:55

Beep6581 commented 9 years ago
Sorry, can not try more images, as Issue 1370 wastes my RT.

Reported by globetrotter.75d on 2012-05-18 20:22:35

Beep6581 commented 9 years ago
Maybe, very strange. When I develop your sample RAW it nicely corrects the little vignetting
without creating the ghosts you have.

This patch adds support for these special Pentax LCPs (and removes the warning for
Slony). However I haven't found a nice Pentax raw with massive vignetting or distortion
yet to test if it the correction really works. The parameter are pretty different in
that profile.

Reported by oduis@hotmail.com on 2012-05-18 20:23:34

Beep6581 commented 9 years ago
I applied the LCP from comment 39 to a landscape PEF. RT crashed every time. I tried
it on a portrait PEF and it didn't crash. I tried it again on the same landscape PEF
and it didn't crash. Will report with a stack backtrace when I manage to reproduce
it.

Clicking on the "X" to remove the LCP from the photo has no effect. In fact, when I
go back in history to undo the LCP then I see in the preview that the effect gets undone
(good) but when I click on "X" nothing should happen - instead the LCP gets applied.

Reported by entertheyoni on 2012-05-18 20:30:17

Beep6581 commented 9 years ago
DrSlony, you must take the correct LCP file that fits your lense. Globetrotter has a
Nikon which will surely not work with your Pentax.
And removing the LCP works fine here.

Reported by oduis@hotmail.com on 2012-05-18 20:36:19

Beep6581 commented 9 years ago
Yes, but RT should't crash. Then again, I can't reproduce.
No luck with removing it using the "x" button other than by stepping back in history
or applying a new profile. I tried on a few photos.

Reported by entertheyoni on 2012-05-18 20:59:46

Beep6581 commented 9 years ago
Latest incarnation, should fix globetrotters problems.

Reported by oduis@hotmail.com on 2012-05-18 21:15:38

Beep6581 commented 9 years ago
Sorry, no improvement. Tested 5 lenses:
- 10.5 f2.8 Fisheye --> okay
- 16-85 --> vignetting
- 105 f2.8 --> vignetting
- 70-300 --> vignetting
- Sigma 30 f1.4 --> crashes always

http://minus.com/m5lLTWPxz/

Reported by globetrotter.75d on 2012-05-18 23:41:35

Beep6581 commented 9 years ago
If vignetting would only happen in the very edges I'd have assumed a FX/DX problem.

Reported by globetrotter.75d on 2012-05-18 23:49:59

Beep6581 commented 9 years ago
Thanks for testing. They all work fine here on my setup, but I think I can reproduce
it when I damage the code. It might be a german/US decimal point parsing problem, leading
to wild numbers.
Can you try this one? One image test for vignetting is enough.
And did the last patch solve the other file browser hang for you like it did for me?

Reported by oduis@hotmail.com on 2012-05-19 05:21:35

Beep6581 commented 9 years ago
Tried that. FYI: I got a German system (too). Used Default profile to test your patch.
Results are even worse, completely overexposed, nearly white. 
Crash on Sigma lens, still.
"Lens correction files" filter is still non-functional.

Downgrade to older lcms did solve the hang, see Issue 1370.

Reported by globetrotter.75d on 2012-05-19 07:59:52