Coder Social home page Coder Social logo

leeburrows / async-image-encoders Goto Github PK

View Code? Open in Web Editor NEW
20.0 7.0 7.0 285 KB

Asynchronously encode BitmapData objects into image file format

License: MIT License

JavaScript 43.29% CSS 10.91% ActionScript 45.80%
actionscript image encoding-library

async-image-encoders's Introduction

#Asynchronous Image Encoders

##ActionScript 3 classes for asynchronously encoding BitmapData source into image file formats

Encodes BitmapData objects over multiple frames to avoid freezing the UI. Ideally suited for mobile AIR where ActionScript Workers are unavailable.

Specify milliseconds per frame to allocate to encoding. Stop processing at any time.

Current supported file formats:

  • .JPG
  • .PNG
  • .BMP

Others can be added by sub-classing AsyncImageEncoderBase to implement asynchronous processing. See ASDocs for further details on implementing your own encoders.

A simple usage example:

package
{
	import com.leeburrows.encoders.AsyncPNGEncoder;
	import com.leeburrows.encoders.supportClasses.AsyncImageEncoderEvent;
	import com.leeburrows.encoders.supportClasses.IAsyncImageEncoder;
	import flash.display.BitmapData;
	import flash.display.Sprite;

	public class PNGEncoderExample extends Sprite
	{
		private var encoder:IAsyncImageEncoder;

		public function PNGEncoderExample()
		{
			//generate a BitmapData object to encode
			var myBitmapData:BitmapData = new BitmapData(1000, 1000, true, 0x80FF9900);
			//create a new PNG encoder
			encoder = new AsyncPNGEncoder();
			//add progress and complete listeners
			encoder.addEventListener(AsyncImageEncoderEvent.PROGRESS, encodeProgressHandler);
			encoder.addEventListener(AsyncImageEncoderEvent.COMPLETE, encodeCompleteHandler);
			//start encoding for 20 milliseconds per frame
			encoder.start(myBitmapData, 20);
		}

		private function encodeProgressHandler(event:AsyncImageEncoderEvent):void
		{
			//trace progress
			trace("encoding progress:", Math.floor(event.percentComplete)+"% complete");
		}

		private function encodeCompleteHandler(event:AsyncImageEncoderEvent):void
		{
			encoder.removeEventListener(AsyncImageEncoderEvent.PROGRESS, encodeProgressHandler);
			encoder.removeEventListener(AsyncImageEncoderEvent.COMPLETE, encodeCompleteHandler);
			//trace size of result
			trace("encoding completed:", encoder.encodedBytes.length+" bytes");
			//do something with the bytes...
			//..save to filesystem?
			//..upload to server?
			//..set as source for flex Image component?
		}
	}
}

###Version History###

  • v1.0.1

    • Initial build.
  • v1.0.2

    • Fixed issue #001 : Validate start() BitmapData Argument.
    • Fixed issue #002 : Consolidate Events.
    • Fixed issue #003 : Private _encodedBytes Property Exposed Via Reference.
  • v1.0.3

    • ASDocs inserted into SWC to facilitate IDE code hinting.
  • v1.0.4

    • Added dispose() method.
    • Fixed issue #004 : Unnecessary memory retention.

async-image-encoders's People

Contributors

leeburrows avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

async-image-encoders's Issues

Add GIF format?

Hi, I could not open a Pull Request, don't know why, sorry.

There is any possibility to add the GIF format too?

Thanks for your work.

Validate start() BitmapData Argument

Currently no validation for bitmapData.
Need to validate:
BitmapData != null
BitmapData width != 0
BitmapData height != 0
Throw error if fails? as don't want RTE to occur within encoder classes as SWC may be used as source and user won't be able to view code where RTE occurred.

crash on iOS

{
  "crashReporterKey" : "bf962e1d2afb09e283c67c252db41ff4c61e36de",
  "kernel" : "Darwin Kernel Version 16.1.0: Thu Sep 29 21:56:11 PDT 2016; root:xnu-3789.22.3~1\/RELEASE_ARM64_T7000",
  "product" : "iPhone7,1",
  "incident" : "A0B83A7E-E2CC-4F70-AA4C-7C1882057A5E",
  "date" : "2016-12-06 20:15:02.43 +0800",
  "build" : "iPhone OS 10.1.1 (14B150)",
  "timeDelta" : 34,
  "memoryStatus" : {
  "compressorSize" : 41006,
  "pageSize" : 4096,
  "compressions" : 4642197,
  "memoryPages" : {
    "active" : 105325,
    "throttled" : 0,
    "fileBacked" : 36011,
    "wired" : 48488,
    "anonymous" : 122122,
    "purgeable" : 96,
    "inactive" : 52513,
    "free" : 2291,

Date/Time:       2016-12-06 19:21:44 +0800
OS Version:      iPhone OS 10.1.1 (Build 14B150)
Architecture:    arm64
Report Version:  19

Command:         ComponentBrowser
Path:            /var/containers/Bundle/Application/45A0BBB4-437C-4A72-810E-B4E28AE982D0/ComponentBrowser.app/ComponentBrowser
Version:         3.0.0 (3.0.0)
Parent:          launchd [1]
PID:             833

Event:           wakeups
Wakeups:         45023 wakeups over the last 35 seconds (1284 wakeups per second average), exceeding limit of 150 wakeups per second over 300 seconds
Duration:        35.07s
Steps:           11

Hardware model:  iPhone7,1
Active cpus:     2


Powerstats for:  ComponentBrowser [833]
UUID:            09BC28EC-19CF-39F3-B8C3-3E17976F23B4
Start time:      2016-12-06 19:22:10 +0800
End time:        2016-12-06 19:22:18 +0800
Parent:          launchd
Microstackshots: 11 samples (100%)
Primary state:   4 samples Frontmost App, User mode, Thread QoS User Interactive
User Activity:   0 samples Idle, 11 samples Active
Power Source:    0 samples on Battery, 11 samples on AC
  6  _pthread_start + 284 (libsystem_pthread.dylib + 14176) [0x18c4f0760]
    6  _pthread_body + 240 (libsystem_pthread.dylib + 14416) [0x18c4f0850]
      5  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 2988000) [0x1003357e0]
        5  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 2988800) [0x100335b00]
          5  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 2988704) [0x100335aa0]
            5  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 1403804) [0x1001b2b9c]
              3  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 1403972) [0x1001b2c44]
                3  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 2997080) [0x100337b58]
                  3  __select + 8 (libsystem_kernel.dylib + 127548) [0x18c42a23c]
                    2  <Kernel mode>
              2  __psynch_mutexdrop + 8 (libsystem_kernel.dylib + 126528) [0x18c429e40]
                2  <Kernel mode, Thread QoS Default>
      1  __NSThread__start__ + 1024 (Foundation + 1090684) [0x18df4347c]
        1  -[UIEventFetcher threadMain] + 136 (UIKit + 10422828) [0x193c95a2c]
          1  -[NSRunLoop runUntilDate:] + 96 (Foundation + 185724) [0x18de6657c]
            1  -[NSRunLoop runMode:beforeDate:] + 304 (Foundation + 51852) [0x18de45a8c]
              1  CFRunLoopRunSpecific + 444 (CoreFoundation + 36936) [0x18d334048]
                1  __CFRunLoopRun + 804 (CoreFoundation + 894912) [0x18d4057c0]
                  1  __CFRunLoopDoSources0 + 524 (CoreFoundation + 904128) [0x18d407bc0]
                    1  __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 24 (CoreFoundation + 905848) [0x18d408278]
                      1  -[UIEventFetcher filterEvents] + 184 (UIKit + 10424340) [0x193c96014]
                        1  CFRunLoopWakeUp + 192 (CoreFoundation + 32568) [0x18d332f38]
                          1  mach_msg_trap + 8 (libsystem_kernel.dylib + 4460) [0x18c40c16c]
                            1  <Kernel mode>
  4  start + 4 (libdyld.dylib + 17848) [0x18c3185b8]
    4  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 1049992) [0x10015c588]
      4  UIApplicationMain + 208 (UIKit + 483380) [0x19331b034]
        4  -[UIApplication _run] + 684 (UIKit + 504572) [0x1933202fc]
          4  GSEventRunModal + 180 (GraphicsServices + 49560) [0x18edba198]
            4  CFRunLoopRunSpecific + 444 (CoreFoundation + 36936) [0x18d334048]
              4  __CFRunLoopRun + 1840 (CoreFoundation + 895948) [0x18d405bcc]
                4  __CFRunLoopDoSource1 + 436 (CoreFoundation + 905448) [0x18d4080e8]
                  4  __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 56 (CoreFoundation + 907572) [0x18d408934]
                    4  __CFMachPortPerform + 180 (CoreFoundation + 808300) [0x18d3f056c]
                      4  IODispatchCalloutFromCFMessage + 372 (IOKit + 24888) [0x18d6c7138]
                        4  CA::Display::DisplayLink::dispatch_items + 444 (QuartzCore + 45160) [0x190667068]
                          4  CA::Display::DisplayLinkItem::dispatch + 44 (QuartzCore + 45500) [0x1906671bc]
                            4  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 115236) [0x100078224]
                              2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 885556) [0x100134334]
                                2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 884288) [0x100133e40]
                                  2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 833784) [0x1001278f8]
                                    2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 3170968) [0x100362298]
                                      2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 3172588) [0x1003628ec]
                                        2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 3172788) [0x1003629b4]
                                          2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 3169560) [0x100361d18]
                                            2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 520340) [0x1000db094]
                                              2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 517560) [0x1000da5b8]
                                                2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 431068) [0x1000c53dc]
                                                  2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 432344) [0x1000c58d8]
                                                    2  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 426592) [0x1000c4260]
                              1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 885520) [0x100134310]
                                1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 880812) [0x1001330ac]
                                  1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 879052) [0x1001329cc]
                                    1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 1176560) [0x10017b3f0]
                                      1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 6240320) [0x10064f840]
                                        1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5828896) [0x1005eb120]
                                          1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5940132) [0x1006063a4]
                                            1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 6240460) [0x10064f8cc]
                                              1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 1176624) [0x10017b430]
                                                1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 2989280) [0x100335ce0]
                                                  1  __psynch_cvwait + 8 (libsystem_kernel.dylib + 126492) [0x18c429e1c]
                                                    1  <Kernel mode>
                              1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 885536) [0x100134320]
                                1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 882936) [0x1001338f8]
                                  1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 2650032) [0x1002e2fb0]
                                    1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 41112) [0x100066098]
                                      1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 39292) [0x10006597c]
                                        1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5510548) [0x10059d594]
                                          1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5510548) [0x10059d594]
                                            1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5510548) [0x10059d594]
                                              1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5510548) [0x10059d594]
                                                1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5510548) [0x10059d594]
                                                  1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5510548) [0x10059d594]
                                                    1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5510548) [0x10059d594]
                                                      1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5522176) [0x1005a0300]
                                                        1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5510548) [0x10059d594]
                                                          1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5512316) [0x10059dc7c]
                                                            1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5546604) [0x1005a626c]
                                                              1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 5832228) [0x1005ebe24]
                                                                1  ??? (<09BC28EC-19CF-39F3-B8C3-3E17976F23B4> + 6247784) [0x100651568]
  1  _pthread_wqthread + 948 (libsystem_pthread.dylib + 4452) [0x18c4ee164]
    1  _dispatch_kevent_worker_thread + 816 (libdispatch.dylib + 92940) [0x18c2fab0c]
      1  _dispatch_root_queue_drain_deferred_item + 248 (libdispatch.dylib + 66220) [0x18c2f42ac]
        1  _dispatch_queue_invoke + 652 (libdispatch.dylib + 18856) [0x18c2e89a8]
          1  _dispatch_queue_serial_drain + 468 (libdispatch.dylib + 62072) [0x18c2f3278]
            1  _dispatch_source_invoke + 820 (libdispatch.dylib + 12644) [0x18c2e7164]
              1  _dispatch_source_latch_and_call + 204 (libdispatch.dylib + 108104) [0x18c2fe648]
                1  _dispatch_continuation_pop + 576 (libdispatch.dylib + 57352) [0x18c2f2008]
                  1  ??? (libcorecrypto.dylib + 401856) [0x18c2e51c0]
                    1  nw_socket_update_input_source + 120 (libnetwork.dylib + 175228) [0x199a8ac7c]
                      1  nw_socket_get_input_frames + 0 (libnetwork.dylib + 159768) [0x199a87018]
                        1  <Thread QoS Default>

  Binary Images:
         0x10005c000 -                ???  ??? <09BC28EC-19CF-39F3-B8C3-3E17976F23B4>
         0x18c283000 -        0x18c2e6fff  libcorecrypto.dylib <3B0C9C05-A798-360E-BC82-1FBE2F50B95C> /usr/lib/system/libcorecrypto.dylib
         0x18c2e4000 -        0x18c313fff  libdispatch.dylib <3840477F-1DCB-32C5-BE29-D805A0B43D93> /usr/lib/system/libdispatch.dylib
         0x18c314000 -        0x18c31bfff  libdyld.dylib <39AAA2DB-E1A1-313C-9C83-8CC0268A2EA0> /usr/lib/system/libdyld.dylib
         0x18c40b000 -        0x18c432fff  libsystem_kernel.dylib <3BB31DBC-9985-3FA4-B33F-4948C9E2AF64> /usr/lib/system/libsystem_kernel.dylib
         0x18c4ed000 -        0x18c4f8fff  libsystem_pthread.dylib <548D3D6A-3B57-38F2-820E-0382C8D77C0E> /usr/lib/system/libsystem_pthread.dylib
         0x18d32b000 -        0x18d6aefff  com.apple.CoreFoundation 6.9 (1348) <D72E357F-5B33-37AA-9693-522564A6032E> /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
         0x18d6c1000 -        0x18d73cfff  com.apple.framework.IOKit 2.0.2 <97DF8046-F49D-3A3C-9A28-AD1C89EC90A9> /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
         0x18de39000 -        0x18e108fff  com.apple.Foundation 6.9 (1349.1) <8C727AEA-9F34-365F-B2FE-552669585011> /System/Library/Frameworks/Foundation.framework/Foundation
         0x18edae000 -        0x18edc5fff  com.apple.GraphicsServices 1.0 (1.0) <6D8EC4C7-41AE-3332-AE48-54A3C69F9C68> /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
         0x19065c000 -        0x190837fff  com.apple.QuartzCore 1.11 (449.40.9) <BF63FBA9-5947-3E54-8ED5-F6E4DB40E3AE> /System/Library/Frameworks/QuartzCore.framework/QuartzCore
         0x1932a5000 -        0x194034fff  com.apple.UIKit 1.0 (1000) <7EF942F4-3C3E-3E4A-A6CE-C127B68ECBAB> /System/Library/Frameworks/UIKit.framework/UIKit
         0x199a60000 -        0x199adbfff  libnetwork.dylib <3357CA81-08A7-3179-BF37-6ACFFD953D64> /usr/lib/libnetwork.dylib

Consolidate Events

Use same event class for progress and complete events.
Seems a bit clunky to use custom event for progress and native event for complete.

Unnecessary memory retention

After encoding has completed (or been cancelled), any unrequired objects should be cleared to free memory.

Such as:

  • sourceBitmapData.
  • encodedBytes (via public method).
  • internal ByteArrays used by subclasses during processing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.