topical media & game development

talk show tell print

graphic-canvas-util-pixastic-actions-sepia.js / js



  /*
   * Pixastic Lib - Sepia filter - v0.1.0
   * Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
   * MIT License [http://www.opensource.org/licenses/mit-license.php]
   */
  
  Pixastic.Actions.sepia = {
  
          process : function(params) {
                  var mode = (parseInt(params.options.mode,10)||0);
                  if (mode < 0) mode = 0;
                  if (mode > 1) mode = 1;
  
                  if (Pixastic.Client.hasCanvasImageData()) {
                          var data = Pixastic.prepareData(params);
                          var rect = params.options.rect;
                          var w = rect.width;
                          var h = rect.height;
                          var w4 = w*4;
                          var y = h;
                          do {
                                  var offsetY = (y-1)*w4;
                                  var x = w;
                                  do {
                                          var offset = offsetY + (x-1)*4;
  
                                          if (mode) {
                                                  // a bit faster, but not as good
                                                  var d = data[offset] * 0.299 + data[offset+1] * 0.587 + data[offset+2] * 0.114;
                                                  var r = (d + 39);
                                                  var g = (d + 14);
                                                  var b = (d - 36);
                                          } else {
                                                  // Microsoft
                                                  var or = data[offset];
                                                  var og = data[offset+1];
                                                  var ob = data[offset+2];
          
                                                  var r = (or * 0.393 + og * 0.769 + ob * 0.189);
                                                  var g = (or * 0.349 + og * 0.686 + ob * 0.168);
                                                  var b = (or * 0.272 + og * 0.534 + ob * 0.131);
                                          }
  
                                          if (r < 0) r = 0; if (r > 255) r = 255;
                                          if (g < 0) g = 0; if (g > 255) g = 255;
                                          if (b < 0) b = 0; if (b > 255) b = 255;
  
                                          data[offset] = r;
                                          data[offset+1] = g;
                                          data[offset+2] = b;
  
                                  } while (--x);
                          } while (--y);
                          return true;
                  }
          },
          checkSupport : function() {
                  return Pixastic.Client.hasCanvasImageData();
          }
  }


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.