程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> alchemy c 圖像的縮放 (三次卷積)

alchemy c 圖像的縮放 (三次卷積)

編輯:C++入門知識

在alchemy c中進行圖片的縮放
傳進的是byteArray 傳出的也是byteArray 千萬不要在alchemy c 中對as的對象進行函數調用 那樣速度很慢.... 達不到煉金術的效果...
好吧 代碼 自己看吧 還是比較簡單的
alchemy c 代碼
#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 * a;
    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 *a) {
    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(a);
}

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 代碼
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(source='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, 0xffffffff);
   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;
  }
 }
}


作者:misschuer

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved