10 ms·
Correct SRGB Dithering
- crazygringo 7y agoThe big takeaway here is that you should never perform brightness arithmetic on sRGB. You need to remove gamma to make brightness linear, do your math, then re-apply gamma. The full correct formula is: https://stackoverflow.com/a/13558570 https://stackoverflow.com/a/13558570 Interestingly gamma correction also includes a linear component. For more info on why: https://poynton.ca/notes/colour_and_gamma/GammaFAQ.html#gamma_correction https://poynton.ca/notes/colour_and_gamma/GammaFAQ.html#gamm...
- raphlinus 7y agoI have actually seen a lot of different explanations why the sRGB EOTF has a linear part, and can come up with a few more theories myself. In the Poynton FAQ, it says it "minimizes the effect of sensor noise in practical cameras and scanners". Wikipedia says "The purpose of the linear section is so the curve does not have an infinite slope at zero, which could cause numerical problems." Poynton's book Digital Video and HD: Algorithms and Interfaces talks about "veiling glare" and then says that the sRGB function almost perfectly inverts L* from the CIELAB color space (a pure 2.2 gamma would not do so). His thesis[1] goes into more detail. One possible theory why there's a linear term is so that negative values are well-defined. In consumer HDMI, reference black is 10 (as opposed to 0 in sRGB), and reference white is 235 (as opposed to 255 in sRGB). Since there no doubt be experts here, I'd love to see an authoritative answer. (I'm considering writing an explanation of sRGB and if so would really like to get this right) [1]: http://poynton.ca/PDFs/Poynton-2018-PhD.pdf http://poynton.ca/PDFs/Poynton-2018-PhD.pdf
- Ono-Sendai 7y agoOne possible reason is that it's hard to construct a numerical approximation (e.g. polynomial approximation) to the power curve around zero. With a linear part you avoid that problem. This is probably what the 'numerical problems' alludes to.
- crazygringo 7y agoOh wow, I had no idea about video black/white being 16/235, rather than 0/255. It took a lot of Googling, but I finally found an article that explains why, in case anyone else is interested: "The 601 system allows colors that are darker than black and brighter than white. This is especially important for cameras, because you may occasionally shoot an object that has a bright spot that is “hotter” than legal white, and might want a way to later recover the detail in this hot spot. Going darker than broadcast black is also used at times for synchronization signals, as well as some primitive keying applications such as “superblack” where systems mask or key out areas of an image “blacker” than black." https://www.provideocoalition.com/luminance_ranges/ https://www.provideocoalition.com/luminance_ranges/
- Fronzie 7y agoThere's also a pragmatic reason: When sampling the PAL/NTSC signal cabling will have applied low-pass filtering, causing over- and under-shoot of the signal. By setting the signal up to have the range 16/235 for an 8-bits A/D converter, this overshoot does not saturate the A/D converter. Lots of consumer electronics is designed with these kind of rules.
- 0x00000000 7y agoFun fact the blender cycles renderer used sRGB for a long time which was just throwing away huge amounts of dynamic range
- a_e_k 7y agoFrom the original 1996 proposal "A Standard Default Color Space for the Internet - sRGB" (https://www.w3.org/Graphics/Color/sRGB https://www.w3.org/Graphics/Color/sRGB): > The effect of the above equations is to closely fit a straightforward gamma 2.2 curve with an slight offset to allow for invertability in integer math. Therefore, we are maintaining consistency with the gamma 2.2 legacy images and the video industry as described previously.
- raphlinus 7y agoRight, should have added that to the list of not-quite-consistent explanations. Also, slight typo, black level is 16 not 10. And to this I'll add another speculative answer, similar to others that have been advanced, but I'm not sure it's ever been explicitly expressed: it means that a piecewise linear approximation in either direction can be done with good accuracy and not a huge number of segments.
- boulos 7y agoIt’s more accurate to say you probably shouldn’t be applying any operations to sRGB values directly. They’re non-linearly “compressed” and most operations assume they’re working in a linear space. Of course, if you want, you can figure out the equivalent “chain rule” of your operation, but realistically sRGB also mostly exists to compress colors into 8-bits per channel which doesn’t allow for great manipulation. Fun fact: this is why LRBni had a “load 8-bit as sRGB and upconvert to linear floating point”.
- contravariant 7y agoTo make matters more interesting the actual gamma curve you should be using should be the one corresponding the the display you use to view the image. Very frequently this isn't sRGB or anything close to it (pure power gamma curves are relatively common, if you're lucky the gamma is somewhere close to 2.4). It is in fact not even rare to encode an image with a gamma curve that's intentionally different from the display that's supposed to be used to view it, in order to, for example, prevent crushing black details or to avoid numerical problems (both reasons for the linear component in sRGB).
- ska 7y agoSo much software gets this sort of thing wrong by either ignoring or incorrectly accounting for gamma.
- londons_explore 7y agoAll internet browsers have to use 'wrong' blending when combining transparent objects. If they 'fixed' the bug, and rendered with correct physical-aware alpha blending, a bunch of websites wouldn't look correct anymore.
- daxterspeed 7y agoIt's rather disappointing that websites can't request linear ("correct") alpha compositing in any way. I suppose it's a bit of fingerprinting, but browsers can already be fingerprinted by gamut support[0]. [0]: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/color-gamut https://developer.mozilla.org/en-US/docs/Web/CSS/@media/colo...
- leni536 7y agoAlpha blending can be an other can of worm. There is premultiplied alpha for correct interpolation between color values with alpha and then there is the lack of per-channel alpha that would be useful for subpixel antialiasing against a transparent background.
- londons_explore 7y agoSubpixel anti-aliasing I suspect is technology that will go the same way as video interlacing... ie. "It works great, has a positive benefit on the user experience, but adds so much system complexity, limits other operations, and is so hard to implement right, that it fell out of use" Today subpixel anti-aliasing is implemented mostly for text only - we've given up implementing it for 2d/3d graphics.
- eat_veggies 7y agoIn that case they could they make it a CSS property? Something like `alpha-mode: physical-aware`, defaulting to the current "wrong" blending. Or is this such a fundamental change that it would be infeasible to support both modes?
- kelsolaar 7y agoFor clarity, precision, and a bit of pedantry, the IEC 61966–2‑1:1999 Standard does not specify an OETF but only an EOTF, so the OETF in the linked article is actually an Inverse EOTF. Some more information here: https://www.colour-science.org/posts/srgb-eotf-pure-gamma-22-or-piece-wise-function/ https://www.colour-science.org/posts/srgb-eotf-pure-gamma-22...
- nayuki 7y agoI wrote about this very topic: https://www.nayuki.io/page/gamma-aware-image-dithering https://www.nayuki.io/page/gamma-aware-image-dithering
- microcolonel 7y agoYour error diffusion dither is strange to me. Why do you not use two-dimensional patterns like Floyd-Steinberg or Jarvis-Judice-Ninke? Is your assumption that you're diffusing error linearly in Morton or Hilbert curves? Because I'm pretty sure that's going to be outperformed in quality by any common 2D error diffusion pattern.
- contravariant 7y agoRounding to the value that's nearest in linear light is not exactly right (if anything evaluating distances in linear light is a bad idea in general, but that aside). What you want is to find out exactly how much of each colour to blend in order to get closest to the actual value and blend the colours in those proportions. The formula in the OP is the right way to do this.
- jes5199 7y agoI’ve worked on code that does this before and I thought I understood it, but something about these explanations are making it way more confusing to me
- nightcracker 7y agoThis is not correct dithering, because uniform noise is not correct. You need triangular noise, and have it somehow be aware of sRGB. Why? Because otherwise your variance is not uniform in your dithering spectrum, and gives noticeable artifacts. So it's not correct to only choose from floor(c) and floor(c)+1, you actually need to choose from round(c) - 1, round(c) and round(c) + 1 with the right probabilities. I've gotten stuck on this for days, and even tried to ask on stackexchange, but to no avail: https://math.stackexchange.com/questions/3200249/maintaining-probability-distribution-under-transform https://math.stackexchange.com/questions/3200249/maintaining... Yet this is important, as dithering with triangular noise gives vastly superior dithering. This is 5 grayscale values with my best found method (which is not fully mathematically correct, but a close approximation): https://i.imgur.com/sNq20Oo.png https://i.imgur.com/sNq20Oo.png The yellow line indicates sRGB signal, the red the error, and the blue the variance. Now compare it to OP's method: https://i.imgur.com/FjBUuCr.png https://i.imgur.com/FjBUuCr.png Notice the significant banding? It's because there are regions where the variance of the dither essentially hits zero, producing a (near) pure color band that's very noticeable.
- DoctorOetker 7y agowhich math package are you using? Mathematica, Macsyma, ...? also, I have uBlock origin, and the imgur .png link shows me the page without the png... (EDIT: I just used wget on the exact same URL and it just downloads the png, it boggles my mind why firefox doesn't just show the PNG, or is this imgur detecting user agent etc?) Also, that's a good observation, that the spatial distribution of variance is noticable. Would you mind sharing the code for the last 2 plots you generated? For incorporating gamma, and using the original HN post terminology, what prevents you from generalizing your technique to the optical (linear) domain instead of the electronic (gamma) domain? Is it because you need to devise a new family of probability functions so that the expectation value in the optical domain is linearly proportional to the input optical value while having the bins spread according to the gamma curve?
- nightcracker 7y agoThe plots in the linked StackExchange question were created using Wolfram Notebooks.