код шейдера масштабирующего картинку:
Код:
<languageVersion : 1.0;>
kernel BilinearResample
< namespace : "com.brooksandrus.pixelbender";
vendor : "Brooks Andrus";
version : 1;
description : "Resizes an image using bilinear resampling. Constrains aspect ratio - divide Math.max( input.width / output.width, input.height / output.height ) and pass in to the scale parameter";
>
{
parameter float scale
<
minValue: 0.0;
maxValue: 1000.0;
defaultValue: 1.0;
>;
input image4 src;
output pixel4 dst;
void
evaluatePixel()
{
// scale should be Math.max( src.width / output.width, src.height / output.height )
dst = sampleLinear( src, outCoord() * scale ); // bilinear scaling
}
}
а вот его использование:
Код AS3:
public function resampleBitmap(desiredWidth:int, desiredHeight:int, cleanup:Boolean = true ):BitmapData{
var input:BitmapData = bitmap.bitmapData.clone();
var outputWidth:Number;
var outputHeight:Number;
if (input.width>desiredWidth){
outputWidth = desiredWidth;
outputHeight = outputWidth/input.width*input.height;
}
else{
outputWidth = input.width;
outputHeight = outputWidth/input.width*input.height;
}
if (outputHeight>desiredHeight){
outputHeight = desiredHeight;
outputWidth = outputHeight/input.height*input.width;
}
var factor:Number = Math.max( input.width/outputWidth, input.height/outputHeight );
// create and configure a Shader object
var shader:Shader = new Shader();
shader.byteCode = BilinearScalingLoader.data;
shader.data.src.input = input; // supply the shader with BitmapData it will manipulate
shader.data.scale.value = [factor]; // scale factor. shader params are all stored in arrays.
// create a bitmap - our shader will return its data (an image) to this bitmap
var output:BitmapData = new BitmapData( outputWidth, outputHeight );
// shader jobs are wicked cool
var job:ShaderJob = new ShaderJob();
job.target = output; // ShaderJob returns to this object
job.shader = shader; // The Shader assigned to this job
job.start( true ); // true flag runs the job synchronously.
if ( cleanup ){
input.dispose();
}
return output;
}
resampleBitmap вызывается с параметрами 600, 600
bitmap - является загруженой Bitmap
BilinearScalingLoader - содержит заранее подгруженый шейдер
в итоге после вызова функции resampleBitmap операционная система просит закрыть приложение