Recent comments

Reply to: unpack() performance?   4 years 5 months ago

Looks like you have some runtime checks turned on (not initialized variables, array bounds check, etc).

Also, Visual Studio's heap debug slows down every program w/ large memory allocations (when run inside Visual Studio)

Reply to: unpack() performance?   4 years 5 months ago

Problem solved.

I think it was due to my C++/CLR project. When tested from Visual Studio in release, unpack time was 11s.
When tested outside of Visual Studio, time was 2.6s.
Then I tested to use the libraw.dll instead of embedding the code in my project directly and unpack time was 1.1 sec, same timing than the postprocessing_benchmark project.

I still don't know why the dll is faster than compiling the code in my project, but that's fine :)

Thank again for confirming that I had a problem!

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

The "has been subtracted at the unpacking stage" statement covers "possibility": if black pattern is not covered by black/cblack[], the only way is to subtract it on unpack to prevent LibRaw user from camera-specific details.

From user's point, there is no difference between 'black subtracted by camera' (e.g. old Nikons) and 'black subtracted on unpack()'

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

hi,

no need to shout at me, I was documenting my progress here for others to hopefully help. Maybe I should have kept it to myself.

> blacks are NOT subtracted by unpack().

... may I humbly refer you to your documentation, which clearly states:

Structure libraw_colordata_t: Color Information
...
unsigned black;
Black level. Depending on the camera, it may be zero (this means that black has been subtracted at the unpacking stage or by the camera itself)
...

I do not wish to use the 4-component-distribution version of the data for several reasons (one, not the least, being performance, my current very crude reverse-engineered version is slightly faster than the original and that is without optimization and converting large loops to CUDA). That is why I was asking questions in order to better understand what happens under the hood.
In the end I want to use libraw for reading/unpacking RAW data and do all post-processing after that step myself, to better integrate it into pipelines. Recreating the reading part would be possible since my usecases only cover about 10 different camera models/brands and I have limitless access to all of them, but libraw (dcraw) is doing a great job there, so why reinvent the wheel ...

Thanks for the advice you gave (pointing me back to ninedegrees)! If we were able to communicate better I would volunteer for reworking the documentation, obviously, that would not be wise. Therefor: Thank you for the work you invest in maintaining libraw!

Mact

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

"... - call "subtract_black()" (just in case black has not been subtracted by "unpack")..."

blacks are NOT subtracted by unpack().
If you wish to use Libraw's imgdata.image[] array (not imgdata.rawdata.*), you also need to call raw2image() or raw2image_ex(). The second one could subtract black if called with (true) arg (and if other parameters you use do not prevent black subtraction on copy).

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

So ... by not using cam_mul[] for scaling, but instead pre_mul[] and reproducing dcraw's one-step scaling-and-white-balancing I am getting a stable result.
Interestingly, this is not the (exact) same hue that dcraw (and libraw) is creating with gamma 1/1, no-auto-bright and output "raw" (unaltered camera color) *or* output "sRGB": dcraw/libraw's output is slightly (not much, really) more on the blue side, ever so slightly less saturated. Not sure why that is. Maybe because I am doing most calculations on double-precision, not integer? Since the difference is really small, I am fine with it.

I do have to normalize the image for dark/maximum (the black value and the maximum value from imgdata.rawdata.color) in order for dcraw's scaling to work correctly. I could not find the function in dcraw that is equivalent to this normalization step, but leaving it out gives too dark results, so it must be correct (I assume that scale_colors actually looks for high/low points in the image, there are some parts of the code that are still opaque to me).
This also means that my assumption above seems to be correct: Normalization is pre-demosaic, not post-demosaic.

My steps now are:
- load file
- unpack
- call "subtract_black()" (just in case black has not been subtracted by "unpack")
- normalize to embedded dark/white values (or "minimum/maximum", dcraw's mixture of terms is not helping ...)
- scale/white-balance based on pre_mul[]
- demosaic
- apply color profile (using the pre-calculated cam-rgb-to-xyz-to-output-rgb)
- apply rotation

BTW: I found lclevy’s discussion on scaling quite helpful (the site is linked from the ninedegreesbelow website).There's a caveat about that website, though (and that MAY have misled me, I cannot remember): The code quoted there about dark-subtraction uses cblack[], NOT the "global" black value. That is due to the code having populated cblack[] with "complete" black values before. This might add to why my scaling was off.

Mact

Reply to: How to create ICC profile for camera   4 years 5 months ago

... you may want to look at www.color.org for fundamentals about ICC profiles (which are coming from the printing world, actually) or, if you want to be slightly more "compatible" with the (RGB based) additive color world, visit opencolorio.org for an alternative approach.

In general, creating a profile from a target shot is no "magic", the problem really is about peeking the "right" spot on the image. In my experience averaging a spot center area (after you have found the target spots by pattern recognition) does not really always give the best results.

You can have a look at lprof (search at Sourceforge) - either for using it as an open source "profiler solution" or for learning from the code how to set up your profiler code.
Alternatively you might want to consider ColorChecker (XRite), which is a free tool that creates quite good ICC profiles.

I hope this helps.

Mact

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

Yes, that phrasing is better - you could copy&paste that into documentation, the simple addition of "correction TO BASE black" makes it clearer!

Mact

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

cblack is *per-channel correction to (base) black*.

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

Hi, Alex,

thanks!

The docs only say "can be zero" about the "black" value (which, in my case, is !=0), but not about cblack (which IS 0 for me). I think the docs could be improved by mentioning the same about the cblack values, would have helped me :-)
The 0 is in cblack even after "unpack()".

I don't see a problem here, it's really just minimizing headache for users ...

Update on my progress will come, I'm now trying to better understand the code with help of the ninedegrees article.

Mact

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

There are two black-level fields.

Quote from docs: https://www.libraw.org/docs/API-datastruct.html#libraw_colordata_t

unsigned black;
Black level. Depending on the camera, it may be zero (this means that black has been subtracted at the unpacking stage or by the camera itself), calculated at the unpacking stage, read from the RAW file, or hardcoded.

unsigned cblack[4102];
Per-channel black level correction. First 4 values are per-channel correction, next two are black level pattern block size, than cblack[4]*cblack[5] correction values (for indexes [6....6+cblack[4]*cblack[5]).

For Sony files (checked w/ A7R-IV sample), black is 512 and cblack[0..3] are zero after open_file() call.

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

Just an interim update:

libraw reports cblack values as all 0 for me after unpack()/subtract_black(). Yet, the actual data (RAW image at imgdata.rawdata.raw_image) has minimum 509 and maximum 6069 before and after subtract_black().

To me it looks like subtract_black() either does not use "imgdata.rawdata.raw_image" (since its data has not been changed) or I need to add another step between unpack() and subtract_black() to actually get the black data populated. I will give reading the code another go.

To be continued.

Mact

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

Colour is what camera measured and recorded (less metameric error). The goal is to preserve colour it measured and present it in a colorimetric space. One of the methods is to find and apply an optimized transform between device data space, which isn't colorimetric, and XYZ or Lab colour space. There are few methods of dealing with metameric error, too. It's one of those calibration class tasks. Next, we move to perceptual matching, and those operations do change colour.

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

That's nomenclature - I understand what you mean and have no problems in using the word "color" in that sense (in the context of libraw, that is). I normally use it in a somewhat different sense - but that does not matter at all here and is of no concern for the discussion.

> They are about projecting the device space into a colorimetric space, where colour is known

Exactly. I would not put it that way, as I consider it misunderstandable, but we are talking about the same thing here.

Mact

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

White balance is what normalizes the responses of red and blue channels to the response of the green ones, so as to provide equal data numbers over a neutral (grey) surface. It's not about colour, it's about space metrics, making it uniform across all (3, 4, or more - depends on CFA) colour channel axes.

None of the operations we are discussing here are about colour per se, in fact. They are about projecting the device space into a colorimetric space, where colour is known. Operations that are about colour are those that change colour.

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

:-)

In my world, white balance *IS* about color ^_^

I will re-read the ninedegrees-article, still wondering about Alex' statement about normalization/scale-to-max, though.
It is possible that I checked the black-values before they got populated, which would explain why they turned up 0.

Mact

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

For ILCE-7RM3 typical black level is 512. If it is not so in your process, I would fix it first thing.

Gamma curve "statement" is based on error / artifacts analysis.

Forget colour for the moment, make other things, including white balance, work.

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

Thanks, I remember having read that niedegreesbelow article before, but I will go through it again. I must have mixed up something.

> "seems like black levels aren't set in my images" - what camera is it, please?

I have tested with various firmware versions from Sony A7r3 and also (if I remember correctly, I may be wrong there) with Canon 800d, 750d and others. The above statement is definitely true for A7r3, though.

> Generally, if you want quality: use floating point, apply white balance and some gamma / tone curve before demosaicking

Agreed on floating point, that will be a (simple) step ASAP. I am interested in the gamma curve statement, though, since my goal is to match about 12 different camera types as good as possible by NOT messing with "random" (excuse the term) curves. I would like to have the process as documented, stable, reproducible and non-"guessed" as I can manage.

> calculate and use appropriate forward colour transforms

This is most probably the step where I have broken something: My conversion (see above) from "camera color space" to anything more or less well-defined (sRGB is only for testing, it is NOT a "good" colorspace of course, I am targeting linear ACES in the end).

What about my question about normalization? Alex said that normalization is to be done after demosaicing, while that sounds not quite right to me (from my understanding you actually NEED to normalize - "scale to max" - the mosaic image before demosaic, but Alex said the opposite in the note mentioned above).

The main difference I see is actually about the "spreading" of the values: If we "cut"/clip the black end (lower end) and move data to the upper end (maximize values by multiplying with "maximum") normalization here must create a different set of colors. If we would normalize BEFORE cutting/clipping, that effect would be smaller. If we normalize AFTER demosaicing, using 3 channels in parallel, we'd only really change luminance, not color.

Or am I wrong somewhere?

Mact

Reply to: Order of operation and correct level-adjustment   4 years 5 months ago

dcraw_process: This is borrowed from dcraw proper, please make sure you've read https://ninedegreesbelow.com/files/dcraw-c-code-annotated-code.html
"seems like black levels aren't set in my images" - what camera is it, please?
Generally, if you want quality: use floating point, apply white balance and some gamma / tone curve before demosaicking, calculate and use appropriate forward colour transforms (device to destination colour space, sRGB/Adobe RGB, etc.). dcraw_process is sort of a hack, you may want to skip it altogether.

Reply to: Decoding RAW without Bayer interpolation to single greyscale   4 years 5 months ago

I don't see why not to check demosaicking first. And while I appreciate your intent, no, I can't assume anything about anybody. I always prefer to know the exact goal, and to start with discussing the setup.

Reply to: Decoding RAW without Bayer interpolation to single greyscale   4 years 5 months ago

I do agree :)

Merely quoting from the 5-years-old question: " I'd like to use this image for sensor testing purposes, and the object is pure B&W." - without knowing the exact setup, the best we can do is suggest things to try. Trying out what quality you get from the "scale BW" approach I outlined only costs about 5 minutes to write and check. Trying out an alternative "convert to HSV and use the V channel" (or Lab or whatever) approach costs another 5-10 minutes. So after half an hour you know what's best :-)

No need to discuss the necessity of demosaicing at all, I just wanted to share a possible approach to the actual problem described. I fully agree that in most scenarios you will want to go RGB->Gray. However, the thread opener was talking about "sensor checking", so I feel comfortable to assume that he knows what he is doing ...

Mact

Reply to: Decoding RAW without Bayer interpolation to single greyscale   4 years 5 months ago

IMO a good thing is to start with stating the purpose. Without that, and without an estimate of the allowed error, nothing can be done. For sensor characterization having light spectrum changing across the scene is a matter of concern. Exact experimental setup and flat field are the first order of business. Before that, any discussion of the need in demosaicking is premature.With decent setup demosaicking can be skipped, but the lightness error introduced by demosaicking is typically less than 2 dE.

Reply to: Decoding RAW without Bayer interpolation to single greyscale   4 years 5 months ago

I agree - you can always get a hue introduced from lighting conditions, lens CA etc. No doubt!
Yet, the answer to the specific question really should have been "within your specific setup, you can actually skip the color interpolation / demosaicing step".
I say this because no matter how good your interpolation process (demosaic) is, it is an interpolation after all, it "guesses" (mathematically calculates) values that aren't really there. With the specific, narrow use case of shooting BW objects (ideally in controlled lighting conditions), you would most likely get a better BW result by skipping the color interpolation.
(I have done this and can say from experience that it does give you more fine detail than any RGB interpolation I have tried ;) )

Mact

Reply to: Decoding RAW without Bayer interpolation to single greyscale   4 years 5 months ago

Since the light "colour" uniformity is not that simple of an issue, having b/w objects doesn't guarantee the image taken is uniformly b/w.

Reply to: Decoding RAW without Bayer interpolation to single greyscale   4 years 5 months ago

Since the question was actually about black and white objects, the - otherwise correct! - caveat of images possibly having color patches in them does not apply.

The question should instead be answered with: Libraw currently does not have such a function and due to its generalistic approach never will have, yet building a solution is rather easy and straight forward: If you know that your objects are reliably black and white anyway, just shoot a gray image, read out the channel differences (i.e. the "scaling" introduced by the color filters) and re-multiply with the appropriate scales.

Mact

Pages