Skip navigation

Request: Port AS3 Corelib encoder functionality

Nov 19, 2008 4:07 PM

  Latest reply: sectore, Jun 21, 2009 8:39 AM
Replies 1 2 Previous Next
  • Currently Being Moderated
    Calculating status...
    Mar 11, 2009 5:45 PM   in reply to piolo96
    It seemed to me that you need is an example:) I gave this as there is a project of "Metalbot"
    My there's nothing:)
    I can compile jpeg.l.bc but I just did not handle the porting of alchemy.
    Accordingly, you will need for this example of "Metalbot" (perhaps it will appear here) to write a class in C -alchemy where the will support any metod of the library
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 13, 2009 2:12 PM   in reply to matthewbmedia
    yes if you could, that would be awesome. What do we need to compile the jpeg.l.bc for? what's the use for it? And if you could please help me on why I can't seem to be able to use jpeg lib or any other lib that'd be great!
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 13, 2009 8:01 PM   in reply to piolo96
    The fact that you can write in C you can not write in AS3.
    To use C libraries, together with AS3 - using C alchemy.
    To do this you need to compile the C library in alchemy and
    write a class in C Alchemy serves as a mediator between the C and AS3.
    Where a minimum must be agreed upon data types.
    You can see an example of Metalbot in action.
    Or that it does not work?
    If you write something to show the code for clarity.
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2009 2:39 PM   in reply to notnick
    I did try Metabolt's sample in action. It does work, BUT at the moment I compile the C file that serves as the mediator with Alchemy to build a new SWC file, and it does not work. Metabol are you around? Can you get in here and explain me why my JPEG library is not getting compiled or something. I have a very simple example that I apply standard alchemy methods but as soon as I start writing some of the lines of Metabolts C file, it throws me an error. The error that I mentioned before,, like 2 posts ago. Here we go: jpegCompression
    If you could take a look at that and tell me where I went wrong, and how will I be able to use the jpeg lib for the compression of images that would be great.
    Thank you
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2009 7:45 PM   in reply to piolo96
    It is clear now.
    A few questions I do not see the logic in your application where load jpg. What goal? Decoding? Resizing?
    At the moment I run your example but I do not see any errors.
    If it is possible to give exact information.
    From the documentation for the jpeg-6b note:
    quote:

    We provide a set of library routines for reading and writing JPEG image files,
    plus two sample applications "cjpeg" and "djpeg", which use the library to
    perform conversion between JPEG and some other popular image file formats..


    Sorry that I do not quite understand :)
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 16, 2009 8:50 PM   in reply to notnick
    Ok so now that you have seen what I have done. The purpose of the application is to resize images down from however big they are, bring them down to usable size. On my C file if you look from line 83 to 109 they are commented out. These are the methods I believe I will be using with the jpeg lib to make it happen. The main issue is that the library is just not working for me and I am not able to get it working. I am using what the "cjpeg" would apply to compress a image to jpeg format, only difference is I am getting a ByteArray, making it into a file so that I could inserted and used on the library. Hope that helps.
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 17, 2009 6:25 PM   in reply to piolo96
    This minimal changed
    Try changing it from Metalbot project the file - JpegTest.as

    quote:

    package {
    import flash.display.BitmapData;
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.geom.Rectangle;
    import flash.utils.ByteArray;
    import flash.utils.getTimer;
    import flash.events.Event;
    import flash.display.Loader;
    import flash.net.URLRequest;

    import hurlant.jpeg.as3_jpeg_wrapper;
    import com.adobe.images.JPGEncoder;

    public class JpegTest extends Sprite {
    private var _loader:Loader = new Loader();
    public function JpegTest() {

    trace("hello.");

    testWrite();

    }
    private function testWrite():void {


    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete) ;
    _loader.load(new URLRequest("acura.jpg"));
    }
    public function onComplete(event:Event):ByteArray{
    var image:Bitmap = Bitmap(_loader.content);
    var b:BitmapData = image.bitmapData;
    addChild(image);

    // as3 way
    var t1:int = getTimer();
    var s:JPGEncoder = new JPGEncoder();
    var jpg:ByteArray = s.encode(b);
    trace("AS3 JPEG ENCODING: "+(getTimer()-t1)+"ms.");
    trace("jpg.length = "+jpg.length);


    // C way
    var t2:int = getTimer();
    var ba:ByteArray = b.getPixels(new Rectangle(0,0,320,200));
    var ret:ByteArray = as3_jpeg_wrapper.write_jpeg_file(ba, 320, 200, 3, 2);
    trace(" C JPEG ENCODING: "+(getTimer()-t2)+"ms.");

    trace(" ret.length="+ret.length);
    return ret;
    }

    }
    }

    AS3 JPEG ENCODING: 1199ms.
    jpg.length = 17388
    setting up as3_jpeg_wrapper library
    C JPEG ENCODING: 245ms.
    ret.length=8963
    But still need to resize the image. Probably what is the manipulation of the Matrix. Unfortunately this has not happened.
    But mostly it works.
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 17, 2009 6:44 PM   in reply to notnick
    //It added the conversion from ByteArray to Bitmap
    package {
    import com.adobe.images.JPGEncoder;

    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.net.URLRequest;
    import flash.utils.ByteArray;
    import flash.utils.getTimer;

    import hurlant.jpeg.as3_jpeg_wrapper;

    public class JpegTest extends Sprite {
    private var _loader:Loader = new Loader();
    private var nJpg:Loader = new Loader();
    public function JpegTest() {

    trace("hello.");

    testWrite();

    }
    private function testWrite():void {


    _loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete) ;
    _loader.load(new URLRequest("acura.jpg"));
    }
    public function onComplete(event:Event):void{
    var image:Bitmap = Bitmap(_loader.content);
    image.width = image.width/2;
    image.height =image.height/2;
    var b:BitmapData = image.bitmapData;
    addChild(image);

    // as3 way
    var t1:int = getTimer();
    var s:JPGEncoder = new JPGEncoder();
    var jpg:ByteArray = s.encode(b);
    trace("AS3 JPEG ENCODING: "+(getTimer()-t1)+"ms.");
    trace("jpg.length = "+jpg.length);
    trace(image.width);
    trace(image.height);

    // C way
    var t2:int = getTimer();
    var ba:ByteArray = b.getPixels(new Rectangle(0,0,image.width,image.height));
    var ret:ByteArray = as3_jpeg_wrapper.write_jpeg_file(ba, image.width, image.height, 3, 2);
    trace(" C JPEG ENCODING: "+(getTimer()-t2)+"ms.");


    //It added the conversion from ByteArray to Bitmap
    nJpg.contentLoaderInfo.addEventListener(Event.COMPLETE,addnewjpg);
    nJpg.loadBytes(ret,null);

    }
    private function addnewjpg(event:Event):void{
    var im:Bitmap = Bitmap(nJpg.content);
    var bmpd:BitmapData = im.bitmapData;
    trace(bmpd.width);
    trace(bmpd.height);
    im.x = 200;
    this.addChild(im);

    }

    }
    }
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 17, 2009 7:51 PM   in reply to matthewbmedia
    cool man that's great so we got the AS3 part covered. I appreciate the help. Now the C part where I got to use the library and apply some kind of routine to compress the image and spit it back out the ByteArrat so that I could use the compressed image in my flash application. That's the part where I am struggling. Thanks again
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 18, 2009 1:41 AM   in reply to piolo96
    All ok! I was interested but there is the main point
    I noticed that the real picture is very big difference in speed, between AS3 and Alchemy
    I do not deepened in the reasons. But look at it.
    AS3 JPEG ENCODING: 1199ms.
    C JPEG ENCODING: 245ms.
    In the original example, where used only generic BitmapData differences were much smaller.
    AS3 JPEG ENCODING: 606ms.
    C JPEG ENCODING: 206ms.
    It seemed to me that you have a problem with C? You can not use the library? What are the causes?
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 18, 2009 10:01 AM   in reply to notnick
    Yes indeed is noticeable faster, and yes I do have problems with the use of the library. For example lets take a look at MetalBot's project. The following is his as3_jpeg_wrapper.gg

    { // start pass-thru C code

    #include <stdlib.h>
    #include <stdio.h>
    #include <jpeglib.h> //This is the library I want to use

    #include "badatadst.h"

    void sztrace(char*);

    AS3_Val no_params = NULL;
    AS3_Val zero_param = NULL;
    AS3_Val ByteArray_class = NULL;

    #define GGINIT_DEFINED true
    static void ggInit()
    {
    sztrace("setting up as3_jpeg_wrapper library");

    no_params = AS3_Array("");
    zero_param = AS3_Int(0);
    AS3_Val flash_utils_namespace = AS3_String("flash.utils");
    ByteArray_class = AS3_NSGetS(flash_utils_namespace, "ByteArray");
    AS3_Release(flash_utils_namespace);
    }

    /* Copy the byteArray data into a malloc'd buffer */
    static void* newMallocFromByteArray(AS3_Val byteArray, unsigned int* size)
    {
    AS3_Val byteArraySize = AS3_GetS(byteArray, "length");
    *size = AS3_IntValue(byteArraySize);
    AS3_Release(byteArraySize);
    void* bytes = malloc(*size);
    AS3_SetS(byteArray, "position", zero_param);
    AS3_ByteArray_readBytes((char*)bytes, byteArray, (int)*size);
    return bytes;
    }

    /* end pass-thru C code */
    }

    import flash.utils.ByteArray;

    public function write_jpeg_file(data:ByteArray, width:int, height:int, bytes_per_pixel:int, color_space:int):ByteArray {
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;

    unsigned int size = 0;
    unsigned char* raw_image32 = newMallocFromByteArray(data, &size);
    unsigned char* raw_image24 = (unsigned char*)malloc(size*3/4);

    AS3_Val array = AS3_New(ByteArray_class, no_params);

    /* this is a pointer to one row of image data */
    JSAMPROW row_pointer[1];

    /* semi-lame conversion from 32bit pixels to 24bit pixels. */
    int i=0,j=0;
    while (i++<size) {
    raw_image24[j++]=raw_image32[i++];
    raw_image24[j++]=raw_image32[i++];
    raw_image24[j++]=raw_image32[i++];
    }

    cinfo.err = jpeg_std_error( &jerr ); // From this line and on I get errors when trying to compile and use in flash / flex, etc.
    jpeg_create_compress(&cinfo);
    jpeg_bytearray_dest(&cinfo, array);

    /* Setting the parameters of the output file here */
    cinfo.image_width = width;
    cinfo.image_height = height;
    cinfo.input_components = bytes_per_pixel;
    cinfo.in_color_space = color_space;
    /* default compression parameters, we shouldn't be worried about these */
    jpeg_set_defaults( &cinfo );
    /* Now do the compression .. */
    jpeg_start_compress( &cinfo, TRUE );
    /* like reading a file, this time write one row at a time */
    while( cinfo.next_scanline < cinfo.image_height )
    {
    row_pointer[0] = &raw_image24[ cinfo.next_scanline * cinfo.image_width * cinfo.input_components];
    jpeg_write_scanlines( &cinfo, row_pointer, 1 );
    }
    /* clean up after we're done compressing */
    jpeg_finish_compress( &cinfo );
    jpeg_destroy_compress( &cinfo );

    free(raw_image32);
    free(raw_image24);

    return array;
    }

    In the function write_jpeg_file is all the functionality that will be used later on in flash / flex through the .SWC file.

    Now when I try to compile this .gg file with gluegen and then through gcc, to make a NEW as3_jpeg_wrapper.swc that should be just like MetalBot original .SWC file. I do not seem to get any error compiling, BUT at the moment I go into flash and replace the new SWC for the old and test the swf movie, it throws me an error,

    Undefined sym: _jpeg_std_error
    at <anonymous>()
    at <anonymous>()

    According to the Undefined sym: _jpeg_std_error, that would have to do with the line cinfo.err = jpeg_std_error( &jerr );
    I never changed a thing from this file and so that is the main issue I have. Without changing a thing, a project that was working properly, I just recompiled the SWC and it does not work anymore. I hope this makes everything clear.
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 18, 2009 10:03 AM   in reply to notnick
    Sorry I sent the message twice.
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 18, 2009 5:46 PM   in reply to piolo96
    Take this Archive
    Try to compile your swc using files from the archive.
    Unfortunately I can not repeat the problem. I have everything compiled without any problems
     
    |
    Mark as:
  • Currently Being Moderated
    Mar 18, 2009 9:02 PM   in reply to notnick
    Is there a way you could walk me through the way you get to the end of it ( the whole process).
    (assuming we don't have the jpeg lib installed) Starting from the point where we download MetalBot's project and we start building from the as3_jpeg_wrapper.gg he has until we get the as3_jpeg_wrapper.swc and we go inside flash / flex and try it with the JpegTest.as

    Thank you
     
    |
    Mark as:
  • Currently Being Moderated
    Apr 2, 2009 2:47 AM   in reply to matthewbmedia
    hello i need your help.
    I use ONLY Abode Flash CS4 (i haven`t any FlashDevelop, Adobe Flex SDK).

    I make flash uploader, and i need use as3_jpeg_wrapper.swc, but i can`t.


    my action:
    1) I add as3_jpeg_wrapper.swc to "C:\Program Files\Adobe\Adobe Flash CS4\Common\Configuration\ActionScript 3.0\libs"
    2) download This file is <a href=" http://flexlife.googlegroups.com/web/jpeg_enc.zip?hl=ru">a project of Metalbot </a>
    3) i Create empty fla
    4) add JpegTest class to empty fla
    5) run and i have error: ReferenceError: Error #1065: varaible DocJpgEncoder isn`t defenition

    Tell me please what can i do to imrove this situation ?
     
    |
    Mark as:
  • Currently Being Moderated
    May 5, 2009 7:46 AM   in reply to matthewbmedia

    Hello,

    I have test the library for record some Jpeg file.

    It's very fast, but i have a problem, how can I modify the source for add a new variable for change the compression ( Number between 0 to 100 )

     

    Do you have any idea, because in the actual state, it's impossible to work with this code...

     

    Thanks a lot for your help.

     

    -Geo

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 21, 2009 8:39 AM   in reply to Geoffrey Morelle

    @Geo: Check this blog post: http://www.websector.de/blog/2009/06/21/speed-up-jpeg-encoding-using-a lchemy/

    There you will find a modification of metalbots example for changing the quality of a compression.

     

    -Jens

     
    |
    Mark as:
1 2 Previous Next
Actions

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