format of ByteArray returned from C is invalid

2019-06-10 20:55发布

问题:

following is code snippet:

AS side:(img is reference to an <Image> instance)

        bitmapData = Bitmap(img.content).bitmapData;
        var pixels:ByteArray = bitmapData.getPixels(bitmapData.rect);
        pixels.position = 0;
        var output:ByteArray = new ByteArray();
        img_width = bitmapData.width;
        img_height = bitmapData.height;
        ////invoke C code by alchemy
        lomoEncoder.encode(pixels, output, img_width, img_height);
        var newImage:Image = new Image();
        //can't show the image
        newImage.source = output;

C code:

AS3_Val dest;
AS3_Val source;
unsigned char* pixels = (unsigned char *)malloc(Size);
AS3_ByteArray_readBytes(pixels, source, Size);
pixels = darkCornerLomoEffect((unsigned char*)pixels, image_width, image_height);
AS3_ByteArray_writeBytes(dest, (char*) pixels, length);

In the AS side, when get the dest from C, loader.load(dest) throw an error:Unhandled IOErrorEvent:. text=Error #2124. So How to deal with the byteArray format, so AS side can reorganize and use it as Image source property?

回答1:

if you have a bytearray and you want to load the image source you can do the following:

var b:ByteArray = new ByteArray();
var f:BitmapData = new BitmapData(100, 100);
f.setPixels(new Rectangle(0, 0, 100, 100), b);

please can you provide a snippet of your AS3 code?



回答2:

I suspect your problem is byte-ordering. You need to flip your bytes after reading from the input ByteArray. For the output ByteArray you either need to flip them again, or set its endian property to Endian.LITTLE_ENDIAN.