当前位置:编程学习 > C/C++ >>

alchemy 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;
         &nb

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,