-
-
Save n1ckfg/d0681ceb354711893d05d2ae3b91943d to your computer and use it in GitHub Desktop.
Revisions
-
n1ckfg renamed this gist
Jun 7, 2023 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
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 ); };