-
-
Save GoToLoop/2e12acf577506fd53267e1d186624d7c to your computer and use it in GitHub Desktop.
Revisions
-
GoToLoop revised this gist
Jun 19, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -26,8 +26,8 @@ p5.Image.prototype.resizeNN = function (w, h) { if (w === width && h === height || !(w | h)) return this; // Scale dimension parameters: if (!w) w = h*width / height | 0; // only when parameter w is 0 if (!h) h = w*height / width | 0; // only when parameter h is 0 const img = new p5.Image(w, h), // creates temporary image sx = w / width, sy = h / height; // scaled coords. for current image -
GoToLoop revised this gist
Jun 19, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,10 +39,10 @@ p5.Image.prototype.resizeNN = function (w, h) { imgInt = new Int32Array(img.pixels.buffer); // Transfer current to temporary pixels[] by 4 bytes (32-bit) at once: for (var x = 0, y = 0; y < h; x = 0) { const curRow = width * ~~(y/sy), tgtRow = w * y++; while (x < w) { const curIdx = curRow + ~~(x/sx), tgtIdx = tgtRow + x++; imgInt[tgtIdx] = pixInt[curIdx]; } -
GoToLoop revised this gist
Jun 19, 2022 . 1 changed file with 6 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,12 @@ /** * Resize the image to a new width and height using nearest neighbor algorithm. * To make the image scale proportionally, * use 0 as the value for the wide or high parameters. * For instance, to make the width of an image 150 pixels, * and change the height using the same proportion, use resize(150, 0). * Otherwise same usage as the regular resize(). * * Note: Disproportionate resizing squashes "pixels" from squares to rectangles. * This works about 10 times slower than the regular resize. * Any suggestions for performance increase are welcome. */ @@ -16,7 +17,7 @@ p5.Image.prototype.resizeNN = function (w, h) { "use strict"; // Locally cache current image's canvas' dimension properties: const { width, height } = this.canvas; // Sanitize dimension parameters: w = ~~Math.abs(w), h = ~~Math.abs(h); @@ -47,11 +48,11 @@ p5.Image.prototype.resizeNN = function (w, h) { } } img.updatePixels(); // updates temp 8-bit RGBa pixels[] w/ its current state // Resize current image to temporary image's dimensions: this.canvas.width = this.width = w, this.canvas.height = this.height = h; this.drawingContext.drawImage(img.canvas, 0, 0, w, h, 0, 0, w, h); return this; }; -
GoToLoop revised this gist
Jul 11, 2020 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,6 +10,8 @@ * Any suggestions for performance increase are welcome. */ // https://GitHub.com/processing/p5.js/issues/1845 p5.Image.prototype.resizeNN = function (w, h) { "use strict"; -
GoToLoop revised this gist
Jul 11, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ /** * Resize the image to a new width and height using nearest neighbor algorithm. * To make the image scale proportionally, use 0 as the value for the wide or high parameters. * For instance, to make the width of an image 150 pixels, * and change the height using the same proportion, use resize(150, 0). -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,8 +32,8 @@ p5.Image.prototype.resizeNN = function (w, h) { this.loadPixels(), img.loadPixels(); // initializes both 8-bit RGBa pixels[] // Create 32-bit viewers for current & temporary 8-bit RGBa pixels[]: const pixInt = new Int32Array(this.pixels.buffer), imgInt = new Int32Array(img.pixels.buffer); // Transfer current to temporary pixels[] by 4 bytes (32-bit) at once: for (let y = 0; y < h; ) { -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ p5.Image.prototype.resizeNN = function (w, h) { const {width, height} = this.canvas; // Sanitize dimension parameters: w = ~~Math.abs(w), h = ~~Math.abs(h); // Quit prematurely if both dimensions are equal or parameters are both 0: if (w === width && h === height || !(w | h)) return this; -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -17,7 +17,7 @@ p5.Image.prototype.resizeNN = function (w, h) { const {width, height} = this.canvas; // Sanitize dimension parameters: w = ~~Math.abs(w), h = ~~(Math.abs(h); // Quit prematurely if both dimensions are equal or parameters are both 0: if (w === width && h === height || !(w | h)) return this; -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -23,8 +23,8 @@ p5.Image.prototype.resizeNN = function (w, h) { if (w === width && h === height || !(w | h)) return this; // Scale dimension parameters: w || (w = h*width / height | 0); // when only parameter w is 0 h || (h = w*height / width | 0); // when only parameter h is 0 const img = new p5.Image(w, h), // creates temporary image sx = w / width, sy = h / height; // scaled coords. for current image -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -20,7 +20,7 @@ p5.Image.prototype.resizeNN = function (w, h) { w = Math.round(Math.abs(w)), h = Math.round(Math.abs(h)); // Quit prematurely if both dimensions are equal or parameters are both 0: if (w === width && h === height || !(w | h)) return this; // Scale dimension parameters: if (!w) w = h*width / height | 0; // when only w is 0 @@ -50,4 +50,6 @@ p5.Image.prototype.resizeNN = function (w, h) { // Resize current image to temporary image's dimensions: this.canvas.width = this.width = w, this.canvas.height = this.height = h; this.drawingContext.drawImage(img.canvas, 0, 0, w, h, 0, 0, w, h); return this; }; -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,10 +19,12 @@ p5.Image.prototype.resizeNN = function (w, h) { // Sanitize dimension parameters: w = Math.round(Math.abs(w)), h = Math.round(Math.abs(h)); // Quit prematurely if both dimensions are equal or parameters are both 0: if (w === width && h === height || !(w | h)) return; // Scale dimension parameters: if (!w) w = h*width / height | 0; // when only w is 0 else if (!h) h = w*height / width | 0; // when only h is 0 const img = new p5.Image(w, h), // creates temporary image sx = w / width, sy = h / height; // scaled coords. for current image -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -25,8 +25,7 @@ p5.Image.prototype.resizeNN = function (w, h) { else if (!h) h = w*height / width | 0; // when only h is 0 const img = new p5.Image(w, h), // creates temporary image sx = w / width, sy = h / height; // scaled coords. for current image this.loadPixels(), img.loadPixels(); // initializes both 8-bit RGBa pixels[] @@ -36,7 +35,7 @@ p5.Image.prototype.resizeNN = function (w, h) { // Transfer current to temporary pixels[] by 4 bytes (32-bit) at once: for (let y = 0; y < h; ) { const curRow = width * ~~(y/sy), tgtRow = w * y++; for (let x = 0; x < w; ) { const curIdx = curRow + ~~(x/sx), tgtIdx = tgtRow + x++; -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ p5.Image.prototype.resizeNN = function (w, h) { "use strict"; // Locally cache current image's canvas' dimension properties: const {width, height} = this.canvas; // Sanitize dimension parameters: -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,7 @@ p5.Image.prototype.resizeNN = function (w, h) { // Transfer current to temporary pixels[] by 4 bytes (32-bit) at once: for (let y = 0; y < h; ) { const curRow = cw * ~~(y/sy), tgtRow = w * y++; for (let x = 0; x < w; ) { const curIdx = curRow + ~~(x/sx), tgtIdx = tgtRow + x++; -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,8 +38,8 @@ p5.Image.prototype.resizeNN = function (w, h) { for (let y = 0; y < h; ) { const curRow = ~~(y/sy) * cw, tgtRow = y++ * w; for (let x = 0; x < w; ) { const curIdx = curRow + ~~(x/sx), tgtIdx = tgtRow + x++; imgInt[tgtIdx] = pixInt[curIdx]; } } -
GoToLoop revised this gist
Mar 8, 2017 . 1 changed file with 40 additions and 40 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,52 +1,52 @@ /** * Resize the image to a new width and height using nearest neigbor algorithm. * To make the image scale proportionally, use 0 as the value for the wide or high parameters. * For instance, to make the width of an image 150 pixels, * and change the height using the same proportion, use resize(150, 0). * Otherwise same usage as the regular resize(). * * Note: Disproportionate resizing squashes the "pixels" from squares to rectangles. * This works about 10 times slower than the regular resize. * Any suggestions for performance increase are welcome. */ p5.Image.prototype.resizeNN = function (w, h) { "use strict"; // Locally cache canvas' dimension properties: const {width, height} = this.canvas; // Sanitize dimension parameters: w = Math.round(Math.abs(w)), h = Math.round(Math.abs(h)); // Scale dimension parameters: if (!(w | h)) w = width, h = height; // when both parameters are 0 else if (!w) w = h*width / height | 0; // when only w is 0 else if (!h) h = w*height / width | 0; // when only h is 0 const img = new p5.Image(w, h), // creates temporary image cw = this.width, // width of current image sx = w / cw, sy = h / this.height; // scaled coords. for current image this.loadPixels(), img.loadPixels(); // initializes both 8-bit RGBa pixels[] // Create 32-bit viewers for current & temporary 8-bit RGBa pixels[]: const pixInt = new Uint32Array(this.pixels.buffer), imgInt = new Uint32Array(img.pixels.buffer); // Transfer current to temporary pixels[] by 4 bytes (32-bit) at once: for (let y = 0; y < h; ) { const curRow = ~~(y/sy) * cw, tgtRow = y++ * w; for (let x = 0; x < w; ++x) { const curIdx = curRow + ~~(x/sx), tgtIdx = tgtRow + x; imgInt[tgtIdx] = pixInt[curIdx]; } } img.updatePixels(); // updates temporary 8-bit RGBa pixels[] w/ its current state // Resize current image to temporary image's dimensions: this.canvas.width = this.width = w, this.canvas.height = this.height = h; this.drawingContext.drawImage(img.canvas, 0, 0, w, h, 0, 0, w, h); }; -
gncgnc revised this gist
Mar 7, 2017 . 1 changed file with 14 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,9 +3,12 @@ * proportionally, use 0 as the value for the wide or high parameter. * For instance, to make the width of an image 150 pixels, and change * the height using the same proportion, use resize(150, 0). * Otherwise same usage as the regular resize(). * * Note: Disproportionate resizing squashes the "pixels" from squares to rectangles. * This works about 10 times slower than the regular resize. Any suggestions for performance increase are welcome. */ p5.Image.prototype.resizeNN = function(width, height) { if (width === 0 && height === 0) { width = this.canvas.width; @@ -20,16 +23,20 @@ p5.Image.prototype.resizeNN = function(width, height) { height = Math.floor(height); var temp = createImage(width,height), xScale = width / this.width , yScale = height / this.height this.loadPixels() temp.loadPixels() for(var x=0; x<temp.width; x++) { for(var y=0; y<temp.height; y++) { var sourceInd = 4*(Math.floor(y/yScale)*this.width + Math.floor(x/xScale)) var targetInd = 4*(y*temp.width + x) var sourceP = this.pixels.slice(sourceInd,sourceInd+4)//this.get(x/wScale, y/hScale) temp.pixels[targetInd] = sourceP[0] temp.pixels[targetInd+1] = sourceP[1] temp.pixels[targetInd+2] = sourceP[2] temp.pixels[targetInd+3] = sourceP[3] } } temp.updatePixels() @@ -38,10 +45,8 @@ p5.Image.prototype.resizeNN = function(width, height) { this.canvas.width = this.width = width; this.canvas.height = this.height = height; this.drawingContext.drawImage(temp.canvas, 0, 0, width, height, 0, 0, width, height ) }; -
gncgnc revised this gist
Mar 7, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ * For instance, to make the width of an image 150 pixels, and change * the height using the same proportion, use resize(150, 0). * **** Note: Disproportionate resizing squashes the "pixels" from squares to rectangles. */ p5.Image.prototype.resizeNN = function(width, height) { if (width === 0 && height === 0) { -
gncgnc created this gist
Mar 7, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,47 @@ /** * Resize the image to a new width and height using nearest neigbor algorithm. To make the image scale * proportionally, use 0 as the value for the wide or high parameter. * For instance, to make the width of an image 150 pixels, and change * the height using the same proportion, use resize(150, 0). * * Note: Disproportionate resizing squashes the "pixels" from squares to rectangles. */ p5.Image.prototype.resizeNN = function(width, height) { if (width === 0 && height === 0) { width = this.canvas.width; height = this.canvas.height; } else if (width === 0) { width = this.canvas.width * height / this.canvas.height; } else if (height === 0) { height = this.canvas.height * width / this.canvas.width; } width = Math.floor(width); height = Math.floor(height); var temp = createImage(width,height), wScale = width / this.width , hScale = height / this.height this.loadPixels() temp.loadPixels() for(var x=0; x<temp.width; x++) { for(var y=0; y<temp.height; y++) { // var ind = 4*(y*img.width + x) var sourceP = this.get(1.0*x/wScale, 1.0*y/hScale) temp.set(x,y,sourceP) } } temp.updatePixels() this.updatePixels() this.canvas.width = this.width = width; this.canvas.height = this.height = height; console.log(temp) this.drawingContext.drawImage(temp.canvas, 0, 0, width, height, 0, 0, width, height ); };