gpt4 book ai didi

actionscript-3 - 如何打乱位图数据?

转载 作者:行者123 更新时间:2023-12-03 16:22:10 25 4
gpt4 key购买 nike

我被要求编写一个程序来获取图像,读取位图数据,然后随机化图像。

我尝试了简单的方法,在每个“”上分割“字符串”,然后对其进行洗牌,但是图像损坏了,知道如何做到这一点吗?

我会在 cocoa、actionscript 3、js 或 php 上执行此操作。

基本上只要我能在 Mac 上使用它,我的客户就会很高兴:)

最佳答案

javascript中逐 block 打乱图像的解决方案:http://fiddle.jshell.net/upgradellc/53wKG/show/编辑:http://jsfiddle.net/upgradellc/53wKG/

  /* Script copyright Max @ www.upgradeyour.com - if it is used or modified, this message must remain intact! */
$( document ).ready( function() {
var imgData1 = 'img_url_or_base64';
new ImageShuffler( imgData1, 3 );
new ImageShuffler( imgData1, 5 );
new ImageShuffler( imgData1, 10 );
} );
function ImageShuffler( imgUrl, numberOfSquares, elementToAddTo ) {
var that = this;
this.url = imgUrl;
this.numberOfSquares = numberOfSquares;
this.elementToAddTo = elementToAddTo || $( 'body' );
this.holder = $( '<div></div>' ).appendTo( $( this.elementToAddTo ) )[0];
this.c1 = $( '<canvas></canvas>' ).appendTo( $( this.holder ) )[0];
this.c2 = $( '<canvas></canvas>' ).appendTo( $( this.holder ) )[0];
this.img = $( '<img src="' + this.url + '">' ).prependTo( $( this.holder ) )[0];
this.img.onload = function() {
that.doShuffleImage()
};
}
ImageShuffler.prototype.doShuffleImage = function() {
this.widthOfSquares = Math.ceil( this.img.width / this.numberOfSquares );
this.heightOfSquares = Math.ceil( this.img.height / this.numberOfSquares );
var extrax = (this.img.width - this.widthOfSquares * this.numberOfSquares);
var extray = (this.img.height - this.heightOfSquares * this.numberOfSquares);
var width = this.removeExtraPixels( this.img.width, extrax, this.widthOfSquares );
var height = this.removeExtraPixels( this.img.height, extray, this.heightOfSquares );
this.c1.width = this.c2.width = width;
this.c1.height = this.c2.height = height;

this.c1c = this.c1.getContext( '2d' );
this.c2c = this.c2.getContext( '2d' );
this.c1c.drawImage( this.img, 0, 0, this.img.width, this.img.height );
var tlc = this.c1c.getImageData( this.img.width - 1, this.img.height - 1, 1, 1 ).data;
this.c1c.fillStyle = "rgb(" + tlc[0] + "," + tlc[1] + "," + tlc[2] + ");";
this.c1c.fillRect( 0, 0, this.c1.width, this.c1.height );
this.c1c.drawImage( this.img, 0, 0, this.img.width, this.img.height );
this.shuffleAll();
};
ImageShuffler.prototype.shuffleAll = function() {
this.c2c.putImageData( this.c1c.getImageData( 0, 0, this.c1.width, this.c1.height ), 0, 0 );
//this.c1c.getImageData( 0, 0, this.img.width, this.img.height );
var timesToShuffle = Math.pow( this.numberOfSquares, 2 );
for( var count = 0; count < timesToShuffle; count++ ) {
p1 = {x: rand( this.c2.width, this.widthOfSquares ), y: rand( this.c2.height, this.heightOfSquares )};
p2 = {x: rand( this.c2.width, this.widthOfSquares ), y: rand( this.c2.height, this.heightOfSquares )};
if( p1.x + this.widthOfSquares < this.c2.width ) {
this.swapTile( p1.x, p1.y, p2.x, p2.y );
}
}
};
ImageShuffler.prototype.swapTile = function( x1, y1, x2, y2 ) {
tile1 = this.c2c.getImageData( x1, y1, this.widthOfSquares, this.heightOfSquares );
tile2 = this.c2c.getImageData( x2, y2, this.widthOfSquares, this.heightOfSquares );
this.c2c.putImageData( tile1, x2, y2 );
this.c2c.putImageData( tile2, x1, y1 );
};

ImageShuffler.prototype.removeExtraPixels = function( currentLength, extraPixels, sizeOfSquare ) {
if( extraPixels < 0 ) {
return currentLength + (-1 * extraPixels);
}
if( extraPixels > 0 ) {
return currentLength + sizeOfSquare - extraPixels;
}
return currentLength;
};

//returns a random number below max which is a multiple of increment
function rand( max, increment ) {
return Math.floor( Math.random() * Math.ceil( max / increment ) ) * increment;
}

javascript中用于打乱图像中所有像素的解决方案: http://jsfiddle.net/upgradellc/2LJwH/1/

        /* Script copyright Max @ www.upgradeyour.com - if it is used or modified, this message must remain intact! */
$( document ).ready( function() {
var imgData1 = '/img/logo.png';
new ImageShuffler( imgData1 );
} );
function ImageShuffler( imgUrl, elementToAddTo ) {
var that = this;
this.url = imgUrl;
this.elementToAddTo = elementToAddTo || $( 'body' );
this.holder = $( '<div></div>' ).appendTo( $( this.elementToAddTo ) )[0];
console.log( this.holder );
this.c1 = $( '<canvas></canvas>' ).appendTo( $( this.holder ) )[0];
this.c2 = $( '<canvas></canvas>' ).appendTo( $( this.holder ) )[0];
this.img = $( '<img src="' + this.url + '">' ).prependTo( $( this.holder ) )[0];
this.img.onload = function() {
that.doShuffleImage()
};
}
ImageShuffler.prototype.doShuffleImage = function() {
this.c1.width = this.c2.width = this.img.width;
this.c1.height = this.c2.height = this.img.height;
this.c1c = this.c1.getContext( '2d' );
this.c2c = this.c2.getContext( '2d' );
this.c1c.drawImage( this.img, 0, 0, this.img.width, this.img.height );
this.c2c.putImageData( shuffleArray( this.c1c.getImageData( 0, 0, this.img.width, this.img.height ) ), 0, 0 );
};

//shuffles the data array
function shuffleArray( arr ) {
var length = arr.data.length;
for( x = 0; x < length; x++ ) {
var p1x = rand( length, 4 ), p2x = rand( length, 4 );
var p1r = arr.data[p1x];
var p1g = arr.data[p1x + 1];
var p1b = arr.data[p1x + 2];
var p1a = arr.data[p1x + 3];
for( i = 0; i < 3; i++ ) {
arr.data[p2x + i] = arr.data[p1x + i];
}
arr.data[p2x] = p1r;
arr.data[p2x + 1] = p1g;
arr.data[p2x + 2] = p1b;
arr.data[p2x + 3] = p1a;
}
return arr;
}

//returns a random number below max which is a multiple of increment
function rand( max, increment ) {
return Math.floor( Math.random() * Math.ceil( max / increment ) ) * increment;
}

关于actionscript-3 - 如何打乱位图数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17225017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com