Skip navigation
mark_aok
Currently Being Moderated

Can't use memory allocated in C to write then read BitmapData in AS

Dec 31, 2010 10:31 AM

Hi all,


I've been attempting to use Alchemy to allocate a chunk of memory in C, and then write a bitmap to the memory (via getPixels) which is I would then be able to modify using my super fast C image processing functions.  I've been following the "Memory allocation in C with direct access in Actionscript (FAST!!)" section from here.

 

The problem is that when I allocate the memory in C, then try to display the image using setImage, all I see is a black box on the screen.  The code below shows how I use getPixels to fill the C memory region with my bitmap data, then use setPixels to fill the BitmapData object which is displayed on the screen.  Does anyone know what I'm doing wrong here?  I've been really stuck on this

 

 

 

ActionScript variables, and event function which runs after my Bitmap is loaded

...   

        private var _dataPosition:uint;
        private var displayedImage:Image;
        private var bmp:Bitmap;

...

public function loaded(e:Event):void {

            bmp = e.target.content as Bitmap;
            // A setImage at the beggining of this function properly displays my image
            displayedImage.setImage(bmp.bitmapData);
           
            var loader:CLibInit = new CLibInit();
            var lib:Object = loader.init();
            var ns:Namespace = new Namespace("cmodule.alchemyrgr");
            var byteArray:ByteArray = (ns::gstate).ds; //point to memory
            var tmpByteArray:ByteArray = new ByteArray();
            var imgSize:int = bmp.width * bmp.height * 4;
           
            _dataPosition = lib.initByteArray(imgSize); //This is the position of the data in memory           
           
            var bounds:Rectangle = new Rectangle(0, 0, bmp.width, bmp.height);
            tmpByteArray = bmp.bitmapData.getPixels(bounds);
           
            byteArray.readBytes(tmpByteArray, 0, imgSize);
            byteArray.position = _dataPosition;
            bmp.bitmapData.setPixels(bounds, byteArray);
            displayedImage.setImage(bmp.bitmapData);
            //lib.clearByteArray(); //Free the bytearray
           
        }

 

C memory allocation function

static AS3_Val initByteArray(void* self, AS3_Val args)
{

    AS3_ArrayValue(args, "IntType", &bufferSize);

    //Allocate buffer of size "bufferSize"
    buffer = (unsigned char*)malloc(bufferSize*sizeof(char));

    //return pointer to the location in memory
    return AS3_Int((int)buffer);
}

 

Thanks in advance!

 

Mark

 
Replies
  • Currently Being Moderated
    Calculating status...
    Jan 12, 2011 5:25 PM   in reply to mark_aok

    Hi Mark,

     

    I too attempted to use a method similar to yours to no avail. I posted my solution for passing bytearray data to/from alchemy here:

     

    http://forums.adobe.com/thread/773517?tstart=0

     

    There is full flash code and C++ code so you should be able to answer all your questions just by reading my post. However, one thing I see about how you're passing your pointer back to flash is:

     

        //return pointer to the location in memory
        return AS3_Int((int)buffer);

     

    I think should be:

     

        //return pointer to the location in memory
        return AS3_Ptr(buffer);

     

    You shouldn't be casting your char array as an int and returning it, just use AS3_Ptr(buffer) and that will return the actual memory address as an int to flash. I'm not 100% sure but I think this could be an issue. I use this method also in my code you can find in the link above so you can see the full implementation there. Hope that helps.

     
    |
    Mark as:
  • Currently Being Moderated
    Jan 17, 2011 7:45 AM   in reply to mark_aok

    I see a bug in your code:

     

    byteArray.readBytes(tmpByteArray, 0, imgSize);

     

    You must have the

     

    byteArray.position = _dataPosition;

     

    line before this, as well as after.  You read the tmpByteArray into some junk alchemy memory, not where you expected.

     
    |
    Mark as:
  • Currently Being Moderated
    Nov 21, 2011 3:37 AM   in reply to mark_aok

    Hi,

     

    simply after the

                tmpByteArray = bmp.bitmapData.getPixels(bounds);

    set

                tmpByteArray.position = 0;

    then use the tmpByteArray for reading.

     

    it seems that the getPixels returns the bytearray but forget to set the position to 0 and leave it (i suppose) to the end...

    so your code start to read from a wrong position...

     

    but in debug mode flash (fdb at least) should throw an error of " ... End of file ..".

     

    hope this helps

     

    PS: i see that the bytearray returned from getPixels is big endian... no good for alchemy.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points