Saturday, July 28, 2012

Why when I convert from linear space I got dark shadows posterization?

This question has been raised a number of times across the years. The first time I faced it was with JDK incorporating lcms1. They had a test profile that was somehow giving bad results when optimized. After close inspection, the profile was found to be operating in linear XYZ space. The complaint was almost the same: when I use linear space as input, optimization doesn’t work. Otherwise, all other combinations are ok. And this seems to be also the actual case; all other combinations are working fine. Try for example to reverse the transform going from regular sRGB to your linear space, you will find all the issue are now gone.

But anyway, there is this case that seems to fail. And that’s true: on this particular case, some dark shadows got a dE > 1.5 if using the default settings. Fine, it happens that for this extreme case, the defaults don’t work. This is the reason why there is called “default” and there is a setting to control it.

So, the short answer is: don’t optimize when using a linear XYZ space as input in 16 bits transforms.

But I guess you want also the long answer. So, here you go.

When you use lcms to create a color transform joining two or more profiles, you are creating a devicelink profile. You don’t see it as a file; it lives in memory, and is destroyed when you delete the transform. But it is there.

Devicelinks can be implemented in different ways, for example they can be implemented as a set of curves, or by a matrix, or by a 3D CLUT table, or by a combination of all elements above. Some of those ways are better than others in terms of xput, others are better in terms of image quality. CMMs have to “guess” which is the best combination of elements for a given set of profiles. There is balance between quality and performance. For some corner cases, optimizing for speed can effectively introduce defects on quality.

The best devicelink representation often depends on the true nature of the space described by the profiles. Specially the input space, But then… the profile only gives you a way to convert form/to Lab to its space, and gives no other clue about the space nature.

An example of ill-formed spaces are those that are operating in linear XYZ gamma space. You should NEVER user linear gamma to store your 8-bit images. Why? Because in 8 bits you have 256 levels, and in linear gamma the separation between those levels is not perceptually uniform. That means you have very few levels to encode the effective dynamic range of your image and many levels are wasted in highlights. Hold on, you would say, RAW images are encoded in linear gamma and they work quite well, isn’t it? You are right… but I said 8 bits, remember? If you move to 16 bits or floating point, you can still use linear encoding, but with some care.

Back to our methods to encode devicelinks. One used by lcms when the transform converts form 16 bits to 16 bits, is a CLUT table. This is just a 3D (or 4D in CMYK) grid with nodes. Pixel values are interpolated across nodes. For example, the distortion you get when going from sRGB to AdobeRGB is stored in a 3D grid of 17 nodes on each R, G, B side. When a pixel arrives, the corresponding nodes that enclose the value are selected and the result is interpolated. In our 17 node example, a value of, say, (100, 100, 100) will go on the 100*(17-1)/255 = 6.7 so the nodes 6 and 7 of each side will be taken for interpolation.

Le’s now take a linear space. Since as said, many colors are collapsed to a relatively few codes due to the gamma encoding, almost all dynamic range is confined to few nodes. That mean In a 17 nodes grid, most image dynamic range will fall in 5 or 6 nodes. And this is the reason you got posterization in shadows: most of dark tones falls in just 1-2 nodes and linear interpolation cannot deal with the non-linear nature of the transform linear-gamma 2.2.

How to solve this? The most evident way is to not use 3D CLUT optimization. The CMM already does that if you use floating point, or if you use 8 bits. In lcms2.03 there is some experimental flag that tries to solve this issue adding an extra tone curve cmsFLAGS_CLUT_POST_LINEARIZATION and cmsFLAGS_CLUT_PRE_LINEARIZATION. I have checked that and found to solve this issue as well.

So, that is the reason why you only see this issue when converting from 16 bits to 16 bits with default flags. Placing a NOOPTIMIZE in all transforms would prevent problems, but at big performance penalty that is hard to explain just to fix this specific case. It is like you have a Ferrari but you go always at 25Mph just because once upon a time you faced a winding road.

My recommendation for programmers would be to allow end user to turn optimization off for general usage, or at least to provide a specialized workflow for RAW handling with optimizations turned off, that is the only place when linear XYZ makes sense. For users, I would recommend to NEVER use linear XYZ spaces. They are good for nothing, nor for storage, nor for image processing. The very few algorithms that need to be done in linear can do and undo the conversion when processing. But anyway, there are people with strong opinions on this field and everybody is free to do whatever they want. This is just a recommendation, please don’t take it as a stone-engraved truth.

29 comments:

  1. That is a hugh explanation for the polarisation which is helpful to know the littleCMS accessing. Thanks for sharing the post.
    Website Development Companies in Coimbatore | Ecommerce Website Development in Trichy

    ReplyDelete
  2. This is a great post it shows some information and best advice I needs for a blog. Thanks and keep on posting. Please visit http://goo.gl/lqMAaP

    ReplyDelete
  3. Thanks for the information. Helped us to convince most on how this process works and what they could achieve by following these guidelines


    Bangalore Web Designing Company, Web Development Company in Bangalore

    ReplyDelete
  4. Your work is time-consuming but you know that it is well done!

    Eliza

    ReplyDelete
  5. This comment has been removed by the author.

    ReplyDelete
  6. Nice post posted by author. I like to read your post. I am waiting for your next post to read. Setup HP Deskjet 3755 Printer

    ReplyDelete
  7. Connect HP Laserjet Printer To Wifi - Get the Simplified Instructions for How to Connect HP Laserjet Pro 400 m451nw Printer Wireless Setup Guidance from Our Experts

    ReplyDelete
  8. Today, Emblix solutions as one of the best and top most service-oriented Digital Marketing Agency in Hyderabad and India, Which provides high-quality result-oriented Digital Services ranging from SEO to Web Design, Social Media Marketing and more, to a broad spectrum of clients from diverse industry segments. Through a well-oiled combination of Quality Solutions, Transparent Pricing, helping brands connect with customers, Flexible Delivery & Contract Model with a firm commitment to deliver on time and to budget, Emblix has successfully built a strong relationship with clients based on mutual trust and respect. Further, Emblix’s extensive market experience and expertise in Digital Marketing helps clients in successfully managing data as a strategic asset.

    ReplyDelete
  9. Thank You for the Post. I like to read your blog. If you are facing issues regarding your canon Printer we are always their to resolve the issues like How to Connect Canon Pixma Printer to Wifi, etc.

    HP Printer in Error State in Mac

    Remove the Spyware through Activation of Windows Defender

    ReplyDelete
  10. It'not my habit to read blogs post but after reading your blog I just want to say that you have discussed great information. seo services east london

    ReplyDelete
  11. The error 15227 QuickBooks appears with some symptoms such as your computer frequently crashes, stuck every now and then, or the QuickBooks crashes automatically without any reason. Several reasons can cause QuickBooks error 15227, such as corrupted or damaged QuickBooks software, partial installation, virus or malware intrusion, or you might have mistakenly deleted some necessary QuickBooks files. You can resolve error 15227 QuickBooks using some solutions. Firstly, you must repair the registry entries and run a virus or malware scan to fix the problem. Also, you can use disk cleanup to remove the temporary files and eliminate the junk files. Make sure that to update the PC device drivers and undo any recent changes. If the problem still exists, then you must uninstall and reinstall the QuickBooks.

    ReplyDelete
  12. Wi-Fi protected setup is one of the best to connect printer wirelessly. For this setup, you should know the WPS Pin Number of your printer. If you don’t know then read the article and know the easiest way to find WPS pin on HP printer.

    ReplyDelete
  13. I found very informative and creative ideas form your post and I hope this post will be very helpful for everyone. thanks for share with us.
    To more info, click here: java assignment help australia

    ReplyDelete
  14. The currency of Saint Kitts and Nevis is eastern caribbean dollar. The plural form of the word eastern caribbean dollar is dollars. The symbol used for this currency is $, and it is abbreviated as XCD. The eastern caribbean dollar is divided into Cent; there are 100 in one eastern caribbean dollar. http://www.confiduss.com/en/jurisdictions/saint-kitts-and-nevis/economy/

    ReplyDelete
  15. You got a really useful blog. I have been here reading for about an hour. I am a newbie and your success is very much an inspiration for me. To Resolve Spectrum Email Not Working Error please contact our team for instant help.

    ReplyDelete
  16. Its good to see the efforts been made in the positive direction about the facts that are being explored to the vision which is needed as much as assignment help is necessary for everyone as inspiration as well as judgement.

    ReplyDelete
  17. I would like to bring this to the notice of the public about how I came in contact with Mr Pedro after I lost my job and was denied a loan by my bank and other financial institutions due to my credit score. I could not pay my children's fees. I was behind on bills, about to be thrown out of the house due to my inability to pay my rent, It was during this period my kids were taken from me by foster care. Then I set out to seek funds online where I lost $3,670 that I borrowed from friends which I was ripped off by two online loan companies. Until i read about Mr Pedro helping people with an loan online in which this email was stated (pedroloanss@gmail.com) somewhere on the internet, Still wasn't convinced because of what i have been through until a relative of mine who is a clergy also told me about the ongoing loan scheme  at a very low interest rate of 2% and lovely repayment terms without penalty for default of payment. I had no choice than to also contact them, which I did. Mr Pedro responded back to me. That day was the best and greatest day of my life which can never be forgotten when I received a credit alert for the $400,000.00 Usd loan amount I applied for. I utilized the loan effectively to pay up my debts and to start up a business and today my kids and I are so happy and fulfilled. You can also contact them through email: (pedroloanss@gmail.com / WhatsApp Text : +18632310632)   Why am I doing this? I am doing this to save as many that are in need of a loan not to be victims of scams on the internet. Thanks and God bless you all, I'm Oleksander Artem from Horizon Park BC , Ukrain.

    ReplyDelete
  18. This blog is an amazing one, the contents here are always very educative and useful, the reason I make it my regular blog of visit so as to widen my knowledge every day, thanks for always sharing useful and interesting information, you can checkout this ui cut off mark for pharmacy

    ReplyDelete
  19. Thanks for sharing this knowledgeable post. What an excellent post and outstanding article. Thanks for your awesome topic. Really I got very valuable information here. if Roadrunner Email Not Working please contact roadrunner support team for solution.

    ReplyDelete
  20. It's fantastic that your blog is now available on the internet. I'm now going to this blog whenever I have an unsolved problem because you wrote it so beautifully. Thank you for sharing this fantastic knowledge with me; it is extremely beneficial to me. You've come to the right place if you're wondering how to rank your blog. You can promote your blog and services by posting free classifieds ads in india.

    ReplyDelete
  21. Experience the empowering impact of Healing Buddha's holistic approach, focusing on physical, emotional, and spiritual well-being.
    pranic healing

    ReplyDelete
  22. Dive into a world of wisdom and knowledge as we explore the latest trends, innovations, and strategies that are shaping the year 2023.
    Startup of the Year 2023

    ReplyDelete