守望者--AIR技术交流

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

搜索
热搜: ANE FlasCC 炼金术
查看: 2453|回复: 0

[基础入门] alchemy c 图像的缩放 (三次卷积)

[复制链接]
  • TA的每日心情
    擦汗
    2018-4-10 15:18
  • 签到天数: 447 天

    [LV.9]以坛为家II

    1742

    主题

    2094

    帖子

    13万

    积分

    超级版主

    Rank: 18Rank: 18Rank: 18Rank: 18Rank: 18

    威望
    562
    贡献
    29
    金币
    52620
    钢镚
    1422

    开源英雄守望者

    发表于 2015-4-7 10:27:56 | 显示全部楼层 |阅读模式
    在alchemy c中进行图片的缩放
    传进的是byteArray 传出的也是byteArray 千万不要在alchemy c 中对as的对象进行函数调用 那样速度很慢.... 达不到炼金术的效果...
    好吧 代码 自己看吧 还是比较简单的
    alchemy c 代码

    1. #include <iostream>
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <string.h>
    5. #include <math.h>
    6. #include "AS3.h"
    7. typedef unsigned int uint;
    8. #define MAXV 4
    9. using namespace std;

    10. double SinXDivX(double x) {
    11.     double a = -1;
    12.     if (x < 0)
    13.         x = -x;
    14.     double x2 = x * x;
    15.     double x3 = x2 * x;
    16.     if (x <= 1)
    17.         return (a + 2) * x3 - (a + 3) * x2 + 1;
    18.     else if (x <= 2)
    19.         return a * x3 - 5 * a * x2 + 8 * a * x - 4 * a;
    20.     return 0;
    21. }

    22. uint availablePixel(uint *src, int srcWidth, int srcHeight, int x, int y) {
    23.     bool flag = true;
    24.     if (x < 0) {
    25.         x = 0;
    26.         flag = false;
    27.     } else if (x >= srcWidth) {
    28.         x = srcWidth - 1;
    29.         flag = false;
    30.     }
    31.     if (y < 0) {
    32.         y = 0;
    33.         flag = false;
    34.     } else if (y >= srcHeight) {
    35.         y = srcHeight - 1;
    36.         flag = false;
    37.     }
    38.     int lenth = srcWidth * srcHeight;
    39.     int in = x + y*srcWidth;
    40.     uint ret = src[in];
    41.     if (!flag)
    42.         ret = ret & 0x00ffffff;
    43.     return ret;
    44. }

    45. uint border_color(double Color) {
    46.     if (Color <= 0)
    47.         return uint(0);
    48.     if (Color >= 255)
    49.         return uint(255);
    50.     return uint(Color);
    51. }

    52. char *strreverse(char *a) {
    53.     char r[10] = { 0 };
    54.     int i, j;
    55.     for (i = 0, j = strlen(a) - 1; a[i]; ++i, --j) {
    56.         r[j] = a[i];
    57.     }
    58.     return r;
    59. }

    60. char *toString(uint val) {
    61.     char a[10] = { 0 };
    62.     char ch[16] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
    63.             'C', 'D', 'E', 'F' };
    64.     int i = 0, rt;
    65.     if (val == 0)
    66.         a[0] = '0';
    67.     while (val) {
    68.         rt = val % 16;
    69.         val >>= 4;
    70.         a[i++] = ch[rt];
    71.     }
    72.     return strreverse(a);
    73. }

    74. uint MatrixMutiple(double a[], uint b[][MAXV], double c[], int ii, int jj) {
    75.     int i, j, k, z;
    76.     double tp[MAXV], ret[MAXV], ttp[MAXV];
    77.     memset(ret, 0.0, sizeof(ret));

    78.     for (i = 0; i < MAXV; ++i) {
    79.         memset(tp, 0.0, sizeof(tp));
    80.         for (j = 0; j < MAXV; ++j) {
    81.             tp[0] += a[j] * (b[j][i] >> 24);
    82.             tp[1] += a[j] * ((b[j][i] & 0x00ffffff) >> 16);
    83.             tp[2] += a[j] * ((b[j][i] & 0x0000ffff) >> 8);
    84.             tp[3] += a[j] * (b[j][i] & 0x000000ff);
    85.         }
    86.         for (k = 0; k < MAXV; ++k) {
    87.             ret[k] += c[i] * tp[k];
    88.         }
    89.     }

    90.     uint af1 = border_color(ret[0] + 0.5) << 24;
    91.     uint r1 = border_color(ret[1] + 0.5) << 16;
    92.     uint g1 = border_color(ret[2] + 0.5) << 8;
    93.     uint b1 = border_color(ret[3] + 0.5);

    94.     return af1 + r1 + g1 + b1;
    95. }

    96. void echo(uint *val, int len) {
    97.     int i = 0;
    98.     for (i = 0; i < len; ++i) {
    99.         AS3_Trace(AS3_String(toString(val[i])));
    100.     }
    101. }

    102. static AS3_Val biCubic(void* self, AS3_Val args) {
    103.     AS3_Val srcByte, dstByte;
    104.     int srcWidth, srcHeight, dstWidth, dstHeight;
    105.     AS3_ArrayValue(args,
    106.             "AS3ValType, IntType, IntType, AS3ValType, IntType, IntType",
    107.             &srcByte, &srcWidth, &srcHeight, &dstByte, &dstWidth, &dstHeight);

    108.     int srcLen = srcWidth * srcHeight;
    109.     int dstLen = dstWidth * dstHeight;
    110.     uint *src = new uint[srcLen];
    111.     uint *dst = new uint[dstLen];

    112.     AS3_SetS(srcByte, "position", AS3_Int(0));
    113.     AS3_ByteArray_readBytes(src, srcByte, 4 * srcLen);
    114.     double widthFactor = 1.0 * srcWidth / dstWidth;
    115.     double heightFactor = 1.0 * srcHeight / dstHeight;

    116.     int i, j, k, z, gf = 0;
    117.     double tx, ty, u, v;
    118.     int x, y;
    119.     double A[MAXV], C[MAXV];
    120.     uint B[MAXV][MAXV];
    121.     for (i = 0; i < dstWidth; ++i) {
    122.         for (j = 0; j < dstHeight; ++j) {
    123.             tx = (i + 0.5) * widthFactor - 0.5;
    124.             ty = (j + 0.5) * heightFactor - 0.5;
    125.             if (tx < 0)
    126.                 tx = -tx;
    127.             if (ty < 0)
    128.                 ty = -ty;
    129.             x = floor(tx);
    130.             y = floor(ty);
    131.             u = tx - x;
    132.             v = ty - y;
    133.             for (k = 0; k < MAXV; ++k) {
    134.                 A[k] = SinXDivX(u + 1.0 - k);
    135.                 C[k] = SinXDivX(v + 1.0 - k);
    136.                 for (z = 0; z < MAXV; ++z) {
    137.                     B[k][z] = availablePixel(src, srcWidth, srcHeight,
    138.                             x + k - 1, y + z - 1);
    139.                 }
    140.             }
    141.             dst[i+j*dstWidth] = MatrixMutiple(A, B, C, i, j);
    142.         }
    143.     }
    144.     //echo(dst, dstLen);

    145.     AS3_SetS(dstByte, "position", AS3_Int(0));
    146.     AS3_ByteArray_writeBytes(dstByte, dst, 4 * dstLen);
    147.     return AS3_True();
    148. }

    149. int main() {
    150.     AS3_Val biCubicMethod = AS3_Function(NULL, biCubic);
    151.     AS3_Val lib = AS3_Object("biCubic:AS3ValType", biCubicMethod);
    152.     AS3_Release(biCubicMethod);
    153.     AS3_LibInit(lib);

    154.     return 0;
    155. }
    复制代码


    as 代码
    1. package {
    2.        
    3.         import cmodule.ImageScaling.CLibInit;
    4.        
    5.         import flash.display.Bitmap;
    6.         import flash.display.BitmapData;
    7.         import flash.display.Sprite;
    8.         import flash.display.StageScaleMode;
    9.         import flash.geom.Rectangle;
    10.         import flash.utils.ByteArray;
    11.         import flash.utils.Timer;
    12.         [SWF(width="1000", height="600", backgroundColor="#000000", frameRate="24")]
    13.         public class Main extends Sprite {
    14.                
    15.                 [Embed(source='f_01.png')]
    16.                 public static const image:Class;
    17.                 public function Main() {
    18.                         this.stage.scaleMode = StageScaleMode.NO_SCALE;
    19.                         var bitmapdata:BitmapData = (new image() as Bitmap).bitmapData;
    20.                         var t1:Number = (new Date()).time;
    21.                         var bitmap:Bitmap = new Bitmap(Main.Cubic(bitmapdata, 256, 256));
    22.                         var t2:Number = (new Date()).time;
    23.                         trace((t2-t1)+"ms");
    24.                         this.addChild(bitmap);
    25.                 }
    26.                
    27.                
    28.                 public static function Cubic (bitmapData:BitmapData, scalingWidth:uint, scalingHeight:uint):BitmapData {
    29.                         var nbd:BitmapData = new BitmapData(scalingWidth, scalingHeight, true, 0xffffffff);
    30.                         var loader:cmodule.ImageScaling.CLibInit = new cmodule.ImageScaling.CLibInit();
    31.                         var lib:Object = loader.init();
    32.                        
    33.                         var byte:ByteArray = bitmapData.getPixels(new Rectangle(0, 0, bitmapData.width, bitmapData.height));
    34.                        
    35.                         var byte2:ByteArray = nbd.getPixels(new Rectangle(0, 0, scalingWidth, scalingHeight));
    36.                        
    37.                         lib.biCubic(byte, bitmapData.width, bitmapData.height, byte2, scalingWidth, scalingHeight);
    38.                         byte2.position = 0;
    39.                         nbd.setPixels(new Rectangle(0, 0, scalingWidth, scalingHeight), byte2);
    40.                         return nbd;
    41.                 }
    42.         }
    43. }
    复制代码



    本文来自:http://www.cppblog.com/misschuer/archive/2012/08/10/186801.aspx
    守望者AIR技术交流社区(www.airmyth.com)
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    
    关闭

    站长推荐上一条 /4 下一条

    QQ|手机版|Archiver|网站地图|小黑屋|守望者 ( 京ICP备14061876号

    GMT+8, 2024-3-29 07:32 , Processed in 0.043082 second(s), 31 queries .

    守望者AIR

    守望者AIR技术交流社区

    本站成立于 2014年12月31日

    快速回复 返回顶部 返回列表