<?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>67564</bug_id>
          
          <creation_ts>2011-09-03 09:23:45 -0700</creation_ts>
          <short_desc>CanvasPixelArray should be implemented as a Uint8Array</short_desc>
          <delta_ts>2012-04-09 17:04:42 -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>Canvas</component>
          <version>528+ (Nightly build)</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>DUPLICATE</resolution>
          <dup_id>82802</dup_id>
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P5</priority>
          <bug_severity>Enhancement</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>0</everconfirmed>
          <reporter name="Joseph Huckaby">jhuckaby</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>guanqun.lu</cc>
    
    <cc>ian</cc>
    
    <cc>mdelaney7</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>461825</commentid>
    <comment_count>0</comment_count>
    <who name="Joseph Huckaby">jhuckaby</who>
    <bug_when>2011-09-03 09:23:45 -0700</bug_when>
    <thetext>Currently, when using a Canvas and createImageData() or getImageData() are called, an &quot;ImageData&quot; object is returned containing the requested pixels as an array in the &quot;data&quot; property.  But the pixel array is implemented as a &quot;CanvasPixelArray&quot; object.  This should instead be implemented as a Uint8Array, backed by an ArrayBuffer.

CanvasPixelData is theoretically already an &quot;accelerated&quot; array, and was introduced before typed array classes.  But returning an actual Uint8Array has a number of important advantages.  For one, it standardizes accelerated arrays to always use the typed array system, instead of having a strange one-off &quot;CanvasPixelData&quot; class.  But most importantly, it would allow developers to cast the array as something else, like a Uint32Array (i.e. create another &quot;view&quot; of the ArrayBuffer).

Right now, setting a single canvas pixel involves 4 separate operations, because the &quot;red&quot;, &quot;green&quot;, &quot;blue&quot; and &quot;alpha&quot; channels are each separate elements in the array.  But if the pixel array could be cast to a Uint32Array, then each pixel would take up only one element, and setting pixels could effectively be four times faster (depending on what the developer is doing).  For example, you could pre-calculate 32-bit pixel values using bit-shift operations, then plop them into the Uint32Array 4 times faster than using an 8-bit array.

Example:

imageData = context.getImageData( 0, 0, 640, 480 );

var buffer = imageData.data.buffer; // get ArrayBuffer out of Uint8Array
var pixels = new Uint32Array( buffer ); // create Uint32Array view of same buffer

// precalculate a &quot;white&quot; opaque pixel as a 32-bit uint
var alpha = 255, blue = 255, green = 255, red = 255;
var pixel = (alpha) + (blue &lt;&lt; 8) + (green &lt;&lt; 16) + (red &lt;&lt; 24);

// pixels.length is only 1/4 of the size it normally would be, so this should be very fast
for (var idx = 0, len = pixels.length; idx &lt; len; idx++) {
   pixels[idx] = pixel;
}

context.putImageData( imageData, 0, 0 );

This change should theoretically not have any adverse affects, and all existing JavaScript canvas code should continue to work, because right now &quot;CanvasPixelArray&quot; is effectively just an accelerated array of 8-bit unsigned integers.  I am merely suggesting changing the underlying implementation to a real Uint8Array, so the data can be type cast if desired.  If the developer doesn&apos;t cast, it will continue to work exactly the same as it does now.

This would be extremely useful for Canvas based games.

Thanks for your time!

- Joe</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>471249</commentid>
    <comment_count>1</comment_count>
    <who name="Joseph Huckaby">jhuckaby</who>
    <bug_when>2011-09-21 16:30:39 -0700</bug_when>
    <thetext>FYI, this has now shown up on the WHATWG:
http://html5.org/tools/web-apps-tracker?from=6558&amp;to=6559

And Firefox 6 has implemented it.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>598781</commentid>
    <comment_count>2</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2012-04-09 17:04:42 -0700</bug_when>
    <thetext>

*** This bug has been marked as a duplicate of bug 82802 ***</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>