-
Notifications
You must be signed in to change notification settings - Fork 0
/
pixastic.custom.js
1357 lines (1140 loc) · 33.3 KB
/
pixastic.custom.js
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Pixastic Lib - Core Functions - v0.1.3
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
var Pixastic = (function() {
function addEvent(el, event, handler) {
if (el.addEventListener)
el.addEventListener(event, handler, false);
else if (el.attachEvent)
el.attachEvent("on" + event, handler);
}
function onready(handler) {
var handlerDone = false;
var execHandler = function() {
if (!handlerDone) {
handlerDone = true;
handler();
}
}
document.write("<"+"script defer src=\"//:\" id=\"__onload_ie_pixastic__\"></"+"script>");
var script = document.getElementById("__onload_ie_pixastic__");
script.onreadystatechange = function() {
if (script.readyState == "complete") {
script.parentNode.removeChild(script);
execHandler();
}
}
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", execHandler, false);
addEvent(window, "load", execHandler);
}
function init() {
var imgEls = getElementsByClass("pixastic", null, "img");
var canvasEls = getElementsByClass("pixastic", null, "canvas");
var elements = imgEls.concat(canvasEls);
for (var i=0;i<elements.length;i++) {
(function() {
var el = elements[i];
var actions = [];
var classes = el.className.split(" ");
for (var c=0;c<classes.length;c++) {
var cls = classes[c];
if (cls.substring(0,9) == "pixastic-") {
var actionName = cls.substring(9);
if (actionName != "")
actions.push(actionName);
}
}
if (actions.length) {
if (el.tagName.toLowerCase() == "img") {
var dataImg = new Image();
dataImg.src = el.src;
if (dataImg.complete) {
for (var a=0;a<actions.length;a++) {
var res = Pixastic.applyAction(el, el, actions[a], null);
if (res)
el = res;
}
} else {
dataImg.onload = function() {
for (var a=0;a<actions.length;a++) {
var res = Pixastic.applyAction(el, el, actions[a], null)
if (res)
el = res;
}
}
}
} else {
setTimeout(function() {
for (var a=0;a<actions.length;a++) {
var res = Pixastic.applyAction(
el, el, actions[a], null
);
if (res)
el = res;
}
},1);
}
}
})();
}
}
if (typeof pixastic_parseonload != "undefined" && pixastic_parseonload)
onready(init);
// getElementsByClass by Dustin Diaz, http://www.dustindiaz.com/getelementsbyclass/
function getElementsByClass(searchClass,node,tag) {
var classElements = new Array();
if ( node == null )
node = document;
if ( tag == null )
tag = '*';
var els = node.getElementsByTagName(tag);
var elsLen = els.length;
var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
for (i = 0, j = 0; i < elsLen; i++) {
if ( pattern.test(els[i].className) ) {
classElements[j] = els[i];
j++;
}
}
return classElements;
}
var debugElement;
function writeDebug(text, level) {
if (!Pixastic.debug) return;
try {
switch (level) {
case "warn" :
console.warn("Pixastic:", text);
break;
case "error" :
console.error("Pixastic:", text);
break;
default:
console.log("Pixastic:", text);
}
} catch(e) {
}
if (!debugElement) {
}
}
// canvas capability checks
var hasCanvas = (function() {
var c = document.createElement("canvas");
var val = false;
try {
val = !!((typeof c.getContext == "function") && c.getContext("2d"));
} catch(e) {}
return function() {
return val;
}
})();
var hasCanvasImageData = (function() {
var c = document.createElement("canvas");
var val = false;
var ctx;
try {
if (typeof c.getContext == "function" && (ctx = c.getContext("2d"))) {
val = (typeof ctx.getImageData == "function");
}
} catch(e) {}
return function() {
return val;
}
})();
var hasGlobalAlpha = (function() {
var hasAlpha = false;
var red = document.createElement("canvas");
if (hasCanvas() && hasCanvasImageData()) {
red.width = red.height = 1;
var redctx = red.getContext("2d");
redctx.fillStyle = "rgb(255,0,0)";
redctx.fillRect(0,0,1,1);
var blue = document.createElement("canvas");
blue.width = blue.height = 1;
var bluectx = blue.getContext("2d");
bluectx.fillStyle = "rgb(0,0,255)";
bluectx.fillRect(0,0,1,1);
redctx.globalAlpha = 0.5;
redctx.drawImage(blue, 0, 0);
var reddata = redctx.getImageData(0,0,1,1).data;
hasAlpha = (reddata[2] != 255);
}
return function() {
return hasAlpha;
}
})();
// return public interface
return {
parseOnLoad : false,
debug : false,
applyAction : function(img, dataImg, actionName, options) {
options = options || {};
var imageIsCanvas = (img.tagName.toLowerCase() == "canvas");
if (imageIsCanvas && Pixastic.Client.isIE()) {
if (Pixastic.debug) writeDebug("Tried to process a canvas element but browser is IE.");
return false;
}
var canvas, ctx;
var hasOutputCanvas = false;
if (Pixastic.Client.hasCanvas()) {
hasOutputCanvas = !!options.resultCanvas;
canvas = options.resultCanvas || document.createElement("canvas");
ctx = canvas.getContext("2d");
}
var w = img.offsetWidth;
var h = img.offsetHeight;
if (imageIsCanvas) {
w = img.width;
h = img.height;
}
// offsetWidth/Height might be 0 if the image is not in the document
if (w == 0 || h == 0) {
if (img.parentNode == null) {
// add the image to the doc (way out left), read its dimensions and remove it again
var oldpos = img.style.position;
var oldleft = img.style.left;
img.style.position = "absolute";
img.style.left = "-9999px";
document.body.appendChild(img);
w = img.offsetWidth;
h = img.offsetHeight;
document.body.removeChild(img);
img.style.position = oldpos;
img.style.left = oldleft;
} else {
if (Pixastic.debug) writeDebug("Image has 0 width and/or height.");
return;
}
}
if (actionName.indexOf("(") > -1) {
var tmp = actionName;
actionName = tmp.substr(0, tmp.indexOf("("));
var arg = tmp.match(/\((.*?)\)/);
if (arg[1]) {
arg = arg[1].split(";");
for (var a=0;a<arg.length;a++) {
thisArg = arg[a].split("=");
if (thisArg.length == 2) {
if (thisArg[0] == "rect") {
var rectVal = thisArg[1].split(",");
options[thisArg[0]] = {
left : parseInt(rectVal[0],10)||0,
top : parseInt(rectVal[1],10)||0,
width : parseInt(rectVal[2],10)||0,
height : parseInt(rectVal[3],10)||0
}
} else {
options[thisArg[0]] = thisArg[1];
}
}
}
}
}
if (!options.rect) {
options.rect = {
left : 0, top : 0, width : w, height : h
};
} else {
options.rect.left = Math.round(options.rect.left);
options.rect.top = Math.round(options.rect.top);
options.rect.width = Math.round(options.rect.width);
options.rect.height = Math.round(options.rect.height);
}
var validAction = false;
if (Pixastic.Actions[actionName] && typeof Pixastic.Actions[actionName].process == "function") {
validAction = true;
}
if (!validAction) {
if (Pixastic.debug) writeDebug("Invalid action \"" + actionName + "\". Maybe file not included?");
return false;
}
if (!Pixastic.Actions[actionName].checkSupport()) {
if (Pixastic.debug) writeDebug("Action \"" + actionName + "\" not supported by this browser.");
return false;
}
if (Pixastic.Client.hasCanvas()) {
if (canvas !== img) {
canvas.width = w;
canvas.height = h;
}
if (!hasOutputCanvas) {
canvas.style.width = w+"px";
canvas.style.height = h+"px";
}
ctx.drawImage(dataImg,0,0,w,h);
if (!img.__pixastic_org_image) {
canvas.__pixastic_org_image = img;
canvas.__pixastic_org_width = w;
canvas.__pixastic_org_height = h;
} else {
canvas.__pixastic_org_image = img.__pixastic_org_image;
canvas.__pixastic_org_width = img.__pixastic_org_width;
canvas.__pixastic_org_height = img.__pixastic_org_height;
}
} else if (Pixastic.Client.isIE() && typeof img.__pixastic_org_style == "undefined") {
img.__pixastic_org_style = img.style.cssText;
}
var params = {
image : img,
canvas : canvas,
width : w,
height : h,
useData : true,
options : options
}
// Ok, let's do it!
var res = Pixastic.Actions[actionName].process(params);
if (!res) {
return false;
}
if (Pixastic.Client.hasCanvas()) {
if (params.useData) {
if (Pixastic.Client.hasCanvasImageData()) {
canvas.getContext("2d").putImageData(params.canvasData, options.rect.left, options.rect.top);
// Opera doesn't seem to update the canvas until we draw something on it, lets draw a 0x0 rectangle.
// Is this still so?
canvas.getContext("2d").fillRect(0,0,0,0);
}
}
if (!options.leaveDOM) {
// copy properties and stuff from the source image
canvas.title = img.title;
canvas.imgsrc = img.imgsrc;
if (!imageIsCanvas) canvas.alt = img.alt;
if (!imageIsCanvas) canvas.imgsrc = img.src;
canvas.className = img.className;
canvas.style.cssText = img.style.cssText;
canvas.name = img.name;
canvas.tabIndex = img.tabIndex;
canvas.id = img.id;
if (img.parentNode && img.parentNode.replaceChild) {
img.parentNode.replaceChild(canvas, img);
}
}
options.resultCanvas = canvas;
return canvas;
}
return img;
},
prepareData : function(params, getCopy) {
var ctx = params.canvas.getContext("2d");
var rect = params.options.rect;
var dataDesc = ctx.getImageData(rect.left, rect.top, rect.width, rect.height);
var data = dataDesc.data;
if (!getCopy) params.canvasData = dataDesc;
return data;
},
// load the image file
process : function(img, actionName, options, callback) {
if (img.tagName.toLowerCase() == "img") {
var dataImg = new Image();
dataImg.src = img.src;
if (dataImg.complete) {
var res = Pixastic.applyAction(img, dataImg, actionName, options);
if (callback) callback(res);
return res;
} else {
dataImg.onload = function() {
var res = Pixastic.applyAction(img, dataImg, actionName, options)
if (callback) callback(res);
}
}
}
if (img.tagName.toLowerCase() == "canvas") {
var res = Pixastic.applyAction(img, img, actionName, options);
if (callback) callback(res);
return res;
}
},
revert : function(img) {
if (Pixastic.Client.hasCanvas()) {
if (img.tagName.toLowerCase() == "canvas" && img.__pixastic_org_image) {
img.width = img.__pixastic_org_width;
img.height = img.__pixastic_org_height;
img.getContext("2d").drawImage(img.__pixastic_org_image, 0, 0);
if (img.parentNode && img.parentNode.replaceChild) {
img.parentNode.replaceChild(img.__pixastic_org_image, img);
}
return img;
}
} else if (Pixastic.Client.isIE()) {
if (typeof img.__pixastic_org_style != "undefined")
img.style.cssText = img.__pixastic_org_style;
}
},
Client : {
hasCanvas : hasCanvas,
hasCanvasImageData : hasCanvasImageData,
hasGlobalAlpha : hasGlobalAlpha,
isIE : function() {
return !!document.all && !!window.attachEvent && !window.opera;
}
},
Actions : {}
}
})();
/*
* Pixastic Lib - Blur filter - v0.1.0
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.blur = {
process : function(params) {
if (typeof params.options.fixMargin == "undefined")
params.options.fixMargin = true;
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
var dataCopy = Pixastic.prepareData(params, true)
/*
var kernel = [
[0.5, 1, 0.5],
[1, 2, 1],
[0.5, 1, 0.5]
];
*/
var kernel = [
[0, 1, 0],
[1, 2, 1],
[0, 1, 0]
];
var weight = 0;
for (var i=0;i<3;i++) {
for (var j=0;j<3;j++) {
weight += kernel[i][j];
}
}
weight = 1 / (weight*2);
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 prevY = (y == 1) ? 0 : y-2;
var nextY = (y == h) ? y - 1 : y;
var offsetYPrev = prevY*w*4;
var offsetYNext = nextY*w*4;
var x = w;
do {
var offset = offsetY + (x*4-4);
var offsetPrev = offsetYPrev + ((x == 1) ? 0 : x-2) * 4;
var offsetNext = offsetYNext + ((x == w) ? x-1 : x) * 4;
data[offset] = (
/*
dataCopy[offsetPrev - 4]
+ dataCopy[offsetPrev+4]
+ dataCopy[offsetNext - 4]
+ dataCopy[offsetNext+4]
+
*/
(dataCopy[offsetPrev]
+ dataCopy[offset-4]
+ dataCopy[offset+4]
+ dataCopy[offsetNext]) * 2
+ dataCopy[offset] * 4
) * weight;
data[offset+1] = (
/*
dataCopy[offsetPrev - 3]
+ dataCopy[offsetPrev+5]
+ dataCopy[offsetNext - 3]
+ dataCopy[offsetNext+5]
+
*/
(dataCopy[offsetPrev+1]
+ dataCopy[offset-3]
+ dataCopy[offset+5]
+ dataCopy[offsetNext+1]) * 2
+ dataCopy[offset+1] * 4
) * weight;
data[offset+2] = (
/*
dataCopy[offsetPrev - 2]
+ dataCopy[offsetPrev+6]
+ dataCopy[offsetNext - 2]
+ dataCopy[offsetNext+6]
+
*/
(dataCopy[offsetPrev+2]
+ dataCopy[offset-2]
+ dataCopy[offset+6]
+ dataCopy[offsetNext+2]) * 2
+ dataCopy[offset+2] * 4
) * weight;
} while (--x);
} while (--y);
return true;
} else if (Pixastic.Client.isIE()) {
params.image.style.filter += " progid:DXImageTransform.Microsoft.Blur(pixelradius=1.5)";
if (params.options.fixMargin) {
params.image.style.marginLeft = (parseInt(params.image.style.marginLeft,10)||0) - 2 + "px";
params.image.style.marginTop = (parseInt(params.image.style.marginTop,10)||0) - 2 + "px";
}
return true;
}
},
checkSupport : function() {
return (Pixastic.Client.hasCanvasImageData() || Pixastic.Client.isIE());
}
}/*
* Pixastic Lib - Brightness/Contrast filter - v0.1.1
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.brightness = {
process : function(params) {
var brightness = parseInt(params.options.brightness,10) || 0;
var contrast = parseFloat(params.options.contrast)||0;
var legacy = !!(params.options.legacy && params.options.legacy != "false");
if (legacy) {
brightness = Math.min(150,Math.max(-150,brightness));
} else {
var brightMul = 1 + Math.min(150,Math.max(-150,brightness)) / 150;
}
contrast = Math.max(0,contrast+1);
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
var rect = params.options.rect;
var w = rect.width;
var h = rect.height;
var p = w*h;
var pix = p*4, pix1, pix2;
var mul, add;
if (contrast != 1) {
if (legacy) {
mul = contrast;
add = (brightness - 128) * contrast + 128;
} else {
mul = brightMul * contrast;
add = - contrast * 128 + 128;
}
} else { // this if-then is not necessary anymore, is it?
if (legacy) {
mul = 1;
add = brightness;
} else {
mul = brightMul;
add = 0;
}
}
var r, g, b;
while (p--) {
if ((r = data[pix-=4] * mul + add) > 255 )
data[pix] = 255;
else if (r < 0)
data[pix] = 0;
else
data[pix] = r;
if ((g = data[pix1=pix+1] * mul + add) > 255 )
data[pix1] = 255;
else if (g < 0)
data[pix1] = 0;
else
data[pix1] = g;
if ((b = data[pix2=pix+2] * mul + add) > 255 )
data[pix2] = 255;
else if (b < 0)
data[pix2] = 0;
else
data[pix2] = b;
}
return true;
}
},
checkSupport : function() {
return Pixastic.Client.hasCanvasImageData();
}
}
/*
* Pixastic Lib - Color adjust filter - v0.1.1
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.coloradjust = {
process : function(params) {
var red = parseFloat(params.options.red) || 0;
var green = parseFloat(params.options.green) || 0;
var blue = parseFloat(params.options.blue) || 0;
red = Math.round(red*255);
green = Math.round(green*255);
blue = Math.round(blue*255);
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
var rect = params.options.rect;
var p = rect.width*rect.height;
var pix = p*4, pix1, pix2;
var r, g, b;
while (p--) {
pix -= 4;
if (red) {
if ((r = data[pix] + red) < 0 )
data[pix] = 0;
else if (r > 255 )
data[pix] = 255;
else
data[pix] = r;
}
if (green) {
if ((g = data[pix1=pix+1] + green) < 0 )
data[pix1] = 0;
else if (g > 255 )
data[pix1] = 255;
else
data[pix1] = g;
}
if (blue) {
if ((b = data[pix2=pix+2] + blue) < 0 )
data[pix2] = 0;
else if (b > 255 )
data[pix2] = 255;
else
data[pix2] = b;
}
}
return true;
}
},
checkSupport : function() {
return (Pixastic.Client.hasCanvasImageData());
}
}
/*
* Pixastic Lib - Desaturation filter - v0.1.1
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.desaturate = {
process : function(params) {
var useAverage = !!(params.options.average && params.options.average != "false");
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
var rect = params.options.rect;
var w = rect.width;
var h = rect.height;
var p = w*h;
var pix = p*4, pix1, pix2;
if (useAverage) {
while (p--)
data[pix-=4] = data[pix1=pix+1] = data[pix2=pix+2] = (data[pix]+data[pix1]+data[pix2])/3
} else {
while (p--)
data[pix-=4] = data[pix1=pix+1] = data[pix2=pix+2] = (data[pix]*0.3 + data[pix1]*0.59 + data[pix2]*0.11);
}
return true;
} else if (Pixastic.Client.isIE()) {
params.image.style.filter += " gray";
return true;
}
},
checkSupport : function() {
return (Pixastic.Client.hasCanvasImageData() || Pixastic.Client.isIE());
}
}/*
* Pixastic Lib - Glow - v0.1.0
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.glow = {
process : function(params) {
var amount = (parseFloat(params.options.amount)||0);
var blurAmount = parseFloat(params.options.radius)||0;
amount = Math.min(1,Math.max(0,amount));
blurAmount = Math.min(5,Math.max(0,blurAmount));
if (Pixastic.Client.hasCanvasImageData()) {
var rect = params.options.rect;
var blurCanvas = document.createElement("canvas");
blurCanvas.width = params.width;
blurCanvas.height = params.height;
var blurCtx = blurCanvas.getContext("2d");
blurCtx.drawImage(params.canvas,0,0);
var scale = 2;
var smallWidth = Math.round(params.width / scale);
var smallHeight = Math.round(params.height / scale);
var copy = document.createElement("canvas");
copy.width = smallWidth;
copy.height = smallHeight;
var clear = true;
var steps = Math.round(blurAmount * 20);
var copyCtx = copy.getContext("2d");
for (var i=0;i<steps;i++) {
var scaledWidth = Math.max(1,Math.round(smallWidth - i));
var scaledHeight = Math.max(1,Math.round(smallHeight - i));
copyCtx.clearRect(0,0,smallWidth,smallHeight);
copyCtx.drawImage(
blurCanvas,
0,0,params.width,params.height,
0,0,scaledWidth,scaledHeight
);
blurCtx.clearRect(0,0,params.width,params.height);
blurCtx.drawImage(
copy,
0,0,scaledWidth,scaledHeight,
0,0,params.width,params.height
);
}
var data = Pixastic.prepareData(params);
var blurData = Pixastic.prepareData({canvas:blurCanvas,options:params.options});
var p = rect.width * rect.height;
var pix = p*4, pix1 = pix + 1, pix2 = pix + 2, pix3 = pix + 3;
while (p--) {
if ((data[pix-=4] += amount * blurData[pix]) > 255) data[pix] = 255;
if ((data[pix1-=4] += amount * blurData[pix1]) > 255) data[pix1] = 255;
if ((data[pix2-=4] += amount * blurData[pix2]) > 255) data[pix2] = 255;
}
return true;
}
},
checkSupport : function() {
return Pixastic.Client.hasCanvasImageData();
}
}
/*
* Pixastic Lib - HSL Adjust - v0.1.1
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.hsl = {
process : function(params) {
var hue = parseInt(params.options.hue,10)||0;
var saturation = (parseInt(params.options.saturation,10)||0) / 100;
var lightness = (parseInt(params.options.lightness,10)||0) / 100;
// this seems to give the same result as Photoshop
if (saturation < 0) {
var satMul = 1+saturation;
} else {
var satMul = 1+saturation*2;
}
hue = (hue%360) / 360;
var hue6 = hue * 6;
var rgbDiv = 1 / 255;
var light255 = lightness * 255;
var lightp1 = 1 + lightness;
var lightm1 = 1 - lightness;
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
var rect = params.options.rect;
var p = rect.width * rect.height;
var pix = p*4, pix1 = pix + 1, pix2 = pix + 2, pix3 = pix + 3;
while (p--) {
var r = data[pix-=4];
var g = data[pix1=pix+1];
var b = data[pix2=pix+2];
if (hue != 0 || saturation != 0) {
// ok, here comes rgb to hsl + adjust + hsl to rgb, all in one jumbled mess.
// It's not so pretty, but it's been optimized to get somewhat decent performance.
// The transforms were originally adapted from the ones found in Graphics Gems, but have been heavily modified.
var vs = r;
if (g > vs) vs = g;
if (b > vs) vs = b;
var ms = r;
if (g < ms) ms = g;
if (b < ms) ms = b;
var vm = (vs-ms);
var l = (ms+vs)/510;
if (l > 0) {
if (vm > 0) {
if (l <= 0.5) {
var s = vm / (vs+ms) * satMul;
if (s > 1) s = 1;
var v = (l * (1+s));
} else {
var s = vm / (510-vs-ms) * satMul;
if (s > 1) s = 1;
var v = (l+s - l*s);
}
if (r == vs) {
if (g == ms)
var h = 5 + ((vs-b)/vm) + hue6;
else
var h = 1 - ((vs-g)/vm) + hue6;
} else if (g == vs) {
if (b == ms)
var h = 1 + ((vs-r)/vm) + hue6;
else
var h = 3 - ((vs-b)/vm) + hue6;
} else {
if (r == ms)
var h = 3 + ((vs-g)/vm) + hue6;
else
var h = 5 - ((vs-r)/vm) + hue6;
}
if (h < 0) h+=6;
if (h >= 6) h-=6;
var m = (l+l-v);
var sextant = h>>0;
if (sextant == 0) {
r = v*255; g = (m+((v-m)*(h-sextant)))*255; b = m*255;
} else if (sextant == 1) {
r = (v-((v-m)*(h-sextant)))*255; g = v*255; b = m*255;
} else if (sextant == 2) {
r = m*255; g = v*255; b = (m+((v-m)*(h-sextant)))*255;
} else if (sextant == 3) {
r = m*255; g = (v-((v-m)*(h-sextant)))*255; b = v*255;
} else if (sextant == 4) {
r = (m+((v-m)*(h-sextant)))*255; g = m*255; b = v*255;
} else if (sextant == 5) {
r = v*255; g = m*255; b = (v-((v-m)*(h-sextant)))*255;
}
}
}
}
if (lightness < 0) {
r *= lightp1;
g *= lightp1;
b *= lightp1;
} else if (lightness > 0) {
r = r * lightm1 + light255;
g = g * lightm1 + light255;
b = b * lightm1 + light255;
}
if (r < 0)
data[pix] = 0
else if (r > 255)
data[pix] = 255
else
data[pix] = r;
if (g < 0)
data[pix1] = 0
else if (g > 255)
data[pix1] = 255
else
data[pix1] = g;
if (b < 0)
data[pix2] = 0
else if (b > 255)
data[pix2] = 255
else
data[pix2] = b;
}
return true;
}
},
checkSupport : function() {
return Pixastic.Client.hasCanvasImageData();
}
}
/*
* Pixastic Lib - Invert filter - v0.1.1
* Copyright (c) 2008 Jacob Seidelin, [email protected], http://blog.nihilogic.dk/
* License: [http://www.pixastic.com/lib/license.txt]
*/
Pixastic.Actions.invert = {
process : function(params) {
if (Pixastic.Client.hasCanvasImageData()) {
var data = Pixastic.prepareData(params);
var invertAlpha = !!params.options.invertAlpha;
var rect = params.options.rect;
var p = rect.width * rect.height;
var pix = p*4, pix1 = pix + 1, pix2 = pix + 2, pix3 = pix + 3;
while (p--) {
data[pix-=4] = 255 - data[pix];
data[pix1-=4] = 255 - data[pix1];
data[pix2-=4] = 255 - data[pix2];
if (invertAlpha)
data[pix3-=4] = 255 - data[pix3];
}
return true;
} else if (Pixastic.Client.isIE()) {
params.image.style.filter += " invert";
return true;
}
},
checkSupport : function() {
return (Pixastic.Client.hasCanvasImageData() || Pixastic.Client.isIE());
}
}
/*
* Pixastic Lib - Posterize effect - v0.1.0