Announcement

Collapse
No announcement yet.

ALL Irfanview operations become lossy when "Try to improve gamma for Resample" is on

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Reported ALL Irfanview operations become lossy when "Try to improve gamma for Resample" is on

    How to trigger:

    See Dialog.png for the feature triggering the bug.
    The bug is active after enabling he feature and restarting Irfanview. The current session is not affected.
    To deactivate the bug, you must disable the feature and restart Irfanview again.

    What happens?

    All IrfanView operations are particulary damaging to dark areas of images.
    Normal lossless operations become lossy.

    All means:
    • Resampling
    • Display of images independent of resampling (i.e. also 100%)
    • Simple file operations like converting file formats, cropping, flipping, changing canvas size, ...


    Test image

    Grey.ASCII.pgm:
    ------------------------------------------------------------------
    P2
    # Created by Frank Klemm
    16 16
    255
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    ------------------------------------------------------------------

    or take attached Grey-Org.png

    When you load the image and save it without any processing, the file looks like this:

    ------------------------------------------------------------------
    P2
    # Damaged by IrfanView
    16 16
    255
    0 0 0 0 6 6 6 6 9 9 9 12 12 12 15 15 15 18 18 20 20 20 22 22 24 24 26 26 28 28 30 30 32 34 34 35 35 37 39 39 40 40 42 44 44 45 47 47 48 50 50 51 53 53 54 55 55 57 58 60 60 61 62 63 63 65 66 67 69 69 70 71 72 74 74 75 76 77 78 80 80 81 82 83 84 85 86 86 88 89 90 91 92 93 94 95 96 97 97 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    ------------------------------------------------------------------

    i.e. pixel values are recoded

    0...3 => 0
    4...7 => 6
    8...10 => 9
    11...13 => 12
    13...16 => 15

    17, 18 => 18
    ...
    33, 34 => 34
    ...
    largest effected pixel value is 98 which is recoded as 97.

    Severity of the Bug

    Not obvious damaging pictures in all lossless operations.
    This is one of the worst kind of bugs ...

    Remark

    Actually there are multiple issues.
    1. Resampling using "Try to improve gamma for Resample" works very poorly.
    2. Settings of the Resampling dialog are effecting the display of Irfanview.
    3. Settings of the Resampling dialog are effecting ALL lossless operations.

    Not to mention that "Try to improve the gamma for resampling" actually means "use linear color space for resampling".

    No excuses are ...
    • This feature is not activated by default.
    • It's been like that forever.
    • Nobody has complained so far.
    • The program is free for private use.
    • You can use other programs.
    • You can disable this feature.
    • ... and other hundred excuses I programmers normally use every day.
    Attached Files

    #2
    Background are multiple reasons:
    • Poorly implemented converter function between non-linear color spaces (sRGB, AdobeRGB, ...) and linear color spaces: Y2L(L2Y(pixel)) != pixel
    • "Option crosstalk", resample settings affection normal load/save operations after a restart.


    L: linear luminance (real intensity, photon count, watts)
    Y: gamma corrected luminance (pixel value)

    Comment


      #3
      Code with similar behaviour

      Nearly similar bugs I got using this less sophisticated converter routines.
      It uses a 512 tap table for converting L back to Y.

      Code:
      #include <stdio.h>
      #include <math.h>
      
      using u8   = unsigned __int8;
      using uint = unsigned int;
      using f32  = float;
      
      namespace Gamma
      {
          constexpr f32 Gamma = 0.45;
          static f32 Sloow_Y2L(const f32 L)
          {
              return L <= 0.0 
                     ? 0.0
                     : ::powf(L, 1.0/Gamma); 
          }
          static f32 Sloow_L2Y(const f32 Y)
          {
              return Y <= 0.0
                     ? 0.0
                     : ::powf(Y, Gamma); 
          }
      }
      
      namespace sRGB
      {
          constexpr f32  c0 = 0.055f;
          constexpr f32  c1 = 1 + c0;
          constexpr f32  c2 = 2.4f;
          constexpr f32  c3 = 12.92f;
          constexpr f32  c4 = 0.0031306684425f;
          constexpr f32  c5 = c3 * c4;
          
          static f32 Sloow_Y2L(const f32 Y)
          {
              return Y < c5 
                     ? Y / c3
                     : ::powf ((Y+c0)/c1, c2);
          }
          static f32 Sloow_L2Y(const f32 Lumi)
          {
              return Lumi < c4 
                     ? c3 * Lumi 
                     : c1 * ::powf(Lumi, 1/c2) - c0;
          }
      }
      
      namespace LUT
      {
          #define ColorSpace  sRGB
          const size_t  TabLen = 512.0;
          const int     RefY   = 255;
      
          static f32  _Y2L[ 256];
          static u8   _L2Y[TabLen+1];
      
          static void Init ()
          {
              for (size_t i = 0; i <  256; i++)
                  _Y2L[i] = ColorSpace::Sloow_Y2L(i / f32(RefY));
              for (size_t i = 0; i <= TabLen; i++)
                  _L2Y[i] = ColorSpace::Sloow_L2Y(f32(i) / TabLen) * RefY + 0.5;
          }
      
          static f32 Y2L(const u8 Y)
          {
              return _Y2L[Y];
          }
          static u8 L2Y(const f32 L)
          {
              const int c = int(TabLen*L + 0.5);
              if (c <      0) return   0;
              if (c > TabLen) return 255;
              return _L2Y[c];
          }
      }
      
      int main()
      {
          LUT::Init();
          
          uint errors = 0;
          for (int i = 0; i < 256; i++)
          {
              const f32 Slow = 255.0 * ColorSpace::Sloow_L2Y(ColorSpace::Sloow_Y2L(i/255.0));
              const u8  Fast = LUT::L2Y(LUT::Y2L(i));
              ::printf ("%3d %3d %c\n", i, Fast, i==Fast ? ' ' : '*');
              if (i != Fast)
                  errors++;
          }
          ::printf ("%u errors\n", errors);
          return 0;
      }

      Comment


        #4
        The simplest fix is to increase the LUT for L2Y-Lookup:

        256 Entries: 73 errors
        512 Entries: 39 errors
        1024 Entries: 18 errors
        2048 Entries: 6 errors
        4096 Entries: No errors

        and besides, the color space conversion really should only be used for resampling (possible bugs, CPU time consumption, Memory space consumption).

        Comment

        Working...
        X