--[[----------------------------------------------------------------------------

KSJpegDecodeServiceEPInit.lua

--------------------------------------------------------------------------------

ADOBE CONFIDENTIAL
------------------
Copyright 2011 Adobe Systems Incorporated
All Rights Reserved.

NOTICE: All information contained herein is, and remain the property of
Adobe Systems Incorporated and its suppliers, if any. The intellectual and
technical concepts contained herein are proprietary to Adobe Systems
Incorporated and its suppliers and may be covered by U.S. and Foreign
Patents, patents in process, and are protected by trade secret or copyright
law.  Dissemination of this information or reproduction of this material is
strictly forbidden unless prior written permission is obtained from
Adobe Systems Incorporated.

------------------------------------------------------------------------------]]

local WFCurrentThread = require 'WFCurrentThread'
local WFRequestDispatcher = require 'WFRequestDispatcher'

local KSJpegDecodeService = require 'KSJpegDecodeService'
local KSData = require 'KSData'
local WFPlatformInfo = require 'WFPlatformInfo'
    
--------------------------------------------------------------------------------

WFCurrentThread.setThreadPriority( WFPlatformInfo.isAndroid and '< high >' or '< low >' )

rawset( _G, '< ksimage-no-accounting >', true )
	-- We are good about cleaning up after ourselves so don't do any extra
	-- GC accounting.
	-- FIXME [mhamburg 2011-03-18]: Perhaps the event processor code should
	-- do a full GC if it gets an error when pcall'ing the handler. Or this
	-- should be an option.

--------------------------------------------------------------------------------

WFRequestDispatcher.onMessageDo {

		message = 'decode',
		simpleResponses = true,

		handler = function( data, sizeLimit )
		
			if not KSData.isData( data ) then
				error( "Argument to decode was not a KSData, type:  "
							.. type( data )
							.. " value: "
							.. tostring( data ) )
			end
					
			WFRequestDispatcher.deferredClose( data )
			
			local decodedImage = KSJpegDecodeService.decode( data, sizeLimit )
			
			assert( decodedImage, "The image data could not be decoded" )
		
			WFRequestDispatcher.deferredClose( decodedImage )
			
			return decodedImage
		
		end,
	
}
