Swap red/blue channels in DNG?

Started by Skinny, May 08, 2023, 01:42:10 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Skinny

Hi guys this is not ML related question but I know that people here have a lot of deep specific knowledge about dng format and raw data.

I used android phone to shoot some raw videos (it is possible with Motion Cam app).
And from this app you can export everything to dng sequence and edit in any program you like.

The problem is for some reason raw images from my phone look like they have red and blue channels swapped..

And I can change channels in post-processing, but I'm afraid the whole raw conversion before it will happen with wrong data. For example stuff like white balance will be processed who knows how..


If you know how I can process DNGs to swap red and blue channels before importing them, it would be awesome. If there is no easy solution to this problem, I can code with C/C++ but I am not really experienced with it, wrote stuff for AVR and ARM mostly just for fun.. If you push me in right direction (DNG processing) and it is not too complicated maybe I can figure it out..

I included one dng frame as an example (7 mb), maybe it is not r/b channels problem?

https://filetransfer.io/data-package/z7ss4jYf#link


This is what I see when just open it in photoshop



And this is after some tweaks after I swap blue and red channels


Walter Schulz

Here is a short article how to determine which channels are switched/damaged: https://www.fastrawviewer.com/blog/dealing-with-damaged-raw-files
Compare channels of 2 frames: One damaged, one proper. Best for consecutive frames.

Skinny

Thanks for the article! There is no proper raws from this phone, they are always like this :)

I edited config and unlocked raw photo option for standard camera app and they look the same too, switched channels..
Jpegs and usual videos are fine though. I think it is supposed to work this way, just some Chinese engineering :)

So I looked at histograms and separate channels, and my theory seems to be right, both green channels are almost identical while red and blue appears to be swapped.

Now I only need to find a way to re-write DNGs with correct pixel information..

names_are_hard

Both BGR and RGB are common data formats, maybe somebody got the metadata wrong, or forgot to swap for whatever the sensor outputs.

Using exiftool on your sample file:


$ exiftool rgb_swap_orig.dng |grep CFA
CFA Repeat Pattern Dim          : 2 2
CFA Pattern 2                   : 2 1 1 0
CFA Plane Color                 : Red,Green,Blue
CFA Layout                      : Rectangular
CFA Pattern                     : [Blue,Green][Green,Red]


You can swap the ordering by editing CFA Pattern 2, or CFA Plane Color.  Probably CFA Pattern too, I didn't test that one.

Editing via exiftool:

exiftool -IFD0:CFAPattern2="0 1 1 2" rgb_swap.dng


Skinny

Awesome! Thank you :D I did not expected this to be just some tag issue

And by the way what is the purpose of specifying IFD0 before the tag name?

I tried changing CFA Pattern 2 and it works, CFA Pattern also could be changed but the image will still look wrong at least in photoshop. And for CFA Plane Color it gives me an error
Warning: Sorry, CFAPlaneColor is not writable


So editing CFA Pattern 2 seems to be the right option, and after reading exiftool manual I decided to do this:

exiftool -IFD0:CFAPattern2="0 1 1 2" -ext dng -r -overwrite_original -progress -pause *path-to-directory*

In case somebody is interested what these options do:
-ext dng is just for safety, process only dng's
-r means recursive so all sub-directories will be processed
-overwrite_original - no backup file copies will be saved
-progress - show how many files left while processing, just for fun
-pause - if I want to execute the command from a shortcut or something, the window will be open at the end.


@ names_are_hard thank you so much!
You guys are awesome, it proves again that ML forum is a really good place when it comes to specific technical knowledge :D

names_are_hard

Cool, glad that works for you.

CFA Plane Color I changed with a hex editor, exiftool doesn't look to support it.

IFD0 is part of the tag hierarchy and according to the docs I read, can be needed if there are multiple sub-fields by the same name.  Is this true?  I don't know.

Thinking about it, if Motion Cam is creating the DNG, rather than an Android API, this is probably their bug.  Could go either way, you could consider raising it with them as a question.

Skinny

It is probably not Motion Cam's bug, because I can take raw pictures with usual built-in camera app by modifying one string in one system file, allowing raw photos, by default they are off. And if I do it, photos look exactly the same with channels swapped so it is the phone itself :)

Thank you so much again for solving this problem, I can now use this old phone as a little almost magic lantern device in my pocket hehe
It even has crop mode with much bigger resolution than standard 1080p mov recording, "card spanning" to write to the internal storage and SD at the same time, but unfortunately no lossless compression or SD card overclock :D
So similar in concepts