<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>30333</bug_id>
          
          <creation_ts>2009-10-13 06:43:04 -0700</creation_ts>
          <short_desc>(Un)premultiplication code is not optimal and broken in alpha==0 case</short_desc>
          <delta_ts>2026-04-01 07:59:49 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>SVG</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>PC</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>CONFIGURATION CHANGED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Sebastian Dröge (slomo)">slomo</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>ahmad.saleem792</cc>
    
    <cc>bfulgham</cc>
    
    <cc>gustavo</cc>
    
    <cc>karlcow</cc>
    
    <cc>krit</cc>
    
    <cc>mdelaney7</cc>
    
    <cc>oliver</cc>
    
    <cc>simon.fraser</cc>
    
    <cc>zalan</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>154324</commentid>
    <comment_count>0</comment_count>
    <who name="Sebastian Dröge (slomo)">slomo</who>
    <bug_when>2009-10-13 06:43:04 -0700</bug_when>
    <thetext>Hi,
attached patch fixes the (Un)premultiplication code in WebCore/platform/graphics/Color.cpp.

Explanation why it&apos;s broken follows</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>154325</commentid>
    <comment_count>1</comment_count>
      <attachid>41101</attachid>
    <who name="Sebastian Dröge (slomo)">slomo</who>
    <bug_when>2009-10-13 06:50:58 -0700</bug_when>
    <thetext>Created attachment 41101
0002-Fix-Color-alpha-un-premultiplication-code.patch

Now to the explanation. First of all, for consistency and correctness the resulting value of (un-)premultiplication with a 0-alpha should result in (0, 0, 0, 0) instead of the input value without changes. This is because:
a) unpremultiplication: if input alpha is 0 all other components *must* be 0 too, otherwise the premultiplication was done incorrectly
b) premultiplication: for non-0 alpha we multiply with alpha, if we do the same for 0 alpha the output will be (0, 0, 0, 0) as it should

For the non-0 alpha cases it makes more sense to round to the nearest integer instead of always rounding upwards. Rounding upwards results in an uneven distribution of color values, i.e. a 0 component is much less likely than every other component value.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>154341</commentid>
    <comment_count>2</comment_count>
      <attachid>41101</attachid>
    <who name="Darin Adler">darin</who>
    <bug_when>2009-10-13 08:46:43 -0700</bug_when>
    <thetext>Comment on attachment 41101
0002-Fix-Color-alpha-un-premultiplication-code.patch

Thanks for this contribution!

The change seems OK, but we need a test case that demonstrates the effects this has. We don&apos;t take bug fixes without test cases. So please add a test case demonstrating the effect this has. One way to create a test case may be to use the &lt;canvas&gt; element, because that supports reading back pixel values from the canvas buffer.

LayoutTests/fast/canvas/script-tests/set-colors.js is an example of a test case that might be adaptable to test this behavior.

An alternative would be to explain why no test case is possible. That seems unlikely, though.

Also, you need to run all existing tests and generate new results for any tests that now will have different results.

review- because there is no test case.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>154533</commentid>
    <comment_count>3</comment_count>
    <who name="Sebastian Dröge (slomo)">slomo</who>
    <bug_when>2009-10-13 21:47:49 -0700</bug_when>
    <thetext>(In reply to comment #2)
&gt; (From update of attachment 41101 [details])
&gt; Thanks for this contribution!
&gt; 
&gt; The change seems OK, but we need a test case that demonstrates the effects this
&gt; has. We don&apos;t take bug fixes without test cases. So please add a test case
&gt; demonstrating the effect this has. One way to create a test case may be to use
&gt; the &lt;canvas&gt; element, because that supports reading back pixel values from the
&gt; canvas buffer.
&gt; 
&gt; LayoutTests/fast/canvas/script-tests/set-colors.js is an example of a test case
&gt; that might be adaptable to test this behavior.

I just noticed this while working on other code that needs alpha premultiplication. I have absolutely *no* idea where this code here is used and how.

I could provide a small test case that produces different results with/without the patch by just calling those functions but no idea how to do it from a canvas element.

&gt; An alternative would be to explain why no test case is possible. That seems
&gt; unlikely, though.
&gt; 
&gt; Also, you need to run all existing tests and generate new results for any tests
&gt; that now will have different results.

How can I do that?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>154545</commentid>
    <comment_count>4</comment_count>
    <who name="Sebastian Dröge (slomo)">slomo</who>
    <bug_when>2009-10-14 01:23:57 -0700</bug_when>
    <thetext>Ok, I know how to run the layout tests now but this is really taking far too long for such a simply patch. I&apos;ll try to write a small testcase for this later though.

Also this code is only used by the cairo backend in a few places, here&apos;s the relevant code from cairo itself and as you can see it does the same.

http://cgit.freedesktop.org/cairo/tree/src/cairo-png.c#n53
http://cgit.freedesktop.org/cairo/tree/src/cairo-png.c#n408</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>154812</commentid>
    <comment_count>5</comment_count>
    <who name="Dirk Schulze">krit</who>
    <bug_when>2009-10-15 09:42:37 -0700</bug_when>
    <thetext>(In reply to comment #4)
&gt; Ok, I know how to run the layout tests now but this is really taking far too
&gt; long for such a simply patch. I&apos;ll try to write a small testcase for this later
&gt; though.
&gt; 
&gt; Also this code is only used by the cairo backend in a few places, here&apos;s the
&gt; relevant code from cairo itself and as you can see it does the same.
&gt; 
&gt; http://cgit.freedesktop.org/cairo/tree/src/cairo-png.c#n53
&gt; http://cgit.freedesktop.org/cairo/tree/src/cairo-png.c#n408

There are already some tests for getImageData and putImageData in LayoutTests/fast/canvas/
This both calls are mainly using this color-code.

Tests that can help you to figure out if your code works are:
   * canvas-putImageData.html
   * canvas-getImageData.html

But I should warn you. canvas-getImageData might still fail because of bug 22150
It would be a great improvement if some more steps pass on canvas-getImageData.html.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>154816</commentid>
    <comment_count>6</comment_count>
    <who name="Sebastian Dröge (slomo)">slomo</who>
    <bug_when>2009-10-15 09:51:06 -0700</bug_when>
    <thetext>Thanks, I&apos;ll take those tests as starting point</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>154818</commentid>
    <comment_count>7</comment_count>
    <who name="Sebastian Dröge (slomo)">slomo</who>
    <bug_when>2009-10-15 09:52:28 -0700</bug_when>
    <thetext>After reading bug #22150 it could be that my patch improves the situation there... we&apos;ll see.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>203342</commentid>
    <comment_count>8</comment_count>
    <who name="Dirk Schulze">krit</who>
    <bug_when>2010-03-24 00:03:31 -0700</bug_when>
    <thetext>Simon added a test case for SVG and premultiplied/not-premultiplied Colors. Maybe this patch should get rid of the hack in bug 34383 and use this SVG test as test case.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>203451</commentid>
    <comment_count>9</comment_count>
    <who name="Simon Fraser (smfr)">simon.fraser</who>
    <bug_when>2010-03-24 09:48:50 -0700</bug_when>
    <thetext>Had to work around this issue in http://trac.webkit.org/changeset/54106</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1053191</commentid>
    <comment_count>10</comment_count>
    <who name="Sebastian Dröge (slomo)">slomo</who>
    <bug_when>2014-12-07 10:48:37 -0800</bug_when>
    <thetext>What should we do with this? Note that if you consider merging this patch, that the mail address used there is not valid anymore.

Simon?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1900934</commentid>
    <comment_count>11</comment_count>
    <who name="Ahmad Saleem">ahmad.saleem792</who>
    <bug_when>2022-09-24 16:05:20 -0700</bug_when>
    <thetext>Is this still an issue? I tried to look for the code from the patch but couldn&apos;t manage to find &quot;colorFromPremultipliedARGB&quot; and it seems that it was deleted in this commit:

https://github.com/WebKit/WebKit/commit/9b9b872709ed845f54ad8f5aa9257bd0212c455d

If not needed, can we close this bug or if this is still an issue, can we get update explanation etc.? Thanks!</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2195909</commentid>
    <comment_count>12</comment_count>
    <who name="Karl Dubost">karlcow</who>
    <bug_when>2026-04-01 07:59:49 -0700</bug_when>
    <thetext>The original functions (colorFromPremultipliedARGB) were deleted and replaced. Modern WebCore uses Color/SRGBA types with proper alpha handling. The premultiplication code has been completely rewritten across PixelBufferConversion.cpp, FilterImage.cpp, etc</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>41101</attachid>
            <date>2009-10-13 06:50:58 -0700</date>
            <delta_ts>2010-06-11 10:29:19 -0700</delta_ts>
            <desc>0002-Fix-Color-alpha-un-premultiplication-code.patch</desc>
            <filename>0002-Fix-Color-alpha-un-premultiplication-code.patch</filename>
            <type>text/plain</type>
            <size>3626</size>
            <attacher name="Sebastian Dröge (slomo)">slomo</attacher>
            
              <data encoding="base64">RnJvbSA3MmMxMjlkYTk0MmQ2ZmEyNzY4ZjdjYzZkMTc5MDg4YjAwYzZhNTkwIE1vbiBTZXAgMTcg
MDA6MDA6MDAgMjAwMQpGcm9tOiA9P1VURi04P3E/U2ViYXN0aWFuPTIwRHI9QzM9QjZnZT89IDxz
ZWJhc3RpYW4uZHJvZWdlQGNvbGxhYm9yYS5jby51az4KRGF0ZTogVHVlLCAxMyBPY3QgMjAwOSAx
NTo0NjoyMSArMDIwMApTdWJqZWN0OiBbUEFUQ0ggMi8yXSBGaXggQ29sb3IgYWxwaGEgKHVuLSlw
cmVtdWx0aXBsaWNhdGlvbiBjb2RlLgoKICAgICAgICBodHRwczovL2J1Z3Mud2Via2l0Lm9yZy9z
aG93X2J1Zy5jZ2k/aWQ9MzAzMzMKCiAgICAgICAgKiBwbGF0Zm9ybS9ncmFwaGljcy9Db2xvci5j
cHA6CiAgICAgICAgKFdlYkNvcmU6OmNvbG9yRnJvbVByZW11bHRpcGx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oxLjYuNC4zCgo=
</data>
<flag name="review"
          id="22418"
          type_id="1"
          status="-"
          setter="darin"
    />
          </attachment>
      

    </bug>

</bugzilla>