Alchemy c Image Scaling (cubic convolution)

Source: Internet
Author: User

Scaling images in alchemy c
Passing in is byteArray and passing out is also byteArray. Never make function calls to the as object in alchemy c at a very slow speed .... it cannot achieve the effect of alchemy...
Okay, the code is simple.
Alchemy c code
# Include <iostream>
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <math. h>
# Include "AS3.h"
Typedef unsigned int uint;
# Define MAXV 4
Using namespace std;

Double SinXDivX (double x ){
Double a =-1;
If (x <0)
X =-x;
Double x2 = x * x;
Double x3 = x2 * x;
If (x <= 1)
Return (a + 2) * x3-(a + 3) * x2 + 1;
Else if (x <= 2)
Return a * x3-5 * a * x2 + 8 * a * x-4 *;
Return 0;
}

Uint availablePixel (uint * src, int srcWidth, int srcHeight, int x, int y ){
Bool flag = true;
If (x <0 ){
X = 0;
Flag = false;
} Else if (x> = srcWidth ){
X = srcWidth-1;
Flag = false;
}
If (y <0 ){
Y = 0;
Flag = false;
} Else if (y> = srcHeight ){
Y = srcHeight-1;
Flag = false;
}
Int lenth = srcWidth * srcHeight;
Int in = x + y * srcWidth;
Uint ret = src [in];
If (! Flag)
Ret = ret & 0x00ffffff;
Return ret;
}

Uint border_color (double Color ){
If (Color <= 0)
Return uint (0 );
If (Color >=255)
Return uint (255 );
Return uint (Color );
}

Char * strreverse (char * ){
Char r [10] = {0 };
Int I, j;
For (I = 0, j = strlen (a)-1; a [I]; ++ I, -- j ){
R [j] = a [I];
}
Return r;
}

Char * toString (uint val ){
Char a [10] = {0 };
Char ch [16] = {'0', '1', '2', '3', '4', '5', '6', '7 ', '8', '9', 'A', 'B ',
'C', 'D', 'E', 'F '};
Int I = 0, rt;
If (val = 0)
A [0] = '0 ';
While (val ){
Rt = val % 16;
Val >>=4;
A [I ++] = ch [rt];
}
Return strreverse ();
}

Uint MatrixMutiple (double a [], uint B [] [MAXV], double c [], int ii, int jj ){
Int I, j, k, z;
Double tp [MAXV], ret [MAXV], ttp [MAXV];
Memset (ret, 0.0, sizeof (ret ));

For (I = 0; I <MAXV; ++ I ){
Memset (tp, 0.0, sizeof (tp ));
For (j = 0; j <MAXV; ++ j ){
Tp [0] + = a [j] * (B [j] [I]> 24 );
Tp [1] + = a [j] * (B [j] [I] & 0x00ffffff)> 16 );
Tp [2] + = a [j] * (B [j] [I] & 0x0000ffff)> 8 );
Tp [3] + = a [j] * (B [j] [I] & 0x000000ff );
}
For (k = 0; k <MAXV; ++ k ){
Ret [k] + = c [I] * tp [k];
}
}

Uint af1 = border_color (ret [0] + 0.5) <24;
Uint r1 = border_color (ret [1] + 0.5) <16;
Uint g1 = border_color (ret [2] + 0.5) <8;
Uint b1 = border_color (ret [3] + 0.5 );

Return af1 + r1 + g1 + b1;
}

Void echo (uint * val, int len ){
Int I = 0;
For (I = 0; I <len; ++ I ){
AS3_Trace (AS3_String (toString (val [I]);
}
}

Static AS3_Val biCubic (void * self, AS3_Val args ){
AS3_Val srcByte, dstByte;
Int srcWidth, srcHeight, dstWidth, dstHeight;
AS3_ArrayValue (args,
"AS3ValType, IntType, IntType, AS3ValType, IntType, IntType ",
& SrcByte, & srcWidth, & srcHeight, & dstByte, & dstWidth, & dstHeight );

Int srcLen = srcWidth * srcHeight;
Int dstLen = dstWidth * dstHeight;
Uint * src = new uint [srcLen];
Uint * dst = new uint [dstLen];

AS3_SetS (srcByte, "position", AS3_Int (0 ));
AS3_ByteArray_readBytes (src, srcByte, 4 * srcLen );
Double widthFactor = 1.0 * srcWidth/dstWidth;
Double heightFactor = 1.0 * srcHeight/dstHeight;

Int I, j, k, z, gf = 0;
Double tx, ty, u, v;
Int x, y;
Double A [MAXV], C [MAXV];
Uint B [MAXV] [MAXV];
For (I = 0; I <dstWidth; ++ I ){
For (j = 0; j <dstHeight; ++ j ){
Tx = (I + 0.5) * widthFactor-0.5;
Ty = (j + 0.5) * heightFactor-0.5;
If (tx <0)
Tx =-tx;
If (ty <0)
Ty =-ty;
X = floor (tx );
Y = floor (ty );
U = tx-x;
V = ty-y;
For (k = 0; k <MAXV; ++ k ){
A [k] = SinXDivX (u + 1.0-k );
C [k] = SinXDivX (v + 1.0-k );
For (z = 0; z <MAXV; ++ z ){
B [k] [z] = availablePixel (src, srcWidth, srcHeight,
X + k-1, y + z-1 );
}
}
Dst [I + j * dstWidth] = MatrixMutiple (A, B, C, I, j );
}
}
// Echo (dst, dstLen );

AS3_SetS (dstByte, "position", AS3_Int (0 ));
AS3_ByteArray_writeBytes (dstByte, dst, 4 * dstLen );
Return AS3_True ();
}

Int main (){
AS3_Val biCubicMethod = AS3_Function (NULL, biCubic );
AS3_Val lib = AS3_Object ("biCubic: AS3ValType", biCubicMethod );
AS3_Release (biCubicMethod );
AS3_LibInit (lib );

Return 0;
}


As code
Package {
 
Import cmodule. ImageScaling. CLibInit;
 
Import flash. display. Bitmap;
Import flash. display. BitmapData;
Import flash. display. Sprite;
Import flash. display. StageScaleMode;
Import flash. geom. Rectangle;
Import flash. utils. ByteArray;
Import flash. utils. Timer;
[SWF (width = "1000", height = "600", backgroundColor = "#000000", frameRate = "24")]
Public class Main extends Sprite {

[Embed(source1_'f_01.png ')]
Public static const image: Class;
Public function Main (){
This. stage. scaleMode = StageScaleMode. NO_SCALE;
Var bitmapdata: BitmapData = (new image () as Bitmap). bitmapData;
Var t1: Number = (new Date (). time;
Var bitmap: Bitmap = new Bitmap (Main. Cubic (bitmapdata, 256,256 ));
Var t2: Number = (new Date (). time;
Trace (t2-t1) + "ms ");
This. addChild (bitmap );
}


Public static function Cubic (bitmapData: BitmapData, scalingWidth: uint, scalingHeight: uint): BitmapData {
Var nbd: BitmapData = new BitmapData (scalingWidth, scalingHeight, true, 0 xffffffff );
Var loader: cmodule. ImageScaling. CLibInit = new cmodule. ImageScaling. CLibInit ();
Var lib: Object = loader. init (); www.2cto.com

Var byte: ByteArray = bitmapData. getPixels (new Rectangle (0, 0, bitmapData. width, bitmapData. height ));

Var byte2: ByteArray = nbd. getPixels (new Rectangle (0, 0, scalingWidth, scalingHeight ));

Lib. biCubic (byte, bitmapData. width, bitmapData. height, byte2, scalingWidth, scalingHeight );
Byte2.position = 0;
Nbd. setPixels (new Rectangle (0, 0, scalingWidth, scalingHeight), byte2 );
Return nbd;
}
}
}


Author: misschuer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.